Search in sources :

Example 1 with WatchOption

use of io.etcd.jetcd.options.WatchOption 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 2 with WatchOption

use of io.etcd.jetcd.options.WatchOption 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 WatchOption

use of io.etcd.jetcd.options.WatchOption in project jetcd by coreos.

the class WatchTest method testCancelledWatchGetsClosed.

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

use of io.etcd.jetcd.options.WatchOption 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)4 Watcher (io.etcd.jetcd.Watch.Watcher)4 WatchOption (io.etcd.jetcd.options.WatchOption)4 TestUtil.randomByteSequence (io.etcd.jetcd.impl.TestUtil.randomByteSequence)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 MethodSource (org.junit.jupiter.params.provider.MethodSource)3 Watch (io.etcd.jetcd.Watch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Parameter (com.beust.jcommander.Parameter)1 Parameters (com.beust.jcommander.Parameters)1 Charsets (com.google.common.base.Charsets)1 Client (io.etcd.jetcd.Client)1 PutResponse (io.etcd.jetcd.kv.PutResponse)1 WatchEvent (io.etcd.jetcd.watch.WatchEvent)1 WatchResponse (io.etcd.jetcd.watch.WatchResponse)1 Optional (java.util.Optional)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CheckedConsumer (org.jooq.lambda.fi.util.function.CheckedConsumer)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1