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());
}
}
Aggregations