Search in sources :

Example 16 with NoopRequest

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

the class ReactorTest method completesWithSuccessBeforeSubscription.

@Test
void completesWithSuccessBeforeSubscription() {
    NoopRequest request = new NoopRequest(Duration.ZERO, mock(RequestContext.class), mock(RetryStrategy.class), mock(CollectionIdentifier.class));
    NoopResponse response = mock(NoopResponse.class);
    request.succeed(response);
    Mono<NoopResponse> mono = Reactor.wrap(request, request.response(), true);
    StepVerifier verifier = StepVerifier.create(mono).expectNext(response).expectComplete();
    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 17 with NoopRequest

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

the class ReactorTest method ignoresCancellationPropagation.

@Test
void ignoresCancellationPropagation() {
    NoopRequest request = new NoopRequest(Duration.ZERO, mock(RequestContext.class), mock(RetryStrategy.class), mock(CollectionIdentifier.class));
    Mono<NoopResponse> mono = Reactor.wrap(request, request.response(), false);
    assertThrows(Exception.class, () -> mono.timeout(Duration.ofMillis(10)).block());
    assertFalse(request.response().isCompletedExceptionally());
    assertFalse(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 18 with NoopRequest

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

the class ReactorTest method noErrorDroppedWhenCancelledViaRequestCanceledException.

@Test
void noErrorDroppedWhenCancelledViaRequestCanceledException() {
    AtomicInteger droppedErrors = new AtomicInteger(0);
    Hooks.onErrorDropped(v -> {
        droppedErrors.incrementAndGet();
    });
    NoopRequest request = new NoopRequest(Duration.ZERO, mock(RequestContext.class), mock(RetryStrategy.class), mock(CollectionIdentifier.class));
    // Because this is a single-stage CompleteableFuture, the RequestCanceledException will raised directly in the
    // internals.
    Mono<NoopResponse> mono = Reactor.wrap(request, request.response(), true);
    Disposable subscriber = mono.subscribe();
    StepVerifier verifier = StepVerifier.create(mono).expectError(RequestCanceledException.class);
    subscriber.dispose();
    verifier.verify();
    assertEquals(0, droppedErrors.get());
}
Also used : NoopRequest(com.couchbase.client.core.msg.kv.NoopRequest) NoopResponse(com.couchbase.client.core.msg.kv.NoopResponse) Disposable(reactor.core.Disposable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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 19 with NoopRequest

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

the class PooledServiceTest method retriesRequestIfEndpointCannotConnect.

/**
 * With direct dispatch in the pool it is possible that endpoint for which the socket is
 * waiting for to be dispatched never goes into a connected state but rather into disconnected
 * (and then subsequent reconnect). As soon as we observe a disconnect state we need to retry the
 * op so that it has a chance to complete somewhere else.
 */
@Test
void retriesRequestIfEndpointCannotConnect() {
    int minEndpoints = 0;
    Endpoint mock1 = mock(Endpoint.class);
    when(mock1.state()).thenReturn(EndpointState.DISCONNECTED);
    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.DISCONNECTED);
    waitUntilCondition(() -> !service.trackedEndpoints.isEmpty());
    assertEquals(1, service.trackedEndpoints().size());
    waitUntilCondition(() -> request.context().retryAttempts() >= 1);
    verify(mock1, never()).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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Endpoint(com.couchbase.client.core.endpoint.Endpoint) Test(org.junit.jupiter.api.Test)

Example 20 with NoopRequest

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

the class PooledServiceTest method retriesIfNoSlotAvailable.

@Test
void retriesIfNoSlotAvailable() {
    int minEndpoints = 0;
    Endpoint mock1 = mock(Endpoint.class);
    when(mock1.state()).thenReturn(EndpointState.CONNECTED);
    when(mock1.outstandingRequests()).thenReturn(1L);
    DirectProcessor<EndpointState> states = DirectProcessor.create();
    when(mock1.states()).thenReturn(states);
    final List<Endpoint> mocks = Collections.singletonList(mock1);
    final AtomicInteger invocation = new AtomicInteger();
    MockedService service = new MockedService(new MockedServiceConfig(minEndpoints, 1, 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"));
    NoopRequest request2 = new NoopRequest(Duration.ofSeconds(1), serviceContext, BestEffortRetryStrategy.INSTANCE, CollectionIdentifier.fromDefault("bucket"));
    service.send(request1);
    service.send(request2);
    assertEquals(1, service.trackedEndpoints().size());
    // The first request is sent into the free slot without retrying
    assertEquals(0, request1.context().retryAttempts());
    // No more slots available, this one goes into retry
    assertTrue(request2.context().retryAttempts() > 0);
    // Simulate the connecting and connected
    states.onNext(EndpointState.CONNECTING);
    states.onNext(EndpointState.CONNECTED);
    waitUntilCondition(() -> {
        Collection<Invocation> invocations = Mockito.mockingDetails(mock1).getInvocations();
        for (Invocation inv : invocations) {
            if (inv.getMethod().getName().equals("send")) {
                if (inv.getArgument(0) == request1) {
                    return true;
                }
            }
        }
        return false;
    });
    verify(mock1, never()).send(request2);
}
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)

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