diff --git a/persistit/core/src/test/java/com/persistit/CleanupManagerTest.java b/persistit/core/src/test/java/com/persistit/CleanupManagerTest.java index a8760dd5b..181ab513b 100644 --- a/persistit/core/src/test/java/com/persistit/CleanupManagerTest.java +++ b/persistit/core/src/test/java/com/persistit/CleanupManagerTest.java @@ -87,16 +87,36 @@ 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)); } 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() - 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); }