@Override protected void initChannel(Channel channel) { ChannelPipeline pipeline = channel.pipeline(); pipeline .addLast("frame length codec", new FrameLengthCodec()) .addLast("mux message codec", new Codec()); for (Map.Entry<String, ChannelHandler> entry : payloadHandlers()) { pipeline.addLast(entry.getKey(), entry.getValue()); } pipeline.addLast("mux client codec", new ClientCodec()); } };
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { if (msg instanceof Message) { Message message = (Message) msg; Request request = message.getRequest(); if (request.expectsResponse()) { setRequest(ctx.channel().attr(KEY).get(), request); } PromiseCombiner combiner = new PromiseCombiner(); combiner.add(write(ctx, message.getPayload())); combiner.add(write(ctx, message)); combiner.finish(promise); } else { throw new RuntimeException("Only Message objects can be written to ClientCodec"); } } }
@Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { setMapping(ctx.channel(), Maps.newConcurrentMap()); }
new RuntimeException( "No response payload received for request id '" + message.getId() + "'")); reset(); return; new RuntimeException( "Multiple response payloads received for request id '" + message.getId() + "'")); reset(); return; Request request = getRequest(getMapping(ctx.channel()), message); if (request == null) { log.error("Unexpected response received for request id '{}': {}", message.getId(), message); new RuntimeException( "Unexpected response received for request id '" + message.getId() + "'")); reset(); return; reset(); } else { if (currentPayload != null) {
@Test public void testRequestExpectedResponse() { Integer payload = new Integer(1); Request request = new Request(UUID.randomUUID(), SettableFuture.create(), SettableFuture.create()); Message message = new Message(request, payload); channel.writeOutbound(message); channel.runPendingTasks(); // assert request in map assertEquals(request, codec.getRequest(codec.getMapping(channel), message)); // assert two objects written in the correct order assertEquals(payload, channel.outboundMessages().poll()); assertEquals(message, channel.outboundMessages().poll()); }
@Test public void testExpectedResponse() throws Exception { Request request = new Request(UUID.randomUUID(), SettableFuture.create(), SettableFuture.create()); codec.setRequest(codec.getMapping(channel), request); Integer payload = new Integer(1); Message message = Message.buildResponse(request.getId()); channel.writeInbound(payload); channel.writeInbound(message); channel.runPendingTasks(); Response response = request.getResponseFuture().get(1, TimeUnit.SECONDS); assertEquals(request.getId(), response.getInResponseTo()); assertEquals(payload, response.getPayload()); }
@Test public void testRequestNoResponse() { Integer payload = new Integer(1); Request request = new Request(UUID.randomUUID(), SettableFuture.create()); Message message = new Message(request, payload); channel.writeOutbound(message); channel.runPendingTasks(); // assert request not in map assertNull(codec.getRequest(codec.getMapping(channel), message)); // assert two objects written in the correct order assertEquals(payload, channel.outboundMessages().poll()); assertEquals(message, channel.outboundMessages().poll()); }
@Before public void setUp() throws Exception { mockStatic(LoggerFactory.class); Logger logger = mock(Logger.class); when(LoggerFactory.getLogger(any(Class.class))).thenReturn(logger); channel = new EmbeddedChannel(); codec = new ClientCodec(); channel.pipeline().addLast(codec); }
@Before public void setUp() { encoder = new ClientCodec(); channel = new EmbeddedChannel(); channel .pipeline() .addLast(new FrameLengthCodec()) .addLast(new Encoder()) // http encoder .addLast(new HttpRequestEncoder()) .addLast(encoder); }