use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestSessionFailRetryLoop method testRetry.
@Test
public void testRetry() 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.RETRY);
retryLoop.start();
try {
client.start();
final AtomicBoolean secondWasDone = new AtomicBoolean(false);
final AtomicBoolean firstTime = new AtomicBoolean(true);
while (retryLoop.shouldContinue()) {
try {
RetryLoop.callWithRetry(client, new Callable<Void>() {
@Override
public Void call() throws Exception {
if (firstTime.compareAndSet(true, false)) {
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;
}
});
RetryLoop.callWithRetry(client, new Callable<Void>() {
@Override
public Void call() throws Exception {
Assert.assertFalse(firstTime.get());
Assert.assertNull(client.getZooKeeper().exists("/foo/bar", false));
secondWasDone.set(true);
return null;
}
});
} catch (Exception e) {
retryLoop.takeException(e);
}
}
Assert.assertTrue(secondWasDone.get());
} finally {
retryLoop.close();
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestSessionFailRetryLoop method testBasicStatic.
@Test
public void testBasicStatic() 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 {
SessionFailRetryLoop.callWithRetry(client, SessionFailRetryLoop.Mode.FAIL, new Callable<Object>() {
@Override
public Object call() throws Exception {
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;
}
});
return null;
}
});
} 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 TestExhibitorEnsembleProvider method testChanging.
@Test
public void testChanging() throws Exception {
TestingServer secondServer = new TestingServer();
try {
String mainConnectionString = "count=1&port=" + server.getPort() + "&server0=localhost";
String secondConnectionString = "count=1&port=" + secondServer.getPort() + "&server0=localhost";
final Semaphore semaphore = new Semaphore(0);
final AtomicReference<String> connectionString = new AtomicReference<String>(mainConnectionString);
Exhibitors exhibitors = new Exhibitors(Lists.newArrayList("foo", "bar"), 1000, dummyConnectionStringProvider);
ExhibitorRestClient mockRestClient = new ExhibitorRestClient() {
@Override
public String getRaw(String hostname, int port, String uriPath, String mimeType) throws Exception {
semaphore.release();
return connectionString.get();
}
};
ExhibitorEnsembleProvider provider = new ExhibitorEnsembleProvider(exhibitors, mockRestClient, "/foo", 10, new RetryOneTime(1));
provider.pollForInitialEnsemble();
Timing timing = new Timing().multiple(4);
final CuratorZookeeperClient client = new CuratorZookeeperClient(provider, timing.session(), timing.connection(), null, new RetryOneTime(2));
client.start();
try {
RetryLoop.callWithRetry(client, new Callable<Object>() {
@Override
public Object call() throws Exception {
client.getZooKeeper().create("/test", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
return null;
}
});
connectionString.set(secondConnectionString);
semaphore.drainPermits();
semaphore.acquire();
// create situation where the current zookeeper gets a sys-disconnected
server.stop();
Stat stat = RetryLoop.callWithRetry(client, new Callable<Stat>() {
@Override
public Stat call() throws Exception {
return client.getZooKeeper().exists("/test", false);
}
});
// it's a different server so should be null
Assert.assertNull(stat);
} finally {
client.close();
}
} finally {
CloseableUtils.closeQuietly(secondServer);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestExhibitorEnsembleProvider method testSimple.
@Test
public void testSimple() throws Exception {
Exhibitors exhibitors = new Exhibitors(Lists.newArrayList("foo", "bar"), 1000, dummyConnectionStringProvider);
ExhibitorRestClient mockRestClient = new ExhibitorRestClient() {
@Override
public String getRaw(String hostname, int port, String uriPath, String mimeType) throws Exception {
return "count=1&port=" + server.getPort() + "&server0=localhost";
}
};
ExhibitorEnsembleProvider provider = new ExhibitorEnsembleProvider(exhibitors, mockRestClient, "/foo", 10, new RetryOneTime(1));
provider.pollForInitialEnsemble();
Timing timing = new Timing();
CuratorZookeeperClient client = new CuratorZookeeperClient(provider, timing.session(), timing.connection(), null, new ExponentialBackoffRetry(timing.milliseconds(), 3));
client.start();
try {
client.blockUntilConnectedOrTimedOut();
client.getZooKeeper().exists("/", false);
} catch (Exception e) {
Assert.fail("provider.getConnectionString(): " + provider.getConnectionString() + " server.getPort(): " + server.getPort(), e);
} finally {
client.close();
}
}
use of org.apache.curator.test.Timing in project druid by druid-io.
the class BatchServerInventoryViewTest method waitForSync.
private static void waitForSync(BatchServerInventoryView batchServerInventoryView, Set<DataSegment> testSegments) throws Exception {
final Timing forWaitingTiming = timing.forWaiting();
Stopwatch stopwatch = Stopwatch.createStarted();
while (Iterables.isEmpty(batchServerInventoryView.getInventory()) || Iterables.get(batchServerInventoryView.getInventory(), 0).getSegments().size() != testSegments.size()) {
Thread.sleep(100);
if (stopwatch.elapsed(TimeUnit.MILLISECONDS) > forWaitingTiming.milliseconds()) {
throw new ISE("BatchServerInventoryView is not updating");
}
}
}
Aggregations