use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestWithCluster method testSessionSurvives.
@Test
public void testSessionSurvives() throws Exception {
Timing timing = new Timing();
CuratorFramework client = null;
TestingCluster cluster = new TestingCluster(3);
cluster.start();
try {
client = CuratorFrameworkFactory.newClient(cluster.getConnectString(), timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
client.start();
client.create().withMode(CreateMode.EPHEMERAL).forPath("/temp", "value".getBytes());
Assert.assertNotNull(client.checkExists().forPath("/temp"));
for (InstanceSpec spec : cluster.getInstances()) {
cluster.killServer(spec);
timing.forWaiting().sleepABit();
cluster.restartServer(spec);
timing.sleepABit();
}
timing.sleepABit();
Assert.assertNotNull(client.checkExists().forPath("/temp"));
} finally {
CloseableUtils.closeQuietly(client);
CloseableUtils.closeQuietly(cluster);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestWithCluster method testSplitBrain.
@Test
public void testSplitBrain() throws Exception {
Timing timing = new Timing();
CuratorFramework client = null;
TestingCluster cluster = new TestingCluster(3);
cluster.start();
try {
// make sure all instances are up
for (InstanceSpec instanceSpec : cluster.getInstances()) {
client = CuratorFrameworkFactory.newClient(instanceSpec.getConnectString(), new RetryOneTime(1));
client.start();
client.checkExists().forPath("/");
client.close();
client = null;
}
client = CuratorFrameworkFactory.newClient(cluster.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
client.start();
final CountDownLatch latch = new CountDownLatch(2);
client.getConnectionStateListenable().addListener(new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if ((newState == ConnectionState.SUSPENDED) || (newState == ConnectionState.LOST)) {
latch.countDown();
}
}
});
client.checkExists().forPath("/");
for (InstanceSpec instanceSpec : cluster.getInstances()) {
if (!instanceSpec.equals(cluster.findConnectionInstance(client.getZookeeperClient().getZooKeeper()))) {
Assert.assertTrue(cluster.killServer(instanceSpec));
}
}
Assert.assertTrue(timing.awaitLatch(latch));
} finally {
CloseableUtils.closeQuietly(client);
CloseableUtils.closeQuietly(cluster);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestFailedDeleteManager method testLostSession.
@Test
public void testLostSession() throws Exception {
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
try {
client.start();
client.create().forPath("/test-me");
final CountDownLatch latch = new CountDownLatch(1);
final Semaphore semaphore = new Semaphore(0);
ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if ((newState == ConnectionState.LOST) || (newState == ConnectionState.SUSPENDED)) {
semaphore.release();
} else if (newState == ConnectionState.RECONNECTED) {
latch.countDown();
}
}
};
client.getConnectionStateListenable().addListener(listener);
server.stop();
Assert.assertTrue(timing.acquireSemaphore(semaphore));
try {
client.delete().guaranteed().forPath("/test-me");
Assert.fail();
} catch (KeeperException.ConnectionLossException e) {
// expected
}
Assert.assertTrue(timing.acquireSemaphore(semaphore));
timing.sleepABit();
server.restart();
Assert.assertTrue(timing.awaitLatch(latch));
timing.sleepABit();
Assert.assertNull(client.checkExists().forPath("/test-me"));
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestFrameworkBackground method testShutdown.
/**
* CURATOR-126
* Shutdown the Curator client while there are still background operations running.
*/
@Test
public void testShutdown() throws Exception {
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).retryPolicy(new RetryOneTime(1)).maxCloseWaitMs(timing.forWaiting().milliseconds()).build();
try {
final AtomicBoolean hadIllegalStateException = new AtomicBoolean(false);
((CuratorFrameworkImpl) client).debugUnhandledErrorListener = new UnhandledErrorListener() {
@Override
public void unhandledError(String message, Throwable e) {
if (e instanceof IllegalStateException) {
hadIllegalStateException.set(true);
}
}
};
client.start();
final CountDownLatch operationReadyLatch = new CountDownLatch(1);
((CuratorFrameworkImpl) client).debugListener = new CuratorFrameworkImpl.DebugBackgroundListener() {
@Override
public void listen(OperationAndData<?> data) {
try {
operationReadyLatch.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
};
// queue a background operation that will block due to the debugListener
client.create().inBackground().forPath("/hey");
timing.sleepABit();
// close the client while the background is still blocked
client.close();
// unblock the background
operationReadyLatch.countDown();
timing.sleepABit();
// should not generate an exception
Assert.assertFalse(hadIllegalStateException.get());
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestFrameworkBackground method testListenerConnectedAtStart.
@Test
public void testListenerConnectedAtStart() throws Exception {
server.stop();
Timing timing = new Timing(2);
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryNTimes(0, 0));
try {
client.start();
final CountDownLatch connectedLatch = new CountDownLatch(1);
final AtomicBoolean firstListenerAction = new AtomicBoolean(true);
final AtomicReference<ConnectionState> firstListenerState = new AtomicReference<ConnectionState>();
ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (firstListenerAction.compareAndSet(true, false)) {
firstListenerState.set(newState);
System.out.println("First listener state is " + newState);
}
if (newState == ConnectionState.CONNECTED) {
connectedLatch.countDown();
}
}
};
client.getConnectionStateListenable().addListener(listener);
// due to CURATOR-72, this was causing a LOST event to precede the CONNECTED event
client.create().inBackground().forPath("/foo");
server.restart();
Assert.assertTrue(timing.awaitLatch(connectedLatch));
Assert.assertFalse(firstListenerAction.get());
ConnectionState firstconnectionState = firstListenerState.get();
Assert.assertEquals(firstconnectionState, ConnectionState.CONNECTED, "First listener state MUST BE CONNECTED but is " + firstconnectionState);
} finally {
CloseableUtils.closeQuietly(client);
}
}
Aggregations