use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestLeaderLatch method testCreateDeleteRace.
@Test
public void testCreateDeleteRace() throws Exception {
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
client.start();
client.create().creatingParentsIfNeeded().forPath(PATH_NAME);
LeaderLatch latch = new LeaderLatch(client, PATH_NAME);
latch.debugResetWaitLatch = new CountDownLatch(1);
latch.start();
latch.close();
timing.sleepABit();
latch.debugResetWaitLatch.countDown();
timing.sleepABit();
Assert.assertEquals(client.getChildren().forPath(PATH_NAME).size(), 0);
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestLeaderLatch method testLostConnection.
@Test
public void testLostConnection() throws Exception {
final int PARTICIPANT_QTY = 10;
List<LeaderLatch> latches = Lists.newArrayList();
final Timing timing = new Timing();
final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
client.start();
final CountDownLatch countDownLatch = new CountDownLatch(1);
client.getConnectionStateListenable().addListener(new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (newState == ConnectionState.LOST) {
countDownLatch.countDown();
}
}
});
for (int i = 0; i < PARTICIPANT_QTY; ++i) {
LeaderLatch latch = new LeaderLatch(client, PATH_NAME);
latch.start();
latches.add(latch);
}
waitForALeader(latches, timing);
server.stop();
Assert.assertTrue(timing.awaitLatch(countDownLatch));
timing.forWaiting().sleepABit();
Assert.assertEquals(getLeaders(latches).size(), 0);
server.restart();
// should reconnect
Assert.assertEquals(waitForALeader(latches, timing).size(), 1);
} finally {
for (LeaderLatch latch : latches) {
CloseableUtils.closeQuietly(latch);
}
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestLeaderLatch method testResetRace.
@Test
public void testResetRace() throws Exception {
Timing timing = new Timing();
LeaderLatch latch = null;
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
client.start();
latch = new LeaderLatch(client, PATH_NAME);
latch.debugResetWaitLatch = new CountDownLatch(1);
// will call reset()
latch.start();
// should not result in two nodes
latch.reset();
timing.sleepABit();
latch.debugResetWaitLatch.countDown();
timing.sleepABit();
Assert.assertEquals(client.getChildren().forPath(PATH_NAME).size(), 1);
} finally {
CloseableUtils.closeQuietly(latch);
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestLeaderLatch method testCorrectWatching.
@Test
public void testCorrectWatching() throws Exception {
final int PARTICIPANT_QTY = 10;
final int PARTICIPANT_ID = 2;
List<LeaderLatch> latches = Lists.newArrayList();
final Timing timing = new Timing();
final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
client.start();
for (int i = 0; i < PARTICIPANT_QTY; ++i) {
LeaderLatch latch = new LeaderLatch(client, PATH_NAME);
latch.start();
latches.add(latch);
}
waitForALeader(latches, timing);
// we need to close a Participant that doesn't be actual leader (first Participant) nor the last
latches.get(PARTICIPANT_ID).close();
// As the previous algorithm assumed that if the watched node is deleted gets the leadership
// we need to ensure that the PARTICIPANT_ID-1 is not getting (wrongly) elected as leader.
Assert.assertTrue(!latches.get(PARTICIPANT_ID - 1).hasLeadership());
} finally {
// removes the already closed participant
latches.remove(PARTICIPANT_ID);
for (LeaderLatch latch : latches) {
CloseableUtils.closeQuietly(latch);
}
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestLeaderLatch method testCallbackNotifyLeader.
@Test
public void testCallbackNotifyLeader() throws Exception {
final int PARTICIPANT_QTY = 10;
final int SILENT_QTY = 3;
final CountDownLatch timesSquare = new CountDownLatch(PARTICIPANT_QTY);
final AtomicLong masterCounter = new AtomicLong(0);
final AtomicLong notLeaderCounter = new AtomicLong(0);
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
ExecutorService exec = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setDaemon(true).setNameFormat("callbackNotifyLeader-%s").build());
List<LeaderLatch> latches = Lists.newArrayList();
for (int i = 0; i < PARTICIPANT_QTY; ++i) {
LeaderLatch.CloseMode closeMode = i < SILENT_QTY ? LeaderLatch.CloseMode.SILENT : LeaderLatch.CloseMode.NOTIFY_LEADER;
final LeaderLatch latch = new LeaderLatch(client, PATH_NAME, "", closeMode);
latch.addListener(new LeaderLatchListener() {
boolean beenLeader = false;
@Override
public void isLeader() {
if (!beenLeader) {
masterCounter.incrementAndGet();
beenLeader = true;
try {
latch.reset();
} catch (Exception e) {
throw Throwables.propagate(e);
}
} else {
masterCounter.incrementAndGet();
CloseableUtils.closeQuietly(latch);
timesSquare.countDown();
}
}
@Override
public void notLeader() {
notLeaderCounter.incrementAndGet();
}
}, exec);
latches.add(latch);
}
try {
client.start();
for (LeaderLatch latch : latches) {
latch.start();
}
timesSquare.await();
Assert.assertEquals(masterCounter.get(), PARTICIPANT_QTY * 2);
Assert.assertEquals(notLeaderCounter.get(), PARTICIPANT_QTY * 2 - SILENT_QTY);
for (LeaderLatch latch : latches) {
Assert.assertEquals(latch.getState(), LeaderLatch.State.CLOSED);
}
} finally {
for (LeaderLatch latch : latches) {
if (latch.getState() != LeaderLatch.State.CLOSED) {
CloseableUtils.closeQuietly(latch);
}
}
CloseableUtils.closeQuietly(client);
}
}
Aggregations