Search in sources :

Example 11 with ChannelFuture

use of com.couchbase.client.core.deps.io.netty.channel.ChannelFuture in project couchbase-jvm-clients by couchbase.

the class SelectBucketHandlerTest method failConnectIfPromiseTimesOut.

/**
 * This test makes sure that the timer fires if the connect future is not completed
 * otherwise.
 */
@Test
void failConnectIfPromiseTimesOut() throws Exception {
    final Duration timeout = Duration.ofMillis(10);
    SelectBucketHandler handler = new SelectBucketHandler(endpointContext, "bucket");
    channel.pipeline().addLast(handler);
    final ChannelFuture connect = channel.connect(new InetSocketAddress("1.2.3.4", 1234));
    channel.attr(ChannelAttributes.SERVER_FEATURE_KEY).set(EnumSet.of(ServerFeature.SELECT_BUCKET));
    channel.pipeline().fireChannelActive();
    Thread.sleep(timeout.toMillis() + 5);
    channel.runScheduledPendingTasks();
    assertTrue(connect.isDone());
    assertTrue(connect.cause() instanceof TimeoutException);
    assertEquals("KV Select Bucket loading timed out after 10ms", connect.cause().getMessage());
}
Also used : ChannelFuture(com.couchbase.client.core.deps.io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) Duration(java.time.Duration) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.jupiter.api.Test)

Example 12 with ChannelFuture

use of com.couchbase.client.core.deps.io.netty.channel.ChannelFuture in project couchbase-jvm-clients by couchbase.

the class ErrorMapLoadingHandlerTest method decodeSuccessfulErrorMapV2.

/**
 * Verify that we can decode v2 of the error map.
 */
@Test
void decodeSuccessfulErrorMapV2() {
    ErrorMapLoadingHandler handler = new ErrorMapLoadingHandler(endpointContext);
    channel.pipeline().addLast(handler);
    assertEquals(handler, channel.pipeline().get(ErrorMapLoadingHandler.class));
    ChannelFuture connectFuture = channel.connect(new InetSocketAddress("1.2.3.4", 1234));
    assertFalse(connectFuture.isDone());
    channel.pipeline().fireChannelActive();
    channel.runPendingTasks();
    ByteBuf writtenRequest = channel.readOutbound();
    verifyRequest(writtenRequest, MemcacheProtocol.Opcode.ERROR_MAP.opcode(), false, false, true);
    assertNotNull(channel.pipeline().get(ErrorMapLoadingHandler.class));
    ReferenceCountUtil.release(writtenRequest);
    ByteBuf response = decodeHexDump(readResource("success_errormapv2_response.txt", ErrorMapLoadingHandlerTest.class));
    channel.writeInbound(response);
    channel.runPendingTasks();
    assertTrue(connectFuture.isSuccess());
    assertEquals(1, eventBus.publishedEvents().size());
    ErrorMapLoadedEvent event = (ErrorMapLoadedEvent) eventBus.publishedEvents().get(0);
    assertEquals(Event.Severity.DEBUG, event.severity());
    Optional<ErrorMap> maybeMap = event.errorMap();
    assertTrue(maybeMap.isPresent());
    assertNotNull(maybeMap.get());
    ErrorMap errorMap = channel.attr(ChannelAttributes.ERROR_MAP_KEY).get();
    assertEquals(errorMap, maybeMap.get());
}
Also used : ChannelFuture(com.couchbase.client.core.deps.io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) ErrorMapLoadedEvent(com.couchbase.client.core.cnc.events.io.ErrorMapLoadedEvent) Test(org.junit.jupiter.api.Test)

Example 13 with ChannelFuture

use of com.couchbase.client.core.deps.io.netty.channel.ChannelFuture in project couchbase-jvm-clients by couchbase.

the class ErrorMapLoadingHandlerTest method decodeSuccessfulResponseWithEmptyMap.

/**
 * This seems to be a real edge case, but when the server returns a successful
 * response but with no map, we should still handle it gracefully.
 */
