@@ -31,6 +31,8 @@ import org.junit.Before
31
31
import org.junit.Rule
32
32
import org.junit.Test
33
33
import org.junit.runner.RunWith
34
+ import java.util.concurrent.CountDownLatch
35
+ import java.util.concurrent.TimeUnit
34
36
35
37
/* *
36
38
* Instrumentation tests for the feature scope.
@@ -205,6 +207,7 @@ class FeatureScopeTest : MockServerTest() {
205
207
) as InternalSdkCore
206
208
testedInternalSdkCore.registerFeature(stubFeature)
207
209
val featureScope = testedInternalSdkCore.getFeature(fakeFeatureName)
210
+ val countDownLatch = CountDownLatch (fakeBatchData.size)
208
211
209
212
// When
210
213
checkNotNull(featureScope)
@@ -215,11 +218,14 @@ class FeatureScopeTest : MockServerTest() {
215
218
fakeBatchMetadata,
216
219
eventType
217
220
)
221
+ countDownLatch.countDown()
218
222
}
219
223
}
220
224
221
225
// Then
222
- Thread .sleep(MEDIUM_WAIT_MS )
226
+ countDownLatch.await(MEDIUM_WAIT_MS , TimeUnit .MILLISECONDS )
227
+ // TODO RUM-9917 Avoid using unconditional wait locks
228
+ Thread .sleep(UPLOAD_CYCLE_MAX_WAIT_MS )
223
229
MockWebServerAssert .assertThat(getMockServerWrapper())
224
230
.withConfiguration(fakeConfiguration)
225
231
.withTrackingConsent(trackingConsent)
@@ -241,6 +247,7 @@ class FeatureScopeTest : MockServerTest() {
241
247
) as InternalSdkCore
242
248
testedInternalSdkCore.registerFeature(stubFeature)
243
249
val featureScope = testedInternalSdkCore.getFeature(fakeFeatureName)
250
+ val countDownLatch = CountDownLatch (fakeBatchData.size)
244
251
245
252
// When
246
253
checkNotNull(featureScope)
@@ -251,11 +258,14 @@ class FeatureScopeTest : MockServerTest() {
251
258
fakeBatchMetadata,
252
259
eventType
253
260
)
261
+ countDownLatch.countDown()
254
262
}
255
263
}
256
264
257
265
// Then
258
- Thread .sleep(MEDIUM_WAIT_MS )
266
+ countDownLatch.await(MEDIUM_WAIT_MS , TimeUnit .MILLISECONDS )
267
+ // TODO RUM-9917 Avoid using unconditional wait locks
268
+ Thread .sleep(UPLOAD_CYCLE_MAX_WAIT_MS )
259
269
MockWebServerAssert .assertThat(getMockServerWrapper())
260
270
.withConfiguration(fakeConfiguration)
261
271
.withTrackingConsent(trackingConsent)
@@ -274,21 +284,29 @@ class FeatureScopeTest : MockServerTest() {
274
284
testedInternalSdkCore.registerFeature(stubFeature)
275
285
val featureScope = testedInternalSdkCore.getFeature(fakeFeatureName)
276
286
checkNotNull(featureScope)
287
+ val countDownLatch = CountDownLatch (fakeBatchData.size)
277
288
featureScope.withWriteContext { _, eventBatchWriter ->
278
289
fakeBatchData.forEach { rawBatchEvent ->
279
290
eventBatchWriter.write(
280
291
rawBatchEvent,
281
292
fakeBatchMetadata,
282
293
eventType
283
294
)
295
+ countDownLatch.countDown()
284
296
}
285
297
}
286
298
287
299
// When
288
300
Datadog .setTrackingConsent(TrackingConsent .NOT_GRANTED )
289
301
290
302
// Then
291
- Thread .sleep(MEDIUM_WAIT_MS )
303
+ countDownLatch.await(MEDIUM_WAIT_MS , TimeUnit .MILLISECONDS )
304
+ with (testedInternalSdkCore.getPersistenceExecutorService()) {
305
+ shutdown()
306
+ awaitTermination(MEDIUM_WAIT_MS , TimeUnit .MILLISECONDS )
307
+ }
308
+ // TODO RUM-9917 Avoid using unconditional wait locks
309
+ Thread .sleep(UPLOAD_CYCLE_MAX_WAIT_MS )
292
310
MockWebServerAssert .assertThat(getMockServerWrapper())
293
311
.withConfiguration(fakeConfiguration)
294
312
.withTrackingConsent(trackingConsent)
@@ -311,13 +329,15 @@ class FeatureScopeTest : MockServerTest() {
311
329
testedInternalSdkCore.registerFeature(stubFeature)
312
330
val featureScope = testedInternalSdkCore.getFeature(fakeFeatureName)
313
331
checkNotNull(featureScope)
332
+ val countDownLatch = CountDownLatch (fakeBatchData.size)
314
333
featureScope.withWriteContext { _, eventBatchWriter ->
315
334
fakeBatchData.forEach { rawBatchEvent ->
316
335
eventBatchWriter.write(
317
336
rawBatchEvent,
318
337
fakeBatchMetadata,
319
338
eventType
320
339
)
340
+ countDownLatch.countDown()
321
341
}
322
342
}
323
343
@@ -326,7 +346,13 @@ class FeatureScopeTest : MockServerTest() {
326
346
Datadog .setTrackingConsent(TrackingConsent .GRANTED )
327
347
328
348
// Then
329
- Thread .sleep(MEDIUM_WAIT_MS )
349
+ countDownLatch.await(MEDIUM_WAIT_MS , TimeUnit .MILLISECONDS )
350
+ with (testedInternalSdkCore.getPersistenceExecutorService()) {
351
+ shutdown()
352
+ awaitTermination(MEDIUM_WAIT_MS , TimeUnit .MILLISECONDS )
353
+ }
354
+ // TODO RUM-9917 Avoid using unconditional wait locks
355
+ Thread .sleep(UPLOAD_CYCLE_MAX_WAIT_MS )
330
356
MockWebServerAssert .assertThat(getMockServerWrapper())
331
357
.withConfiguration(fakeConfiguration)
332
358
.withTrackingConsent(trackingConsent)
@@ -350,6 +376,7 @@ class FeatureScopeTest : MockServerTest() {
350
376
val featureScope = testedInternalSdkCore.getFeature(fakeFeatureName)
351
377
checkNotNull(featureScope)
352
378
Datadog .stopInstance()
379
+ val countDownLatch = CountDownLatch (fakeBatchData.size)
353
380
354
381
// When
355
382
featureScope.withWriteContext { _, eventBatchWriter ->
@@ -359,11 +386,14 @@ class FeatureScopeTest : MockServerTest() {
359
386
fakeBatchMetadata,
360
387
eventType
361
388
)
389
+ countDownLatch.countDown()
362
390
}
363
391
}
364
392
365
393
// Then
366
- Thread .sleep(MEDIUM_WAIT_MS )
394
+ countDownLatch.await(MEDIUM_WAIT_MS , TimeUnit .MILLISECONDS )
395
+ // TODO RUM-9917 Avoid using unconditional wait locks
396
+ Thread .sleep(UPLOAD_CYCLE_MAX_WAIT_MS )
367
397
MockWebServerAssert .assertThat(getMockServerWrapper())
368
398
.withConfiguration(fakeConfiguration)
369
399
.withTrackingConsent(trackingConsent)
0 commit comments