Example usage for io.netty.channel.embedded EmbeddedChannel EmbeddedChannel

List of usage examples for io.netty.channel.embedded EmbeddedChannel EmbeddedChannel

Introduction

In this page you can find the example usage for io.netty.channel.embedded EmbeddedChannel EmbeddedChannel.

Prototype

public EmbeddedChannel(ChannelId channelId, ChannelHandler... handlers) 

Source Link

Document

Create a new instance with the channel ID set to the given ID and the pipeline initialized with the specified handlers.

Usage

From source file:com.cloudera.livy.client.local.rpc.TestKryoMessageCodec.java

License:Apache License

@Test
public void testEmbeddedChannel() throws Exception {
    EmbeddedChannel c = new EmbeddedChannel(new LoggingHandler(getClass()), new KryoMessageCodec(0));
    c.writeAndFlush(MESSAGE);//from   w  w w.  j  a va 2s.co m
    assertEquals(1, c.outboundMessages().size());
    assertFalse(MESSAGE.getClass().equals(c.outboundMessages().peek().getClass()));
    c.writeInbound(c.readOutbound());
    assertEquals(1, c.inboundMessages().size());
    assertEquals(MESSAGE, c.readInbound());
    c.close();
}

From source file:com.github.ambry.rest.HealthCheckHandlerTest.java

License:Open Source License

/**
 * Creates an {@link EmbeddedChannel} that incorporates an instance of {@link HealthCheckHandler}
 * and {@link EchoMethodHandler}./*from   w  w w.j  a v  a  2s  . com*/
 * @return an {@link EmbeddedChannel} that incorporates an instance of {@link HealthCheckHandler}
 * and {@link EchoMethodHandler}.
 */
private EmbeddedChannel createChannel() {
    return new EmbeddedChannel(new HealthCheckHandler(restServerState, new NettyMetrics(new MetricRegistry())),
            new EchoMethodHandler());
}

From source file:com.github.ambry.rest.NettyMessageProcessorTest.java

License:Open Source License

/**
 * Creates an {@link EmbeddedChannel} that incorporates an instance of {@link NettyMessageProcessor}.
 * @return an {@link EmbeddedChannel} that incorporates an instance of {@link NettyMessageProcessor}.
 *///from   w  w w  .  j  a  va2s.c o  m
private EmbeddedChannel createChannel() {
    NettyMetrics nettyMetrics = new NettyMetrics(new MetricRegistry());
    NettyConfig nettyConfig = new NettyConfig(new VerifiableProperties(new Properties()));
    NettyMessageProcessor processor = new NettyMessageProcessor(nettyMetrics, nettyConfig, requestHandler);
    return new EmbeddedChannel(new ChunkedWriteHandler(), processor);
}

From source file:com.github.ambry.rest.NettyResponseChannelTest.java

License:Open Source License

/**
 * Tests behaviour of various functions of {@link NettyResponseChannel} under write failures.
 * @throws Exception/*from  w ww  .  ja v a  2s .  c o  m*/
 */
@Test
public void behaviourUnderWriteFailuresTest() throws Exception {
    onResponseCompleteUnderWriteFailureTest(TestingUri.ImmediateResponseComplete);
    onResponseCompleteUnderWriteFailureTest(TestingUri.OnResponseCompleteWithNonRestException);

    // writing to channel with a outbound handler that generates an Exception
    try {
        String content = "@@randomContent@@@";
        MockNettyMessageProcessor processor = new MockNettyMessageProcessor();
        ChannelOutboundHandler badOutboundHandler = new ExceptionOutboundHandler();
        EmbeddedChannel channel = new EmbeddedChannel(badOutboundHandler, processor);
        channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, "/", null));
        // channel gets closed because of write failure
        channel.writeInbound(createContent(content, true));
        fail("Callback for write would have thrown an Exception");
    } catch (Exception e) {
        assertEquals("Exception not as expected", ExceptionOutboundHandler.EXCEPTION_MESSAGE, e.getMessage());
    }

    // writing to channel with a outbound handler that generates an Error
    EmbeddedChannel channel = new EmbeddedChannel(new ErrorOutboundHandler(), new MockNettyMessageProcessor());
    try {
        channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET,
                TestingUri.WriteFailureWithThrowable.toString(), null));
    } catch (Error e) {
        assertEquals("Unexpected error", ErrorOutboundHandler.ERROR_MESSAGE, e.getMessage());
    }

    channel = createEmbeddedChannel();
    channel.writeInbound(
            RestTestUtils.createRequest(HttpMethod.GET, TestingUri.ResponseFailureMidway.toString(), null));
    assertFalse("Channel is not closed at the remote end", channel.isActive());
}

