Search in sources :

Example 6 with WatchResponse

use of io.etcd.jetcd.watch.WatchResponse in project jetcd by coreos.

the class WatchTest method testWatchClose.

@ParameterizedTest
@MethodSource("parameters")
public void testWatchClose(final Client client) throws Exception {
    final ByteSequence key = randomByteSequence();
    final ByteSequence value = randomByteSequence();
    final List<WatchResponse> events = Collections.synchronizedList(new ArrayList<>());
    try (Watcher watcher = client.getWatchClient().watch(key, events::add)) {
        client.getKVClient().put(key, value).get();
        await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> assertThat(events).isNotEmpty());
    }
    client.getKVClient().put(key, randomByteSequence()).get();
    await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> assertThat(events).hasSize(1));
    assertThat(events.get(0).getEvents()).hasSize(1);
    assertThat(events.get(0).getEvents().get(0).getEventType()).isEqualTo(EventType.PUT);
    assertThat(events.get(0).getEvents().get(0).getKeyValue().getKey()).isEqualTo(key);
    assertThat(events.get(0).getEvents().get(0).getKeyValue().getValue()).isEqualTo(value);
}
Also used : Watcher(io.etcd.jetcd.Watch.Watcher) WatchResponse(io.etcd.jetcd.watch.WatchResponse) ByteSequence(io.etcd.jetcd.ByteSequence) TestUtil.randomByteSequence(io.etcd.jetcd.impl.TestUtil.randomByteSequence) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 7 with WatchResponse

use of io.etcd.jetcd.watch.WatchResponse 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);
    }
}
Also used : Watch(io.etcd.jetcd.Watch) Watcher(io.etcd.jetcd.Watch.Watcher) AtomicReference(java.util.concurrent.atomic.AtomicReference) WatchResponse(io.etcd.jetcd.watch.WatchResponse) ByteSequence(io.etcd.jetcd.ByteSequence) TestUtil.randomByteSequence(io.etcd.jetcd.impl.TestUtil.randomByteSequence) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 8 with WatchResponse

use of io.etcd.jetcd.watch.WatchResponse in project jetcd by coreos.

the class WatchTest method testWatchOnPut.

@ParameterizedTest
@MethodSource("parameters")
public void testWatchOnPut(final Client client) throws Exception {
    final ByteSequence key = randomByteSequence();
    final ByteSequence value = randomByteSequence();
    final AtomicReference<WatchResponse> ref = new AtomicReference<>();
    try (Watcher watcher = client.getWatchClient().watch(key, ref::set)) {
        client.getKVClient().put(key, 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);
    }
}
Also used : Watcher(io.etcd.jetcd.Watch.Watcher) AtomicReference(java.util.concurrent.atomic.AtomicReference) WatchResponse(io.etcd.jetcd.watch.WatchResponse) ByteSequence(io.etcd.jetcd.ByteSequence) TestUtil.randomByteSequence(io.etcd.jetcd.impl.TestUtil.randomByteSequence) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

ByteSequence (io.etcd.jetcd.ByteSequence)8 Watcher (io.etcd.jetcd.Watch.Watcher)8 WatchResponse (io.etcd.jetcd.watch.WatchResponse)8 TestUtil.randomByteSequence (io.etcd.jetcd.impl.TestUtil.randomByteSequence)7 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 MethodSource (org.junit.jupiter.params.provider.MethodSource)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 Client (io.etcd.jetcd.Client)2 Watch (io.etcd.jetcd.Watch)2 Test (org.junit.jupiter.api.Test)2 KV (io.etcd.jetcd.KV)1 PutResponse (io.etcd.jetcd.kv.PutResponse)1 WatchOption (io.etcd.jetcd.options.WatchOption)1 WatchEvent (io.etcd.jetcd.watch.WatchEvent)1 CountDownLatch (java.util.concurrent.CountDownLatch)1