Skip to content

Commit 823885b

Browse files
committed
INT-4088 ZookeeperLeaderTests: fix race condition
Two initiators for the same path, same `SmartLifecycleRoleController` and, finally, same `adapter`. So, one initiator after `yield()` stops the `adapter` and at the same time another starts it. Since there is no barrier in between events and assertion, we end up with an early "re-granting". * Add `CountDownLatch yieldBarrier` to `countDown()` after performing second `adapter.isRunning()` assert * `LeaderEventPublisher` waits for the `yieldBarrier` after the first `OnRevokedEvent`
1 parent 8036a4b commit 823885b

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

spring-integration-zookeeper/src/test/java/org/springframework/integration/zookeeper/event/ZookeeperLeaderTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import java.util.Collections;
2626
import java.util.concurrent.BlockingQueue;
27+
import java.util.concurrent.CountDownLatch;
2728
import java.util.concurrent.LinkedBlockingQueue;
2829
import java.util.concurrent.TimeUnit;
2930

@@ -62,6 +63,8 @@ public class ZookeeperLeaderTests extends ZookeeperTestSupport {
6263
private final SmartLifecycleRoleController controller = new SmartLifecycleRoleController(
6364
Collections.singletonList("sitest"), Collections.<SmartLifecycle>singletonList(this.adapter));
6465

66+
private final CountDownLatch yieldBarrier = new CountDownLatch(1);
67+
6568
@Test
6669
public void testLeader() throws Exception {
6770
assertFalse(this.adapter.isRunning());
@@ -87,6 +90,8 @@ public void testLeader() throws Exception {
8790

8891
assertFalse(this.adapter.isRunning());
8992

93+
this.yieldBarrier.countDown();
94+
9095
event = this.events.poll(30, TimeUnit.SECONDS);
9196
assertNotNull(event);
9297
assertThat(event, instanceOf(OnGrantedEvent.class));
@@ -105,13 +110,25 @@ public void testLeader() throws Exception {
105110
private LeaderEventPublisher publisher() {
106111
return new DefaultLeaderEventPublisher(new ApplicationEventPublisher() {
107112

113+
volatile boolean onRevokedEventHappened;
114+
108115
@Override
109116
public void publishEvent(Object event) {
110117
}
111118

112119
@Override
113120
public void publishEvent(ApplicationEvent event) {
114121
AbstractLeaderEvent leadershipEvent = (AbstractLeaderEvent) event;
122+
if (this.onRevokedEventHappened) {
123+
try {
124+
yieldBarrier.await(10, TimeUnit.SECONDS);
125+
}
126+
catch (InterruptedException e) {
127+
Thread.currentThread().interrupt();
128+
throw new RuntimeException(e);
129+
}
130+
}
131+
onRevokedEventHappened = event instanceof OnRevokedEvent;
115132
controller.onApplicationEvent((AbstractLeaderEvent) event);
116133
events.add(leadershipEvent);
117134
}

0 commit comments

Comments
 (0)