Search in sources :

Example 6 with CloseableClient

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);
}
Also used : LeaseKeepAliveResponse(io.etcd.jetcd.lease.LeaseKeepAliveResponse) CloseableClient(io.etcd.jetcd.support.CloseableClient) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 7 with CloseableClient

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());
}
Also used : LeaseKeepAliveResponse(io.etcd.jetcd.lease.LeaseKeepAliveResponse) CompletableFuture(java.util.concurrent.CompletableFuture) CloseableClient(io.etcd.jetcd.support.CloseableClient)

Aggregations

CloseableClient (io.etcd.jetcd.support.CloseableClient)7 Test (org.junit.jupiter.api.Test)6 LeaseKeepAliveResponse (io.etcd.jetcd.api.LeaseKeepAliveResponse)4 LeaseKeepAliveResponse (io.etcd.jetcd.lease.LeaseKeepAliveResponse)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 StreamObserver (io.grpc.stub.StreamObserver)1 CompletableFuture (java.util.concurrent.CompletableFuture)1