Search in sources :

Example 1 with CloseableClient

use of io.etcd.jetcd.support.CloseableClient in project jetcd by coreos.

the class LeaseUnitTest method testKeepAliveCloseSomeListeners.

@SuppressWarnings("PMD.UnusedLocalVariable")
@Test
public void testKeepAliveCloseSomeListeners() {
    final StreamObserver<io.etcd.jetcd.lease.LeaseKeepAliveResponse> observer = Observers.observer(response -> {
    });
    final CloseableClient client1 = this.leaseCli.keepAlive(LEASE_ID_2, observer);
    final CloseableClient client2 = this.leaseCli.keepAlive(LEASE_ID_1, observer);
    client1.close();
    // expect closing closingListener doesn't affect sending keep alive requests for LEASE_ID_2.
    verify(this.requestStreamObserverMock, after(1200).atLeast(2)).onNext(argThat(hasLeaseID(LEASE_ID_1)));
    client2.close();
}
Also used : LeaseKeepAliveResponse(io.etcd.jetcd.api.LeaseKeepAliveResponse) CloseableClient(io.etcd.jetcd.support.CloseableClient) Test(org.junit.jupiter.api.Test)

Example 2 with CloseableClient

use of io.etcd.jetcd.support.CloseableClient in project jetcd by coreos.

the class LeaseUnitTest method testKeepAliveResetOnStreamErrors.

/*
    // TODO: sometime this.responseObserverRef.get().onNext(lrp) blocks even though client has received msg;
    // seems like a bug in grpc test framework.
    @Ignore
    @Test
    public void testKeepAliveReceivesExpiredLease() {
    KeepAliveListener listener = this.leaseCli.keepAlive(LEASE_ID_1);
    LeaseKeepAliveResponse lrp = LeaseKeepAliveResponse
        .newBuilder()
        .setID(LEASE_ID_1)
        .setTTL(0)
        .build();
    this.responseObserverRef.get().onNext(lrp);
    
    // expect lease expired exception.
    assertThatThrownBy(() -> listener.listen())
        .hasCause(new IllegalStateException("Lease " + LEASE_ID_1 + " expired"));
    
    // expect no more keep alive requests for LEASE_ID after receiving lease expired response.
    verify(this.requestStreamObserverMock, after(1000).atMost(1))
        .onNext(argThat(hasLeaseID(LEASE_ID_1)));
    
    listener.close();
    }
    */
@Test
public void testKeepAliveResetOnStreamErrors() {
    final StreamObserver<io.etcd.jetcd.lease.LeaseKeepAliveResponse> observer = Observers.observer(response -> {
    });
    try (CloseableClient client = this.leaseCli.keepAlive(LEASE_ID_1, observer)) {
        Throwable t = Status.ABORTED.asRuntimeException();
        // onError triggers reset() to be executed.
        // scheduler will execute reset() in 500 ms.
        responseObserverRef.get().onError(t);
        // expect keep alive requests are still sending even with reset.
        verify(this.requestStreamObserverMock, timeout(2000).atLeast(3)).onNext(argThat(hasLeaseID(LEASE_ID_1)));
    }
}
Also used : LeaseKeepAliveResponse(io.etcd.jetcd.api.LeaseKeepAliveResponse) CloseableClient(io.etcd.jetcd.support.CloseableClient) Test(org.junit.jupiter.api.Test)

Example 3 with CloseableClient

use of io.etcd.jetcd.support.CloseableClient in project jetcd by coreos.

the class LeaseUnitTest method testKeepAliveAfterFirstKeepAliveTimeout.

@Test
public void testKeepAliveAfterFirstKeepAliveTimeout() throws InterruptedException {
    final StreamObserver<io.etcd.jetcd.lease.LeaseKeepAliveResponse> observer = Observers.observer(response -> {
    });
    try (CloseableClient listener = this.leaseCli.keepAlive(LEASE_ID_1, observer)) {
        // expect at least some KeepAlive requests are sent within
        // firstKeepAliveTimeout(5000 ms) + some quiet time (1000 ms).
        verify(this.requestStreamObserverMock, after(6000).atLeastOnce()).onNext(argThat(hasLeaseID(LEASE_ID_1)));
        // reset mock to a empty state.
        Mockito.<StreamObserver>reset(this.requestStreamObserverMock);
        // verify no keepAlive requests are sent within one second after firstKeepAliveTimeout.
        verify(this.requestStreamObserverMock, after(1000).times(0)).onNext(argThat(hasLeaseID(LEASE_ID_1)));
    }
}
Also used : LeaseKeepAliveResponse(io.etcd.jetcd.api.LeaseKeepAliveResponse) StreamObserver(io.grpc.stub.StreamObserver) CloseableClient(io.etcd.jetcd.support.CloseableClient) Test(org.junit.jupiter.api.Test)

Example 4 with CloseableClient

use of io.etcd.jetcd.support.CloseableClient in project dubbo by alibaba.

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 5 with CloseableClient

use of io.etcd.jetcd.support.CloseableClient in project jetcd by coreos.

the class LeaseUnitTest method testKeepAliveCloseOnlyListener.

@Test
public void testKeepAliveCloseOnlyListener() {
    final StreamObserver<io.etcd.jetcd.lease.LeaseKeepAliveResponse> observer = Observers.observer(response -> {
    });
    final CloseableClient client = this.leaseCli.keepAlive(LEASE_ID_1, observer);
    client.close();
    // expect no more keep alive requests are sent after the initial one
    // within 1 second after closing the only listener.
    verify(this.requestStreamObserverMock, after(1000).atMost(1)).onNext(argThat(hasLeaseID(LEASE_ID_1)));
}
Also used : LeaseKeepAliveResponse(io.etcd.jetcd.api.LeaseKeepAliveResponse) CloseableClient(io.etcd.jetcd.support.CloseableClient) Test(org.junit.jupiter.api.Test)

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