Skip to content

Commit d79cff0

Browse files
authored
Hopefully resolve sporadic test failure (#4309)
1 parent 985573c commit d79cff0

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed

smoke-tests/apps/SamplingOverrides/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ plugins {
44

55
dependencies {
66
implementation("org.hsqldb:hsqldb:2.5.1")
7+
8+
smokeTestImplementation("org.awaitility:awaitility:4.2.0")
79
}

smoke-tests/apps/SamplingOverrides/src/smokeTest/java/com/microsoft/applicationinsights/smoketest/SamplingOverrides4Test.java

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.TOMCAT_8_JAVA_8_OPENJ9;
1616
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.WILDFLY_13_JAVA_8;
1717
import static com.microsoft.applicationinsights.smoketest.EnvironmentValue.WILDFLY_13_JAVA_8_OPENJ9;
18-
import static java.util.concurrent.TimeUnit.NANOSECONDS;
19-
import static java.util.concurrent.TimeUnit.SECONDS;
2018
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.awaitility.Awaitility.await;
2120

2221
import org.junit.jupiter.api.Test;
2322
import org.junit.jupiter.api.extension.RegisterExtension;
@@ -30,25 +29,20 @@ abstract class SamplingOverrides4Test {
3029
@Test
3130
@TargetUri(value = "/health-check", callCount = 100)
3231
void testSampling() throws Exception {
33-
// super super low chance that number of sampled requests is less than 25
34-
long start = System.nanoTime();
35-
while (testing.mockedIngestion.getCountForType("RequestData") < 25
36-
&& NANOSECONDS.toSeconds(System.nanoTime() - start) < 10) {}
37-
// wait ten more seconds to before checking that we didn't receive too many
38-
Thread.sleep(SECONDS.toMillis(10));
39-
int requestCount = testing.mockedIngestion.getCountForType("RequestData");
40-
int dependencyCount = testing.mockedIngestion.getCountForType("RemoteDependencyData");
41-
int logCount = testing.mockedIngestion.getCountForType("MessageData");
42-
// super super low chance that number of sampled requests/dependencies/traces
43-
// is less than 25 or greater than 75
44-
assertThat(requestCount).isGreaterThanOrEqualTo(25);
45-
assertThat(requestCount).isLessThanOrEqualTo(75);
46-
// super super low chance that number of sampled dependencies/traces
47-
// is less than 2 or greater than 20
48-
assertThat(dependencyCount).isGreaterThanOrEqualTo(2);
49-
assertThat(dependencyCount).isLessThanOrEqualTo(20);
50-
assertThat(logCount).isGreaterThanOrEqualTo(2);
51-
assertThat(logCount).isLessThanOrEqualTo(20);
32+
await()
33+
.untilAsserted(
34+
() -> {
35+
int requestCount = testing.mockedIngestion.getCountForType("RequestData");
36+
int dependencyCount = testing.mockedIngestion.getCountForType("RemoteDependencyData");
37+
int logCount = testing.mockedIngestion.getCountForType("MessageData");
38+
39+
assertThat(requestCount).isGreaterThanOrEqualTo(25);
40+
assertThat(requestCount).isLessThanOrEqualTo(75);
41+
assertThat(dependencyCount).isGreaterThanOrEqualTo(2);
42+
assertThat(dependencyCount).isLessThanOrEqualTo(20);
43+
assertThat(logCount).isGreaterThanOrEqualTo(2);
44+
assertThat(logCount).isLessThanOrEqualTo(20);
45+
});
5246

5347
testing
5448
.mockedIngestion

0 commit comments

Comments
 (0)