use of io.etcd.jetcd.Watch in project jetcd by coreos.
the class WatchUnitTest method testWatcherCreateOnInvalidWatchID.
@Test
public void testWatcherCreateOnInvalidWatchID() throws InterruptedException {
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<Throwable> ref = new AtomicReference<>();
Watch.Listener listener = Watch.listener(r -> {
}, t -> {
ref.set(t);
latch.countDown();
});
try (Watch.Watcher watcher = watchClient.watch(KEY, listener)) {
WatchResponse createdResponse = createWatchResponse(-1);
responseObserverRef.get().onNext(createdResponse);
latch.await(4, TimeUnit.SECONDS);
assertThat(ref.get()).isNotNull();
assertThat(ref.get()).isInstanceOf(EtcdException.class).hasMessageContaining("etcd server failed to create watch id");
}
}
use of io.etcd.jetcd.Watch 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 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.Watch in project jetcd by coreos.
the class WatchTest method testProgressRequest.
@ParameterizedTest
@MethodSource("parameters")
public void testProgressRequest(final Client client) throws Exception {
final ByteSequence key = randomByteSequence();
final ByteSequence value = randomByteSequence();
final Watch watchClient = client.getWatchClient();
final AtomicReference<WatchResponse> emptyWatcherEventRef = new AtomicReference<>();
final AtomicReference<WatchResponse> activeWatcherEventRef = new AtomicReference<>();
try (Watcher activeWatcher = watchClient.watch(key, activeWatcherEventRef::set);
Watcher emptyWatcher = watchClient.watch(key.concat(randomByteSequence()), emptyWatcherEventRef::set)) {
// Check that a requestProgress returns identical revisions initially
watchClient.requestProgress();
await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> {
assertThat(activeWatcherEventRef.get()).isNotNull();
assertThat(emptyWatcherEventRef.get()).isNotNull();
});
WatchResponse activeEvent = activeWatcherEventRef.get();
WatchResponse emptyEvent = emptyWatcherEventRef.get();
assertThat(activeEvent).satisfies(WatchResponse::isProgressNotify);
assertThat(emptyEvent).satisfies(WatchResponse::isProgressNotify);
assertThat(activeEvent.getHeader().getRevision()).isEqualTo(emptyEvent.getHeader().getRevision());
// Put a value being watched by only the active watcher
activeWatcherEventRef.set(null);
emptyWatcherEventRef.set(null);
client.getKVClient().put(key, value).get();
await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> {
assertThat(activeWatcherEventRef.get()).isNotNull();
});
activeEvent = activeWatcherEventRef.get();
emptyEvent = emptyWatcherEventRef.get();
assertThat(emptyEvent).isNull();
assertThat(activeEvent).isNotNull();
long latestRevision = activeEvent.getHeader().getRevision();
// verify the next progress notify brings both watchers to the latest revision
activeWatcherEventRef.set(null);
emptyWatcherEventRef.set(null);
watchClient.requestProgress();
await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> {
assertThat(activeWatcherEventRef.get()).isNotNull();
assertThat(emptyWatcherEventRef.get()).isNotNull();
});
activeEvent = activeWatcherEventRef.get();
emptyEvent = emptyWatcherEventRef.get();
assertThat(activeEvent).satisfies(WatchResponse::isProgressNotify);
assertThat(emptyEvent).satisfies(WatchResponse::isProgressNotify);
assertThat(activeEvent.getHeader().getRevision()).isEqualTo(emptyEvent.getHeader().getRevision()).isEqualTo(latestRevision);
}
}
use of io.etcd.jetcd.Watch in project jetcd by coreos.
the class WatchTokenExpireTest method testRefreshExpiredToken.
@Test
public void testRefreshExpiredToken() throws Exception {
setUpEnvironment();
Client authClient = createAuthClient();
Watch authWatchClient = authClient.getWatchClient();
KV authKVClient = authClient.getKVClient();
authKVClient.put(key, TestUtil.randomByteSequence()).get(1, TimeUnit.SECONDS);
Thread.sleep(3000);
AtomicInteger modifications = new AtomicInteger();
// watch should handle token refresh automatically
// token is already expired when we attempt to create a watch
Watch.Watcher watcher = authWatchClient.watch(key, response -> {
modifications.incrementAndGet();
});
// create single thread pool, so that tasks are executed one after another
ExecutorService executor = Executors.newFixedThreadPool(1);
List<Future<?>> futures = new ArrayList<>(2);
Client anotherClient = createAuthClient();
for (int i = 0; i < 2; ++i) {
futures.add(executor.submit(() -> {
try {
// wait 3 seconds for token to expire. during the test token will be refreshed twice
Thread.sleep(3000);
anotherClient.getKVClient().put(key, TestUtil.randomByteSequence()).get(1, TimeUnit.SECONDS);
} catch (Exception e) {
throw new RuntimeException(e);
}
}));
}
await().atMost(15, TimeUnit.SECONDS).untilAsserted(() -> assertThat(modifications.get()).isEqualTo(2));
executor.shutdownNow();
futures.forEach(f -> assertThat(f).isDone());
anotherClient.close();
watcher.close();
authWatchClient.close();
authClient.close();
}
Aggregations