Search in sources :

Example 1 with WatchResponse

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();
    }
}
Also used : Watcher(io.etcd.jetcd.Watch.Watcher) AtomicReference(java.util.concurrent.atomic.AtomicReference) WatchEvent(io.etcd.jetcd.watch.WatchEvent) 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 2 with WatchResponse

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();
    }
}
Also used : Watcher(io.etcd.jetcd.Watch.Watcher) PutResponse(io.etcd.jetcd.kv.PutResponse) WatchResponse(io.etcd.jetcd.watch.WatchResponse) ByteSequence(io.etcd.jetcd.ByteSequence) TestUtil.randomByteSequence(io.etcd.jetcd.impl.TestUtil.randomByteSequence) WatchOption(io.etcd.jetcd.options.WatchOption) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 3 with WatchResponse

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

Example 4 with WatchResponse

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);
        }
    }
}
Also used : Watch(io.etcd.jetcd.Watch) Watcher(io.etcd.jetcd.Watch.Watcher) AtomicReference(java.util.concurrent.atomic.AtomicReference) KV(io.etcd.jetcd.KV) Client(io.etcd.jetcd.Client) WatchResponse(io.etcd.jetcd.watch.WatchResponse) ByteSequence(io.etcd.jetcd.ByteSequence) Test(org.junit.jupiter.api.Test)

Example 5 with WatchResponse

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);
    }
}
Also used : Watcher(io.etcd.jetcd.Watch.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) 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