diff --git a/smoke-tests/apps/SamplingOverrides/build.gradle.kts b/smoke-tests/apps/SamplingOverrides/build.gradle.kts index 1b1cfb106e9..e87c7fd988a 100644 --- a/smoke-tests/apps/SamplingOverrides/build.gradle.kts +++ b/smoke-tests/apps/SamplingOverrides/build.gradle.kts @@ -4,4 +4,6 @@ plugins { dependencies { implementation("org.hsqldb:hsqldb:2.5.1") + + smokeTestImplementation("org.awaitility:awaitility:4.2.0") } diff --git a/smoke-tests/apps/SamplingOverrides/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SamplingOverrides4Test.java b/smoke-tests/apps/SamplingOverrides/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SamplingOverrides4Test.java index 6f3734f13a7..08afa746188 100644 --- a/smoke-tests/apps/SamplingOverrides/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SamplingOverrides4Test.java +++ b/smoke-tests/apps/SamplingOverrides/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SamplingOverrides4Test.java @@ -15,9 +15,8 @@ import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_8_OPENJ9; import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.WILDFLY_13_JAVA_8; import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.WILDFLY_13_JAVA_8_OPENJ9; -import static java.util.concurrent.TimeUnit.NANOSECONDS; -import static java.util.concurrent.TimeUnit.SECONDS; import static org.assertj.core.api.Assertions.assertThat; +import static org.awaitility.Awaitility.await; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; @@ -30,25 +29,20 @@ abstract class SamplingOverrides4Test { @Test @TargetUri(value = "/health-check", callCount = 100) void testSampling() throws Exception { - // super super low chance that number of sampled requests is less than 25 - long start = System.nanoTime(); - while (testing.mockedIngestion.getCountForType("RequestData") < 25 - && NANOSECONDS.toSeconds(System.nanoTime() - start) < 10) {} - // wait ten more seconds to before checking that we didn't receive too many - Thread.sleep(SECONDS.toMillis(10)); - int requestCount = testing.mockedIngestion.getCountForType("RequestData"); - int dependencyCount = testing.mockedIngestion.getCountForType("RemoteDependencyData"); - int logCount = testing.mockedIngestion.getCountForType("MessageData"); - // super super low chance that number of sampled requests/dependencies/traces - // is less than 25 or greater than 75 - assertThat(requestCount).isGreaterThanOrEqualTo(25); - assertThat(requestCount).isLessThanOrEqualTo(75); - // super super low chance that number of sampled dependencies/traces - // is less than 2 or greater than 20 - assertThat(dependencyCount).isGreaterThanOrEqualTo(2); - assertThat(dependencyCount).isLessThanOrEqualTo(20); - assertThat(logCount).isGreaterThanOrEqualTo(2); - assertThat(logCount).isLessThanOrEqualTo(20); + await() + .untilAsserted( + () -> { + int requestCount = testing.mockedIngestion.getCountForType("RequestData"); + int dependencyCount = testing.mockedIngestion.getCountForType("RemoteDependencyData"); + int logCount = testing.mockedIngestion.getCountForType("MessageData"); + + assertThat(requestCount).isGreaterThanOrEqualTo(25); + assertThat(requestCount).isLessThanOrEqualTo(75); + assertThat(dependencyCount).isGreaterThanOrEqualTo(2); + assertThat(dependencyCount).isLessThanOrEqualTo(20); + assertThat(logCount).isGreaterThanOrEqualTo(2); + assertThat(logCount).isLessThanOrEqualTo(20); + }); testing .mockedIngestion