Example usage for io.netty.buffer Unpooled wrappedBuffer

List of usage examples for io.netty.buffer Unpooled wrappedBuffer

Introduction

In this page you can find the example usage for io.netty.buffer Unpooled wrappedBuffer.

Prototype

public static ByteBuf wrappedBuffer(ByteBuffer... buffers) 

Source Link

Document

Creates a new big-endian composite buffer which wraps the slices of the specified NIO buffers without copying them.

Usage

From source file:com.linkedin.mitm.proxy.factory.ErrorResponseFactory.java

License:Open Source License

public static FullHttpResponse createInternalError(Throwable throwable) {
    ByteBuf badRequestBody = Unpooled.wrappedBuffer(throwable.getMessage().getBytes(Charset.forName("UTF-8")));
    return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR,
            badRequestBody);/*  w  ww .  ja  va2  s .  c om*/
}

From source file:com.linkedin.pinot.server.request.ScheduledRequestHandlerTest.java

License:Apache License

@Test
public void testBadRequest() throws Exception {
    ScheduledRequestHandler handler = new ScheduledRequestHandler(queryScheduler, serverMetrics);
    String requestBadString = "foobar";
    byte[] requestData = requestBadString.getBytes();
    ByteBuf buffer = Unpooled.wrappedBuffer(requestData);
    ListenableFuture<byte[]> response = handler.processRequest(channelHandlerContext, buffer);
    // The handler method is expected to return immediately
    Assert.assertTrue(response.isDone());
    byte[] responseBytes = response.get();
    Assert.assertTrue(responseBytes.length > 0);
    DataTable expectedDT = new DataTableImplV2();
    expectedDT.addException(QueryException.INTERNAL_ERROR);
    Assert.assertEquals(responseBytes, expectedDT.toBytes());
}

From source file:com.linkedin.pinot.server.request.ScheduledRequestHandlerTest.java

License:Apache License

private ByteBuf getSerializedInstanceRequest(InstanceRequest request) {
    SerDe serDe = new SerDe(new TCompactProtocol.Factory());
    byte[] requestData = serDe.serialize(request);
    return Unpooled.wrappedBuffer(requestData);
}

From source file:com.linkedin.pinot.transport.common.ResponseFutureTest.java

License:Apache License

private static void setResponse(ResponseFuture f, String message) {
    ByteBuf b = Unpooled.wrappedBuffer(message.getBytes());
    f.onSuccess(b);
}

From source file:com.linkedin.pinot.transport.netty.NettyCloseChannelTest.java

License:Apache License

@Test
/**/*from ww w  .j  a  v a2  s.co m*/
 * Client sends a request. Before Server generates the response, the client closes the channel (scenario:as the client is shutting down)
 */
public void testCloseClientChannel() throws Exception {
    NettyClientMetrics metric = new NettyClientMetrics(null, "abc");
    Timer timer = new HashedWheelTimer();
    String response = "dummy response";
    int port = 9089;
    CountDownLatch latch = new CountDownLatch(1);
    MyRequestHandler handler = new MyRequestHandler(response, latch);
    MyRequestHandlerFactory handlerFactory = new MyRequestHandlerFactory(handler);
    NettyTCPServer serverConn = new NettyTCPServer(port, handlerFactory, null);
    Thread serverThread = new Thread(serverConn, "ServerMain");
    serverThread.start();
    Thread.sleep(1000);
    ServerInstance server = new ServerInstance("localhost", port);
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    NettyTCPClientConnection clientConn = new NettyTCPClientConnection(server, eventLoopGroup, timer, metric);
    LOGGER.info("About to connect the client !!");
    boolean connected = clientConn.connect();
    LOGGER.info("Client connected !!");
    Assert.assertTrue(connected, "connected");
    Thread.sleep(1000);
    String request = "dummy request";
    LOGGER.info("Sending the request !!");
    ResponseFuture serverRespFuture = clientConn.sendRequest(Unpooled.wrappedBuffer(request.getBytes()), 1L,
            5000L);
    //Close the client
    clientConn.close();
    latch.countDown();
    ByteBuf serverResp = serverRespFuture.getOne();
    clientConn.close();
    serverConn.shutdownGracefully();
    Assert.assertNull(serverResp);
    Assert.assertFalse(serverRespFuture.isCancelled(), "Is Cancelled");
    Assert.assertTrue(serverRespFuture.getError() != null, "Got Exception");
    serverConn.shutdownGracefully();
    System.out.println(metric);
}

From source file:com.linkedin.pinot.transport.netty.NettyCloseChannelTest.java

License:Apache License

@Test
/**/* www. j a  v  a 2  s.  c  om*/
 * Client sends a request. Server closes the channel ( scenario: as it is shutting down)
 */
