Search in sources :

Example 1 with WatchEvent

use of io.etcd.jetcd.watch.WatchEvent 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 WatchEvent

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

the class CommandWatch method accept.

@Override
public void accept(Client client) throws Exception {
    CountDownLatch latch = new CountDownLatch(maxEvents);
    Watcher watcher = null;
    try {
        ByteSequence watchKey = ByteSequence.from(key, Charsets.UTF_8);
        WatchOption watchOpts = WatchOption.newBuilder().withRevision(rev).build();
        watcher = client.getWatchClient().watch(watchKey, watchOpts, response -> {
            for (WatchEvent event : response.getEvents()) {
                LOGGER.info("type={}, key={}, value={}", event.getEventType().toString(), Optional.ofNullable(event.getKeyValue().getKey()).map(bs -> bs.toString(Charsets.UTF_8)).orElse(""), Optional.ofNullable(event.getKeyValue().getValue()).map(bs -> bs.toString(Charsets.UTF_8)).orElse(""));
            }
            latch.countDown();
        });
        latch.await();
    } catch (Exception e) {
        if (watcher != null) {
            watcher.close();
        }
        throw e;
    }
}
Also used : Charsets(com.google.common.base.Charsets) Parameters(com.beust.jcommander.Parameters) Logger(org.slf4j.Logger) Client(io.etcd.jetcd.Client) WatchEvent(io.etcd.jetcd.watch.WatchEvent) Parameter(com.beust.jcommander.Parameter) LoggerFactory(org.slf4j.LoggerFactory) CountDownLatch(java.util.concurrent.CountDownLatch) ByteSequence(io.etcd.jetcd.ByteSequence) Optional(java.util.Optional) CheckedConsumer(org.jooq.lambda.fi.util.function.CheckedConsumer) WatchOption(io.etcd.jetcd.options.WatchOption) Watcher(io.etcd.jetcd.Watch.Watcher) Watcher(io.etcd.jetcd.Watch.Watcher) WatchEvent(io.etcd.jetcd.watch.WatchEvent) CountDownLatch(java.util.concurrent.CountDownLatch) ByteSequence(io.etcd.jetcd.ByteSequence) WatchOption(io.etcd.jetcd.options.WatchOption)

Example 3 with WatchEvent

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

the class WatchUnitTest method assertEqualOnWatchResponses.

private static void assertEqualOnWatchResponses(io.etcd.jetcd.watch.WatchResponse expected, io.etcd.jetcd.watch.WatchResponse actual) {
    assertThat(actual.getEvents().size()).isEqualTo(expected.getEvents().size());
    for (int idx = 0; idx < expected.getEvents().size(); idx++) {
        WatchEvent act = actual.getEvents().get(idx);
        WatchEvent exp = actual.getEvents().get(idx);
        assertThat(act.getEventType()).isEqualTo(exp.getEventType());
        assertEqualOnKeyValues(act.getKeyValue(), exp.getKeyValue());
        assertEqualOnKeyValues(act.getPrevKV(), exp.getPrevKV());
    }
}
Also used : WatchEvent(io.etcd.jetcd.watch.WatchEvent)

Example 4 with WatchEvent

use of io.etcd.jetcd.watch.WatchEvent 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

WatchEvent (io.etcd.jetcd.watch.WatchEvent)4 ByteSequence (io.etcd.jetcd.ByteSequence)3 Client (io.etcd.jetcd.Client)2 Watcher (io.etcd.jetcd.Watch.Watcher)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Parameter (com.beust.jcommander.Parameter)1 Parameters (com.beust.jcommander.Parameters)1 Charsets (com.google.common.base.Charsets)1 ByteString (com.google.protobuf.ByteString)1 Watch (io.etcd.jetcd.Watch)1 ClosedClientException (io.etcd.jetcd.common.exception.ClosedClientException)1 TestUtil.randomByteSequence (io.etcd.jetcd.impl.TestUtil.randomByteSequence)1 WatchOption (io.etcd.jetcd.options.WatchOption)1 WatchResponse (io.etcd.jetcd.watch.WatchResponse)1 Optional (java.util.Optional)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 CheckedConsumer (org.jooq.lambda.fi.util.function.CheckedConsumer)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1