use of io.etcd.jetcd.options.WatchOption in project jetcd by coreos.
the class WatchTest method testWatchFutureRevisionIsNotOverwrittenOnCreation.
@ParameterizedTest
@MethodSource("parameters")
public void testWatchFutureRevisionIsNotOverwrittenOnCreation(final Client client) throws Exception {
final ByteSequence key = randomByteSequence();
final ByteSequence value = randomByteSequence();
final List<WatchResponse> events = Collections.synchronizedList(new ArrayList<>());
PutResponse putResponse = client.getKVClient().put(key, value).get();
long lastSeenRevision = putResponse.getHeader().getRevision();
WatchOption watchOption = WatchOption.newBuilder().withRevision(lastSeenRevision + 1).build();
try (Watcher watcher = client.getWatchClient().watch(key, watchOption, events::add)) {
// resumes (recreates) the watch
cluster.restart();
// await().duration() would be better but it's broken
Thread.sleep(2000);
assertThat(events.isEmpty()).as("verify that received events list is empty").isTrue();
}
}
use of io.etcd.jetcd.options.WatchOption 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.options.WatchOption in project jetcd by coreos.
the class WatchTest method testCancelledWatchGetsClosed.
@ParameterizedTest
@MethodSource("parameters")
public void testCancelledWatchGetsClosed(final Client client) throws Exception {
final ByteSequence key = randomByteSequence();
final Watch wc = client.getWatchClient();
long revision = getCompactedRevision(client, key);
final WatchOption options = WatchOption.newBuilder().withRevision(revision).build();
final AtomicReference<Throwable> ref = new AtomicReference<>();
final AtomicReference<Boolean> completed = new AtomicReference<>();
Watch.Listener listener = Watch.listener(TestUtil::noOpWatchResponseConsumer, ref::set, () -> {
completed.set(Boolean.TRUE);
});
try (Watcher watcher = wc.watch(key, options, listener)) {
await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> assertThat(ref.get()).isNotNull());
assertThat(ref.get().getClass()).isEqualTo(CompactedException.class);
assertThat(completed.get()).isNotNull();
assertThat(completed.get()).isEqualTo(Boolean.TRUE);
}
}
use of io.etcd.jetcd.options.WatchOption in project jetcd by coreos.
the class WatchTest method testWatchCompacted.
@ParameterizedTest
@MethodSource("parameters")
public void testWatchCompacted(final Client client) throws Exception {
final ByteSequence key = randomByteSequence();
final AtomicReference<Throwable> ref = new AtomicReference<>();
// Try to listen from previous revision on
final WatchOption options = WatchOption.newBuilder().withRevision(getCompactedRevision(client, key)).build();
final Watch wc = client.getWatchClient();
try (Watcher watcher = wc.watch(key, options, Watch.listener(TestUtil::noOpWatchResponseConsumer, ref::set))) {
await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> assertThat(ref.get()).isNotNull());
assertThat(ref.get().getClass()).isEqualTo(CompactedException.class);
}
}
Aggregations