Skip to content

Commit

Permalink
Fixing flaky tests
Browse files Browse the repository at this point in the history
  • Loading branch information
krmahadevan committed Mar 21, 2024
1 parent 69dc232 commit 26a57a2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ public void flakyTest() {
}
}

@Test(timeOut = 2, priority = 4)
@Test(timeOut = 25, priority = 4)
public void timingOutTest() throws InterruptedException {
TimeUnit.MILLISECONDS.sleep(10);
TimeUnit.SECONDS.sleep(10);
Assert.fail();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,17 @@ public void anotherSampleTest() {

private void sleepSilently() {
try {
TimeUnit.MILLISECONDS.sleep(500 * random.nextInt(10));
TimeUnit.MILLISECONDS.sleep(500 * random());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}

private static long random() {
int value = random.nextInt(10);
if (value == 0) {
return 1;
}
return value;
}
}
7 changes: 5 additions & 2 deletions testng-core/src/test/java/test/thread/issue188/IssueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,20 @@ public void testSuiteLevelParallelMode() {
String allTimeStamps =
keyset.stream().map(Objects::toString).collect(Collectors.joining(","));
long prev = keyset.get(0);
int permissibleLag = 40;
for (int i = 1; i < keyset.size(); i++) {
long current = keyset.get(i);
long diff = current - prev;
Assertions.assertThat(diff)
.withFailMessage(
"Test methods should have started within a lag of max 40 ms but it was "
"Test methods should have started within a lag of max "
+ permissibleLag
+ " ms but it was "
+ diff
+ " ms ["
+ allTimeStamps
+ "]")
.isLessThanOrEqualTo(40);
.isLessThanOrEqualTo(permissibleLag);
prev = current;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public Option[] config() {
return options(defaultTestngOsgiOptions());
}

@Test
// TODO: Enable this test once the PR https://github.com/ops4j/org.ops4j.pax.exam2/pull/1112
// gets merged and there's a new release done.
@Test(enabled = false)
public void versionShouldStartWithDigit() throws Exception {
Class<?> versionClass = Class.forName("org.testng.internal.Version");
Method getVersionStringMethod = versionClass.getMethod("getVersionString");
Expand Down

0 comments on commit 26a57a2

Please sign in to comment.