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
This commit is contained in:
Artem Bilan
2017-04-06 10:47:22 -04:00
parent 903aca1e7b
commit d5c633d703

View File

@@ -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();