cn.claim(c);
}
<<<<<<< HEAD
/**
* A coordinate listener that stores events and calls a latch.
*/
class UnitTestCoordinateListener implements CoordinateListener {
final public List events = new ArrayList();
final private CountDownLatch latch;
UnitTestCoordinateListener(CountDownLatch latch) {
this.latch = latch;
}
@Override
public void onConfigEvent(Event event, String message) {
events.add(event);
latch.countDown();
}
}
/**
* Sets up a claimed coordinate and a connection listener.
* @param connectedLatch
* @return
*/
private UnitTestCoordinateListener setUpListenerEnvironment(CountDownLatch connectedLatch) {
Coordinate c = Coordinate.parse("1.service.user.cell");
ZkCloudname cn = new ZkCloudname.Builder().setConnectString("localhost:" + zkport).build().connect();
cn.createCoordinate(c);
ServiceHandle handle = cn.claim(c);
UnitTestCoordinateListener listener = new UnitTestCoordinateListener(connectedLatch);
handle.registerCoordinateListener(listener);
return listener;
}
@Test
public void testCoordinateListenerInitialEvent() throws Exception {
final CountDownLatch connectedLatch = new CountDownLatch(1);
UnitTestCoordinateListener listener = setUpListenerEnvironment(connectedLatch);
assertTrue(connectedLatch.await(2, TimeUnit.SECONDS));
assertEquals(1, listener.events.size());
assertEquals(CoordinateListener.Event.COORDINATE_OK, listener.events.get(0));
}
@Test
public void testCoordinateListenerConnectionDies() throws Exception {
final CountDownLatch connectedLatch = new CountDownLatch(2);
UnitTestCoordinateListener listener = setUpListenerEnvironment(connectedLatch);
log.info("Killing zookeeper");
ezk.shutdown();
assertTrue(connectedLatch.await(20, TimeUnit.SECONDS));
assertEquals(2, listener.events.size());
assertEquals(CoordinateListener.Event.COORDINATE_OK, listener.events.get(0));
assertEquals(CoordinateListener.Event.LOST_CONNECTION_TO_STORAGE, listener.events.get(1));
}
@Test
public void testCoordinateListenerCoordinateLost() throws Exception {
final CountDownLatch connectedLatch = new CountDownLatch(2);
UnitTestCoordinateListener listener = setUpListenerEnvironment(connectedLatch);
log.info("Deleting coordinate");
zk.delete("/cn/cell/user/service/1/status", -1);
assertTrue(connectedLatch.await(20, TimeUnit.SECONDS));
assertEquals(2, listener.events.size());
assertEquals(CoordinateListener.Event.COORDINATE_OK, listener.events.get(0));
assertEquals(CoordinateListener.Event.COORDINATE_VANISHED, listener.events.get(1));
}
@Test
public void testCoordinateListenerCoordinateCorrupted() throws Exception {
final CountDownLatch connectedLatch = new CountDownLatch(2);
UnitTestCoordinateListener listener = setUpListenerEnvironment(connectedLatch);
log.info("Corrupting coordinate.");
String garbage = "sdfgsdfgsfgdsdfgsdfgsdfg";
byte[] garbageBytes = garbage.getBytes("UTF-16LE");
zk.setData("/cn/cell/user/service/1/status", garbageBytes, -1);
assertTrue(connectedLatch.await(20, TimeUnit.SECONDS));
assertEquals(2, listener.events.size());
assertEquals(CoordinateListener.Event.COORDINATE_OK, listener.events.get(0));
assertEquals(CoordinateListener.Event.COORDINATE_CORRUPTED, listener.events.get(1));
}
@Test
public void testCoordinateListenerCoordinateOutOfSync() throws Exception {
final CountDownLatch connectedLatch = new CountDownLatch(2);
UnitTestCoordinateListener listener = setUpListenerEnvironment(connectedLatch);
log.info("Writing different coordinate.");
String source = "\"{\\\"state\\\":\\\"STARTING\\\",\\\"message\\\":\\\"Lost hamster.\\\"}\" {}";
byte[] byteArray = source.getBytes(Util.CHARSET_NAME);
zk.setData("/cn/cell/user/service/1/status", byteArray, -1);
assertTrue(connectedLatch.await(20, TimeUnit.SECONDS));
assertEquals(2, listener.events.size());
assertEquals(CoordinateListener.Event.COORDINATE_OK, listener.events.get(0));
assertEquals(CoordinateListener.Event.COORDINATE_OUT_OF_SYNC, listener.events.get(1));
}
=======
@Test
public void testDestroyBasic() throws Exception {
Coordinate c = Coordinate.parse("1.service.user.cell");
ZkCloudname cn = new ZkCloudname.Builder().setConnectString("localhost:" + zkport).build().connect();
cn.createCoordinate(c);
assertTrue(pathExists("/cn/cell/user/service/1/config"));
cn.destroyCoordinate(c);
assertFalse(pathExists("/cn/cell/user/service"));
assertTrue(pathExists("/cn/cell/user"));
}
@Test
public void testDestroyTwoInstances() throws Exception {
Coordinate c1 = Coordinate.parse("1.service.user.cell");
Coordinate c2 = Coordinate.parse("2.service.user.cell");
ZkCloudname cn = new ZkCloudname.Builder().setConnectString("localhost:" + zkport).build().connect();
cn.createCoordinate(c1);
cn.createCoordinate(c2);
assertTrue(pathExists("/cn/cell/user/service/1/config"));
assertTrue(pathExists("/cn/cell/user/service/2/config"));
cn.destroyCoordinate(c1);
assertFalse(pathExists("/cn/cell/user/service/1"));
assertTrue(pathExists("/cn/cell/user/service/2/config"));
}
@Test (expected = CloudnameException.CoordinateIsClaimed.class)
public void testDestroyClaimed() throws Exception {
Coordinate c = Coordinate.parse("1.service.user.cell");
ZkCloudname cn = new ZkCloudname.Builder().setConnectString("localhost:" + zkport).build().connect();
cn.createCoordinate(c);
ServiceHandle handle = cn.claim(c);
cn.destroyCoordinate(c);
}
>>>>>>> 745a07ffec20e7bc0bcfb6ca418aa18a5812d257
private boolean pathExists(String path) throws Exception {
return (null != zk.exists(path, false));
} |