Search in sources :

Example 1 with Watch

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

the class WatchUnitTest method testWatcherCreateOnInvalidWatchID.

@Test
public void testWatcherCreateOnInvalidWatchID() throws InterruptedException {
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<Throwable> ref = new AtomicReference<>();
    Watch.Listener listener = Watch.listener(r -> {
    }, t -> {
        ref.set(t);
        latch.countDown();
    });
    try (Watch.Watcher watcher = watchClient.watch(KEY, listener)) {
        WatchResponse createdResponse = createWatchResponse(-1);
        responseObserverRef.get().onNext(createdResponse);
        latch.await(4, TimeUnit.SECONDS);
        assertThat(ref.get()).isNotNull();
        assertThat(ref.get()).isInstanceOf(EtcdException.class).hasMessageContaining("etcd server failed to create watch id");
    }
}
Also used : Watch(io.etcd.jetcd.Watch) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) WatchResponse(io.etcd.jetcd.api.WatchResponse) EtcdException(io.etcd.jetcd.common.exception.EtcdException) Test(org.junit.jupiter.api.Test)

Example 2 with Watch

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

Example 3 with Watch

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

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

the class WatchTest method testProgressRequest.

@ParameterizedTest
@MethodSource("parameters")
public void testProgressRequest(final Client client) throws Exception {
    final ByteSequence key = randomByteSequence();
    final ByteSequence value = randomByteSequence();
    final Watch watchClient = client.getWatchClient();
    final AtomicReference<WatchResponse> emptyWatcherEventRef = new AtomicReference<>();
    final AtomicReference<WatchResponse> activeWatcherEventRef = new AtomicReference<>();
    try (Watcher activeWatcher = watchClient.watch(key, activeWatcherEventRef::set);
        Watcher emptyWatcher = watchClient.watch(key.concat(randomByteSequence()), emptyWatcherEventRef::set)) {
        // Check that a requestProgress returns identical revisions initially
        watchClient.requestProgress();
        await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> {
            assertThat(activeWatcherEventRef.get()).isNotNull();
            assertThat(emptyWatcherEventRef.get()).isNotNull();
        });
        WatchResponse activeEvent = activeWatcherEventRef.get();
        WatchResponse emptyEvent = emptyWatcherEventRef.get();
        assertThat(activeEvent).satisfies(WatchResponse::isProgressNotify);
        assertThat(emptyEvent).satisfies(WatchResponse::isProgressNotify);
        assertThat(activeEvent.getHeader().getRevision()).isEqualTo(emptyEvent.getHeader().getRevision());
        // Put a value being watched by only the active watcher
        activeWatcherEventRef.set(null);
        emptyWatcherEventRef.set(null);
        client.getKVClient().put(key, value).get();
        await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> {
            assertThat(activeWatcherEventRef.get()).isNotNull();
        });
        activeEvent = activeWatcherEventRef.get();
        emptyEvent = emptyWatcherEventRef.get();
        assertThat(emptyEvent).isNull();
        assertThat(activeEvent).isNotNull();
        long latestRevision = activeEvent.getHeader().getRevision();
        // verify the next progress notify brings both watchers to the latest revision
        activeWatcherEventRef.set(null);
        emptyWatcherEventRef.set(null);
        watchClient.requestProgress();
        await().atMost(TIME_OUT_SECONDS, TimeUnit.SECONDS).untilAsserted(() -> {
            assertThat(activeWatcherEventRef.get()).isNotNull();
            assertThat(emptyWatcherEventRef.get()).isNotNull();
        });
        activeEvent = activeWatcherEventRef.get();
        emptyEvent = emptyWatcherEventRef.get();
        assertThat(activeEvent).satisfies(WatchResponse::isProgressNotify);
        assertThat(emptyEvent).satisfies(WatchResponse::isProgressNotify);
        assertThat(activeEvent.getHeader().getRevision()).isEqualTo(emptyEvent.getHeader().getRevision()).isEqualTo(latestRevision);
    }
}
Also used : Watch(io.etcd.jetcd.Watch) 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 5 with Watch

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

the class WatchTokenExpireTest method testRefreshExpiredToken.

@Test
public void testRefreshExpiredToken() throws Exception {
    setUpEnvironment();
    Client authClient = createAuthClient();
    Watch authWatchClient = authClient.getWatchClient();
    KV authKVClient = authClient.getKVClient();
    authKVClient.put(key, TestUtil.randomByteSequence()).get(1, TimeUnit.SECONDS);
    Thread.sleep(3000);
    AtomicInteger modifications = new AtomicInteger();
    // watch should handle token refresh automatically
    // token is already expired when we attempt to create a watch
    Watch.Watcher watcher = authWatchClient.watch(key, response -> {
        modifications.incrementAndGet();
    });
    // create single thread pool, so that tasks are executed one after another
    ExecutorService executor = Executors.newFixedThreadPool(1);
    List<Future<?>> futures = new ArrayList<>(2);
    Client anotherClient = createAuthClient();
    for (int i = 0; i < 2; ++i) {
        futures.add(executor.submit(() -> {
            try {
                // wait 3 seconds for token to expire. during the test token will be refreshed twice
                Thread.sleep(3000);
                anotherClient.getKVClient().put(key, TestUtil.randomByteSequence()).get(1, TimeUnit.SECONDS);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }));
    }
    await().atMost(15, TimeUnit.SECONDS).untilAsserted(() -> assertThat(modifications.get()).isEqualTo(2));
    executor.shutdownNow();
    futures.forEach(f -> assertThat(f).isDone());
    anotherClient.close();
    watcher.close();
    authWatchClient.close();
    authClient.close();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Watch(io.etcd.jetcd.Watch) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) KV(io.etcd.jetcd.KV) Client(io.etcd.jetcd.Client) 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