use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestLeaderSelector method testLeaderNodeDeleteOnInterrupt.
@Test
public void testLeaderNodeDeleteOnInterrupt() throws Exception {
Timing timing = new Timing();
LeaderSelector selector = null;
CuratorFramework client = null;
try {
client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
final CountDownLatch reconnectedLatch = new CountDownLatch(1);
ConnectionStateListener connectionStateListener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (newState == ConnectionState.RECONNECTED) {
reconnectedLatch.countDown();
}
}
};
client.getConnectionStateListenable().addListener(connectionStateListener);
client.start();
final BlockingQueue<Thread> queue = new ArrayBlockingQueue<Thread>(1);
LeaderSelectorListener listener = new LeaderSelectorListener() {
@Override
public void takeLeadership(CuratorFramework client) throws Exception {
queue.add(Thread.currentThread());
try {
Thread.currentThread().join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
}
};
selector = new LeaderSelector(client, "/leader", listener);
selector.start();
Thread leaderThread = queue.take();
server.stop();
leaderThread.interrupt();
server.restart();
Assert.assertTrue(timing.awaitLatch(reconnectedLatch));
timing.sleepABit();
Assert.assertEquals(client.getChildren().forPath("/leader").size(), 0);
} finally {
CloseableUtils.closeQuietly(selector);
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestLeaderSelector method testKillServerThenCloseShouldElectNewLeader.
/**
* This is similar to TestLeaderSelector.testKillSessionThenCloseShouldElectNewLeader
* The differences are:
* it restarts the TestingServer instead of killing the session
* it uses autoRequeue instead of explicitly calling requeue
*/
@Test
public void testKillServerThenCloseShouldElectNewLeader() throws Exception {
final Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
client.start();
try {
final Semaphore semaphore = new Semaphore(0);
final CountDownLatch interruptedLatch = new CountDownLatch(1);
final AtomicInteger leaderCount = new AtomicInteger(0);
LeaderSelectorListener listener = new LeaderSelectorListenerAdapter() {
@Override
public void takeLeadership(CuratorFramework client) throws Exception {
leaderCount.incrementAndGet();
try {
semaphore.release();
try {
Thread.currentThread().join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
interruptedLatch.countDown();
}
} finally {
leaderCount.decrementAndGet();
}
}
};
LeaderSelector leaderSelector1 = new LeaderSelector(client, PATH_NAME, listener);
LeaderSelector leaderSelector2 = new LeaderSelector(client, PATH_NAME, listener);
boolean leaderSelector1Closed = false;
boolean leaderSelector2Closed = false;
leaderSelector1.autoRequeue();
leaderSelector2.autoRequeue();
leaderSelector1.start();
leaderSelector2.start();
Assert.assertTrue(timing.acquireSemaphore(semaphore, 1));
int port = server.getPort();
server.stop();
timing.sleepABit();
server = new TestingServer(port);
Assert.assertTrue(timing.awaitLatch(interruptedLatch));
timing.sleepABit();
Assert.assertTrue(timing.acquireSemaphore(semaphore, 1));
Assert.assertEquals(leaderCount.get(), 1);
if (leaderSelector1.hasLeadership()) {
leaderSelector1.close();
leaderSelector1Closed = true;
} else if (leaderSelector2.hasLeadership()) {
leaderSelector2.close();
leaderSelector2Closed = true;
} else {
fail("No leaderselector has leadership!");
}
// Verify that the other leader took over leadership.
Assert.assertTrue(timing.acquireSemaphore(semaphore, 1));
Assert.assertEquals(leaderCount.get(), 1);
if (!leaderSelector1Closed) {
leaderSelector1.close();
}
if (!leaderSelector2Closed) {
leaderSelector2.close();
}
} finally {
client.close();
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestLeaderSelector method testInterruptLeadership.
@Test
public void testInterruptLeadership() throws Exception {
LeaderSelector selector = null;
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
client.start();
final CountDownLatch isLeaderLatch = new CountDownLatch(1);
final CountDownLatch losingLeaderLatch = new CountDownLatch(1);
LeaderSelectorListener listener = new LeaderSelectorListener() {
@Override
public void takeLeadership(CuratorFramework client) throws Exception {
isLeaderLatch.countDown();
try {
Thread.currentThread().join();
} finally {
losingLeaderLatch.countDown();
}
}
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
}
};
selector = new LeaderSelector(client, "/leader", listener);
selector.start();
Assert.assertTrue(timing.awaitLatch(isLeaderLatch));
selector.interruptLeadership();
Assert.assertTrue(timing.awaitLatch(losingLeaderLatch));
} finally {
CloseableUtils.closeQuietly(selector);
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestLeaderSelector method testKillSessionThenCloseShouldElectNewLeader.
@Test
public void testKillSessionThenCloseShouldElectNewLeader() throws Exception {
final Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
client.start();
try {
final Semaphore semaphore = new Semaphore(0);
final CountDownLatch interruptedLatch = new CountDownLatch(1);
final AtomicInteger leaderCount = new AtomicInteger(0);
LeaderSelectorListener listener = new LeaderSelectorListenerAdapter() {
@Override
public void takeLeadership(CuratorFramework client) throws Exception {
leaderCount.incrementAndGet();
try {
semaphore.release();
try {
Thread.currentThread().join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
interruptedLatch.countDown();
}
} finally {
leaderCount.decrementAndGet();
}
}
};
LeaderSelector leaderSelector1 = new LeaderSelector(client, PATH_NAME, listener);
LeaderSelector leaderSelector2 = new LeaderSelector(client, PATH_NAME, listener);
boolean leaderSelector1Closed = false;
boolean leaderSelector2Closed = false;
leaderSelector1.start();
leaderSelector2.start();
Assert.assertTrue(timing.acquireSemaphore(semaphore, 1));
KillSession.kill(client.getZookeeperClient().getZooKeeper(), server.getConnectString());
Assert.assertTrue(timing.awaitLatch(interruptedLatch));
timing.sleepABit();
boolean requeued1 = leaderSelector1.requeue();
boolean requeued2 = leaderSelector2.requeue();
Assert.assertTrue(requeued1);
Assert.assertTrue(requeued2);
Assert.assertTrue(timing.acquireSemaphore(semaphore, 1));
Assert.assertEquals(leaderCount.get(), 1);
if (leaderSelector1.hasLeadership()) {
leaderSelector1.close();
leaderSelector1Closed = true;
} else if (leaderSelector2.hasLeadership()) {
leaderSelector2.close();
leaderSelector2Closed = true;
} else {
fail("No leaderselector has leadership!");
}
// Verify that the other leader took over leadership.
Assert.assertTrue(timing.acquireSemaphore(semaphore, 1));
Assert.assertEquals(leaderCount.get(), 1);
if (!leaderSelector1Closed) {
leaderSelector1.close();
}
if (!leaderSelector2Closed) {
leaderSelector2.close();
}
} finally {
client.close();
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestLeaderSelectorWithExecutor method test.
@Test
public void test() throws Exception {
Timing timing = new Timing();
LeaderSelector leaderSelector = null;
CuratorFramework client = CuratorFrameworkFactory.builder().retryPolicy(new ExponentialBackoffRetry(100, 3)).connectString(server.getConnectString()).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).build();
try {
client.start();
MyLeaderSelectorListener listener = new MyLeaderSelectorListener();
ExecutorService executorPool = Executors.newFixedThreadPool(20);
leaderSelector = new LeaderSelector(client, "/test", threadFactory, executorPool, listener);
leaderSelector.autoRequeue();
leaderSelector.start();
timing.sleepABit();
Assert.assertEquals(listener.getLeaderCount(), 1);
} finally {
CloseableUtils.closeQuietly(leaderSelector);
CloseableUtils.closeQuietly(client);
}
}
Aggregations