@Test
void decodeSuccessfulResponseWithEmptyMap() {
    ErrorMapLoadingHandler handler = new ErrorMapLoadingHandler(endpointContext);
    channel.pipeline().addLast(handler);
    assertEquals(handler, channel.pipeline().get(ErrorMapLoadingHandler.class));
    ChannelFuture connectFuture = channel.connect(new InetSocketAddress("1.2.3.4", 1234));
    assertFalse(connectFuture.isDone());
    channel.pipeline().fireChannelActive();
    channel.runPendingTasks();
    ByteBuf writtenRequest = channel.readOutbound();
    verifyRequest(writtenRequest, MemcacheProtocol.Opcode.ERROR_MAP.opcode(), false, false, true);
    assertNotNull(channel.pipeline().get(ErrorMapLoadingHandler.class));
    ReferenceCountUtil.release(writtenRequest);
    ByteBuf response = decodeHexDump(readResource("success_empty_errormap_response.txt", ErrorMapLoadingHandlerTest.class));
    channel.writeInbound(response);
    channel.runPendingTasks();
    assertTrue(connectFuture.isSuccess());
    assertEquals(2, eventBus.publishedEvents().size());
    ErrorMapUndecodableEvent undecodableEvent = (ErrorMapUndecodableEvent) eventBus.publishedEvents().get(0);
    assertEquals("KV Error Map loaded but undecodable. " + "Message: \"No content in response\", Content: \"\"", undecodableEvent.description());
    ErrorMapLoadedEvent event = (ErrorMapLoadedEvent) eventBus.publishedEvents().get(1);
    assertEquals(Event.Severity.DEBUG, event.severity());
    Optional<ErrorMap> maybeMap = event.errorMap();
    assertFalse(maybeMap.isPresent());
}
Also used : ChannelFuture(com.couchbase.client.core.deps.io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) ErrorMapUndecodableEvent(com.couchbase.client.core.cnc.events.io.ErrorMapUndecodableEvent) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) ErrorMapLoadedEvent(com.couchbase.client.core.cnc.events.io.ErrorMapLoadedEvent) Test(org.junit.jupiter.api.Test)

Example 14 with ChannelFuture

use of com.couchbase.client.core.deps.io.netty.channel.ChannelFuture in project couchbase-jvm-clients by couchbase.

the class ErrorMapLoadingHandlerTest method decodeSuccessfulErrorMap.

/**
 * Verify that when a good error map comes back, we parse and return it properly.
 */
@Test
void decodeSuccessfulErrorMap() {
    ErrorMapLoadingHandler handler = new ErrorMapLoadingHandler(endpointContext);
    channel.pipeline().addLast(handler);
    assertEquals(handler, channel.pipeline().get(ErrorMapLoadingHandler.class));
    ChannelFuture connectFuture = channel.connect(new InetSocketAddress("1.2.3.4", 1234));
    assertFalse(connectFuture.isDone());
    channel.pipeline().fireChannelActive();
    channel.runPendingTasks();
    ByteBuf writtenRequest = channel.readOutbound();
    verifyRequest(writtenRequest, MemcacheProtocol.Opcode.ERROR_MAP.opcode(), false, false, true);
    assertNotNull(channel.pipeline().get(ErrorMapLoadingHandler.class));
    ReferenceCountUtil.release(writtenRequest);
    ByteBuf response = decodeHexDump(readResource("success_errormap_response.txt", ErrorMapLoadingHandlerTest.class));
    channel.writeInbound(response);
    channel.runPendingTasks();
    assertTrue(connectFuture.isSuccess());
    assertEquals(1, eventBus.publishedEvents().size());
    ErrorMapLoadedEvent event = (ErrorMapLoadedEvent) eventBus.publishedEvents().get(0);
    assertEquals(Event.Severity.DEBUG, event.severity());
    Optional<ErrorMap> maybeMap = event.errorMap();
    assertTrue(maybeMap.isPresent());
    assertNotNull(maybeMap.get());
    ErrorMap errorMap = channel.attr(ChannelAttributes.ERROR_MAP_KEY).get();
    assertEquals(errorMap, maybeMap.get());
}
Also used : ChannelFuture(com.couchbase.client.core.deps.io.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) ErrorMapLoadedEvent(com.couchbase.client.core.cnc.events.io.ErrorMapLoadedEvent) Test(org.junit.jupiter.api.Test)

