public Message(Request request, Object payload) { this.request = request; this.id = request.getId(); if (request.expectsResponse()) { op = Op.RequestExpectResponse; } else { op = Op.RequestNoResponse; } this.payload = payload; }
public static Op fromByte(byte ordinal) { return Op.values()[ordinal]; } }
@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()); }
@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()); } };
@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); }
public static Message buildResponse(UUID id) { return new Message(id, Op.Response); } }
@Test public void connectSucceeds() throws Exception { ConnectionPool pool = new ConnectionPool(connector); pool.start(); }
public byte toByte() { return (byte) ordinal(); }
protected Bootstrap cloneBootstrap() { return buildBootstrap().clone().handler(handler()); }
@VisibleForTesting void setRequest(Map<UUID, Request> mapping, Request request) { mapping.put(request.getId(), request); }
public ListenableFuture<Channel> connect() { SettableFuture<Channel> promise = SettableFuture.create(); connectBootstrap(promise); return promise; }
@Override public void onFailure(Throwable throwable) { log.error("Error connecting to " + connector.address(), throwable); } });
public void registerResponseCallback(FutureCallback<Response> callback) { registerResponseCallback(callback, MoreExecutors.directExecutor()); }
@Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { setMapping(ctx.channel(), Maps.newConcurrentMap()); }
@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()); }
public static Message buildResponse(UUID id, Op op) { return new Message(id, op); }
@Test(expected = RuntimeException.class) public void connectFails() { LocalConnector flakyConnector = new LocalConnector("test-flaky-connection") { @Override public ListenableFuture<Channel> connect() { SettableFuture<Channel> result = SettableFuture.create(); result.setException(new RuntimeException("bad hair day")); return result; } }; ConnectionPool pool = new ConnectionPool(flakyConnector); pool.start(); } }
public static Op fromBytes(byte[] opBytes) { return Op.values()[Ints.fromByteArray(opBytes)]; }