public void testCloseServerChannel() throws Exception {
    NettyClientMetrics metric = new NettyClientMetrics(null, "abc");
    Timer timer = new HashedWheelTimer();
    int port = 9089;
    MyRequestHandler handler = new MyRequestHandler("empty", null);
    MyRequestHandlerFactory handlerFactory = new MyRequestHandlerFactory(handler);
    NettyTCPServer serverConn = new NettyCloseTCPServer(port, handlerFactory);
    Thread serverThread = new Thread(serverConn, "ServerMain");
    serverThread.start();
    Thread.sleep(1000);
    ServerInstance server = new ServerInstance("localhost", port);
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    NettyTCPClientConnection clientConn = new NettyTCPClientConnection(server, eventLoopGroup, timer, metric);
    LOGGER.info("About to connect the client !!");
    boolean connected = clientConn.connect();
    LOGGER.info("Client connected !!");
    Assert.assertTrue(connected, "connected");
    Thread.sleep(1000);
    String request = "dummy request";
    LOGGER.info("Sending the request !!");
    ResponseFuture serverRespFuture = clientConn.sendRequest(Unpooled.wrappedBuffer(request.getBytes()), 1L,
            5000L);
    ByteBuf serverResp = serverRespFuture.getOne();
    clientConn.close();
    serverConn.shutdownGracefully();
    Assert.assertNull(serverResp);
    Assert.assertFalse(serverRespFuture.isCancelled(), "Is Cancelled");
    Assert.assertTrue(serverRespFuture.getError() != null, "Got Exception");
    System.out.println(metric);
}

From source file:com.linkedin.pinot.transport.netty.NettySingleConnectionIntegrationTest.java

License:Apache License

@Test
/**//w  ww.  j  a  v  a 2  s.co m
 * Test Single small request response
 * @throws Exception
 */
public void testSingleSmallRequestResponse() throws Exception {
    NettyClientMetrics metric = new NettyClientMetrics(null, "abc");
    Timer timer = new HashedWheelTimer();

    String response = "dummy response";
    int port = 9089;
    MyRequestHandler handler = new MyRequestHandler(response, null);
    MyRequestHandlerFactory handlerFactory = new MyRequestHandlerFactory(handler);
    NettyTCPServer serverConn = new NettyTCPServer(port, handlerFactory, null);
    Thread serverThread = new Thread(serverConn, "ServerMain");
    serverThread.start();
    Thread.sleep(1000);
    ServerInstance server = new ServerInstance("localhost", port);
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    NettyTCPClientConnection clientConn = new NettyTCPClientConnection(server, eventLoopGroup, timer, metric);
    try {
        LOGGER.info("About to connect the client !!");
        boolean connected = clientConn.connect();
        LOGGER.info("Client connected !!");
        Assert.assertTrue(connected, "connected");
        Thread.sleep(1000);
        String request = "dummy request";
        LOGGER.info("Sending the request !!");
        ResponseFuture serverRespFuture = clientConn.sendRequest(Unpooled.wrappedBuffer(request.getBytes()), 1L,
                5000L);
        LOGGER.info("Request  sent !!");
        ByteBuf serverResp = serverRespFuture.getOne();
        byte[] b2 = new byte[serverResp.readableBytes()];
        serverResp.readBytes(b2);
        String gotResponse = new String(b2);
        Assert.assertEquals(gotResponse, response, "Response Check at client");
        Assert.assertEquals(handler.getRequest(), request, "Request Check at server");
        System.out.println(metric);
    } finally {
        if (null != clientConn) {
            clientConn.close();
        }

        if (null != serverConn) {
            serverConn.shutdownGracefully();
        }
    }
}

From source file:com.linkedin.pinot.transport.netty.NettySingleConnectionIntegrationTest.java

License:Apache License

@Test
public void testCancelOutstandingRequest() throws Exception {
    NettyClientMetrics metric = new NettyClientMetrics(null, "abc");
    String response = "dummy response";
    int port = 9089;
    CountDownLatch latch = new CountDownLatch(1);
    MyRequestHandler handler = new MyRequestHandler(response, latch);
    MyRequestHandlerFactory handlerFactory = new MyRequestHandlerFactory(handler);
    NettyTCPServer serverConn = new NettyTCPServer(port, handlerFactory, null);
    Thread serverThread = new Thread(serverConn, "ServerMain");
    serverThread.start();//  w  w w .j  a v  a 2  s .  c o  m
    Thread.sleep(1000);
    ServerInstance server = new ServerInstance("localhost", port);
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    NettyTCPClientConnection clientConn = new NettyTCPClientConnection(server, eventLoopGroup,
            new HashedWheelTimer(), metric);
    LOGGER.info("About to connect the client !!");
    boolean connected = clientConn.connect();
    LOGGER.info("Client connected !!");
    Assert.assertTrue(connected, "connected");
    Thread.sleep(1000);
    String request = "dummy request";
    LOGGER.info("Sending the request !!");
    ResponseFuture serverRespFuture = clientConn.sendRequest(Unpooled.wrappedBuffer(request.getBytes()), 1L,
            5000L);
    serverRespFuture.cancel(false);
    latch.countDown();
    ByteBuf serverResp = serverRespFuture.getOne();
    Assert.assertNull(serverResp);
    Assert.assertTrue(serverRespFuture.isCancelled(), "Is Cancelled");
    clientConn.close();
    serverConn.shutdownGracefully();
}

