From 26a57a24ab81c62ec68fb227137431997569e9be Mon Sep 17 00:00:00 2001 From: Krishnan Mahadevan Date: Thu, 21 Mar 2024 14:32:53 +0530 Subject: [PATCH] Fixing flaky tests --- .../listeners/issue2916/ElaborateSampleTestCase.java | 5 +++-- .../java/test/thread/issue188/Issue188TestSample.java | 10 +++++++++- .../src/test/java/test/thread/issue188/IssueTest.java | 7 +++++-- .../test/java/org/testng/test/osgi/PlainOsgiTest.java | 4 +++- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/testng-core/src/test/java/test/listeners/issue2916/ElaborateSampleTestCase.java b/testng-core/src/test/java/test/listeners/issue2916/ElaborateSampleTestCase.java index 2a8831ea99..ef0b4c9e7e 100644 --- a/testng-core/src/test/java/test/listeners/issue2916/ElaborateSampleTestCase.java +++ b/testng-core/src/test/java/test/listeners/issue2916/ElaborateSampleTestCase.java @@ -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(); } } diff --git a/testng-core/src/test/java/test/thread/issue188/Issue188TestSample.java b/testng-core/src/test/java/test/thread/issue188/Issue188TestSample.java index d19c692b47..d9a354a381 100644 --- a/testng-core/src/test/java/test/thread/issue188/Issue188TestSample.java +++ b/testng-core/src/test/java/test/thread/issue188/Issue188TestSample.java @@ -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; + } } diff --git a/testng-core/src/test/java/test/thread/issue188/IssueTest.java b/testng-core/src/test/java/test/thread/issue188/IssueTest.java index d9afc6c37e..90104ac2a0 100644 --- a/testng-core/src/test/java/test/thread/issue188/IssueTest.java +++ b/testng-core/src/test/java/test/thread/issue188/IssueTest.java @@ -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; } } diff --git a/testng-test-osgi/src/test/java/org/testng/test/osgi/PlainOsgiTest.java b/testng-test-osgi/src/test/java/org/testng/test/osgi/PlainOsgiTest.java index 6f8635a695..05033dab2a 100644 --- a/testng-test-osgi/src/test/java/org/testng/test/osgi/PlainOsgiTest.java +++ b/testng-test-osgi/src/test/java/org/testng/test/osgi/PlainOsgiTest.java @@ -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");