Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun committed Dec 21, 2024
1 parent a054902 commit 5d9043f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .mvn/extensions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<extension>
<groupId>com.gradle</groupId>
<artifactId>develocity-maven-extension</artifactId>
<version>1.21.4</version>
<version>1.23</version>
</extension>
<extension>
<groupId>com.gradle</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,18 +624,21 @@ public void processResult(CuratorFramework client, CuratorEvent event) throws Ex
client.getChildren().inBackground(callback).forPath(ZKPaths.makePath(latchPath, null));
}

@VisibleForTesting
volatile CountDownLatch debugHandleReconnectedLatch = null;

@VisibleForTesting
protected void handleStateChange(ConnectionState newState) {
switch (newState) {
case RECONNECTED: {
try {
if (client.getConnectionStateErrorPolicy().isErrorState(ConnectionState.SUSPENDED)
|| !hasLeadership.get()) {
getChildren();
if (debugHandleReconnectedLatch != null) {
debugHandleReconnectedLatch.await();
}
getChildren();
} catch (Exception e) {
ThreadUtils.checkInterrupted(e);
log.error("failed to reset leader latch", e);
log.error("failed to recheck leadership on reconnected", e);
setLeadership(false);
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,63 @@ public void testUncreatedPathGetLeader() throws Exception {
}
}

// @see https://issues.apache.org/jira/browse/CURATOR-724
@Test
public void testGetChildrenHitsNoNode() throws Exception {
final String latchPath = "/testGetChildrenHitsNoNode";
final Timing2 timing = new Timing2();
final BlockingQueue<TestEvent> events0 = new LinkedBlockingQueue<>();
final BlockingQueue<TestEvent> events1 = new LinkedBlockingQueue<>();

final List<Closeable> closeableResources = new ArrayList<>();
try {
final String id0 = "id0";
final CuratorFramework client0 = createAndStartClient(server.getConnectString(), timing, id0, null);
closeableResources.add(client0);
final LeaderLatch latch0 = createAndStartLeaderLatch(client0, latchPath, id0, events0);
closeableResources.add(latch0);

assertThat(events0.poll(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS))
.isNotNull()
.isEqualTo(new TestEvent(id0, TestEventType.GAINED_LEADERSHIP));

final String id1 = "id1";
final CuratorFramework client1 = createAndStartClient(server.getConnectString(), timing, id1, null);
closeableResources.add(client1);
final LeaderLatch latch1 = createAndStartLeaderLatch(client1, latchPath, id1, events1);
closeableResources.add(latch1);

// wait for the non-leading LeaderLatch (i.e. latch1) instance to be done with its creation
// this call is time-consuming but necessary because we don't have a handle to detect the end of the reset
// call
timing.forWaiting().sleepABit();

assertTrue(latch0.hasLeadership());
assertFalse(latch1.hasLeadership());

// ensure we can observe the leadership transferred to latch1
latch0.debugHandleReconnectedLatch = new CountDownLatch(1);

// scale to zero - recreate the cluster
final int port = server.getPort();
server.close();
server = new TestingServer(port, true);

assertThat(events1.poll(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS))
.isNotNull()
.isEqualTo(new TestEvent(id1, TestEventType.GAINED_LEADERSHIP));

latch0.debugHandleReconnectedLatch.countDown();
assertThat(events0.poll(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS))
.isNotNull()
.isEqualTo(new TestEvent(id0, TestEventType.LOST_LEADERSHIP));
} finally {
// reverse is necessary for closing the LeaderLatch instances before closing the corresponding client
Collections.reverse(closeableResources);
closeableResources.forEach(CloseableUtils::closeQuietly);
}
}

@Test
public void testWatchedNodeDeletedOnReconnect() throws Exception {
final String latchPath = "/foo/bar";
Expand Down

0 comments on commit 5d9043f

Please sign in to comment.