@@ -2,8 +2,8 @@ package com.datadog.android.core.sampling
2
2
3
3
import com.datadog.android.utils.forge.Configurator
4
4
import com.datadog.trace.sampling.JavaDeterministicSampler
5
+ import fr.xgouchet.elmyr.Forge
5
6
import fr.xgouchet.elmyr.annotation.FloatForgery
6
- import fr.xgouchet.elmyr.annotation.LongForgery
7
7
import fr.xgouchet.elmyr.junit5.ForgeConfiguration
8
8
import fr.xgouchet.elmyr.junit5.ForgeExtension
9
9
import org.assertj.core.api.Assertions.assertThat
@@ -36,11 +36,15 @@ internal class DeterministicSamplerTest {
36
36
37
37
private var stubIdConverter: (ULong ) -> ULong = { it }
38
38
39
+ private lateinit var fakeTraceIds: List <Long >
40
+
39
41
@Mock
40
42
lateinit var mockSampleRateProvider: () -> Float
41
43
42
44
@BeforeEach
43
- fun `set up` () {
45
+ fun `set up` (forge : Forge ) {
46
+ val listSize = forge.anInt(256 , 1024 )
47
+ fakeTraceIds = forge.aList(listSize) { aLong() }
44
48
testedSampler = DeterministicSampler (
45
49
stubIdConverter,
46
50
mockSampleRateProvider
@@ -65,15 +69,14 @@ internal class DeterministicSamplerTest {
65
69
66
70
@RepeatedTest(128 )
67
71
fun `M return consistent results W sample() {java implementation}` (
68
- @LongForgery traceIds : List <Long >,
69
72
@FloatForgery(min = 0f , max = 100f ) fakeSampleRate : Float
70
73
) {
71
74
// Given
72
75
whenever(mockSampleRateProvider.invoke()) doReturn fakeSampleRate
73
76
val javaSampler = JavaDeterministicSampler (fakeSampleRate / 100f )
74
77
75
78
// When
76
- traceIds .forEach {
79
+ fakeTraceIds .forEach {
77
80
val result = testedSampler.sample(it.toULong())
78
81
val expectedResult = javaSampler.sample(it)
79
82
@@ -83,34 +86,32 @@ internal class DeterministicSamplerTest {
83
86
84
87
@RepeatedTest(128 )
85
88
fun `the sampler will sample the values based on the fixed sample rate` (
86
- @LongForgery traceIds : List <Long >,
87
89
@FloatForgery(min = 0f , max = 100f ) fakeSampleRate : Float
88
90
) {
89
91
// Given
90
92
whenever(mockSampleRateProvider.invoke()) doReturn fakeSampleRate
91
93
var sampledIn = 0
92
94
93
95
// When
94
- traceIds .forEach {
96
+ fakeTraceIds .forEach {
95
97
if (testedSampler.sample(it.toULong())) {
96
98
sampledIn++
97
99
}
98
100
}
99
101
100
102
// Then
101
- assertThat(sampledIn.toFloat()).isCloseTo(traceIds.size * fakeSampleRate / 100f , Offset .offset(7.5f ))
103
+ val offset = 2.5f * fakeTraceIds.size
104
+ assertThat(sampledIn.toFloat()).isCloseTo(fakeTraceIds.size * fakeSampleRate / 100f , Offset .offset(offset))
102
105
}
103
106
104
107
@Test
105
- fun `when sample rate is 0 all values will be dropped` (
106
- @LongForgery traceIds : List <Long >
107
- ) {
108
+ fun `when sample rate is 0 all values will be dropped` () {
108
109
// Given
109
110
whenever(mockSampleRateProvider.invoke()) doReturn 0f
110
111
var sampledIn = 0
111
112
112
113
// When
113
- traceIds .forEach {
114
+ fakeTraceIds .forEach {
114
115
if (testedSampler.sample(it.toULong())) {
115
116
sampledIn++
116
117
}
@@ -121,22 +122,20 @@ internal class DeterministicSamplerTest {
121
122
}
122
123
123
124
@Test
124
- fun `when sample rate is 100 all values will pass` (
125
- @LongForgery traceIds : List <Long >
126
- ) {
125
+ fun `when sample rate is 100 all values will pass` () {
127
126
// Given
128
127
whenever(mockSampleRateProvider.invoke()) doReturn 100f
129
128
var sampledIn = 0
130
129
131
130
// When
132
- traceIds .forEach {
131
+ fakeTraceIds .forEach {
133
132
if (testedSampler.sample(it.toULong())) {
134
133
sampledIn++
135
134
}
136
135
}
137
136
138
137
// Then
139
- assertThat(sampledIn).isEqualTo(traceIds .size)
138
+ assertThat(sampledIn).isEqualTo(fakeTraceIds .size)
140
139
}
141
140
142
141
@Test
0 commit comments