Search in sources :

Example 6 with WatchResponse

use of io.etcd.jetcd.api.WatchResponse 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 7 with WatchResponse

use of io.etcd.jetcd.api.WatchResponse in project jetcd by coreos.

the class WatchUnitTest method testWatcherDelete.

@Test
public void testWatcherDelete() throws InterruptedException {
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<io.etcd.jetcd.watch.WatchResponse> ref = new AtomicReference<>();
    Watch.Listener listener = Watch.listener(response -> {
        ref.set(response);
        latch.countDown();
    });
    try (Watch.Watcher watcher = watchClient.watch(KEY, listener)) {
        WatchResponse createdResponse = createWatchResponse(0);
        responseObserverRef.get().onNext(createdResponse);
        WatchResponse deleteResponse = WatchResponse.newBuilder().setWatchId(0).addEvents(Event.newBuilder().setType(EventType.DELETE).build()).build();
        responseObserverRef.get().onNext(deleteResponse);
        latch.await(4, TimeUnit.SECONDS);
        assertThat(ref.get()).isNotNull();
        assertEqualOnWatchResponses(ref.get(), new io.etcd.jetcd.watch.WatchResponse(deleteResponse));
    }
}
Also used : Watch(io.etcd.jetcd.Watch) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) WatchResponse(io.etcd.jetcd.api.WatchResponse) Test(org.junit.jupiter.api.Test)

Example 8 with WatchResponse

use of io.etcd.jetcd.api.WatchResponse in project dubbo by alibaba.

the class JEtcdClientTest method testWatchWithGrpc.

@Test
public void testWatchWithGrpc() {
    String path = "/dubbo/config/test_watch_with_grpc/configurators";
    String endpoint = "http://127.0.0.1:2379";
    CountDownLatch latch = new CountDownLatch(1);
    try (Client client = Client.builder().endpoints(endpoint).build()) {
        ManagedChannel channel = getChannel(client);
        StreamObserver<WatchRequest> observer = WatchGrpc.newStub(channel).watch(new StreamObserver<WatchResponse>() {

            @Override
            public void onNext(WatchResponse response) {
                for (Event event : response.getEventsList()) {
                    Assertions.assertEquals("PUT", event.getType().toString());
                    Assertions.assertEquals(path, event.getKv().getKey().toString(UTF_8));
                    Assertions.assertEquals("Hello", event.getKv().getValue().toString(UTF_8));
                    latch.countDown();
                }
            }

            @Override
            public void onError(Throwable throwable) {
            }

            @Override
            public void onCompleted() {
            }
        });
        WatchCreateRequest.Builder builder = WatchCreateRequest.newBuilder().setKey(ByteString.copyFrom(path, UTF_8));
        observer.onNext(WatchRequest.newBuilder().setCreateRequest(builder).build());
        // try to modify the key
        client.getKVClient().put(ByteSequence.from(path, UTF_8), ByteSequence.from("Hello", UTF_8));
        latch.await(5, TimeUnit.SECONDS);
    } catch (Exception e) {
        Assertions.fail(e.getMessage());
    }
}
Also used : WatchRequest(io.etcd.jetcd.api.WatchRequest) ByteString(com.google.protobuf.ByteString) CountDownLatch(java.util.concurrent.CountDownLatch) WatchResponse(io.etcd.jetcd.api.WatchResponse) ClosedClientException(io.etcd.jetcd.common.exception.ClosedClientException) WatchCreateRequest(io.etcd.jetcd.api.WatchCreateRequest) ManagedChannel(io.grpc.ManagedChannel) WatchEvent(io.etcd.jetcd.watch.WatchEvent) Event(io.etcd.jetcd.api.Event) Client(io.etcd.jetcd.Client) Test(org.junit.jupiter.api.Test)

Example 9 with WatchResponse

use of io.etcd.jetcd.api.WatchResponse in project jetcd by coreos.

the class WatchUnitTest method testWatcherListenOnResponse.

@Test
public void testWatcherListenOnResponse() throws InterruptedException {
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<io.etcd.jetcd.watch.WatchResponse> ref = new AtomicReference<>();
    Watch.Listener listener = Watch.listener(response -> {
        ref.set(response);
        latch.countDown();
    });
    try (Watch.Watcher watcher = watchClient.watch(KEY, WatchOption.DEFAULT, listener)) {
        WatchResponse createdResponse = createWatchResponse(0);
        responseObserverRef.get().onNext(createdResponse);
        WatchResponse putResponse = WatchResponse.newBuilder().setWatchId(0).addEvents(Event.newBuilder().setType(EventType.PUT).build()).build();
        responseObserverRef.get().onNext(putResponse);
        latch.await(4, TimeUnit.SECONDS);
        assertThat(ref.get()).isNotNull();
        assertThat(ref.get().getEvents().size()).isEqualTo(1);
        assertThat(ref.get().getEvents().get(0).getEventType()).isEqualTo(WatchEvent.EventType.PUT);
    }
}
Also used : Watch(io.etcd.jetcd.Watch) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) WatchResponse(io.etcd.jetcd.api.WatchResponse) Test(org.junit.jupiter.api.Test)

Example 10 with WatchResponse

use of io.etcd.jetcd.api.WatchResponse in project jetcd by coreos.

the class WatchUnitTest method testWatchOnUnrecoverableConnectionIssue.

@Test
public void testWatchOnUnrecoverableConnectionIssue() 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, WatchOption.DEFAULT, listener)) {
        WatchResponse createdResponse = createWatchResponse(0);
        responseObserverRef.get().onNext(createdResponse);
        responseObserverRef.get().onError(Status.ABORTED.withDescription("connection error").asRuntimeException());
        latch.await(4, TimeUnit.SECONDS);
        assertThat(ref.get()).isNotNull();
        assertThat(ref.get()).isInstanceOf(EtcdException.class).hasMessageContaining("connection error");
    }
}
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)

Aggregations

WatchResponse (io.etcd.jetcd.api.WatchResponse)11 CountDownLatch (java.util.concurrent.CountDownLatch)11 Test (org.junit.jupiter.api.Test)11 Watch (io.etcd.jetcd.Watch)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 EtcdException (io.etcd.jetcd.common.exception.EtcdException)6 ByteString (com.google.protobuf.ByteString)2 Client (io.etcd.jetcd.Client)2 Event (io.etcd.jetcd.api.Event)2 WatchCreateRequest (io.etcd.jetcd.api.WatchCreateRequest)2 WatchRequest (io.etcd.jetcd.api.WatchRequest)2 ClosedClientException (io.etcd.jetcd.common.exception.ClosedClientException)2 WatchEvent (io.etcd.jetcd.watch.WatchEvent)2 ManagedChannel (io.grpc.ManagedChannel)2 WatchCancelRequest (io.etcd.jetcd.api.WatchCancelRequest)1 ArrayList (java.util.ArrayList)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1