Search in sources :

Example 1 with WatchCancelRequest

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