ZookeeperSessionFactory factory = new ZookeeperSessionFactory("127.0.0.1:" + port,5000);
// create a session from the session factory
ZookeeperSession session = (ZookeeperSession)factory.createSession();
ClusterId clusterId = new ClusterId(appname,"testNoServerOnStartup");
// hook a test watch to make sure that callbacks work correctly
TestWatcher callback = new TestWatcher(session)
{
@Override public void process() { called.set(true); }
};
// now accessing the cluster should get us an error.
boolean gotCorrectError = false;
try { session.getSubdirs(clusterId.asPath(), callback); } catch (ClusterInfoException e) { gotCorrectError = true; }
assertTrue(gotCorrectError);
// now lets startup the server.
ZookeeperTestServer server = null;
try
{
server = new ZookeeperTestServer();
server.start();
// create a cluster from the session
TestUtils.createClusterLevel(clusterId,session);
// wait until this works.
assertTrue(TestUtils.poll(baseTimeoutMillis, callback, new Condition<TestWatcher>() {
@Override public boolean conditionMet(TestWatcher o){ return o.called.get(); }
}));
callback.called.set(false); // reset the callbacker ...
// now see if the cluster works.
assertTrue(TestUtils.poll(baseTimeoutMillis, callback, new Condition<TestWatcher>() {
@Override public boolean conditionMet(TestWatcher o){ return !o.called.get(); }
}));
session.getSubdirs(clusterId.asPath(), callback);
ZooKeeper origZk = session.zkref.get();
ZookeeperTestServer.forceSessionExpiration(origZk);
// wait for the callback
assertTrue(TestUtils.poll(baseTimeoutMillis, callback, new Condition<TestWatcher>() {
@Override public boolean conditionMet(TestWatcher o){ return o.called.get(); }
}));
// unfortunately I cannot check the getActiveSlots for failure because there's a race condition I can't fix.
// No matter how fast I check it's possible that it's okay again OR that allSlots hasn't been cleared.
//
// however, they should eventually recover.
gotCorrectError = true;
for (long endTime = System.currentTimeMillis() + baseTimeoutMillis; endTime > System.currentTimeMillis() && gotCorrectError;)
{
Thread.sleep(1);
try { session.getSubdirs(clusterId.asPath(), callback); gotCorrectError = false; } catch (ClusterInfoException e) { }
}
session.getSubdirs(clusterId.asPath(), callback);
// And join should work
gotCorrectError = true;
for (long endTime = System.currentTimeMillis() + baseTimeoutMillis; endTime > System.currentTimeMillis() && gotCorrectError;)
{
Thread.sleep(1);
try { session.mkdir(clusterId.asPath() + "/join-1", null, DirMode.EPHEMERAL); gotCorrectError = false; } catch (ClusterInfoException e) { }
}
assertFalse(gotCorrectError);
}
finally