From source file:com.github.ambry.rest.NettyResponseChannelTest.java

License:Open Source License

/**
 * Creates a new {@link EmbeddedChannel} with a {@link ChunkedWriteHandler} and {@link MockNettyMessageProcessor} in
 * the pipeline.//from   w ww . j  a va 2s.c o m
 * @return the created {@link EmbeddedChannel}.
 */
private EmbeddedChannel createEmbeddedChannel() {
    ChunkedWriteHandler chunkedWriteHandler = new ChunkedWriteHandler();
    MockNettyMessageProcessor processor = new MockNettyMessageProcessor();
    return new EmbeddedChannel(chunkedWriteHandler, processor);
}

From source file:com.github.ambry.rest.NettyResponseChannelTest.java

License:Open Source License

/**
 * Checks that no exceptions are thrown by {@link RestResponseChannel#onResponseComplete(Exception)} when
 * there are write failures./*  ww  w  .j  ava2  s . c o  m*/
 * @param uri the uri to hit.
 */
private void onResponseCompleteUnderWriteFailureTest(TestingUri uri) {
    MockNettyMessageProcessor processor = new MockNettyMessageProcessor();
    ExceptionOutboundHandler exceptionOutboundHandler = new ExceptionOutboundHandler();
    EmbeddedChannel channel = new EmbeddedChannel(exceptionOutboundHandler, processor);
    // no exception because onResponseComplete() swallows it.
    channel.writeInbound(RestTestUtils.createRequest(HttpMethod.GET, uri.toString(), null));
    assertFalse("Channel is not closed at the remote end", channel.isActive());
}

From source file:com.github.ambry.rest.PublicAccessLogHandlerTest.java

License:Open Source License

/**
 * Creates an {@link EmbeddedChannel} that incorporates an instance of {@link PublicAccessLogHandler}
 * and {@link EchoMethodHandler}.//from ww  w .j  a v a  2  s  .co m
 * @return an {@link EmbeddedChannel} that incorporates an instance of {@link PublicAccessLogHandler}
 * nad {@link EchoMethodHandler}.
 */
private EmbeddedChannel createChannel() {
    return new EmbeddedChannel(
            new PublicAccessLogHandler(publicAccessLogger, new NettyMetrics(new MetricRegistry())),
            new EchoMethodHandler());
}

From source file:com.github.ambry.rest.PublicAccessLogRequestHandlerTest.java

License:Open Source License

/**
 * Creates an {@link EmbeddedChannel} that incorporates an instance of {@link PublicAccessLogRequestHandler}
 * and {@link EchoMethodHandler}./*w w w. ja va2 s. c  o  m*/
 * @return an {@link EmbeddedChannel} that incorporates an instance of {@link PublicAccessLogRequestHandler}
 * nad {@link EchoMethodHandler}.
 */
private EmbeddedChannel createChannel() {
    return new EmbeddedChannel(
            new PublicAccessLogRequestHandler(publicAccessLogger, new NettyMetrics(new MetricRegistry())),
            new EchoMethodHandler());
}

From source file:com.github.sparkfy.network.ProtocolSuite.java

License:Apache License

private void testServerToClient(Message msg) {
    EmbeddedChannel serverChannel = new EmbeddedChannel(new FileRegionEncoder(), new MessageEncoder());
    serverChannel.writeOutbound(msg);/*from w w w  .ja  v  a2  s.co  m*/

    EmbeddedChannel clientChannel = new EmbeddedChannel(NettyUtils.createFrameDecoder(), new MessageDecoder());

    while (!serverChannel.outboundMessages().isEmpty()) {
        clientChannel.writeInbound(serverChannel.readOutbound());
    }

    assertEquals(1, clientChannel.inboundMessages().size());
    assertEquals(msg, clientChannel.readInbound());
}

From source file:com.github.sparkfy.network.ProtocolSuite.java

License:Apache License

private void testClientToServer(Message msg) {
    EmbeddedChannel clientChannel = new EmbeddedChannel(new FileRegionEncoder(), new MessageEncoder());
    clientChannel.writeOutbound(msg);/*  w w  w  . ja va  2 s  .  c o  m*/

    EmbeddedChannel serverChannel = new EmbeddedChannel(NettyUtils.createFrameDecoder(), new MessageDecoder());

    while (!clientChannel.outboundMessages().isEmpty()) {
        serverChannel.writeInbound(clientChannel.readOutbound());
    }

    assertEquals(1, serverChannel.inboundMessages().size());
    assertEquals(msg, serverChannel.readInbound());
}