use of org.apache.curator.test.TestingCluster in project curator by apache.
the class TestInterProcessSemaphoreCluster method testCluster.
@Test
public void testCluster() throws Exception {
final int QTY = 20;
final int OPERATION_TIME_MS = 1000;
final String PATH = "/foo/bar/lock";
ExecutorService executorService = Executors.newFixedThreadPool(QTY);
ExecutorCompletionService<Void> completionService = new ExecutorCompletionService<Void>(executorService);
final Timing timing = new Timing();
List<SemaphoreClient> semaphoreClients = Lists.newArrayList();
TestingCluster cluster = createAndStartCluster(3);
try {
final AtomicInteger opCount = new AtomicInteger(0);
for (int i = 0; i < QTY; ++i) {
SemaphoreClient semaphoreClient = new SemaphoreClient(cluster.getConnectString(), PATH, new Callable<Void>() {
@Override
public Void call() throws Exception {
opCount.incrementAndGet();
Thread.sleep(OPERATION_TIME_MS);
return null;
}
});
completionService.submit(semaphoreClient);
semaphoreClients.add(semaphoreClient);
}
timing.forWaiting().sleepABit();
assertNotNull(SemaphoreClient.getActiveClient());
final CountDownLatch latch = new CountDownLatch(1);
CuratorFramework client = CuratorFrameworkFactory.newClient(cluster.getConnectString(), timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (newState == ConnectionState.LOST) {
latch.countDown();
}
}
};
client.getConnectionStateListenable().addListener(listener);
client.start();
try {
client.getZookeeperClient().blockUntilConnectedOrTimedOut();
cluster.stop();
latch.await();
} finally {
CloseableUtils.closeQuietly(client);
}
long startTicks = System.currentTimeMillis();
for (; ; ) {
int thisOpCount = opCount.get();
Thread.sleep(2 * OPERATION_TIME_MS);
if (thisOpCount == opCount.get()) {
// checking that the op count isn't increasing
break;
}
assertTrue((System.currentTimeMillis() - startTicks) < timing.forWaiting().milliseconds());
}
int thisOpCount = opCount.get();
Iterator<InstanceSpec> iterator = cluster.getInstances().iterator();
cluster = new TestingCluster(iterator.next(), iterator.next());
cluster.start();
timing.forWaiting().sleepABit();
startTicks = System.currentTimeMillis();
for (; ; ) {
Thread.sleep(2 * OPERATION_TIME_MS);
if (opCount.get() > thisOpCount) {
// checking that semaphore has started working again
break;
}
assertTrue((System.currentTimeMillis() - startTicks) < timing.forWaiting().milliseconds());
}
} finally {
for (SemaphoreClient semaphoreClient : semaphoreClients) {
CloseableUtils.closeQuietly(semaphoreClient);
}
CloseableUtils.closeQuietly(cluster);
executorService.shutdownNow();
}
}
use of org.apache.curator.test.TestingCluster in project curator by apache.
the class TestLeaderSelectorCluster method testRestart.
@Test
public void testRestart() throws Exception {
final Timing timing = new Timing();
CuratorFramework client = null;
TestingCluster cluster = createAndStartCluster(3);
try {
client = CuratorFrameworkFactory.newClient(cluster.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
client.start();
final Semaphore semaphore = new Semaphore(0);
LeaderSelectorListener listener = new LeaderSelectorListener() {
@Override
public void takeLeadership(CuratorFramework client) throws Exception {
List<String> names = client.getChildren().forPath("/leader");
assertTrue(names.size() > 0);
semaphore.release();
}
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
}
};
LeaderSelector selector = new LeaderSelector(client, "/leader", listener);
selector.autoRequeue();
selector.start();
assertTrue(timing.acquireSemaphore(semaphore));
InstanceSpec connectionInstance = cluster.findConnectionInstance(client.getZookeeperClient().getZooKeeper());
cluster.killServer(connectionInstance);
assertTrue(timing.multiple(4).acquireSemaphore(semaphore));
} finally {
CloseableUtils.closeQuietly(client);
CloseableUtils.closeQuietly(cluster);
}
}
use of org.apache.curator.test.TestingCluster in project curator by apache.
the class TestFrameworkEdges method testProtectionWithKilledSession.
@Test
public void testProtectionWithKilledSession() throws Exception {
// not needed
server.stop();
try (TestingCluster cluster = createAndStartCluster(3)) {
InstanceSpec instanceSpec0 = cluster.getServers().get(0).getInstanceSpec();
CountDownLatch serverStoppedLatch = new CountDownLatch(1);
RetryPolicy retryPolicy = new RetryForever(100) {
@Override
public boolean allowRetry(int retryCount, long elapsedTimeMs, RetrySleeper sleeper) {
if (serverStoppedLatch.getCount() > 0) {
try {
cluster.killServer(instanceSpec0);
} catch (Exception e) {
// ignore
}
serverStoppedLatch.countDown();
}
return super.allowRetry(retryCount, elapsedTimeMs, sleeper);
}
};
try (CuratorFramework client = CuratorFrameworkFactory.newClient(instanceSpec0.getConnectString(), timing.session(), timing.connection(), retryPolicy)) {
BlockingQueue<String> createdNode = new LinkedBlockingQueue<>();
BackgroundCallback callback = (__, event) -> {
if (event.getType() == CuratorEventType.CREATE) {
createdNode.offer(event.getPath());
}
};
client.start();
client.create().forPath("/test");
ErrorListenerPathAndBytesable<String> builder = client.create().withProtection().withMode(CreateMode.EPHEMERAL).inBackground(callback);
((CreateBuilderImpl) builder).failNextCreateForTesting = true;
builder.forPath("/test/hey");
assertTrue(timing.awaitLatch(serverStoppedLatch));
// wait for session to expire
timing.forSessionSleep().sleep();
cluster.restartServer(instanceSpec0);
String path = timing.takeFromQueue(createdNode);
List<String> children = client.getChildren().forPath("/test");
assertEquals(Collections.singletonList(ZKPaths.getNodeFromPath(path)), children);
}
}
}
use of org.apache.curator.test.TestingCluster in project curator by apache.
the class TestReconfiguration method testAdd.
@Test
public void testAdd() throws Exception {
try (CuratorFramework client = newClient()) {
client.start();
QuorumVerifier oldConfig = toQuorumVerifier(client.getConfig().forEnsemble());
assertConfig(oldConfig, cluster.getInstances());
CountDownLatch latch = setChangeWaiter(client);
try (TestingCluster newCluster = new TestingCluster(TestingCluster.makeSpecs(1, false))) {
newCluster.start();
client.reconfig().joining(toReconfigSpec(newCluster.getInstances())).fromConfig(oldConfig.getVersion()).forEnsemble();
assertTrue(timing.awaitLatch(latch));
byte[] newConfigData = client.getConfig().forEnsemble();
QuorumVerifier newConfig = toQuorumVerifier(newConfigData);
List<InstanceSpec> newInstances = Lists.newArrayList(cluster.getInstances());
newInstances.addAll(newCluster.getInstances());
assertConfig(newConfig, newInstances);
assertEquals(EnsembleTracker.configToConnectionString(newConfig), ensembleProvider.getConnectionString());
}
}
}
use of org.apache.curator.test.TestingCluster in project curator by apache.
the class TestReconfiguration method testNewMembers.
@Test
// it's what this test is inteded to do and it keeps failing - disable for now
@Disabled
public void testNewMembers() throws Exception {
cluster.close();
cluster = null;
TestingCluster smallCluster = null;
TestingCluster localCluster = new TestingCluster(5);
try {
List<TestingZooKeeperServer> servers = localCluster.getServers();
List<InstanceSpec> smallClusterInstances = Lists.newArrayList();
for (// only start 3 of the 5
int i = 0; // only start 3 of the 5
i < 3; // only start 3 of the 5
++i) {
TestingZooKeeperServer server = servers.get(i);
server.start();
smallClusterInstances.add(server.getInstanceSpec());
}
smallCluster = new TestingCluster(smallClusterInstances);
try (CuratorFramework client = newClient(smallCluster.getConnectString())) {
client.start();
QuorumVerifier oldConfig = toQuorumVerifier(client.getConfig().forEnsemble());
assertEquals(oldConfig.getAllMembers().size(), 5);
assertConfig(oldConfig, localCluster.getInstances());
CountDownLatch latch = setChangeWaiter(client);
client.reconfig().withNewMembers(toReconfigSpec(smallClusterInstances)).forEnsemble();
assertTrue(timing.awaitLatch(latch));
byte[] newConfigData = client.getConfig().forEnsemble();
QuorumVerifier newConfig = toQuorumVerifier(newConfigData);
assertEquals(newConfig.getAllMembers().size(), 3);
assertConfig(newConfig, smallClusterInstances);
assertEquals(EnsembleTracker.configToConnectionString(newConfig), ensembleProvider.getConnectionString());
}
} finally {
CloseableUtils.closeQuietly(smallCluster);
CloseableUtils.closeQuietly(localCluster);
}
}
Aggregations