Search in sources :

Example 1 with Event

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

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

the class JEtcdClientTest method testCancelWatchWithGrpc.

@Test
public void testCancelWatchWithGrpc() {
    String path = "/dubbo/config/testCancelWatchWithGrpc/configurators";
    String endpoint = "http://127.0.0.1:2379";
    CountDownLatch updateLatch = new CountDownLatch(1);
    CountDownLatch cancelLatch = new CountDownLatch(1);
    final AtomicLong watchID = new AtomicLong(-1L);
    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) {
                watchID.set(response.getWatchId());
                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));
                    updateLatch.countDown();
                }
                if (response.getCanceled()) {
                    // received the cancel response
                    cancelLatch.countDown();
                }
            }

            @Override
            public void onError(Throwable throwable) {
            }

            @Override
            public void onCompleted() {
            }
        });
        // create
        WatchCreateRequest.Builder builder = WatchCreateRequest.newBuilder().setKey(ByteString.copyFrom(path, UTF_8));
        // make the grpc call to watch the key
        observer.onNext(WatchRequest.newBuilder().setCreateRequest(builder).build());
        // try to put the value
        client.getKVClient().put(ByteSequence.from(path, UTF_8), ByteSequence.from("Hello", UTF_8));
        // response received, latch counts down to zero
        updateLatch.await();
        WatchCancelRequest watchCancelRequest = WatchCancelRequest.newBuilder().setWatchId(watchID.get()).build();
        WatchRequest cancelRequest = WatchRequest.newBuilder().setCancelRequest(watchCancelRequest).build();
        observer.onNext(cancelRequest);
        // try to put the value
        client.getKVClient().put(ByteSequence.from(path, UTF_8), ByteSequence.from("Hello world", UTF_8));
        cancelLatch.await();
    } 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) WatchCancelRequest(io.etcd.jetcd.api.WatchCancelRequest) AtomicLong(java.util.concurrent.atomic.AtomicLong) 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)

Aggregations

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 WatchResponse (io.etcd.jetcd.api.WatchResponse)2 ClosedClientException (io.etcd.jetcd.common.exception.ClosedClientException)2 WatchEvent (io.etcd.jetcd.watch.WatchEvent)2 ManagedChannel (io.grpc.ManagedChannel)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Test (org.junit.jupiter.api.Test)2 WatchCancelRequest (io.etcd.jetcd.api.WatchCancelRequest)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1