From d5c633d7032bfd8bcc86f115535a25f8316278db Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Thu, 6 Apr 2017 10:47:22 -0400 Subject: [PATCH] Fix LockRegLeaderInitiatorTests race condition https://build.spring.io/browse/INT-FATS5IC-129/ Since leader selection is done on background thread in the `LockRegistryLeaderInitiatorTests`, there is no guarantee that after `Thread.sleep(100)` the lock will be obtained and leader state is changed. * Waiting for the `granted` `CountDownLatch` is much robust condition --- .../leader/LockRegistryLeaderInitiatorTests.java | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/spring-integration-core/src/test/java/org/springframework/integration/support/leader/LockRegistryLeaderInitiatorTests.java b/spring-integration-core/src/test/java/org/springframework/integration/support/leader/LockRegistryLeaderInitiatorTests.java index a2b4a2ad21..a5b9307b5d 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/support/leader/LockRegistryLeaderInitiatorTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/support/leader/LockRegistryLeaderInitiatorTests.java @@ -70,12 +70,12 @@ public class LockRegistryLeaderInitiatorTests { assertThat(this.initiator.getContext().isLeader(), is(false)); this.initiator.start(); assertThat(this.initiator.isRunning(), is(true)); - this.granted.await(10, TimeUnit.SECONDS); + assertTrue(this.granted.await(10, TimeUnit.SECONDS)); assertThat(this.initiator.getContext().isLeader(), is(true)); Thread.sleep(200L); assertThat(this.initiator.getContext().isLeader(), is(true)); this.initiator.stop(); - this.revoked.await(10, TimeUnit.SECONDS); + assertTrue(this.revoked.await(10, TimeUnit.SECONDS)); assertThat(this.initiator.getContext().isLeader(), is(false)); } @@ -84,7 +84,7 @@ public class LockRegistryLeaderInitiatorTests { assertThat(this.initiator.getContext().isLeader(), is(false)); this.initiator.start(); assertThat(this.initiator.isRunning(), is(true)); - this.granted.await(10, TimeUnit.SECONDS); + assertTrue(this.granted.await(10, TimeUnit.SECONDS)); assertThat(this.initiator.getContext().isLeader(), is(true)); this.initiator.getContext().yield(); assertThat(this.revoked.await(10, TimeUnit.SECONDS), is(true)); @@ -173,23 +173,19 @@ public class LockRegistryLeaderInitiatorTests { first.start(); second.start(); - Thread.sleep(100); - // first initiator should lead and publish granted event + assertThat(firstGranted.await(10, TimeUnit.SECONDS), is(true)); assertThat(first.getContext().isLeader(), is(true)); assertThat(second.getContext().isLeader(), is(false)); - assertThat(firstGranted.await(10, TimeUnit.SECONDS), is(true)); // simulate first registry instance unable to obtain lock, for example due to lock timeout firstLocked.set(false); - Thread.sleep(100); - // second initiator should take lead and publish granted event, first initiator should publish revoked event - assertThat(second.getContext().isLeader(), is(true)); - assertThat(first.getContext().isLeader(), is(false)); assertThat(secondGranted.await(10, TimeUnit.SECONDS), is(true)); assertThat(firstRevoked.await(10, TimeUnit.SECONDS), is(true)); + assertThat(second.getContext().isLeader(), is(true)); + assertThat(first.getContext().isLeader(), is(false)); first.stop(); second.stop();