Search in sources :

Example 1 with Watcher

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);
}
Also used : Watcher(io.etcd.jetcd.Watch.Watcher) Client(io.etcd.jetcd.Client) EtcdException(io.etcd.jetcd.common.exception.EtcdException) TestUtil.randomByteSequence(io.etcd.jetcd.impl.TestUtil.randomByteSequence) ByteSequence(io.etcd.jetcd.ByteSequence) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with Watcher

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();
    }
}
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 3 with Watcher

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();
    }
}
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 4 with Watcher

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);
    }
}
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 5 with Watcher

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);
        }
    }
}
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)

Aggregations

ByteSequence (io.etcd.jetcd.ByteSequence)12 Watcher (io.etcd.jetcd.Watch.Watcher)12 TestUtil.randomByteSequence (io.etcd.jetcd.impl.TestUtil.randomByteSequence)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 WatchResponse (io.etcd.jetcd.watch.WatchResponse)8 MethodSource (org.junit.jupiter.params.provider.MethodSource)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 Client (io.etcd.jetcd.Client)4 Watch (io.etcd.jetcd.Watch)4 WatchOption (io.etcd.jetcd.options.WatchOption)4 WatchEvent (io.etcd.jetcd.watch.WatchEvent)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.jupiter.api.Test)2 Parameter (com.beust.jcommander.Parameter)1 Parameters (com.beust.jcommander.Parameters)1 Charsets (com.google.common.base.Charsets)1 KV (io.etcd.jetcd.KV)1 EtcdException (io.etcd.jetcd.common.exception.EtcdException)1 PutResponse (io.etcd.jetcd.kv.PutResponse)1 Optional (java.util.Optional)1