Search in sources :

Example 11 with Watcher

use of io.etcd.jetcd.Watch.Watcher 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)

Example 12 with Watcher

use of io.etcd.jetcd.Watch.Watcher in project jetcd by coreos.

the class WatchTest method testWatchCompacted.

@ParameterizedTest
@MethodSource("parameters")
public void testWatchCompacted(final Client client) throws Exception {
    final ByteSequence key = randomByteSequence();
    final AtomicReference<Throwable> ref = new AtomicReference<>();
    // Try to listen from previous revision on
    final WatchOption options = WatchOption.newBuilder().withRevision(getCompactedRevision(client, key)).build();
    final Watch wc = client.getWatchClient();
    try (Watcher watcher = wc.watch(key, options, Watch.listener(TestUtil::noOpWatchResponseConsumer, ref::set))) {
        await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> assertThat(ref.get()).isNotNull());
        assertThat(ref.get().getClass()).isEqualTo(CompactedException.class);
    }
}
Also used : Watch(io.etcd.jetcd.Watch) Watcher(io.etcd.jetcd.Watch.Watcher) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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)

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