@Test
public void testCrashedServerMultiInstances() throws Exception
{
List<Closeable> closeables = Lists.newArrayList();
TestingServer server = new TestingServer();
closeables.add(server);
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), instance1)
{
@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 = new TestingServer(server.getPort(), server.getTempDirectory());
closeables.add(server);
timing.acquireSemaphore(semaphore, 2);
Assert.assertEquals(discovery.queryForInstances("test").size(), 2);
}