Skip to content

Commit 32cc7ea

Browse files
committed
fix(test): fix timing flakes in ChronometerTest and ThroughputControlTestCase
ChronometerTest: - Add @BeforeAll warmUp() (5 × sleep(20) iterations) to prime JVM timer threads before the actual assertions run - Increase TOLERANCE 200ms → 1000ms to absorb OS scheduler jitter when running the full suite under load; still reliably catches real bugs (elapsed returning 0, wrong units, negative values, etc.) ThroughputControlTestCase.testFifty: - Widen upper bound 4500ms → 6000ms; 10 tps × 50 tx guarantees exactly 4000ms minimum but the 500ms ceiling was too tight on a loaded machine (failed at 4586ms)
1 parent ebfa2ca commit 32cc7ea

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

jpos/src/test/java/org/jpos/util/ChronometerTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,34 @@
1818

1919
package org.jpos.util;
2020

21+
import org.junit.jupiter.api.BeforeAll;
2122
import org.junit.jupiter.api.Test;
2223

2324
import static org.junit.jupiter.api.Assertions.assertTrue;
2425

2526
public class ChronometerTest {
26-
private long TOLERANCE = 200L;
27+
// Generous tolerance to absorb JVM cold-start and OS scheduler jitter,
28+
// especially when running the full test suite under load.
29+
// A 1-second ceiling on a 25 ms sleep still reliably catches bugs where
30+
// elapsed() returns 0, wraps, measures in wrong units, etc.
31+
private long TOLERANCE = 1000L;
32+
33+
/**
34+
* Prime the JVM timer infrastructure before any timing-sensitive tests run.
35+
* Without this, the very first Thread.sleep() can measure several hundred
36+
* milliseconds due to JVM class-loading and timer-thread initialisation,
37+
* causing spurious failures in the assertions below.
38+
*/
39+
@BeforeAll
40+
static void warmUp() throws InterruptedException {
41+
for (int i = 0; i < 5; i++) {
42+
Chronometer w = new Chronometer();
43+
Thread.sleep(20);
44+
w.elapsed();
45+
w.lap();
46+
}
47+
}
48+
2749
@Test
2850
public void testElapsed() throws InterruptedException {
2951
Chronometer c = new Chronometer();

jpos/src/test/java/org/jpos/util/ThroughputControlTestCase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ public void testFifty () throws Exception {
6767
"50 transactions should take at least 4 seconds but took " + elapsed
6868
);
6969
assertTrue(
70-
elapsed < 4500L,
71-
"50 transactions shouldn't take more than approximately 4.5 seconds but took " + elapsed
70+
elapsed < 6000L,
71+
"50 transactions shouldn't take more than approximately 6 seconds but took " + elapsed
7272
);
7373

7474

0 commit comments

Comments
 (0)