Search in sources :

Example 1 with NoopRequest

use of com.couchbase.client.core.msg.kv.NoopRequest in project couchbase-jvm-clients by couchbase.

the class ReactorTest method propagatesCancellation.

@Test
void propagatesCancellation() {
    NoopRequest request = new NoopRequest(Duration.ZERO, mock(RequestContext.class), mock(RetryStrategy.class), mock(CollectionIdentifier.class));
    Mono<NoopResponse> mono = Reactor.wrap(request, request.response(), true);
    assertThrows(Exception.class, () -> mono.timeout(Duration.ofMillis(10)).block());
    assertTrue(request.response().isCompletedExceptionally());
    assertTrue(request.response().isDone());
}
Also used : NoopRequest(com.couchbase.client.core.msg.kv.NoopRequest) NoopResponse(com.couchbase.client.core.msg.kv.NoopResponse) RequestContext(com.couchbase.client.core.msg.RequestContext) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier) Test(org.junit.jupiter.api.Test)

Example 2 with NoopRequest

use of com.couchbase.client.core.msg.kv.NoopRequest in project couchbase-jvm-clients by couchbase.

the class ReactorTest method completesWithErrorAfterSubscription.

@Test
void completesWithErrorAfterSubscription() {
    NoopRequest request = new NoopRequest(Duration.ZERO, mock(RequestContext.class), mock(RetryStrategy.class), mock(CollectionIdentifier.class));
    Mono<NoopResponse> mono = Reactor.wrap(request, request.response(), true);
    RequestCanceledException exception = mock(RequestCanceledException.class);
    StepVerifier verifier = StepVerifier.create(mono).expectError(RequestCanceledException.class);
    request.fail(exception);
    verifier.verify();
}
Also used : NoopRequest(com.couchbase.client.core.msg.kv.NoopRequest) NoopResponse(com.couchbase.client.core.msg.kv.NoopResponse) RequestCanceledException(com.couchbase.client.core.error.RequestCanceledException) RequestContext(com.couchbase.client.core.msg.RequestContext) StepVerifier(reactor.test.StepVerifier) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier) Test(org.junit.jupiter.api.Test)

Example 3 with NoopRequest

use of com.couchbase.client.core.msg.kv.NoopRequest in project couchbase-jvm-clients by couchbase.

the class ReactorTest method completesWithSuccessAfterSubscription.

@Test
void completesWithSuccessAfterSubscription() {
    NoopRequest request = new NoopRequest(Duration.ZERO, mock(RequestContext.class), mock(RetryStrategy.class), mock(CollectionIdentifier.class));
    Mono<NoopResponse> mono = Reactor.wrap(request, request.response(), true);
    NoopResponse response = mock(NoopResponse.class);
    StepVerifier verifier = StepVerifier.create(mono).expectNext(response).expectComplete();
    request.succeed(response);
    verifier.verify();
}
Also used : NoopRequest(com.couchbase.client.core.msg.kv.NoopRequest) NoopResponse(com.couchbase.client.core.msg.kv.NoopResponse) RequestContext(com.couchbase.client.core.msg.RequestContext) StepVerifier(reactor.test.StepVerifier) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier) Test(org.junit.jupiter.api.Test)

Example 4 with NoopRequest

use of com.couchbase.client.core.msg.kv.NoopRequest in project couchbase-jvm-clients by couchbase.

the class PooledServiceTest method dispatchesDirectlyIfSlotAvailable.

@Test
void dispatchesDirectlyIfSlotAvailable() {
    int minEndpoints = 0;
    Endpoint mock1 = mock(Endpoint.class);
    when(mock1.state()).thenReturn(EndpointState.CONNECTED);
    DirectProcessor<EndpointState> states = DirectProcessor.create();
    when(mock1.states()).thenReturn(states);
    when(mock1.outstandingRequests()).thenReturn(0L);
    final List<Endpoint> mocks = Collections.singletonList(mock1);
    final AtomicInteger invocation = new AtomicInteger();
    MockedService service = new MockedService(new MockedServiceConfig(minEndpoints, 2, Duration.ofMillis(500), false), () -> mocks.get(invocation.getAndIncrement()), new FirstEndpointSelectionStrategy());
    service.connect();
    assertTrue(service.trackedEndpoints().isEmpty());
    NoopRequest request = new NoopRequest(Duration.ofSeconds(1), serviceContext, BestEffortRetryStrategy.INSTANCE, CollectionIdentifier.fromDefault("bucket"));
    service.send(request);
    // Simulate the connecting and connected
    states.onNext(EndpointState.CONNECTING);
    states.onNext(EndpointState.CONNECTED);
    waitUntilCondition(() -> !service.trackedEndpoints.isEmpty());
    assertEquals(1, service.trackedEndpoints().size());
    assertEquals(0, request.context().retryAttempts());
    waitUntilCondition(() -> {
        Collection<Invocation> invocations = Mockito.mockingDetails(mock1).getInvocations();
        for (Invocation inv : invocations) {
            if (inv.getMethod().getName().equals("send")) {
                if (inv.getArgument(0) == request) {
                    return true;
                }
            }
        }
        return false;
    });
    verify(mock1, times(1)).send(request);
}
Also used : EndpointState(com.couchbase.client.core.endpoint.EndpointState) NoopRequest(com.couchbase.client.core.msg.kv.NoopRequest) Endpoint(com.couchbase.client.core.endpoint.Endpoint) Invocation(org.mockito.invocation.Invocation) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Endpoint(com.couchbase.client.core.endpoint.Endpoint) Test(org.junit.jupiter.api.Test)

