use of io.etcd.jetcd.watch.WatchResponse 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.WatchResponse 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.watch.WatchResponse in project jetcd by coreos.
the class WatchTest method testNamespacedAndNotNamespacedClient.
@Test
public void testNamespacedAndNotNamespacedClient() throws Exception {
final ByteSequence key = randomByteSequence();
final ByteSequence nsKey = ByteSequence.from(namespace.concat(key).getBytes());
final Client client = TestUtil.client(cluster).build();
final Client nsClient = TestUtil.client(cluster).namespace(namespace).build();
final ByteSequence value = randomByteSequence();
final AtomicReference<WatchResponse> ref = new AtomicReference<>();
// From client with namespace watch for key. Since client is namespaced it should watch for namespaced key.
try (Watcher watcher = nsClient.getWatchClient().watch(key, ref::set)) {
// Using non-namespaced client put namespaced key.
client.getKVClient().put(nsKey, value).get();
await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> assertThat(ref.get()).isNotNull());
assertThat(ref.get()).isNotNull();
assertThat(ref.get().getEvents().size()).isEqualTo(1);
assertThat(ref.get().getEvents().get(0).getEventType()).isEqualTo(EventType.PUT);
assertThat(ref.get().getEvents().get(0).getKeyValue().getKey()).isEqualTo(key);
}
}
use of io.etcd.jetcd.watch.WatchResponse in project jetcd by coreos.
the class WatchResumeTest method testWatchOnPut.
@Test
public void testWatchOnPut() throws Exception {
try (Client client = TestUtil.client(cluster).build()) {
Watch watchClient = client.getWatchClient();
KV kvClient = client.getKVClient();
final ByteSequence key = TestUtil.randomByteSequence();
final ByteSequence value = TestUtil.randomByteSequence();
final AtomicReference<WatchResponse> ref = new AtomicReference<>();
try (Watcher watcher = watchClient.watch(key, ref::set)) {
cluster.restart();
kvClient.put(key, value).get(1, TimeUnit.SECONDS);
await().atMost(30, TimeUnit.SECONDS).untilAsserted(() -> assertThat(ref.get()).isNotNull());
assertThat(ref.get().getEvents().size()).isEqualTo(1);
assertThat(ref.get().getEvents().get(0).getEventType()).isEqualTo(EventType.PUT);
assertThat(ref.get().getEvents().get(0).getKeyValue().getKey()).isEqualTo(key);
}
}
}
use of io.etcd.jetcd.watch.WatchResponse in project jetcd by coreos.
the class WatchTest method testMultipleWatch.
@ParameterizedTest
@MethodSource("parameters")
public void testMultipleWatch(final Client client) throws Exception {
final ByteSequence key = randomByteSequence();
final CountDownLatch latch = new CountDownLatch(2);
final ByteSequence value = randomByteSequence();
final List<WatchResponse> res = Collections.synchronizedList(new ArrayList<>(2));
try (Watcher w1 = client.getWatchClient().watch(key, res::add);
Watcher w2 = client.getWatchClient().watch(key, res::add)) {
client.getKVClient().put(key, value).get();
latch.await(4, TimeUnit.SECONDS);
await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> assertThat(res).hasSize(2));
assertThat(res.get(0)).usingRecursiveComparison().isEqualTo(res.get(1));
assertThat(res.get(0).getEvents().size()).isEqualTo(1);
assertThat(res.get(0).getEvents().get(0).getEventType()).isEqualTo(EventType.PUT);
assertThat(res.get(0).getEvents().get(0).getKeyValue().getKey()).isEqualTo(key);
}
}
Aggregations