Search in sources :

Example 6 with Watch

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

Example 7 with Watch

use of io.etcd.jetcd.Watch in project dubbo by alibaba.

the class JEtcdClientTest method test_watch_when_modify.

@Test
public void test_watch_when_modify() {
    String path = "/dubbo/config/jetcd-client-unit-test/configurators";
    String endpoint = "http://127.0.0.1:2379";
    CountDownLatch latch = new CountDownLatch(1);
    ByteSequence key = ByteSequence.from(path, UTF_8);
    Watch.Listener listener = Watch.listener(response -> {
        for (WatchEvent event : response.getEvents()) {
            Assertions.assertEquals("PUT", event.getEventType().toString());
            Assertions.assertEquals(path, event.getKeyValue().getKey().toString(UTF_8));
            Assertions.assertEquals("Hello", event.getKeyValue().getValue().toString(UTF_8));
            latch.countDown();
        }
    });
    try (Client client = Client.builder().endpoints(endpoint).build();
        Watch watch = client.getWatchClient();
        Watch.Watcher watcher = watch.watch(key, listener)) {
        // try to modify the key
        client.getKVClient().put(ByteSequence.from(path, UTF_8), ByteSequence.from("Hello", UTF_8));
        latch.await();
    } catch (Exception e) {
        Assertions.fail(e.getMessage());
    }
}
Also used : Watch(io.etcd.jetcd.Watch) ByteString(com.google.protobuf.ByteString) WatchEvent(io.etcd.jetcd.watch.WatchEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Client(io.etcd.jetcd.Client) ByteSequence(io.etcd.jetcd.ByteSequence) ClosedClientException(io.etcd.jetcd.common.exception.ClosedClientException) Test(org.junit.jupiter.api.Test)

Aggregations

Watch (io.etcd.jetcd.Watch)7 ByteSequence (io.etcd.jetcd.ByteSequence)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 Watcher (io.etcd.jetcd.Watch.Watcher)4 Test (org.junit.jupiter.api.Test)4 Client (io.etcd.jetcd.Client)3 TestUtil.randomByteSequence (io.etcd.jetcd.impl.TestUtil.randomByteSequence)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 MethodSource (org.junit.jupiter.params.provider.MethodSource)3 KV (io.etcd.jetcd.KV)2 WatchOption (io.etcd.jetcd.options.WatchOption)2 WatchResponse (io.etcd.jetcd.watch.WatchResponse)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ByteString (com.google.protobuf.ByteString)1 WatchResponse (io.etcd.jetcd.api.WatchResponse)1 ClosedClientException (io.etcd.jetcd.common.exception.ClosedClientException)1 EtcdException (io.etcd.jetcd.common.exception.EtcdException)1 WatchEvent (io.etcd.jetcd.watch.WatchEvent)1 ArrayList (java.util.ArrayList)1 ExecutorService (java.util.concurrent.ExecutorService)1