use of org.apache.curator.test.TestingCluster in project curator by apache.
the class TestReconfiguration method testAddAsync.
@Test
public void testAddAsync() 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();
final CountDownLatch callbackLatch = new CountDownLatch(1);
BackgroundCallback callback = new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
if (event.getType() == CuratorEventType.RECONFIG) {
callbackLatch.countDown();
}
}
};
client.reconfig().inBackground(callback).joining(toReconfigSpec(newCluster.getInstances())).fromConfig(oldConfig.getVersion()).forEnsemble();
assertTrue(timing.awaitLatch(callbackLatch));
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 TestWithCluster method testSessionSurvives.
@Test
public void testSessionSurvives() throws Exception {
Timing timing = new Timing();
CuratorFramework client = null;
TestingCluster cluster = createAndStartCluster(3);
try {
client = CuratorFrameworkFactory.newClient(cluster.getConnectString(), timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
client.start();
final CountDownLatch reconnectedLatch = new CountDownLatch(1);
ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (newState == ConnectionState.RECONNECTED) {
reconnectedLatch.countDown();
;
}
}
};
client.getConnectionStateListenable().addListener(listener);
client.create().withMode(CreateMode.EPHEMERAL).forPath("/temp", "value".getBytes());
assertNotNull(client.checkExists().forPath("/temp"));
for (InstanceSpec spec : cluster.getInstances()) {
cluster.killServer(spec);
timing.sleepABit();
cluster.restartServer(spec);
timing.sleepABit();
}
assertTrue(timing.awaitLatch(reconnectedLatch));
assertNotNull(client.checkExists().forPath("/temp"));
} finally {
CloseableUtils.closeQuietly(client);
CloseableUtils.closeQuietly(cluster);
}
}
use of org.apache.curator.test.TestingCluster in project curator by apache.
the class TestReconfiguration method testAddAndRemove.
@Test
public void testAddAndRemove() 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();
Collection<InstanceSpec> oldInstances = cluster.getInstances();
InstanceSpec us = cluster.findConnectionInstance(client.getZookeeperClient().getZooKeeper());
InstanceSpec removeSpec = oldInstances.iterator().next();
if (us.equals(removeSpec)) {
Iterator<InstanceSpec> iterator = oldInstances.iterator();
iterator.next();
removeSpec = iterator.next();
}
Collection<InstanceSpec> instances = newCluster.getInstances();
client.reconfig().leaving(Integer.toString(removeSpec.getServerId())).joining(toReconfigSpec(instances)).fromConfig(oldConfig.getVersion()).forEnsemble();
assertTrue(timing.awaitLatch(latch));
byte[] newConfigData = client.getConfig().forEnsemble();
QuorumVerifier newConfig = toQuorumVerifier(newConfigData);
ArrayList<InstanceSpec> newInstances = Lists.newArrayList(oldInstances);
newInstances.addAll(instances);
newInstances.remove(removeSpec);
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 testAddWithoutEnsembleTracker.
@Test
public void testAddWithoutEnsembleTracker() throws Exception {
final String initialClusterCS = cluster.getConnectString();
try (CuratorFramework client = newClient(cluster.getConnectString(), false)) {
assertEquals(((CuratorFrameworkImpl) client).getEnsembleTracker(), null);
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(ensembleProvider.getConnectionString(), initialClusterCS);
assertNotEquals(EnsembleTracker.configToConnectionString(newConfig), ensembleProvider.getConnectionString());
assertEquals(client.getZookeeperClient().getCurrentConnectionString(), initialClusterCS);
final CountDownLatch reconnectLatch = new CountDownLatch(1);
client.getConnectionStateListenable().addListener((cfClient, newState) -> {
if (newState == ConnectionState.RECONNECTED)
reconnectLatch.countDown();
});
client.getZookeeperClient().getZooKeeper().getTestable().injectSessionExpiration();
assertTrue(reconnectLatch.await(2, TimeUnit.SECONDS));
assertEquals(client.getZookeeperClient().getCurrentConnectionString(), initialClusterCS);
assertEquals(ensembleProvider.getConnectionString(), initialClusterCS);
newConfigData = client.getConfig().forEnsemble();
assertNotEquals(EnsembleTracker.configToConnectionString(newConfig), ensembleProvider.getConnectionString());
}
}
}
use of org.apache.curator.test.TestingCluster in project curator by apache.
the class TestCuratorCacheConsistency method testConsistencyAfterSimulation.
@Test
public void testConsistencyAfterSimulation() throws Exception {
int clientQty = random.nextInt(10, 20);
int maxDepth = random.nextInt(5, 10);
log.info("clientQty: {}, maxDepth: {}", clientQty, maxDepth);
List<Client> clients = Collections.emptyList();
Map<String, String> actualTree;
AtomicReference<Exception> errorSignal = new AtomicReference<>();
try (TestingCluster cluster = new TestingCluster(clusterSize)) {
cluster.start();
initializeBasePath(cluster);
try {
clients = buildClients(cluster, clientQty, errorSignal);
workLoop(cluster, clients, maxDepth, errorSignal);
log.info("Test complete - sleeping to allow events to complete");
timing.sleepABit();
} finally {
clients.forEach(Client::close);
}
actualTree = buildActual(cluster);
}
log.info("client qty: {}", clientQty);
Map<Integer, List<String>> errorsList = clients.stream().map(client -> findErrors(client, actualTree)).filter(errorsEntry -> !errorsEntry.getValue().isEmpty()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
if (!errorsList.isEmpty()) {
log.error("{} clients had errors", errorsList.size());
errorsList.forEach((index, errorList) -> {
log.error("Client {}", index);
errorList.forEach(log::error);
log.error("");
});
fail("Errors found");
}
}
Aggregations