|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.contrib.dynamic.policy.tracesampling; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 10 | +import static org.mockito.ArgumentMatchers.any; |
| 11 | +import static org.mockito.Mockito.mock; |
| 12 | +import static org.mockito.Mockito.verify; |
| 13 | + |
| 14 | +import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer; |
| 15 | +import io.opentelemetry.sdk.trace.samplers.Sampler; |
| 16 | +import org.junit.jupiter.api.Test; |
| 17 | + |
| 18 | +class TraceSamplingRatePolicyTest { |
| 19 | + |
| 20 | + @Test |
| 21 | + void constructorStoresProbabilityAndType() { |
| 22 | + TraceSamplingRatePolicy policy = new TraceSamplingRatePolicy(0.25); |
| 23 | + |
| 24 | + assertThat(policy.getProbability()).isEqualTo(0.25); |
| 25 | + assertThat(policy.getType()).isEqualTo(TraceSamplingRatePolicy.POLICY_TYPE); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + void constructorNormalizesNegativeZeroToPositiveZero() { |
| 30 | + TraceSamplingRatePolicy negativeZero = new TraceSamplingRatePolicy(-0.0); |
| 31 | + TraceSamplingRatePolicy positiveZero = new TraceSamplingRatePolicy(0.0); |
| 32 | + |
| 33 | + assertThat(negativeZero.getProbability()).isEqualTo(0.0); |
| 34 | + assertThat(Double.doubleToRawLongBits(negativeZero.getProbability())) |
| 35 | + .isEqualTo(Double.doubleToRawLongBits(0.0)); |
| 36 | + assertThat(negativeZero).isEqualTo(positiveZero); |
| 37 | + assertThat(negativeZero.hashCode()).isEqualTo(positiveZero.hashCode()); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void constructorRejectsOutOfRangeOrNaNProbabilities() { |
| 42 | + assertThatThrownBy(() -> new TraceSamplingRatePolicy(Double.NaN)) |
| 43 | + .isInstanceOf(IllegalArgumentException.class) |
| 44 | + .hasMessage("probability must be within [0.0, 1.0]"); |
| 45 | + assertThatThrownBy(() -> new TraceSamplingRatePolicy(-0.001)) |
| 46 | + .isInstanceOf(IllegalArgumentException.class) |
| 47 | + .hasMessage("probability must be within [0.0, 1.0]"); |
| 48 | + assertThatThrownBy(() -> new TraceSamplingRatePolicy(1.001)) |
| 49 | + .isInstanceOf(IllegalArgumentException.class) |
| 50 | + .hasMessage("probability must be within [0.0, 1.0]"); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void equalsAndHashCodeUseProbability() { |
| 55 | + TraceSamplingRatePolicy a = new TraceSamplingRatePolicy(0.5); |
| 56 | + TraceSamplingRatePolicy b = new TraceSamplingRatePolicy(0.5); |
| 57 | + TraceSamplingRatePolicy c = new TraceSamplingRatePolicy(0.75); |
| 58 | + |
| 59 | + assertThat(a).isEqualTo(b); |
| 60 | + assertThat(a.hashCode()).isEqualTo(b.hashCode()); |
| 61 | + assertThat(a).isNotEqualTo(c); |
| 62 | + assertThat(a).isNotEqualTo(null); |
| 63 | + assertThat(a).isNotEqualTo("not-a-policy"); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void initializeRejectsNullCustomizer() { |
| 68 | + assertThatThrownBy(() -> TraceSamplingRatePolicy.initialize(null)) |
| 69 | + .isInstanceOf(NullPointerException.class) |
| 70 | + .hasMessage("autoConfiguration cannot be null"); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void initializeStoresDelegatingSamplerAndRegistersCustomizer() { |
| 75 | + AutoConfigurationCustomizer customizer = mock(AutoConfigurationCustomizer.class); |
| 76 | + |
| 77 | + TraceSamplingRatePolicy.initialize(customizer); |
| 78 | + |
| 79 | + assertThat(TraceSamplingRatePolicy.getInitializedSampler()).isNotNull(); |
| 80 | + verify(customizer).addSamplerCustomizer(any()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void createSamplerAcceptsBoundaryProbabilities() { |
| 85 | + Sampler zero = TraceSamplingRatePolicy.createSampler(0.0); |
| 86 | + Sampler negativeZero = TraceSamplingRatePolicy.createSampler(-0.0); |
| 87 | + Sampler one = TraceSamplingRatePolicy.createSampler(1.0); |
| 88 | + |
| 89 | + assertThat(zero).isNotNull(); |
| 90 | + assertThat(negativeZero).isNotNull(); |
| 91 | + assertThat(one).isNotNull(); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void createSamplerRejectsOutOfRangeOrNaNProbabilities() { |
| 96 | + assertThatThrownBy(() -> TraceSamplingRatePolicy.createSampler(Double.NaN)) |
| 97 | + .isInstanceOf(IllegalArgumentException.class) |
| 98 | + .hasMessage("probability must be within [0.0, 1.0]"); |
| 99 | + assertThatThrownBy(() -> TraceSamplingRatePolicy.createSampler(-0.01)) |
| 100 | + .isInstanceOf(IllegalArgumentException.class) |
| 101 | + .hasMessage("probability must be within [0.0, 1.0]"); |
| 102 | + assertThatThrownBy(() -> TraceSamplingRatePolicy.createSampler(1.01)) |
| 103 | + .isInstanceOf(IllegalArgumentException.class) |
| 104 | + .hasMessage("probability must be within [0.0, 1.0]"); |
| 105 | + } |
| 106 | +} |
0 commit comments