use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestServiceDiscovery method testCrashedServerMultiInstances.
@Test
public void testCrashedServerMultiInstances() 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> instance1 = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build();
ServiceInstance<String> instance2 = ServiceInstance.<String>builder().payload("thing").name("test").port(10065).build();
ServiceDiscovery<String> discovery = new ServiceDiscoveryImpl<String>(client, "/test", new JsonInstanceSerializer<String>(String.class), new FastjsonServiceDefinitionSerializer<>(String.class), instance1, false) {
@Override
protected void internalRegisterService(ServiceInstance<String> service) throws Exception {
super.internalRegisterService(service);
semaphore.release();
}
};
closeables.add(discovery);
discovery.start();
discovery.registerService(instance2);
timing.acquireSemaphore(semaphore, 2);
Assert.assertEquals(discovery.queryForInstances("test").size(), 2);
KillSession.kill(client.getZookeeperClient().getZooKeeper(), server.getConnectString());
server.stop();
server.restart();
closeables.add(server);
timing.acquireSemaphore(semaphore, 2);
Assert.assertEquals(discovery.queryForInstances("test").size(), 2);
} finally {
for (Closeable c : closeables) {
CloseableUtils.closeQuietly(c);
}
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestServiceDiscovery method testUnregisterService.
// CURATOR-164
@Test
public void testUnregisterService() throws Exception {
final String name = "name";
final CountDownLatch restartLatch = new CountDownLatch(1);
List<Closeable> closeables = Lists.newArrayList();
InstanceSerializer<String> slowSerializer = new JsonInstanceSerializer<String>(String.class) {
private boolean first = true;
@Override
public byte[] serialize(ServiceInstance<String> instance) throws Exception {
if (first) {
System.out.println("Serializer first registration.");
first = false;
} else {
System.out.println("Waiting for reconnect to finish.");
// Simulate the serialize method being slow.
// This could just be a timed wait, but that's kind of non-deterministic.
restartLatch.await();
}
return super.serialize(instance);
}
};
try {
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
closeables.add(client);
client.start();
ServiceInstance<String> instance = ServiceInstance.<String>builder().payload("thing").name(name).port(10064).build();
ServiceDiscovery<String> discovery = ServiceDiscoveryBuilder.builder(String.class).basePath("/test").client(client).thisInstance(instance).serializer(slowSerializer).watchInstances(true).build();
closeables.add(discovery);
discovery.start();
Assert.assertFalse(discovery.queryForInstances(name).isEmpty(), "Service should start registered.");
server.stop();
server.restart();
discovery.unregisterService(instance);
restartLatch.countDown();
// Wait for the rest of registration to finish.
new Timing().sleepABit();
Assert.assertTrue(discovery.queryForInstances(name).isEmpty(), "Service should have unregistered.");
} finally {
Collections.reverse(closeables);
for (Closeable c : closeables) {
CloseableUtils.closeQuietly(c);
}
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestWatchedInstances method testWatchedInstances.
@Test
public void testWatchedInstances() throws Exception {
Timing timing = new Timing();
List<Closeable> closeables = Lists.newArrayList();
try {
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
closeables.add(client);
client.start();
ServiceInstance<String> instance = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build();
ServiceDiscovery<String> discovery = ServiceDiscoveryBuilder.builder(String.class).basePath("/test").client(client).thisInstance(instance).watchInstances(true).build();
closeables.add(discovery);
discovery.start();
Assert.assertEquals(discovery.queryForNames(), Arrays.asList("test"));
List<ServiceInstance<String>> list = Lists.newArrayList();
list.add(instance);
Assert.assertEquals(discovery.queryForInstances("test"), list);
ServiceDiscoveryImpl<String> discoveryImpl = (ServiceDiscoveryImpl<String>) discovery;
ServiceInstance<String> changedInstance = ServiceInstance.<String>builder().id(instance.getId()).address(instance.getAddress()).payload("different").name(instance.getName()).port(instance.getPort()).build();
String path = discoveryImpl.pathForInstance("test", instance.getId());
byte[] bytes = discoveryImpl.getSerializer().serialize(changedInstance);
client.setData().forPath(path, bytes);
timing.sleepABit();
ServiceInstance<String> registeredService = discoveryImpl.getRegisteredService(instance.getId());
Assert.assertNotNull(registeredService);
Assert.assertEquals(registeredService.getPayload(), "different");
} finally {
Collections.reverse(closeables);
for (Closeable c : closeables) {
CloseableUtils.closeQuietly(c);
}
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestSessionFailRetryLoop method testBasic.
@Test
public void testBasic() throws Exception {
Timing timing = new Timing();
final CuratorZookeeperClient client = new CuratorZookeeperClient(server.getConnectString(), timing.session(), timing.connection(), null, new RetryOneTime(1));
SessionFailRetryLoop retryLoop = client.newSessionFailRetryLoop(SessionFailRetryLoop.Mode.FAIL);
retryLoop.start();
try {
client.start();
try {
while (retryLoop.shouldContinue()) {
try {
RetryLoop.callWithRetry(client, new Callable<Void>() {
@Override
public Void call() throws Exception {
Assert.assertNull(client.getZooKeeper().exists("/foo/bar", false));
KillSession.kill(client.getZooKeeper(), server.getConnectString());
client.getZooKeeper();
client.blockUntilConnectedOrTimedOut();
Assert.assertNull(client.getZooKeeper().exists("/foo/bar", false));
return null;
}
});
} catch (Exception e) {
retryLoop.takeException(e);
}
}
Assert.fail();
} catch (SessionFailRetryLoop.SessionFailedException dummy) {
// correct
}
} finally {
retryLoop.close();
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestChildReaper method testNamespace.
@Test
public void testNamespace() throws Exception {
Timing timing = new Timing();
ChildReaper reaper = null;
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).retryPolicy(new RetryOneTime(1)).namespace("foo").build();
try {
client.start();
for (int i = 0; i < 10; ++i) {
client.create().creatingParentsIfNeeded().forPath("/test/" + Integer.toString(i));
}
reaper = new ChildReaper(client, "/test", Reaper.Mode.REAP_UNTIL_DELETE, 1);
reaper.start();
timing.forWaiting().sleepABit();
Stat stat = client.checkExists().forPath("/test");
Assert.assertEquals(stat.getNumChildren(), 0);
stat = client.usingNamespace(null).checkExists().forPath("/foo/test");
Assert.assertNotNull(stat);
Assert.assertEquals(stat.getNumChildren(), 0);
} finally {
CloseableUtils.closeQuietly(reaper);
CloseableUtils.closeQuietly(client);
}
}
Aggregations