use of io.etcd.jetcd.support.CloseableClient in project jetcd by coreos.
the class LeaseTest method testKeepAlive.
@Test
public void testKeepAlive() throws ExecutionException, InterruptedException {
long leaseID = leaseClient.grant(2).get().getID();
kvClient.put(KEY, VALUE, PutOption.newBuilder().withLeaseId(leaseID).build()).get();
assertThat(kvClient.get(KEY).get().getCount()).isEqualTo(1);
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<LeaseKeepAliveResponse> responseRef = new AtomicReference<>();
StreamObserver<LeaseKeepAliveResponse> observer = Observers.observer(response -> {
responseRef.set(response);
latch.countDown();
});
try (CloseableClient c = leaseClient.keepAlive(leaseID, observer)) {
latch.await(5, TimeUnit.SECONDS);
LeaseKeepAliveResponse response = responseRef.get();
assertThat(response.getTTL()).isGreaterThan(0);
}
Thread.sleep(3000);
assertThat(kvClient.get(KEY).get().getCount()).isEqualTo(0);
}
use of io.etcd.jetcd.support.CloseableClient in project jetcd by coreos.
the class LeaseImpl method keepAliveOnce.
@Override
public CompletableFuture<LeaseKeepAliveResponse> keepAliveOnce(long leaseId) {
final CompletableFuture<LeaseKeepAliveResponse> future = new CompletableFuture<>();
final CloseableClient ka = keepAlive(leaseId, new StreamObserver<LeaseKeepAliveResponse>() {
@Override
public void onNext(LeaseKeepAliveResponse value) {
future.complete(value);
}
@Override
public void onError(Throwable t) {
future.completeExceptionally(toEtcdException(t));
}
@Override
public void onCompleted() {
}
});
return future.whenCompleteAsync((val, throwable) -> ka.close(), connectionManager().getExecutorService());
}
Aggregations