use of org.apache.curator.test.TestingCluster in project knox by apache.
the class KafkaZookeeperURLManagerTest method setup.
@Before
public void setup() throws Exception {
cluster = new TestingCluster(3);
cluster.start();
CuratorFramework zooKeeperClient = CuratorFrameworkFactory.builder().connectString(cluster.getConnectString()).retryPolicy(new ExponentialBackoffRetry(1000, 3)).build();
zooKeeperClient.start();
zooKeeperClient.create().forPath("/brokers");
zooKeeperClient.create().forPath("/brokers/ids");
zooKeeperClient.close();
}
use of org.apache.curator.test.TestingCluster in project xian by happyyangyuan.
the class TestLeaderLatchCluster method testInCluster.
@Test
public void testInCluster() throws Exception {
final int PARTICIPANT_QTY = 3;
List<ClientAndLatch> clients = Lists.newArrayList();
Timing timing = new Timing();
TestingCluster cluster = new TestingCluster(PARTICIPANT_QTY);
try {
cluster.start();
List<InstanceSpec> instances = Lists.newArrayList(cluster.getInstances());
for (int i = 0; i < PARTICIPANT_QTY; ++i) {
CuratorFramework client = CuratorFrameworkFactory.newClient(instances.get(i).getConnectString(), timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
LeaderLatch latch = new LeaderLatch(client, "/latch");
clients.add(new ClientAndLatch(client, latch, i));
client.start();
latch.start();
}
ClientAndLatch leader = waitForALeader(clients, timing);
Assert.assertNotNull(leader);
cluster.killServer(instances.get(leader.index));
Thread.sleep(timing.multiple(2).session());
leader = waitForALeader(clients, timing);
Assert.assertNotNull(leader);
Assert.assertEquals(getLeaders(clients).size(), 1);
} finally {
for (ClientAndLatch client : clients) {
CloseableUtils.closeQuietly(client.latch);
CloseableUtils.closeQuietly(client.client);
}
CloseableUtils.closeQuietly(cluster);
}
}
use of org.apache.curator.test.TestingCluster in project xian by happyyangyuan.
the class TestLeaderSelectorCluster method testRestart.
@Test
public void testRestart() 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();
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");
Assert.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();
Assert.assertTrue(timing.acquireSemaphore(semaphore));
InstanceSpec connectionInstance = cluster.findConnectionInstance(client.getZookeeperClient().getZooKeeper());
cluster.killServer(connectionInstance);
Assert.assertTrue(timing.multiple(4).acquireSemaphore(semaphore));
} finally {
CloseableUtils.closeQuietly(client);
CloseableUtils.closeQuietly(cluster);
}
}
use of org.apache.curator.test.TestingCluster in project xian by happyyangyuan.
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();
TestingCluster cluster = new TestingCluster(3);
List<SemaphoreClient> semaphoreClients = Lists.newArrayList();
try {
cluster.start();
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();
Assert.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;
}
Assert.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;
}
Assert.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 activemq-artemis by apache.
the class CuratorDistributedLockTest method setupEnv.
@Override
public void setupEnv() throws Throwable {
clusterSpecs = new InstanceSpec[zkNodes];
for (int i = 0; i < zkNodes; i++) {
clusterSpecs[i] = new InstanceSpec(tmpFolder.newFolder(), BASE_SERVER_PORT + i, -1, -1, true, -1, SERVER_TICK_MS, -1);
}
testingServer = new TestingCluster(clusterSpecs);
testingServer.start();
// start waits for quorumPeer!=null but not that it has started...
Wait.waitFor(this::ensembleHasLeader);
connectString = testingServer.getConnectString();
super.setupEnv();
}
Aggregations