Skip to content

Commit c9ab2b9

Browse files
committed
Speed up some tests in FeatureScopeTest by reducing unconditional wait time
1 parent 4df0cde commit c9ab2b9

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

dd-sdk-android-core/src/test/kotlin/com/datadog/android/utils/forge/Configurator.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ internal class Configurator :
2424
forge.addFactory(CustomAttributesForgeryFactory())
2525
forge.addFactory(ConfigurationForgeryFactory())
2626
forge.addFactory(ConfigurationCoreForgeryFactory())
27-
forge.addFactory(ConfigurationForgeryFactory())
2827
forge.addFactory(FilePersistenceConfigForgeryFactory())
2928
forge.addFactory(AndroidInfoProviderForgeryFactory())
3029
forge.addFactory(FeatureStorageConfigurationForgeryFactory())

reliability/core-it/src/androidTest/kotlin/com/datadog/android/core/integration/tests/BaseTest.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ abstract class BaseTest {
1414
companion object {
1515
internal val LONG_WAIT_MS = TimeUnit.SECONDS.toMillis(60)
1616
internal val MEDIUM_WAIT_MS = TimeUnit.SECONDS.toMillis(30)
17+
18+
// TODO RUM-9917 Avoid using unconditional wait locks
19+
// to align with UploadFrequency max value + 1s for async execution
20+
internal val UPLOAD_CYCLE_MAX_WAIT_MS = TimeUnit.SECONDS.toMillis(6)
1721
internal const val SHORT_WAIT_MS = 500L
1822
}
1923
}

reliability/core-it/src/androidTest/kotlin/com/datadog/android/core/integration/tests/FeatureScopeTest.kt

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import org.junit.Before
3131
import org.junit.Rule
3232
import org.junit.Test
3333
import org.junit.runner.RunWith
34+
import java.util.concurrent.CountDownLatch
35+
import java.util.concurrent.TimeUnit
3436

3537
/**
3638
* Instrumentation tests for the feature scope.
@@ -205,6 +207,7 @@ class FeatureScopeTest : MockServerTest() {
205207
) as InternalSdkCore
206208
testedInternalSdkCore.registerFeature(stubFeature)
207209
val featureScope = testedInternalSdkCore.getFeature(fakeFeatureName)
210+
val countDownLatch = CountDownLatch(fakeBatchData.size)
208211

209212
// When
210213
checkNotNull(featureScope)
@@ -215,11 +218,14 @@ class FeatureScopeTest : MockServerTest() {
215218
fakeBatchMetadata,
216219
eventType
217220
)
221+
countDownLatch.countDown()
218222
}
219223
}
220224

221225
// 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)
223229
MockWebServerAssert.assertThat(getMockServerWrapper())
224230
.withConfiguration(fakeConfiguration)
225231
.withTrackingConsent(trackingConsent)
@@ -241,6 +247,7 @@ class FeatureScopeTest : MockServerTest() {
241247
) as InternalSdkCore
242248
testedInternalSdkCore.registerFeature(stubFeature)
243249
val featureScope = testedInternalSdkCore.getFeature(fakeFeatureName)
250+
val countDownLatch = CountDownLatch(fakeBatchData.size)
244251

245252
// When
246253
checkNotNull(featureScope)
@@ -251,11 +258,14 @@ class FeatureScopeTest : MockServerTest() {
251258
fakeBatchMetadata,
252259
eventType
253260
)
261+
countDownLatch.countDown()
254262
}
255263
}
256264

257265
// 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)
259269
MockWebServerAssert.assertThat(getMockServerWrapper())
260270
.withConfiguration(fakeConfiguration)
261271
.withTrackingConsent(trackingConsent)
@@ -274,21 +284,29 @@ class FeatureScopeTest : MockServerTest() {
274284
testedInternalSdkCore.registerFeature(stubFeature)
275285
val featureScope = testedInternalSdkCore.getFeature(fakeFeatureName)
276286
checkNotNull(featureScope)
287+
val countDownLatch = CountDownLatch(fakeBatchData.size)
277288
featureScope.withWriteContext { _, eventBatchWriter ->
278289
fakeBatchData.forEach { rawBatchEvent ->
279290
eventBatchWriter.write(
280291
rawBatchEvent,
281292
fakeBatchMetadata,
282293
eventType
283294
)
295+
countDownLatch.countDown()
284296
}
285297
}
286298

287299
// When
288300
Datadog.setTrackingConsent(TrackingConsent.NOT_GRANTED)
289301

290302
// 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)
292310
MockWebServerAssert.assertThat(getMockServerWrapper())
293311
.withConfiguration(fakeConfiguration)
294312
.withTrackingConsent(trackingConsent)
@@ -311,13 +329,15 @@ class FeatureScopeTest : MockServerTest() {
311329
testedInternalSdkCore.registerFeature(stubFeature)
312330
val featureScope = testedInternalSdkCore.getFeature(fakeFeatureName)
313331
checkNotNull(featureScope)
332+
val countDownLatch = CountDownLatch(fakeBatchData.size)
314333
featureScope.withWriteContext { _, eventBatchWriter ->
315334
fakeBatchData.forEach { rawBatchEvent ->
316335
eventBatchWriter.write(
317336
rawBatchEvent,
318337
fakeBatchMetadata,
319338
eventType
320339
)
340+
countDownLatch.countDown()
321341
}
322342
}
323343

@@ -326,7 +346,13 @@ class FeatureScopeTest : MockServerTest() {
326346
Datadog.setTrackingConsent(TrackingConsent.GRANTED)
327347

328348
// 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)
330356
MockWebServerAssert.assertThat(getMockServerWrapper())
331357
.withConfiguration(fakeConfiguration)
332358
.withTrackingConsent(trackingConsent)
@@ -350,6 +376,7 @@ class FeatureScopeTest : MockServerTest() {
350376
val featureScope = testedInternalSdkCore.getFeature(fakeFeatureName)
351377
checkNotNull(featureScope)
352378
Datadog.stopInstance()
379+
val countDownLatch = CountDownLatch(fakeBatchData.size)
353380

354381
// When
355382
featureScope.withWriteContext { _, eventBatchWriter ->
@@ -359,11 +386,14 @@ class FeatureScopeTest : MockServerTest() {
359386
fakeBatchMetadata,
360387
eventType
361388
)
389+
countDownLatch.countDown()
362390
}
363391
}
364392

365393
// 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)
367397
MockWebServerAssert.assertThat(getMockServerWrapper())
368398
.withConfiguration(fakeConfiguration)
369399
.withTrackingConsent(trackingConsent)

0 commit comments

Comments
 (0)