From source file:com.linkedin.pinot.transport.netty.NettySingleConnectionIntegrationTest.java

License:Apache License

@Test
public void testConcurrentRequestDispatchError() throws Exception {
    NettyClientMetrics metric = new NettyClientMetrics(null, "abc");
    String response = "dummy response";
    int port = 9089;
    CountDownLatch latch = new CountDownLatch(1);
    MyRequestHandler handler = new MyRequestHandler(response, latch);
    MyRequestHandlerFactory handlerFactory = new MyRequestHandlerFactory(handler);
    NettyTCPServer serverConn = new NettyTCPServer(port, handlerFactory, null);
    Thread serverThread = new Thread(serverConn, "ServerMain");
    serverThread.start();/*from  w ww  .  ja  va 2 s .  c o m*/
    Thread.sleep(1000);
    ServerInstance server = new ServerInstance("localhost", port);
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    NettyTCPClientConnection clientConn = new NettyTCPClientConnection(server, eventLoopGroup,
            new HashedWheelTimer(), metric);
    LOGGER.info("About to connect the client !!");
    boolean connected = clientConn.connect();
    LOGGER.info("Client connected !!");
    Assert.assertTrue(connected, "connected");
    Thread.sleep(1000);
    String request = "dummy request";
    LOGGER.info("Sending the request !!");
    ResponseFuture serverRespFuture = clientConn.sendRequest(Unpooled.wrappedBuffer(request.getBytes()), 1L,
            5000L);
    boolean gotException = false;
    try {
        clientConn.sendRequest(Unpooled.wrappedBuffer(request.getBytes()), 1L, 5000L);
    } catch (IllegalStateException ex) {
        gotException = true;
        // Second request should have failed.
        LOGGER.info("got exception ", ex);
    }
    latch.countDown();
    ByteBuf serverResp = serverRespFuture.getOne();
    byte[] b2 = new byte[serverResp.readableBytes()];
    serverResp.readBytes(b2);
    String gotResponse = new String(b2);
    Assert.assertEquals(gotResponse, response, "Response Check at client");
    Assert.assertEquals(handler.getRequest(), request, "Request Check at server");
    clientConn.close();
    serverConn.shutdownGracefully();
    Assert.assertTrue(gotException, "GotException ");
}

From source file:com.linkedin.pinot.transport.netty.NettySingleConnectionIntegrationTest.java

License:Apache License

@Test
/**// w  w w.ja va2 s. c o m
 * Test Single Large  ( 2 MB) request response
 * @throws Exception
 */
public void testSingleLargeRequestResponse() throws Exception {
    NettyClientMetrics metric = new NettyClientMetrics(null, "abc");
    String response_prefix = "response_";
    String response = generatePayload(response_prefix, 1024 * 1024 * 2);
    int port = 9089;
    MyRequestHandler handler = new MyRequestHandler(response, null);
    MyRequestHandlerFactory handlerFactory = new MyRequestHandlerFactory(handler);
    NettyTCPServer serverConn = new NettyTCPServer(port, handlerFactory, null);
    Thread serverThread = new Thread(serverConn, "ServerMain");
    serverThread.start();
    Thread.sleep(1000);
    ServerInstance server = new ServerInstance("localhost", port);
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    NettyTCPClientConnection clientConn = new NettyTCPClientConnection(server, eventLoopGroup,
            new HashedWheelTimer(), metric);
    try {
        LOGGER.info("About to connect the client !!");
        boolean connected = clientConn.connect();
        LOGGER.info("Client connected !!");
        Assert.assertTrue(connected, "connected");
        Thread.sleep(1000);
        String request_prefix = "request_";
        String request = generatePayload(request_prefix, 1024 * 1024 * 2);
        LOGGER.info("Sending the request !!");
        ResponseFuture serverRespFuture = clientConn.sendRequest(Unpooled.wrappedBuffer(request.getBytes()), 1L,
                5000L);
        LOGGER.info("Request  sent !!");
        ByteBuf serverResp = serverRespFuture.getOne();
        byte[] b2 = new byte[serverResp.readableBytes()];
        serverResp.readBytes(b2);
        String gotResponse = new String(b2);
        Assert.assertEquals(gotResponse, response, "Response Check at client");
        Assert.assertEquals(handler.getRequest(), request, "Request Check at server");
    } finally {
        if (null != clientConn) {
            clientConn.close();
        }
        if (null != serverConn) {
            serverConn.shutdownGracefully();
        }
    }
}