use of io.etcd.jetcd.Watch.Watcher in project jetcd by coreos.
the class WatchErrorTest method testWatchOnError.
@ParameterizedTest
@ValueSource(strings = { "test-namespace/", "" })
public void testWatchOnError(String ns) {
final Client client = ns != null && ns.length() == 0 ? TestUtil.client(cluster).namespace(bytesOf(ns)).build() : TestUtil.client(cluster).build();
final ByteSequence key = randomByteSequence();
final List<Throwable> events = Collections.synchronizedList(new ArrayList<>());
try (Watcher watcher = client.getWatchClient().watch(key, TestUtil::noOpWatchResponseConsumer, events::add)) {
cluster.cluster().stop();
await().atMost(15, TimeUnit.SECONDS).untilAsserted(() -> assertThat(events).isNotEmpty());
}
assertThat(events).allMatch(EtcdException.class::isInstance);
}
use of io.etcd.jetcd.Watch.Watcher 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.Watcher 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.Watcher 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.Watcher 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);
}
}
}
Aggregations