use of io.etcd.jetcd.watch.WatchEvent in project jetcd by coreos.
the class WatchTest method testWatchOnDelete.
@ParameterizedTest
@MethodSource("parameters")
public void testWatchOnDelete(final Client client) throws Exception {
final ByteSequence key = randomByteSequence();
final ByteSequence value = randomByteSequence();
final AtomicReference<WatchResponse> ref = new AtomicReference<>();
client.getKVClient().put(key, value).get();
try (Watcher watcher = client.getWatchClient().watch(key, ref::set)) {
client.getKVClient().delete(key).get();
await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> assertThat(ref.get()).isNotNull());
assertThat(ref.get().getEvents().size()).isEqualTo(1);
WatchEvent event = ref.get().getEvents().get(0);
assertThat(event.getEventType()).isEqualTo(EventType.DELETE);
assertThat(Arrays.equals(event.getKeyValue().getKey().getBytes(), key.getBytes())).isTrue();
}
}
use of io.etcd.jetcd.watch.WatchEvent in project jetcd by coreos.
the class CommandWatch method accept.
@Override
public void accept(Client client) throws Exception {
CountDownLatch latch = new CountDownLatch(maxEvents);
Watcher watcher = null;
try {
ByteSequence watchKey = ByteSequence.from(key, Charsets.UTF_8);
WatchOption watchOpts = WatchOption.newBuilder().withRevision(rev).build();
watcher = client.getWatchClient().watch(watchKey, watchOpts, response -> {
for (WatchEvent event : response.getEvents()) {
LOGGER.info("type={}, key={}, value={}", event.getEventType().toString(), Optional.ofNullable(event.getKeyValue().getKey()).map(bs -> bs.toString(Charsets.UTF_8)).orElse(""), Optional.ofNullable(event.getKeyValue().getValue()).map(bs -> bs.toString(Charsets.UTF_8)).orElse(""));
}
latch.countDown();
});
latch.await();
} catch (Exception e) {
if (watcher != null) {
watcher.close();
}
throw e;
}
}
use of io.etcd.jetcd.watch.WatchEvent in project jetcd by coreos.
the class WatchUnitTest method assertEqualOnWatchResponses.
private static void assertEqualOnWatchResponses(io.etcd.jetcd.watch.WatchResponse expected, io.etcd.jetcd.watch.WatchResponse actual) {
assertThat(actual.getEvents().size()).isEqualTo(expected.getEvents().size());
for (int idx = 0; idx < expected.getEvents().size(); idx++) {
WatchEvent act = actual.getEvents().get(idx);
WatchEvent exp = actual.getEvents().get(idx);
assertThat(act.getEventType()).isEqualTo(exp.getEventType());
assertEqualOnKeyValues(act.getKeyValue(), exp.getKeyValue());
assertEqualOnKeyValues(act.getPrevKV(), exp.getPrevKV());
}
}
use of io.etcd.jetcd.watch.WatchEvent in project dubbo by alibaba.
the class JEtcdClientTest method test_watch_when_modify.
@Test
public void test_watch_when_modify() {
String path = "/dubbo/config/jetcd-client-unit-test/configurators";
String endpoint = "http://127.0.0.1:2379";
CountDownLatch latch = new CountDownLatch(1);
ByteSequence key = ByteSequence.from(path, UTF_8);
Watch.Listener listener = Watch.listener(response -> {
for (WatchEvent event : response.getEvents()) {
Assertions.assertEquals("PUT", event.getEventType().toString());
Assertions.assertEquals(path, event.getKeyValue().getKey().toString(UTF_8));
Assertions.assertEquals("Hello", event.getKeyValue().getValue().toString(UTF_8));
latch.countDown();
}
});
try (Client client = Client.builder().endpoints(endpoint).build();
Watch watch = client.getWatchClient();
Watch.Watcher watcher = watch.watch(key, listener)) {
// try to modify the key
client.getKVClient().put(ByteSequence.from(path, UTF_8), ByteSequence.from("Hello", UTF_8));
latch.await();
} catch (Exception e) {
Assertions.fail(e.getMessage());
}
}
Aggregations