Example 5 with NoopRequest

use of com.couchbase.client.core.msg.kv.NoopRequest in project couchbase-jvm-clients by couchbase.

the class PooledServiceTest method cleansIdleConnections.

@Test
void cleansIdleConnections() throws Exception {
    int minEndpoints = 0;
    long now = System.nanoTime();
    Endpoint mock1 = mock(Endpoint.class);
    when(mock1.state()).thenReturn(EndpointState.CONNECTED);
    DirectProcessor<EndpointState> states = DirectProcessor.create();
    when(mock1.states()).thenReturn(states);
    when(mock1.outstandingRequests()).thenReturn(1L);
    when(mock1.lastResponseReceived()).thenReturn(now);
    Endpoint mock2 = mock(Endpoint.class);
    when(mock2.state()).thenReturn(EndpointState.CONNECTED);
    when(mock2.states()).thenReturn(DirectProcessor.create());
    when(mock2.outstandingRequests()).thenReturn(1L);
    when(mock2.lastResponseReceived()).thenReturn(now);
    final List<Endpoint> mocks = Arrays.asList(mock1, mock2);
    final AtomicInteger invocation = new AtomicInteger();
    MockedService service = new MockedService(new MockedServiceConfig(minEndpoints, 2, Duration.ofMillis(500), false), () -> mocks.get(invocation.getAndIncrement()), new FirstEndpointSelectionStrategy());
    service.connect();
    assertTrue(service.trackedEndpoints().isEmpty());
    NoopRequest request1 = new NoopRequest(Duration.ofSeconds(1), serviceContext, BestEffortRetryStrategy.INSTANCE, CollectionIdentifier.fromDefault("bucket"));
    service.send(request1);
    NoopRequest request2 = new NoopRequest(Duration.ofSeconds(1), serviceContext, BestEffortRetryStrategy.INSTANCE, CollectionIdentifier.fromDefault("bucket"));
    service.send(request2);
    waitUntilCondition(() -> service.trackedEndpoints.size() == 2);
    // Simulate the connecting and connected
    states.onNext(EndpointState.CONNECTING);
    states.onNext(EndpointState.CONNECTED);
    when(mock1.outstandingRequests()).thenReturn(0L);
    Thread.sleep(600);
    verify(mock1, times(1)).disconnect();
    verify(mock2, never()).disconnect();
}
Also used : EndpointState(com.couchbase.client.core.endpoint.EndpointState) NoopRequest(com.couchbase.client.core.msg.kv.NoopRequest) Endpoint(com.couchbase.client.core.endpoint.Endpoint) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Endpoint(com.couchbase.client.core.endpoint.Endpoint) Test(org.junit.jupiter.api.Test)

Aggregations

NoopRequest (com.couchbase.client.core.msg.kv.NoopRequest)20 Test (org.junit.jupiter.api.Test)20 NoopResponse (com.couchbase.client.core.msg.kv.NoopResponse)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 Endpoint (com.couchbase.client.core.endpoint.Endpoint)9 CollectionIdentifier (com.couchbase.client.core.io.CollectionIdentifier)8 RequestContext (com.couchbase.client.core.msg.RequestContext)8 RetryStrategy (com.couchbase.client.core.retry.RetryStrategy)8 EndpointState (com.couchbase.client.core.endpoint.EndpointState)7 StepVerifier (reactor.test.StepVerifier)6 RequestCanceledException (com.couchbase.client.core.error.RequestCanceledException)3 CoreIntegrationTest (com.couchbase.client.core.util.CoreIntegrationTest)3 TestNodeConfig (com.couchbase.client.test.TestNodeConfig)3 Invocation (org.mockito.invocation.Invocation)2 Disposable (reactor.core.Disposable)2 Bootstrap (com.couchbase.client.core.deps.io.netty.bootstrap.Bootstrap)1 Channel (com.couchbase.client.core.deps.io.netty.channel.Channel)1 SocketChannel (com.couchbase.client.core.deps.io.netty.channel.socket.SocketChannel)1 NioSocketChannel (com.couchbase.client.core.deps.io.netty.channel.socket.nio.NioSocketChannel)1 CancellationReason (com.couchbase.client.core.msg.CancellationReason)1