From cd487b500f4e50fe6b96780d52f04d7fadfe0738 Mon Sep 17 00:00:00 2001 From: Ivan Malygin Date: Fri, 3 Nov 2023 15:41:46 -0400 Subject: [PATCH] 9559 Improved asserts for latches. Signed-off-by: Ivan Malygin --- .../MerkleDbCompactionCoordinatorTest.java | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/platform-sdk/swirlds-jasperdb/src/test/java/com/swirlds/merkledb/MerkleDbCompactionCoordinatorTest.java b/platform-sdk/swirlds-jasperdb/src/test/java/com/swirlds/merkledb/MerkleDbCompactionCoordinatorTest.java index 1018b9d8706d..766c09f72c90 100644 --- a/platform-sdk/swirlds-jasperdb/src/test/java/com/swirlds/merkledb/MerkleDbCompactionCoordinatorTest.java +++ b/platform-sdk/swirlds-jasperdb/src/test/java/com/swirlds/merkledb/MerkleDbCompactionCoordinatorTest.java @@ -36,6 +36,7 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -170,16 +171,16 @@ void testCompactPathToKeyValueAsync_compactionDisabled() throws IOException, Int void testCompactionCancelled() throws IOException, InterruptedException { CountDownLatch compactLatch = new CountDownLatch(1); CountDownLatch testLatch = new CountDownLatch(3); - initCompactorMock(objectKeyToPath, nextBoolean(), testLatch, compactLatch); - initCompactorMock(pathToHashKeyValue, nextBoolean(), testLatch, compactLatch); - initCompactorMock(hashStoreDisk, nextBoolean(), testLatch, compactLatch); + initCompactorMock(objectKeyToPath, nextBoolean(), testLatch, compactLatch, new AtomicBoolean()); + initCompactorMock(pathToHashKeyValue, nextBoolean(), testLatch, compactLatch, new AtomicBoolean()); + initCompactorMock(hashStoreDisk, nextBoolean(), testLatch, compactLatch, new AtomicBoolean()); coordinator.compactDiskStoreForObjectKeyToPathAsync(); coordinator.compactDiskStoreForHashesAsync(); coordinator.compactPathToKeyValueAsync(); // let all compactions get to the latch - assertAwait(testLatch); + assertTrue(await(testLatch), "Test latch wasn't released"); // latch is released by interruption of the compaction thread coordinator.stopAndDisableBackgroundCompaction(); @@ -216,12 +217,15 @@ void testCompactionWithNullNullables() throws IOException, InterruptedException CountDownLatch compactLatch = new CountDownLatch(1); CountDownLatch testLatch = new CountDownLatch(1); - initCompactorMock(pathToHashKeyValue, nextBoolean(), testLatch, compactLatch); + AtomicBoolean compactLatchAwaitResult = new AtomicBoolean(false); + initCompactorMock(pathToHashKeyValue, nextBoolean(), testLatch, compactLatch, compactLatchAwaitResult); coordinator.compactPathToKeyValueAsync(); // let all compactions get to the latch - assertAwait(testLatch); + assertTrue(await(testLatch), "Test latch wasn't released"); + compactLatch.countDown(); + assertEventuallyTrue(compactLatchAwaitResult::get, Duration.ofSeconds(1), "Compaction latch wasn't released"); // latch is released by interruption of the compaction thread coordinator.stopAndDisableBackgroundCompaction(); @@ -253,20 +257,25 @@ private void testCompaction( throws IOException, InterruptedException { CountDownLatch testLatch = new CountDownLatch(1); CountDownLatch compactLatch = new CountDownLatch(1); - initCompactorMock(compactorToTest, compactionPassed, testLatch, compactLatch); + AtomicBoolean compactLatchAwaitResult = new AtomicBoolean(false); + initCompactorMock(compactorToTest, compactionPassed, testLatch, compactLatch, compactLatchAwaitResult); // run twice to make sure that the second call is discarded because one compaction is already in progress methodCall.run(); methodCall.run(); if (expectCompactionStarted) { - assertAwait(testLatch); + assertTrue(await(testLatch), "Test latch wasn't released"); } compactLatch.countDown(); + if (expectCompactionStarted) { + assertEventuallyTrue( + compactLatchAwaitResult::get, Duration.ofSeconds(1), "Compaction latch wasn't released"); + } assertCompactable(compactorToTest, expectCompactionStarted); reset(objectKeyToPath, pathToHashKeyValue, hashStoreDisk, statisticsUpdater); - initCompactorMock(compactorToTest, compactionPassed, testLatch, compactLatch); + initCompactorMock(compactorToTest, compactionPassed, testLatch, compactLatch, new AtomicBoolean()); // the second time it should succeed as well methodCall.run(); @@ -277,7 +286,7 @@ private void testCompactionFailed(DataFileCompactor compactorToTest, Runnable me throws IOException, InterruptedException { CountDownLatch latch = new CountDownLatch(1); when(compactorToTest.compact()).thenAnswer(invocation -> { - assertAwait(latch); + await(latch); throw new RuntimeException("testCompactionFailed"); }); @@ -297,8 +306,8 @@ private void testCompactionFailed(DataFileCompactor compactorToTest, Runnable me "Unexpected mock state"); } - private static void assertAwait(CountDownLatch latch) throws InterruptedException { - assertTrue(latch.await(10, TimeUnit.SECONDS), "Latch wasn't released"); + private boolean await(CountDownLatch latch) throws InterruptedException { + return latch.await(500, TimeUnit.MILLISECONDS); } @SuppressWarnings("unchecked") @@ -306,11 +315,12 @@ private void initCompactorMock( DataFileCompactor compactorToTest, boolean compactionPassed, CountDownLatch testLatch, - CountDownLatch compactLatch) + CountDownLatch compactLatch, + AtomicBoolean awaitResult) throws IOException, InterruptedException { when(compactorToTest.compact()).thenAnswer(invocation -> { testLatch.countDown(); - assertAwait(compactLatch); + awaitResult.set(await(compactLatch)); return compactionPassed; }); }