### Question:
SelectBucketHandler extends ChannelDuplexHandler { @Override public void connect(final ChannelHandlerContext ctx, final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) { interceptedConnectPromise = promise; ChannelPromise downstream = ctx.newPromise(); downstream.addListener(f -> { if (!f.isSuccess() && !interceptedConnectPromise.isDone()) { ConnectTimings.record(ctx.channel(), this.getClass()); interceptedConnectPromise.tryFailure(f.cause()); } }); ctx.connect(remoteAddress, localAddress, downstream); } SelectBucketHandler(final EndpointContext endpointContext, final String bucketName); @Override void connect(final ChannelHandlerContext ctx, final SocketAddress remoteAddress,
                      final SocketAddress localAddress, final ChannelPromise promise); @Override void channelActive(final ChannelHandlerContext ctx); @Override void channelRead(final ChannelHandlerContext ctx, final Object msg);  }### Answer:
@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); } }; SelectBucketHandler handler = new SelectBucketHandler(endpointContext, "bucket"); channel.pipeline().addLast(failingHandler).addLast(handler); ChannelFuture connect = channel.connect(new InetSocketAddress("1.2.3.4", 1234)); assertEquals(connectException, connect.awaitUninterruptibly().cause()); }
@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(Collections.singletonList(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() ); }