use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestBlockUntilConnected method testBlockUntilConnectedSessionExpired.
/**
* Test that we got disconnected before calling blockUntilConnected and we reconnect we receive a session expired event.
*/
@Test
public void testBlockUntilConnectedSessionExpired() throws Exception {
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).build();
final CountDownLatch lostLatch = new CountDownLatch(1);
client.getConnectionStateListenable().addListener(new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (newState == ConnectionState.LOST) {
lostLatch.countDown();
}
}
});
final CountDownLatch expiredLatch = new CountDownLatch(1);
client.getCuratorListenable().addListener(new CuratorListener() {
@Override
public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
if (event.getType() == CuratorEventType.WATCHED && event.getWatchedEvent().getState() == Watcher.Event.KeeperState.Expired) {
expiredLatch.countDown();
}
}
});
ConnectionStateAccessor.setDebugWaitOnExpiredForClient(client);
try {
client.start();
// Block until we're connected
Assert.assertTrue(client.blockUntilConnected(5, TimeUnit.SECONDS), "Failed to connect");
final long sessionTimeoutMs = client.getZookeeperClient().getConnectionTimeoutMs();
// Kill the server
CloseableUtils.closeQuietly(server);
// Wait until we hit the lost state
Assert.assertTrue(timing.awaitLatch(lostLatch), "Failed to reach LOST state");
Thread.sleep(sessionTimeoutMs);
server = new TestingServer(server.getPort(), server.getTempDirectory());
// Wait until we get expired event
Assert.assertTrue(timing.awaitLatch(expiredLatch), "Failed to get Expired event");
final boolean blockUntilConnected5Seconds = client.blockUntilConnected(5, TimeUnit.SECONDS);
Assert.assertTrue(client.getZookeeperClient().isConnected(), "ConnectionState.isConnected returned false");
Assert.assertTrue(blockUntilConnected5Seconds, "BlockUntilConnected returned false");
} catch (Exception e) {
Assert.fail("Unexpected exception " + e);
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestFailedDeleteManager method testWithNamespaceAndLostSessionAlt.
@Test
public void testWithNamespaceAndLostSessionAlt() throws Exception {
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).retryPolicy(new ExponentialBackoffRetry(100, 3)).build();
try {
client.start();
CuratorFramework namespaceClient = client.usingNamespace("foo");
namespaceClient.create().forPath("/test-me");
final CountDownLatch latch = new CountDownLatch(1);
final Semaphore semaphore = new Semaphore(0);
ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if ((newState == ConnectionState.LOST) || (newState == ConnectionState.SUSPENDED)) {
semaphore.release();
} else if (newState == ConnectionState.RECONNECTED) {
latch.countDown();
}
}
};
namespaceClient.getConnectionStateListenable().addListener(listener);
server.stop();
Assert.assertTrue(timing.acquireSemaphore(semaphore));
try {
namespaceClient.delete().guaranteed().forPath("/test-me");
Assert.fail();
} catch (KeeperException.ConnectionLossException e) {
// expected
}
Assert.assertTrue(timing.acquireSemaphore(semaphore));
timing.sleepABit();
server.restart();
Assert.assertTrue(timing.awaitLatch(latch));
timing.sleepABit();
Assert.assertNull(namespaceClient.checkExists().forPath("/test-me"));
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestFailedDeleteManager method testWithNamespaceAndLostSession.
@Test
public void testWithNamespaceAndLostSession() throws Exception {
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).retryPolicy(new ExponentialBackoffRetry(100, 3)).namespace("aisa").build();
try {
client.start();
client.create().forPath("/test-me");
final CountDownLatch latch = new CountDownLatch(1);
final Semaphore semaphore = new Semaphore(0);
ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if ((newState == ConnectionState.LOST) || (newState == ConnectionState.SUSPENDED)) {
semaphore.release();
} else if (newState == ConnectionState.RECONNECTED) {
latch.countDown();
}
}
};
client.getConnectionStateListenable().addListener(listener);
server.stop();
Assert.assertTrue(timing.acquireSemaphore(semaphore));
try {
client.delete().guaranteed().forPath("/test-me");
Assert.fail();
} catch (KeeperException.ConnectionLossException e) {
// expected
}
Assert.assertTrue(timing.acquireSemaphore(semaphore));
timing.sleepABit();
server.restart();
Assert.assertTrue(timing.awaitLatch(latch));
timing.sleepABit();
Assert.assertNull(client.checkExists().forPath("/test-me"));
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestServiceDiscovery method testCrashedServer.
@Test
public void testCrashedServer() throws Exception {
List<Closeable> closeables = Lists.newArrayList();
try {
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
closeables.add(client);
client.start();
final Semaphore semaphore = new Semaphore(0);
ServiceInstance<String> instance = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build();
ServiceDiscovery<String> discovery = new ServiceDiscoveryImpl<String>(client, "/test", new JsonInstanceSerializer<String>(String.class), new FastjsonServiceDefinitionSerializer<>(String.class), instance, false) {
@Override
protected void internalRegisterService(ServiceInstance<String> service) throws Exception {
super.internalRegisterService(service);
semaphore.release();
}
};
closeables.add(discovery);
discovery.start();
timing.acquireSemaphore(semaphore);
Assert.assertEquals(discovery.queryForInstances("test").size(), 1);
KillSession.kill(client.getZookeeperClient().getZooKeeper(), server.getConnectString());
server.stop();
server.restart();
closeables.add(server);
timing.acquireSemaphore(semaphore);
Assert.assertEquals(discovery.queryForInstances("test").size(), 1);
} finally {
for (Closeable c : closeables) {
CloseableUtils.closeQuietly(c);
}
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestServiceDiscovery method testCrashedInstance.
@Test
public void testCrashedInstance() throws Exception {
List<Closeable> closeables = Lists.newArrayList();
try {
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
closeables.add(client);
client.start();
ServiceInstance<String> instance = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build();
ServiceDiscovery<String> discovery = new ServiceDiscoveryImpl<String>(client, "/test", new JsonInstanceSerializer<String>(String.class), new FastjsonServiceDefinitionSerializer<>(String.class), instance, false);
closeables.add(discovery);
discovery.start();
Assert.assertEquals(discovery.queryForInstances("test").size(), 1);
KillSession.kill(client.getZookeeperClient().getZooKeeper(), server.getConnectString());
Thread.sleep(timing.multiple(1.5).session());
Assert.assertEquals(discovery.queryForInstances("test").size(), 1);
} finally {
Collections.reverse(closeables);
for (Closeable c : closeables) {
CloseableUtils.closeQuietly(c);
}
}
}
Aggregations