From 91b8e6068bf9150bf35581ba92ca394c6f26d585 Mon Sep 17 00:00:00 2001 From: Valera V Harseko Date: Fri, 10 Jul 2026 10:07:04 +0300 Subject: [PATCH 1/2] Stabilize flaky CleanupManagerTest.testCleanupHappens The test offered 500 cleanup actions and waited only while getEnqueuedCount() > 0. That counter drops to 0 as soon as the background CLEANUP_MANAGER thread dequeues the batch -- before performAction runs -- so on a slow runner the wait loop exited with no action yet performed and _counter still 0 (expected:<500> but was:<0>, seen on ubuntu-latest JDK 17). Wait on the manager's own performed/error counters (which advance only after each action completes) until all 500 are processed, with a 30s cap. --- .../test/java/com/persistit/CleanupManagerTest.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/persistit/core/src/test/java/com/persistit/CleanupManagerTest.java b/persistit/core/src/test/java/com/persistit/CleanupManagerTest.java index a8760dd5b..ed6144e1d 100644 --- a/persistit/core/src/test/java/com/persistit/CleanupManagerTest.java +++ b/persistit/core/src/test/java/com/persistit/CleanupManagerTest.java @@ -91,8 +91,17 @@ public void testCleanupHappens() throws Exception { cm().offer(new CleanupMockAction(i)); } cm().setPollInterval(100); - for (int i = 0; i < 10 && cm().getEnqueuedCount() > 0; i++) { - Thread.sleep(1000); + /* + * Wait until the actions have actually been performed. getEnqueuedCount() + * drops to 0 as soon as the background CLEANUP_MANAGER thread dequeues the + * batch -- before performAction runs -- so looping on it can exit with the + * actions not yet run and _counter still 0 on a slow runner + * (expected:<500> but was:<0>). Poll the manager's own performed/error + * counters, which advance only after each action completes. + */ + final long expires = System.currentTimeMillis() + 30000; + while (cm().getPerformedCount() + cm().getErrorCount() < 500 && System.currentTimeMillis() < expires) { + Thread.sleep(50); } assertEquals(500, _counter); assertEquals(1, cm().getErrorCount()); From e525d94b173dcef3fcf16e96582419fdff0a3dcd Mon Sep 17 00:00:00 2001 From: Valera V Harseko Date: Sat, 11 Jul 2026 10:38:44 +0300 Subject: [PATCH 2/2] Compare cleanup counters against a baseline in testCleanupHappens The manager's performed/error counters are cumulative for the Persistit instance, and startup maintenance may already have performed cleanup actions before the test enqueues its own - in particular the pruneTimelyResources() call added to Persistit.initialize() by #266. Waiting for and asserting absolute values could then exit early or fail; take a baseline before enqueueing and compare deltas instead. --- .../java/com/persistit/CleanupManagerTest.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/persistit/core/src/test/java/com/persistit/CleanupManagerTest.java b/persistit/core/src/test/java/com/persistit/CleanupManagerTest.java index ed6144e1d..181ab513b 100644 --- a/persistit/core/src/test/java/com/persistit/CleanupManagerTest.java +++ b/persistit/core/src/test/java/com/persistit/CleanupManagerTest.java @@ -87,6 +87,16 @@ private static class ExpectedException extends PersistitException { @Test public void testCleanupHappens() throws Exception { + /* + * The manager's performed/error counters are cumulative for the + * Persistit instance, and startup maintenance may already have + * performed cleanup actions before this test enqueues its own (e.g. + * the pruneTimelyResources() call at the end of + * Persistit.initialize()). Compare against a baseline taken before + * enqueueing rather than against absolute values. + */ + final long performedBefore = cm().getPerformedCount(); + final long errorsBefore = cm().getErrorCount(); for (int i = 1; i <= 500; i++) { cm().offer(new CleanupMockAction(i)); } @@ -100,12 +110,13 @@ public void testCleanupHappens() throws Exception { * counters, which advance only after each action completes. */ final long expires = System.currentTimeMillis() + 30000; - while (cm().getPerformedCount() + cm().getErrorCount() < 500 && System.currentTimeMillis() < expires) { + while (cm().getPerformedCount() - performedBefore + cm().getErrorCount() - errorsBefore < 500 + && System.currentTimeMillis() < expires) { Thread.sleep(50); } assertEquals(500, _counter); - assertEquals(1, cm().getErrorCount()); - assertEquals(499, cm().getPerformedCount()); + assertEquals(1, cm().getErrorCount() - errorsBefore); + assertEquals(499, cm().getPerformedCount() - performedBefore); assertFalse("cleanup actions did not run in sequence order", _outOfOrder); }