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