use of com.couchbase.client.core.deps.io.netty.channel.ChannelFuture in project couchbase-jvm-clients by couchbase.
the class FeatureNegotiatingHandlerTest method connectInstantlyIfNoFeaturesNeeded.
/**
* This test makes sure that if no server features need to be negotiated, we are not even
* sending a request and completing the connect phase immediately.
*/
@Test
void connectInstantlyIfNoFeaturesNeeded() {
FeatureNegotiatingHandler handler = new FeatureNegotiatingHandler(endpointContext, Collections.emptySet());
channel.pipeline().addLast(handler);
assertEquals(handler, channel.pipeline().get(FeatureNegotiatingHandler.class));
ChannelFuture connect = channel.connect(new InetSocketAddress("1.2.3.4", 1234));
channel.runPendingTasks();
assertTrue(connect.isSuccess());
assertNull(channel.pipeline().get(FeatureNegotiatingHandler.class));
}
use of com.couchbase.client.core.deps.io.netty.channel.ChannelFuture in project couchbase-jvm-clients by couchbase.
the class FeatureNegotiatingHandlerTest method decodeNonSuccessfulHelloResponse.
/**
* This test checks that a non-successful response is properly handled.
*/
@Test
void decodeNonSuccessfulHelloResponse() {
Set<ServerFeature> toNegotiate = EnumSet.of(ServerFeature.TCPNODELAY, ServerFeature.XATTR, ServerFeature.XERROR, ServerFeature.SELECT_BUCKET, ServerFeature.SNAPPY, ServerFeature.TRACING);
FeatureNegotiatingHandler handler = new FeatureNegotiatingHandler(endpointContext, toNegotiate);
channel.pipeline().addLast(handler);
assertEquals(handler, channel.pipeline().get(FeatureNegotiatingHandler.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.HELLO.opcode(), true, false, true);
assertNotNull(channel.pipeline().get(FeatureNegotiatingHandler.class));
ByteBuf response = decodeHexDump(readResource("error_hello_response.txt", FeatureNegotiatingHandlerTest.class));
channel.writeInbound(response);
channel.runPendingTasks();
assertTrue(connectFuture.isSuccess());
assertEquals(2, eventBus.publishedEvents().size());
FeaturesNegotiationFailedEvent failureEvent = (FeaturesNegotiationFailedEvent) eventBus.publishedEvents().get(0);
assertEquals(Event.Severity.WARN, failureEvent.severity());
assertEquals("HELLO Negotiation failed (Status 0x1)", failureEvent.description());
FeaturesNegotiatedEvent event = (FeaturesNegotiatedEvent) eventBus.publishedEvents().get(1);
assertEquals("Negotiated []", event.description());
assertEquals(Event.Severity.DEBUG, event.severity());
Set<ServerFeature> serverFeatures = channel.attr(ChannelAttributes.SERVER_FEATURE_KEY).get();
assertTrue(serverFeatures.isEmpty());
assertNull(channel.pipeline().get(FeatureNegotiatingHandler.class));
ReferenceCountUtil.release(writtenRequest);
}
use of com.couchbase.client.core.deps.io.netty.channel.ChannelFuture in project couchbase-jvm-clients by couchbase.
the class FeatureNegotiatingHandlerTest method decodeAndIgnoreNonAskedForFeaturesInResponse.
/**
* Should the server return non-asked-for features, ignore them.
*/
@Test
void decodeAndIgnoreNonAskedForFeaturesInResponse() {
Set<ServerFeature> toNegotiate = EnumSet.of(ServerFeature.SNAPPY, ServerFeature.TRACING);
FeatureNegotiatingHandler handler = new FeatureNegotiatingHandler(endpointContext, toNegotiate);
channel.pipeline().addLast(handler);
assertEquals(handler, channel.pipeline().get(FeatureNegotiatingHandler.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.HELLO.opcode(), true, false, true);
assertNotNull(channel.pipeline().get(FeatureNegotiatingHandler.class));
ByteBuf response = decodeHexDump(readResource("success_hello_response.txt", FeatureNegotiatingHandlerTest.class));
channel.writeInbound(response);
channel.runPendingTasks();
assertTrue(connectFuture.isSuccess());
assertEquals(2, eventBus.publishedEvents().size());
UnsolicitedFeaturesReturnedEvent unsolicitedEvent = (UnsolicitedFeaturesReturnedEvent) eventBus.publishedEvents().get(0);
assertEquals("Received unsolicited features during HELLO [TCPNODELAY, XATTR, XERROR, SELECT_BUCKET]", unsolicitedEvent.description());
assertEquals(Event.Severity.WARN, unsolicitedEvent.severity());
FeaturesNegotiatedEvent event = (FeaturesNegotiatedEvent) eventBus.publishedEvents().get(1);
assertEquals("Negotiated [SNAPPY, TRACING]", event.description());
Set<ServerFeature> serverFeatures = channel.attr(ChannelAttributes.SERVER_FEATURE_KEY).get();
assertEquals(toNegotiate, serverFeatures);
assertNull(channel.pipeline().get(FeatureNegotiatingHandler.class));
ReferenceCountUtil.release(writtenRequest);
}
use of com.couchbase.client.core.deps.io.netty.channel.ChannelFuture in project couchbase-jvm-clients by couchbase.
the class FeatureNegotiatingHandlerTest method failConnectIfPromiseTimesOut.
/**
* This test makes sure that the timer fires if the connect future is not completed
* otherwise.
*/
@Test
void failConnectIfPromiseTimesOut() throws Exception {
Duration timeout = Duration.ofMillis(100);
when(timeoutConfig.connectTimeout()).thenReturn(timeout);
FeatureNegotiatingHandler handler = new FeatureNegotiatingHandler(endpointContext, Collections.singleton(ServerFeature.TRACING));
channel.pipeline().addLast(handler);
final ChannelFuture connect = channel.connect(new InetSocketAddress("1.2.3.4", 1234));
channel.pipeline().fireChannelActive();
Thread.sleep(timeout.toMillis() + 5);
channel.runScheduledPendingTasks();
assertTrue(connect.isDone());
assertTrue(connect.cause() instanceof TimeoutException);
assertEquals("KV Feature Negotiation timed out after 100ms", connect.cause().getMessage());
}
use of com.couchbase.client.core.deps.io.netty.channel.ChannelFuture in project couchbase-jvm-clients by couchbase.
the class FeatureNegotiatingHandlerTest method propagateConnectFailureFromDownstream.
/**
* This test verifies that if a downstream promise fails that the error
* is propagated through the captured promise.
*/
@Test
void propagateConnectFailureFromDownstream() {
final Exception connectException = new Exception("I failed");
ChannelDuplexHandler failingHandler = new ChannelDuplexHandler() {
@Override
public void connect(final ChannelHandlerContext ctx, final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) {
promise.setFailure(connectException);
}
};
FeatureNegotiatingHandler handler = new FeatureNegotiatingHandler(endpointContext, Collections.singleton(ServerFeature.TRACING));
channel.pipeline().addLast(failingHandler).addLast(handler);
ChannelFuture connect = channel.connect(new InetSocketAddress("1.2.3.4", 1234));
assertEquals(connectException, connect.awaitUninterruptibly().cause());
}
Aggregations