List of usage examples for io.netty.channel DefaultChannelPromise DefaultChannelPromise
public DefaultChannelPromise(Channel channel)
From source file:com.github.spapageo.jannel.client.JannelClientTest.java
License:Open Source License
@Test public void testIdentifyAddsTheCorrectlyHandlersToThePipelineWithEnabledWriteTimeout() throws Exception { Channel channel = mock(Channel.class, Answers.RETURNS_SMART_NULLS.get()); mockWriteHandler = mock(ChannelHandler.class); DefaultChannelPromise completedFuture = new DefaultChannelPromise(channel); completedFuture.setSuccess();/*from w ww . j a v a2 s. c o m*/ ChannelPipeline channelPipeline = mock(ChannelPipeline.class); when(channelPipeline.addLast(anyString(), any(ChannelHandler.class))).thenReturn(channelPipeline); when(channelPipeline.addLast(any(EventExecutorGroup.class), anyString(), any(ChannelHandler.class))) .thenReturn(channelPipeline); when(channel.pipeline()).thenReturn(channelPipeline); when(channel.isActive()).thenReturn(true); when(channel.writeAndFlush(any())).thenReturn(completedFuture); when(channelHandlerProvider.getChangeHandler(eq(HandlerType.WRITE_TIMEOUT_HANDLER), any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class))) .thenReturn(mockWriteHandler); when(channelHandlerProvider.getChangeHandler(eq(HandlerType.LENGTH_FRAME_ENCODER), any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class))) .thenReturn(mockLengthWriteHandler); when(channelHandlerProvider.getChangeHandler(eq(HandlerType.LENGTH_FRAME_DECODER), any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class))) .thenReturn(mockLengthReadHandler); when(channelHandlerProvider.getChangeHandler(eq(HandlerType.MESSAGE_ENCODER), any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class))) .thenReturn(mockEncoderHandler); when(channelHandlerProvider.getChangeHandler(eq(HandlerType.MESSAGE_DECODER), any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class))) .thenReturn(mockDecoderHandler); when(channelHandlerProvider.getChangeHandler(eq(HandlerType.MESSAGE_LOGGER), any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class))) .thenReturn(mockLoggerHandler); when(channelHandlerProvider.getChangeHandler(eq(HandlerType.SESSION_WRAPPER), any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class))) .thenReturn(mockSessionHandler); when(bootstrap.connect(anyString(), anyInt())).thenReturn(completedFuture); InOrder pipelineOrder = inOrder(channelPipeline); ClientSessionConfiguration configuration = new ClientSessionConfiguration(); configuration.setWriteTimeout(1000); jannelClient.identify(configuration, null); pipelineOrder.verify(channelPipeline).addLast(HandlerType.WRITE_TIMEOUT_HANDLER.name(), mockWriteHandler); pipelineOrder.verify(channelPipeline).addLast(HandlerType.LENGTH_FRAME_DECODER.name(), mockLengthReadHandler); pipelineOrder.verify(channelPipeline).addLast(HandlerType.LENGTH_FRAME_ENCODER.name(), mockLengthWriteHandler); pipelineOrder.verify(channelPipeline).addLast(HandlerType.MESSAGE_DECODER.name(), mockDecoderHandler); pipelineOrder.verify(channelPipeline).addLast(HandlerType.MESSAGE_ENCODER.name(), mockEncoderHandler); pipelineOrder.verify(channelPipeline).addLast(HandlerType.MESSAGE_LOGGER.name(), mockLoggerHandler); pipelineOrder.verify(channelPipeline).addLast(eventExecutors, HandlerType.SESSION_WRAPPER.name(), mockSessionHandler); pipelineOrder.verify(channelPipeline).remove(JannelClient.DummyChannelHandler.class); verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.WRITE_TIMEOUT_HANDLER), eq(configuration), any(ClientSession.class), eq(transcoder)); verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.LENGTH_FRAME_DECODER), eq(configuration), any(ClientSession.class), eq(transcoder)); verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.LENGTH_FRAME_ENCODER), eq(configuration), any(ClientSession.class), eq(transcoder)); verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.MESSAGE_DECODER), eq(configuration), any(ClientSession.class), eq(transcoder)); verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.MESSAGE_ENCODER), eq(configuration), any(ClientSession.class), eq(transcoder)); verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.MESSAGE_LOGGER), eq(configuration), any(ClientSession.class), eq(transcoder)); verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.SESSION_WRAPPER), eq(configuration), any(ClientSession.class), eq(transcoder)); }
From source file:com.github.spapageo.jannel.client.JannelClientTest.java
License:Open Source License
@Test public void testIdentifyAddsTheCorrectlyHandlersToThePipelineWithDisabledWriteTimeout() throws Exception { Channel channel = mock(Channel.class, Answers.RETURNS_SMART_NULLS.get()); DefaultChannelPromise completedFuture = new DefaultChannelPromise(channel); completedFuture.setSuccess();/*from w w w .j a v a 2 s . c o m*/ ChannelPipeline channelPipeline = mock(ChannelPipeline.class); when(channelPipeline.addLast(anyString(), any(ChannelHandler.class))).thenReturn(channelPipeline); when(channelPipeline.addLast(any(EventExecutorGroup.class), anyString(), any(ChannelHandler.class))) .thenReturn(channelPipeline); when(channel.pipeline()).thenReturn(channelPipeline); when(channel.isActive()).thenReturn(true); when(channel.writeAndFlush(any())).thenReturn(completedFuture); when(channelHandlerProvider.getChangeHandler(eq(HandlerType.WRITE_TIMEOUT_HANDLER), any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class))) .thenReturn(mockWriteHandler); when(channelHandlerProvider.getChangeHandler(eq(HandlerType.LENGTH_FRAME_ENCODER), any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class))) .thenReturn(mockLengthWriteHandler); when(channelHandlerProvider.getChangeHandler(eq(HandlerType.LENGTH_FRAME_DECODER), any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class))) .thenReturn(mockLengthReadHandler); when(channelHandlerProvider.getChangeHandler(eq(HandlerType.MESSAGE_ENCODER), any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class))) .thenReturn(mockEncoderHandler); when(channelHandlerProvider.getChangeHandler(eq(HandlerType.MESSAGE_DECODER), any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class))) .thenReturn(mockDecoderHandler); when(channelHandlerProvider.getChangeHandler(eq(HandlerType.MESSAGE_LOGGER), any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class))) .thenReturn(mockLoggerHandler); when(channelHandlerProvider.getChangeHandler(eq(HandlerType.SESSION_WRAPPER), any(ClientSessionConfiguration.class), any(ClientSession.class), any(Transcoder.class))) .thenReturn(mockSessionHandler); when(bootstrap.connect(anyString(), anyInt())).thenReturn(completedFuture); InOrder pipelineOrder = inOrder(channelPipeline); ClientSessionConfiguration configuration = new ClientSessionConfiguration(); jannelClient.identify(configuration, null); pipelineOrder.verify(channelPipeline).addLast(HandlerType.LENGTH_FRAME_DECODER.name(), mockLengthReadHandler); pipelineOrder.verify(channelPipeline).addLast(HandlerType.LENGTH_FRAME_ENCODER.name(), mockLengthWriteHandler); pipelineOrder.verify(channelPipeline).addLast(HandlerType.MESSAGE_DECODER.name(), mockDecoderHandler); pipelineOrder.verify(channelPipeline).addLast(HandlerType.MESSAGE_ENCODER.name(), mockEncoderHandler); pipelineOrder.verify(channelPipeline).addLast(HandlerType.MESSAGE_LOGGER.name(), mockLoggerHandler); pipelineOrder.verify(channelPipeline).addLast(eventExecutors, HandlerType.SESSION_WRAPPER.name(), mockSessionHandler); pipelineOrder.verify(channelPipeline).remove(JannelClient.DummyChannelHandler.class); verify(channelHandlerProvider, times(0)).getChangeHandler(eq(HandlerType.WRITE_TIMEOUT_HANDLER), eq(configuration), any(ClientSession.class), eq(transcoder)); verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.LENGTH_FRAME_DECODER), eq(configuration), any(ClientSession.class), eq(transcoder)); verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.LENGTH_FRAME_ENCODER), eq(configuration), any(ClientSession.class), eq(transcoder)); verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.MESSAGE_DECODER), eq(configuration), any(ClientSession.class), eq(transcoder)); verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.MESSAGE_ENCODER), eq(configuration), any(ClientSession.class), eq(transcoder)); verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.MESSAGE_LOGGER), eq(configuration), any(ClientSession.class), eq(transcoder)); verify(channelHandlerProvider).getChangeHandler(eq(HandlerType.SESSION_WRAPPER), eq(configuration), any(ClientSession.class), eq(transcoder)); }
From source file:com.github.spapageo.jannel.client.JannelClientTest.java
License:Open Source License
@Test public void testIdentifyConnectsToCorrectRemoteServerWithConnectionTimeout() throws Exception { Channel channel = mock(Channel.class, Answers.RETURNS_SMART_NULLS.get()); mockWriteHandler = mock(ChannelHandler.class); DefaultChannelPromise completedFuture = new DefaultChannelPromise(channel); completedFuture.setSuccess();/*from w ww . j a v a 2s.com*/ ChannelPipeline channelPipeline = mock(ChannelPipeline.class); when(channelPipeline.addLast(anyString(), any(ChannelHandler.class))).thenReturn(channelPipeline); when(channelPipeline.addLast(any(EventExecutorGroup.class), anyString(), any(ChannelHandler.class))) .thenReturn(channelPipeline); when(channel.pipeline()).thenReturn(channelPipeline); when(channel.isActive()).thenReturn(true); when(channel.writeAndFlush(any())).thenReturn(completedFuture); when(bootstrap.connect(anyString(), anyInt())).thenReturn(completedFuture); ClientSessionConfiguration configuration = new ClientSessionConfiguration(); configuration.setHost("testHost"); configuration.setPort(1111); configuration.setConnectTimeout(10000); jannelClient.identify(configuration, null); verify(bootstrap).connect(configuration.getHost(), configuration.getPort()); verify(bootstrap).option(ChannelOption.valueOf("connectTimeoutMillis"), configuration.getConnectTimeout()); }
From source file:com.github.spapageo.jannel.client.JannelClientTest.java
License:Open Source License
@Test public void testIdentifySendsCorrectIdentifyCommand() throws Exception { Channel channel = mock(Channel.class, Answers.RETURNS_SMART_NULLS.get()); mockWriteHandler = mock(ChannelHandler.class); DefaultChannelPromise completedFuture = new DefaultChannelPromise(channel); completedFuture.setSuccess();/*from w ww . j a v a 2 s . c om*/ ChannelPipeline channelPipeline = mock(ChannelPipeline.class); when(channelPipeline.addLast(anyString(), any(ChannelHandler.class))).thenReturn(channelPipeline); when(channelPipeline.addLast(any(EventExecutorGroup.class), anyString(), any(ChannelHandler.class))) .thenReturn(channelPipeline); when(channel.pipeline()).thenReturn(channelPipeline); when(channel.isActive()).thenReturn(true); when(channel.writeAndFlush(any())).thenReturn(completedFuture); when(bootstrap.connect(anyString(), anyInt())).thenReturn(completedFuture); ClientSessionConfiguration configuration = new ClientSessionConfiguration(); configuration.setClientId("testId"); jannelClient.identify(configuration, null); ArgumentCaptor<Admin> captor = ArgumentCaptor.forClass(Admin.class); verify(channel).writeAndFlush(captor.capture()); Admin command = captor.getValue(); assertEquals("Wrong command type", AdminCommand.IDENTIFY, command.getAdminCommand()); assertEquals("Wrong client id", configuration.getClientId(), command.getBoxId()); }
From source file:com.github.spapageo.jannel.client.JannelClientTest.java
License:Open Source License
@Test(expected = PrematureChannelClosureException.class) public void testIdentifyCloseChannelOnFailure() throws Exception { Channel channel = mock(Channel.class, Answers.RETURNS_SMART_NULLS.get()); mockWriteHandler = mock(ChannelHandler.class); DefaultChannelPromise completedFuture = new DefaultChannelPromise(channel); completedFuture.setSuccess();// w w w .j ava2s . co m DefaultChannelPromise failedFuture = new DefaultChannelPromise(channel); failedFuture.setFailure(new PrematureChannelClosureException("test")); ChannelPipeline channelPipeline = mock(ChannelPipeline.class); when(channelPipeline.addLast(anyString(), any(ChannelHandler.class))).thenReturn(channelPipeline); when(channelPipeline.addLast(any(EventExecutorGroup.class), anyString(), any(ChannelHandler.class))) .thenReturn(channelPipeline); when(channel.pipeline()).thenReturn(channelPipeline); when(channel.isActive()).thenReturn(true); when(channel.writeAndFlush(any())).thenReturn(failedFuture); when(channel.close()).thenReturn(completedFuture); when(bootstrap.connect(anyString(), anyInt())).thenReturn(completedFuture); ClientSessionConfiguration configuration = new ClientSessionConfiguration(); jannelClient.identify(configuration, null); }
From source file:com.lambdaworks.redis.protocol.CommandHandlerTest.java
License:Apache License
@Before public void before() throws Exception { when(context.channel()).thenReturn(channel); when(channel.pipeline()).thenReturn(pipeline); when(channel.eventLoop()).thenReturn(eventLoop); when(eventLoop.submit(any(Runnable.class))).thenAnswer(invocation -> { Runnable r = (Runnable) invocation.getArguments()[0]; r.run();//from w w w . jav a 2 s . c o m return null; }); when(clientResources.commandLatencyCollector()) .thenReturn(new DefaultCommandLatencyCollector(DefaultCommandLatencyCollectorOptions.create())); when(channel.writeAndFlush(any())).thenAnswer(invocation -> { if (invocation.getArguments()[0] instanceof RedisCommand) { q.add((RedisCommand) invocation.getArguments()[0]); } if (invocation.getArguments()[0] instanceof Collection) { q.addAll((Collection) invocation.getArguments()[0]); } return new DefaultChannelPromise(channel); }); sut = new CommandHandler<>(ClientOptions.create(), clientResources, q); sut.setRedisChannelHandler(channelHandler); }
From source file:com.lambdaworks.redis.protocol.CommandHandlerTest.java
License:Apache License
@Test public void testChannelActiveWithBufferedAndQueuedCommandsRetainsOrder() throws Exception { Command<String, String, String> bufferedCommand1 = new Command<>(CommandType.SET, new StatusOutput<>(new Utf8StringCodec()), null); Command<String, String, String> bufferedCommand2 = new Command<>(CommandType.GET, new StatusOutput<>(new Utf8StringCodec()), null); Command<String, String, String> queuedCommand1 = new Command<>(CommandType.PING, new StatusOutput<>(new Utf8StringCodec()), null); Command<String, String, String> queuedCommand2 = new Command<>(CommandType.AUTH, new StatusOutput<>(new Utf8StringCodec()), null); q.add(queuedCommand1);/*w w w. j av a2 s .c o m*/ q.add(queuedCommand2); Collection buffer = (Collection) ReflectionTestUtils.getField(sut, "commandBuffer"); buffer.add(bufferedCommand1); buffer.add(bufferedCommand2); reset(channel); when(channel.writeAndFlush(any())).thenAnswer(invocation -> new DefaultChannelPromise(channel)); when(channel.eventLoop()).thenReturn(eventLoop); when(channel.pipeline()).thenReturn(pipeline); sut.channelRegistered(context); sut.channelActive(context); assertThat(q).isEmpty(); assertThat(buffer).isEmpty(); ArgumentCaptor<Object> objectArgumentCaptor = ArgumentCaptor.forClass(Object.class); verify(channel).writeAndFlush(objectArgumentCaptor.capture()); assertThat((Collection) objectArgumentCaptor.getValue()).containsSequence(queuedCommand1, queuedCommand2, bufferedCommand1, bufferedCommand2); }
From source file:com.lambdaworks.redis.pubsub.PubSubCommandHandlerTest.java
License:Apache License
@SuppressWarnings("unchecked") @Before// w w w . j a v a2s .co m public void before() throws Exception { when(context.channel()).thenReturn(channel); when(channel.pipeline()).thenReturn(pipeline); when(channel.eventLoop()).thenReturn(eventLoop); when(eventLoop.submit(any(Runnable.class))).thenAnswer(invocation -> { Runnable r = (Runnable) invocation.getArguments()[0]; r.run(); return null; }); when(clientResources.commandLatencyCollector()) .thenReturn(new DefaultCommandLatencyCollector(DefaultCommandLatencyCollectorOptions.create())); when(channel.writeAndFlush(any())).thenAnswer(invocation -> { if (invocation.getArguments()[0] instanceof RedisCommand) { stack.add((RedisCommand) invocation.getArguments()[0]); } if (invocation.getArguments()[0] instanceof Collection) { stack.addAll((Collection) invocation.getArguments()[0]); } return new DefaultChannelPromise(channel); }); sut = new PubSubCommandHandler<>(ClientOptions.create(), clientResources, StringCodec.UTF8); sut.setRedisChannelHandler(channelHandler); stack = (Queue) ReflectionTestUtils.getField(sut, "stack"); }
From source file:com.vmware.dcp.common.http.netty.NettyWebSocketRequestHandler.java
License:Open Source License
private void performWebsocketHandshake(final ChannelHandlerContext ctx, FullHttpRequest nettyRequest) { WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(this.handshakePath, null, false);//from w ww. j av a 2s. com this.handshaker = factory.newHandshaker(nettyRequest); if (this.handshaker == null) { WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); } else { ChannelPromise promise = new DefaultChannelPromise(ctx.channel()); promise.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { ctx.channel().close(); } ctx.channel().closeFuture().addListener(f -> { for (java.util.Map.Entry<URI, Set<String>> e : NettyWebSocketRequestHandler.this.serviceSubscriptions .entrySet()) { WebSocketService svc = NettyWebSocketRequestHandler.this.webSocketServices .get(e.getKey()); if (svc != null) { deleteServiceSubscriptions(svc); } NettyWebSocketRequestHandler.this.host.stopService(svc); } }); } }); DefaultHttpHeaders responseHeaders = new DefaultHttpHeaders(); this.handshaker.handshake(ctx.channel(), nettyRequest, responseHeaders, promise); } }
From source file:com.vmware.xenon.common.http.netty.NettyWebSocketRequestHandler.java
License:Open Source License
private void performWebsocketHandshake(final ChannelHandlerContext ctx, FullHttpRequest nettyRequest) { WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(this.handshakePath, null, false);/*from w w w .j av a 2 s . co m*/ this.handshaker = factory.newHandshaker(nettyRequest); if (this.handshaker == null) { WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); } else { ChannelPromise promise = new DefaultChannelPromise(ctx.channel()); promise.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { ctx.channel().close(); } ctx.channel().closeFuture().addListener(f -> { for (java.util.Map.Entry<URI, Set<String>> e : NettyWebSocketRequestHandler.this.serviceSubscriptions .entrySet()) { WebSocketService svc = NettyWebSocketRequestHandler.this.webSocketServices .get(e.getKey()); if (svc != null) { deleteServiceSubscriptions(svc); } NettyWebSocketRequestHandler.this.host.stopService(svc); } }); } }); DefaultHttpHeaders responseHeaders = new DefaultHttpHeaders(); CharSequence token = nettyRequest.headers().get(Operation.REQUEST_AUTH_TOKEN_HEADER, null); if (token == null) { String cookie = nettyRequest.headers().get(HttpHeaderNames.COOKIE); if (cookie != null) { token = CookieJar.decodeCookies(cookie).get(AuthenticationConstants.REQUEST_AUTH_TOKEN_COOKIE); } } this.authToken = token == null ? null : token.toString(); this.handshaker.handshake(ctx.channel(), nettyRequest, responseHeaders, promise); } }