Example 15 with ChannelFuture

use of com.couchbase.client.core.deps.io.netty.channel.ChannelFuture in project couchbase-jvm-clients by couchbase.

the class ErrorMapLoadingHandlerTest method failConnectIfPromiseTimesOut.

/**
 * This test makes sure that the timer fires if the connect future is not completed
 * otherwise.
 */
@Test
void failConnectIfPromiseTimesOut() throws Exception {
    channel = new EmbeddedChannel();
    eventBus = new SimpleEventBus(true);
    CoreEnvironment env = mock(CoreEnvironment.class);
    TimeoutConfig timeoutConfig = mock(TimeoutConfig.class);
    when(env.eventBus()).thenReturn(eventBus);
    when(env.timeoutConfig()).thenReturn(timeoutConfig);
    when(timeoutConfig.connectTimeout()).thenReturn(Duration.ofMillis(100));
    CoreContext coreContext = new CoreContext(mock(Core.class), 1, env, mock(Authenticator.class));
    EndpointContext endpointContext = new EndpointContext(coreContext, new HostAndPort("127.0.0.1", 1234), null, ServiceType.KV, Optional.empty(), Optional.empty(), Optional.empty());
    ErrorMapLoadingHandler handler = new ErrorMapLoadingHandler(endpointContext);
    channel.pipeline().addLast(handler);
    final ChannelFuture connect = channel.connect(new InetSocketAddress("1.2.3.4", 1234));
    channel.pipeline().fireChannelActive();
    Thread.sleep(Duration.ofMillis(100).toMillis() + 5);
    channel.runScheduledPendingTasks();
    assertTrue(connect.isDone());
    assertTrue(connect.cause() instanceof TimeoutException);
    assertEquals("KV Error Map loading timed out after 100ms", connect.cause().getMessage());
}
Also used : ChannelFuture(com.couchbase.client.core.deps.io.netty.channel.ChannelFuture) TimeoutConfig(com.couchbase.client.core.env.TimeoutConfig) CoreContext(com.couchbase.client.core.CoreContext) CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) InetSocketAddress(java.net.InetSocketAddress) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) HostAndPort(com.couchbase.client.core.util.HostAndPort) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) Authenticator(com.couchbase.client.core.env.Authenticator) Core(com.couchbase.client.core.Core) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.jupiter.api.Test)

Aggregations

ChannelFuture (com.couchbase.client.core.deps.io.netty.channel.ChannelFuture)20 InetSocketAddress (java.net.InetSocketAddress)20 Test (org.junit.jupiter.api.Test)20 ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)11 TimeoutException (java.util.concurrent.TimeoutException)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 ChannelDuplexHandler (com.couchbase.client.core.deps.io.netty.channel.ChannelDuplexHandler)4 ChannelHandlerContext (com.couchbase.client.core.deps.io.netty.channel.ChannelHandlerContext)4 ChannelPromise (com.couchbase.client.core.deps.io.netty.channel.ChannelPromise)4 SocketAddress (java.net.SocketAddress)4 ErrorMapLoadedEvent (com.couchbase.client.core.cnc.events.io.ErrorMapLoadedEvent)3 FeaturesNegotiatedEvent (com.couchbase.client.core.cnc.events.io.FeaturesNegotiatedEvent)3 AuthenticationFailureException (com.couchbase.client.core.error.AuthenticationFailureException)3 Core (com.couchbase.client.core.Core)2 CoreContext (com.couchbase.client.core.CoreContext)2 SimpleEventBus (com.couchbase.client.core.cnc.SimpleEventBus)2 EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)2 EndpointContext (com.couchbase.client.core.endpoint.EndpointContext)2 Authenticator (com.couchbase.client.core.env.Authenticator)2 CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)2