use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestFailedDeleteManager method testBasic.
@Test
public void testBasic() throws Exception {
final String PATH = "/one/two/three";
Timing timing = new Timing();
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
builder.connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).connectionTimeoutMs(timing.connection()).sessionTimeoutMs(timing.session());
CuratorFrameworkImpl client = new CuratorFrameworkImpl(builder);
client.start();
try {
client.create().creatingParentsIfNeeded().forPath(PATH);
Assert.assertNotNull(client.checkExists().forPath(PATH));
// cause the next delete to fail
server.stop();
try {
client.delete().forPath(PATH);
Assert.fail();
} catch (KeeperException.ConnectionLossException e) {
// expected
}
server.restart();
Assert.assertNotNull(client.checkExists().forPath(PATH));
// cause the next delete to fail
server.stop();
try {
client.delete().guaranteed().forPath(PATH);
Assert.fail();
} catch (KeeperException.ConnectionLossException e) {
// expected
}
server.restart();
final int TRIES = 5;
for (int i = 0; i < TRIES; ++i) {
if (client.checkExists().forPath(PATH) != null) {
timing.sleepABit();
}
}
Assert.assertNull(client.checkExists().forPath(PATH));
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestLeaderSelectorCluster method testLostRestart.
@Test
public void testLostRestart() throws Exception {
final 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 RetryOneTime(1));
client.start();
client.sync().forPath("/");
final AtomicReference<Exception> error = new AtomicReference<Exception>(null);
final AtomicReference<String> lockNode = new AtomicReference<String>(null);
final Semaphore semaphore = new Semaphore(0);
final CountDownLatch lostLatch = new CountDownLatch(1);
final CountDownLatch internalLostLatch = new CountDownLatch(1);
LeaderSelectorListener listener = new LeaderSelectorListener() {
@Override
public void takeLeadership(CuratorFramework client) throws Exception {
try {
List<String> names = client.getChildren().forPath("/leader");
if (names.size() != 1) {
semaphore.release();
Exception exception = new Exception("Names size isn't 1: " + names.size());
error.set(exception);
return;
}
lockNode.set(names.get(0));
semaphore.release();
if (!timing.multiple(4).awaitLatch(internalLostLatch)) {
error.set(new Exception("internalLostLatch await failed"));
}
} finally {
lostLatch.countDown();
}
}
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (newState == ConnectionState.LOST) {
internalLostLatch.countDown();
}
}
};
LeaderSelector selector = new LeaderSelector(client, "/leader", listener);
selector.start();
Assert.assertTrue(timing.multiple(4).acquireSemaphore(semaphore));
if (error.get() != null) {
throw new AssertionError(error.get());
}
Collection<InstanceSpec> instances = cluster.getInstances();
cluster.stop();
Assert.assertTrue(timing.multiple(4).awaitLatch(lostLatch));
timing.sleepABit();
Assert.assertFalse(selector.hasLeadership());
Assert.assertNotNull(lockNode.get());
cluster = new TestingCluster(instances.toArray(new InstanceSpec[instances.size()]));
cluster.start();
try {
// simulate the lock deleting due to session expiration
client.delete().forPath(ZKPaths.makePath("/leader", lockNode.get()));
} catch (Exception ignore) {
// ignore
}
Assert.assertTrue(semaphore.availablePermits() == 0);
Assert.assertFalse(selector.hasLeadership());
selector.requeue();
Assert.assertTrue(timing.multiple(4).acquireSemaphore(semaphore));
} finally {
CloseableUtils.closeQuietly(client);
CloseableUtils.closeQuietly(cluster);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestSharedCount method testMultiClientVersioned.
@Test
public void testMultiClientVersioned() throws Exception {
Timing timing = new Timing();
CuratorFramework client1 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
CuratorFramework client2 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
SharedCount count1 = new SharedCount(client1, "/count", 0);
SharedCount count2 = new SharedCount(client2, "/count", 0);
try {
client1.start();
client2.start();
count1.start();
count2.start();
VersionedValue<Integer> versionedValue = count1.getVersionedValue();
Assert.assertTrue(count1.trySetCount(versionedValue, 10));
timing.sleepABit();
versionedValue = count2.getVersionedValue();
Assert.assertTrue(count2.trySetCount(versionedValue, 20));
timing.sleepABit();
VersionedValue<Integer> versionedValue1 = count1.getVersionedValue();
VersionedValue<Integer> versionedValue2 = count2.getVersionedValue();
Assert.assertTrue(count2.trySetCount(versionedValue2, 30));
Assert.assertFalse(count1.trySetCount(versionedValue1, 40));
versionedValue1 = count1.getVersionedValue();
Assert.assertTrue(count1.trySetCount(versionedValue1, 40));
} finally {
CloseableUtils.closeQuietly(count2);
CloseableUtils.closeQuietly(count1);
CloseableUtils.closeQuietly(client2);
CloseableUtils.closeQuietly(client1);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestInterProcessSemaphoreCluster method testKilledServerWithEnsembleProvider.
@Test
public void testKilledServerWithEnsembleProvider() throws Exception {
final int CLIENT_QTY = 10;
final Timing timing = new Timing();
final String PATH = "/foo/bar/lock";
ExecutorService executorService = Executors.newFixedThreadPool(CLIENT_QTY);
ExecutorCompletionService<Void> completionService = new ExecutorCompletionService<Void>(executorService);
TestingCluster cluster = new TestingCluster(3);
try {
cluster.start();
final AtomicReference<String> connectionString = new AtomicReference<String>(cluster.getConnectString());
final EnsembleProvider provider = new EnsembleProvider() {
@Override
public void start() throws Exception {
}
@Override
public String getConnectionString() {
return connectionString.get();
}
@Override
public void close() throws IOException {
}
};
final Semaphore acquiredSemaphore = new Semaphore(0);
final AtomicInteger acquireCount = new AtomicInteger(0);
final CountDownLatch suspendedLatch = new CountDownLatch(CLIENT_QTY);
for (int i = 0; i < CLIENT_QTY; ++i) {
completionService.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
CuratorFramework client = CuratorFrameworkFactory.builder().ensembleProvider(provider).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).retryPolicy(new ExponentialBackoffRetry(100, 3)).build();
try {
final Semaphore suspendedSemaphore = new Semaphore(0);
client.getConnectionStateListenable().addListener(new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if ((newState == ConnectionState.SUSPENDED) || (newState == ConnectionState.LOST)) {
suspendedLatch.countDown();
suspendedSemaphore.release();
}
}
});
client.start();
InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, PATH, 1);
while (!Thread.currentThread().isInterrupted()) {
Lease lease = null;
try {
lease = semaphore.acquire();
acquiredSemaphore.release();
acquireCount.incrementAndGet();
suspendedSemaphore.acquire();
} catch (Exception e) {
// just retry
} finally {
if (lease != null) {
acquireCount.decrementAndGet();
CloseableUtils.closeQuietly(lease);
}
}
}
} finally {
CloseableUtils.closeQuietly(client);
}
return null;
}
});
}
Assert.assertTrue(timing.acquireSemaphore(acquiredSemaphore));
Assert.assertEquals(1, acquireCount.get());
cluster.close();
timing.awaitLatch(suspendedLatch);
timing.forWaiting().sleepABit();
Assert.assertEquals(0, acquireCount.get());
cluster = new TestingCluster(3);
cluster.start();
connectionString.set(cluster.getConnectString());
timing.forWaiting().sleepABit();
Assert.assertTrue(timing.acquireSemaphore(acquiredSemaphore));
timing.forWaiting().sleepABit();
Assert.assertEquals(1, acquireCount.get());
} finally {
executorService.shutdown();
executorService.awaitTermination(10, TimeUnit.SECONDS);
executorService.shutdownNow();
CloseableUtils.closeQuietly(cluster);
}
}
use of org.apache.curator.test.Timing in project xian by happyyangyuan.
the class TestLeaderLatch method testProperCloseWithoutConnectionEstablished.
@Test
public void testProperCloseWithoutConnectionEstablished() throws Exception {
server.stop();
Timing timing = new Timing();
LeaderLatch latch = null;
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
client.start();
final AtomicBoolean resetCalled = new AtomicBoolean(false);
final CountDownLatch cancelStartTaskLatch = new CountDownLatch(1);
latch = new LeaderLatch(client, PATH_NAME) {
@Override
void reset() throws Exception {
resetCalled.set(true);
super.reset();
}
@Override
protected boolean cancelStartTask() {
if (super.cancelStartTask()) {
cancelStartTaskLatch.countDown();
return true;
}
return false;
}
};
latch.start();
latch.close();
latch = null;
Assert.assertTrue(timing.awaitLatch(cancelStartTaskLatch));
Assert.assertFalse(resetCalled.get());
} finally {
CloseableUtils.closeQuietly(latch);
CloseableUtils.closeQuietly(client);
}
}
Aggregations