use of org.apache.curator.x.async.AsyncCuratorFramework in project tutorials by eugenp.
the class ConfigurationManagementManualTest method givenPath_whenWatchAKeyAndStoreAValue_thenWatcherIsTriggered.
@Test
public void givenPath_whenWatchAKeyAndStoreAValue_thenWatcherIsTriggered() throws Exception {
try (CuratorFramework client = newClient()) {
client.start();
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
String key = getKey();
String expected = "my_value";
// Create key structure
async.create().forPath(key);
List<String> changes = new ArrayList<>();
// Watch data value
async.watched().getData().forPath(key).event().thenAccept(watchedEvent -> {
try {
changes.add(new String(client.getData().forPath(watchedEvent.getPath())));
} catch (Exception e) {
// fail ...
}
});
// Set data value for our key
async.setData().forPath(key, expected.getBytes());
await().until(() -> assertThat(changes.size() > 0).isTrue());
}
}
use of org.apache.curator.x.async.AsyncCuratorFramework in project tutorials by eugenp.
the class ConfigurationManagementManualTest method givenPath_whenCreateKey_thenValueIsStored.
@Test
public void givenPath_whenCreateKey_thenValueIsStored() throws Exception {
try (CuratorFramework client = newClient()) {
client.start();
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
String key = getKey();
String expected = "my_value";
// Create key nodes structure
client.create().forPath(key);
// Set data value for our key
async.setData().forPath(key, expected.getBytes());
// Get data value
AtomicBoolean isEquals = new AtomicBoolean();
async.getData().forPath(key).thenAccept(data -> isEquals.set(new String(data).equals(expected)));
await().until(() -> assertThat(isEquals.get()).isTrue());
}
}
use of org.apache.curator.x.async.AsyncCuratorFramework in project tutorials by eugenp.
the class ConnectionManagementManualTest method givenRunningZookeeper_whenOpenConnectionUsingAsyncNotBlocking_thenClientIsOpened.
@Test
public void givenRunningZookeeper_whenOpenConnectionUsingAsyncNotBlocking_thenClientIsOpened() throws InterruptedException {
int sleepMsBetweenRetries = 100;
int maxRetries = 3;
RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries);
try (CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy)) {
client.start();
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
AtomicBoolean exists = new AtomicBoolean(false);
async.checkExists().forPath("/").thenAcceptAsync(s -> exists.set(s != null));
await().until(() -> assertThat(exists.get()).isTrue());
}
}
use of org.apache.curator.x.async.AsyncCuratorFramework in project tutorials by eugenp.
the class ConnectionManagementManualTest method givenRunningZookeeper_whenOpenConnectionUsingAsyncBlocking_thenClientIsOpened.
@Test
public void givenRunningZookeeper_whenOpenConnectionUsingAsyncBlocking_thenClientIsOpened() throws InterruptedException {
int sleepMsBetweenRetries = 100;
int maxRetries = 3;
RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries);
try (CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy)) {
client.start();
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
AtomicBoolean exists = new AtomicBoolean(false);
async.checkExists().forPath("/").thenAccept(s -> exists.set(s != null));
await().until(() -> assertThat(exists.get()).isTrue());
}
}
use of org.apache.curator.x.async.AsyncCuratorFramework in project tutorials by eugenp.
the class ModelTypedExamplesManualTest method givenPath_whenStoreAModel_thenNodesAreCreated.
@Test
public void givenPath_whenStoreAModel_thenNodesAreCreated() throws InterruptedException {
ModelSpec<HostConfig> mySpec = ModelSpec.builder(ZPath.parseWithIds("/config/dev"), JacksonModelSerializer.build(HostConfig.class)).build();
try (CuratorFramework client = newClient()) {
client.start();
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
ModeledFramework<HostConfig> modeledClient = ModeledFramework.wrap(async, mySpec);
modeledClient.set(new HostConfig("host-name", 8080));
modeledClient.read().whenComplete((value, e) -> {
if (e != null) {
fail("Cannot read host config", e);
} else {
assertThat(value).isNotNull();
assertThat(value.getHostname()).isEqualTo("host-name");
assertThat(value.getPort()).isEqualTo(8080);
}
});
}
}
Aggregations