Skip to content

Commit

Permalink
Add countdownlatch in test to coordinate the thread to avoid concuree…
Browse files Browse the repository at this point in the history
…ncy issue caused test failure

Signed-off-by: zane-neo <[email protected]>
  • Loading branch information
zane-neo committed Oct 16, 2024
1 parent af9d70d commit 7f24452
Showing 1 changed file with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -139,11 +141,24 @@ private void assertPassphraseRead(String source, String expected) {

public void testInitExecutionOrder() throws Exception {
AtomicInteger order = new AtomicInteger(0);

Thread mockThread = new Thread(() -> { assertEquals(0, order.getAndIncrement()); });
CountDownLatch countDownLatch = new CountDownLatch(1);
Thread mockThread = new Thread(() -> {
assertEquals(0, order.getAndIncrement());
countDownLatch.countDown();
});

Node mockNode = mock(Node.class);
doAnswer(invocation -> {
try {
boolean threadStarted = countDownLatch.await(1000, TimeUnit.MILLISECONDS);
assertTrue(
"Waited for one second but the keepAliveThread isn't started, please check the execution order of"
+ "keepAliveThread.start and node.start",
threadStarted
);
} catch (InterruptedException e) {
fail("Thread interrupted");
}
assertEquals(1, order.getAndIncrement());
return null;
}).when(mockNode).start();
Expand Down

0 comments on commit 7f24452

Please sign in to comment.