Example usage for io.netty.channel DefaultChannelPromise DefaultChannelPromise

List of usage examples for io.netty.channel DefaultChannelPromise DefaultChannelPromise

Introduction

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

Prototype

public DefaultChannelPromise(Channel channel) 

Source Link

Document

Creates a new instance.

Usage

From source file:io.apigee.trireme.container.netty.NettyHttpResponse.java

License:Open Source License

@Override
public HttpFuture sendChunk(ByteBuffer buf, boolean lastChunk) {
    ChannelFuture future = null;/*from w w w  . j av  a 2 s .com*/
    if (buf != null) {
        if (log.isDebugEnabled()) {
            log.debug("sendChunk: Sending HTTP chunk {}", buf);
        }
        DefaultHttpContent chunk = new DefaultHttpContent(NettyServer.copyBuffer(buf));
        future = channel.write(chunk);
    }

    if (lastChunk) {
        future = sendLastChunk();
    }
    channel.flush();
    if (lastChunk && !keepAlive) {
        shutDown();
    }

    if (future == null) {
        DefaultChannelPromise doneFuture = new DefaultChannelPromise(channel);
        doneFuture.setSuccess();
        future = doneFuture;
    }
    return new NettyHttpFuture(future);
}

From source file:io.grpc.netty.NettyClientStreamTest.java

License:Apache License

@Test
public void removeUserAgentFromApplicationHeaders() {
    Metadata metadata = new Metadata();
    metadata.put(GrpcUtil.USER_AGENT_KEY, "bad agent");
    listener = mock(ClientStreamListener.class);
    Mockito.reset(writeQueue);//  w w  w  .ja  v a  2 s.  co m
    ChannelPromise completedPromise = new DefaultChannelPromise(channel).setSuccess();
    when(writeQueue.enqueue(any(QueuedCommand.class), anyBoolean())).thenReturn(completedPromise);

    stream = new NettyClientStream(new TransportStateImpl(handler, DEFAULT_MAX_MESSAGE_SIZE), methodDescriptor,
            new Metadata(), channel, AsciiString.of("localhost"), AsciiString.of("http"),
            AsciiString.of("good agent"), StatsTraceContext.NOOP, transportTracer, CallOptions.DEFAULT);
    stream.start(listener);

    ArgumentCaptor<CreateStreamCommand> cmdCap = ArgumentCaptor.forClass(CreateStreamCommand.class);
    verify(writeQueue).enqueue(cmdCap.capture(), eq(false));
    assertThat(ImmutableListMultimap.copyOf(cmdCap.getValue().headers())).containsEntry(Utils.USER_AGENT,
            AsciiString.of("good agent"));
}

From source file:io.grpc.netty.NettyStreamTestBase.java

License:Apache License

/** Set up for test. */
@Before//from w w  w  .  ja  va2s.  c  om
public void setUp() {
    MockitoAnnotations.initMocks(this);

    when(channel.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT);
    when(channel.pipeline()).thenReturn(pipeline);
    when(channel.eventLoop()).thenReturn(eventLoop);
    when(channel.newPromise()).thenReturn(new DefaultChannelPromise(channel));
    when(channel.voidPromise()).thenReturn(new DefaultChannelPromise(channel));
    ChannelPromise completedPromise = new DefaultChannelPromise(channel).setSuccess();
    when(channel.write(any())).thenReturn(completedPromise);
    when(channel.writeAndFlush(any())).thenReturn(completedPromise);
    when(writeQueue.enqueue(any(QueuedCommand.class), anyBoolean())).thenReturn(completedPromise);
    when(pipeline.firstContext()).thenReturn(ctx);
    when(eventLoop.inEventLoop()).thenReturn(true);
    when(http2Stream.id()).thenReturn(STREAM_ID);

    doAnswer(new Answer<Void>() {
        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            Runnable runnable = (Runnable) invocation.getArguments()[0];
            runnable.run();
            return null;
        }
    }).when(eventLoop).execute(any(Runnable.class));

    stream = createStream();
}

From source file:io.grpc.netty.NettyStreamTestBase.java

License:Apache License

@Test
public void shouldBeReadyForDataAfterWritingSmallMessage() throws IOException {
    sendHeadersIfServer();/* ww  w.  j  a v  a2  s.  c o  m*/
    // Make sure the writes don't complete so we "back up"
    ChannelPromise uncompletedPromise = new DefaultChannelPromise(channel);
    when(writeQueue.enqueue(any(QueuedCommand.class), anyBoolean())).thenReturn(uncompletedPromise);

    assertTrue(stream.isReady());
    byte[] msg = smallMessage();
    stream.writeMessage(new ByteArrayInputStream(msg));
    stream.flush();
    assertTrue(stream.isReady());
    verify(listener(), never()).onReady();
}

From source file:io.grpc.netty.NettyStreamTestBase.java

License:Apache License

@Test
public void shouldNotBeReadyForDataAfterWritingLargeMessage() throws IOException {
    sendHeadersIfServer();//from w ww  .java  2 s  . com
    // Make sure the writes don't complete so we "back up"
    ChannelPromise uncompletedPromise = new DefaultChannelPromise(channel);
    when(writeQueue.enqueue(any(QueuedCommand.class), anyBoolean())).thenReturn(uncompletedPromise);

    assertTrue(stream.isReady());
    byte[] msg = largeMessage();
    stream.writeMessage(new ByteArrayInputStream(msg));
    stream.flush();
    assertFalse(stream.isReady());
    verify(listener(), never()).onReady();
}

From source file:io.lettuce.core.protocol.DefaultEndpointTest.java

License:Apache License

@Before
public void before() {

    promise = new DefaultChannelPromise(channel);
    when(channel.writeAndFlush(any())).thenAnswer(invocation -> {
        if (invocation.getArguments()[0] instanceof RedisCommand) {
            queue.add((RedisCommand) invocation.getArguments()[0]);
        }/*from w w w .j a  v  a2  s . c o  m*/

        if (invocation.getArguments()[0] instanceof Collection) {
            queue.addAll((Collection) invocation.getArguments()[0]);
        }
        return promise;
    });

    when(channel.write(any())).thenAnswer(invocation -> {
        if (invocation.getArguments()[0] instanceof RedisCommand) {
            queue.add((RedisCommand) invocation.getArguments()[0]);
        }

        if (invocation.getArguments()[0] instanceof Collection) {
            queue.addAll((Collection) invocation.getArguments()[0]);
        }
        return promise;
    });

    sut = new DefaultEndpoint(ClientOptions.create(), clientResources);
    sut.setConnectionFacade(connectionFacade);
}

From source file:net.holmes.core.service.http.HttpFileRequestHandlerTest.java

License:Open Source License

@Test
public void testFileRequestHandler() throws Exception {
    File indexHtml = File.createTempFile("index", ".html");
    indexHtml.deleteOnExit();// ww w. j av a2s  .  c  o m

    HttpHeaders headers = new DefaultHttpHeaders();
    headers.add(HOST, "localhost");

    ConfigurationDao configurationDao = createMock(ConfigurationDao.class);
    ChannelHandlerContext context = createMock(ChannelHandlerContext.class);
    FullHttpRequest httpRequest = createMock(FullHttpRequest.class);
    Channel channel = createMock(Channel.class);

    expect(configurationDao.getParameter(HTTP_SERVER_CACHE_SECOND)).andReturn(60);
    expect(httpRequest.headers()).andReturn(headers).atLeastOnce();
    expect(httpRequest.getProtocolVersion()).andReturn(HTTP_1_1).atLeastOnce();
    expect(context.write(isA(HttpResponse.class))).andReturn(new DefaultChannelPromise(channel)).atLeastOnce();
    expect(context.write(isA(ChunkedFile.class))).andReturn(new DefaultChannelPromise(channel)).atLeastOnce();
    expect(context.writeAndFlush(isA(LastHttpContent.class))).andReturn(new DefaultChannelPromise(channel))
            .atLeastOnce();

    HttpFileRequest request = new HttpFileRequest(httpRequest, new File(indexHtml.getAbsolutePath()),
            MimeType.valueOf("text/html"), false);

    replay(context, httpRequest, channel, configurationDao);
    HttpFileRequestHandler handler = new HttpFileRequestHandler(configurationDao);
    handler.channelRead0(context, request);
    verify(context, httpRequest, channel, configurationDao);
}

From source file:net.holmes.core.service.http.HttpFileRequestHandlerTest.java

License:Open Source License

@Test
public void testFileRequestHandlerNoCache() throws Exception {
    File indexHtml = File.createTempFile("index", ".html");
    indexHtml.deleteOnExit();/*www  .j  a  v a2 s  .  c o m*/

    HttpHeaders headers = new DefaultHttpHeaders();
    headers.add(HOST, "localhost");

    ConfigurationDao configurationDao = createMock(ConfigurationDao.class);
    ChannelHandlerContext context = createMock(ChannelHandlerContext.class);
    FullHttpRequest httpRequest = createMock(FullHttpRequest.class);
    Channel channel = createMock(Channel.class);

    expect(configurationDao.getParameter(HTTP_SERVER_CACHE_SECOND)).andReturn(0);
    expect(httpRequest.headers()).andReturn(headers).atLeastOnce();
    expect(httpRequest.getProtocolVersion()).andReturn(HTTP_1_1).atLeastOnce();
    expect(context.write(isA(HttpResponse.class))).andReturn(new DefaultChannelPromise(channel)).atLeastOnce();
    expect(context.write(isA(ChunkedFile.class))).andReturn(new DefaultChannelPromise(channel)).atLeastOnce();
    expect(context.writeAndFlush(isA(LastHttpContent.class))).andReturn(new DefaultChannelPromise(channel))
            .atLeastOnce();

    HttpFileRequest request = new HttpFileRequest(httpRequest, new File(indexHtml.getAbsolutePath()),
            MimeType.valueOf("text/html"), true);

    replay(context, httpRequest, channel, configurationDao);
    HttpFileRequestHandler handler = new HttpFileRequestHandler(configurationDao);
    handler.channelRead0(context, request);
    verify(context, httpRequest, channel, configurationDao);
}

From source file:net.holmes.core.service.http.HttpFileRequestHandlerTest.java

License:Open Source License

@Test
public void testFileRequestHandlerWithOffset() throws Exception {
    File indexHtml = File.createTempFile("index", ".html");
    FileWriter fw = new FileWriter(indexHtml);
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write("some content in index.html");
    bw.close();//from   w w w  .j  a v a  2s.  c  om
    indexHtml.deleteOnExit();

    HttpHeaders headers = new DefaultHttpHeaders();
    headers.add(HOST, "localhost");
    headers.add(RANGE, "bytes=5-");

    ConfigurationDao configurationDao = createMock(ConfigurationDao.class);
    ChannelHandlerContext context = createMock(ChannelHandlerContext.class);
    FullHttpRequest httpRequest = createMock(FullHttpRequest.class);
    Channel channel = createMock(Channel.class);

    expect(configurationDao.getParameter(HTTP_SERVER_CACHE_SECOND)).andReturn(60);
    expect(httpRequest.headers()).andReturn(headers).atLeastOnce();
    expect(httpRequest.getProtocolVersion()).andReturn(HTTP_1_1).atLeastOnce();
    expect(context.write(isA(HttpResponse.class))).andReturn(new DefaultChannelPromise(channel)).atLeastOnce();
    expect(context.write(isA(ChunkedFile.class))).andReturn(new DefaultChannelPromise(channel)).atLeastOnce();
    expect(context.writeAndFlush(isA(LastHttpContent.class))).andReturn(new DefaultChannelPromise(channel))
            .atLeastOnce();

    HttpFileRequest request = new HttpFileRequest(httpRequest, new File(indexHtml.getAbsolutePath()),
            MimeType.valueOf("text/html"), false);

    replay(context, httpRequest, channel, configurationDao);
    HttpFileRequestHandler handler = new HttpFileRequestHandler(configurationDao);
    handler.channelRead0(context, request);
    verify(context, httpRequest, channel, configurationDao);
}

From source file:net.holmes.core.service.http.HttpFileRequestHandlerTest.java

License:Open Source License

@Test
public void testFileRequestHandlerWithoutKeepAlive() throws Exception {
    File indexHtml = File.createTempFile("index", ".html");
    indexHtml.deleteOnExit();/*  w ww.j a  v  a  2s  .co  m*/

    HttpHeaders headers = new DefaultHttpHeaders();
    headers.add(HOST, "localhost");
    headers.add(CONNECTION, CLOSE);

    ConfigurationDao configurationDao = createMock(ConfigurationDao.class);
    ChannelHandlerContext context = createMock(ChannelHandlerContext.class);
    FullHttpRequest httpRequest = createMock(FullHttpRequest.class);
    Channel channel = createMock(Channel.class);

    expect(configurationDao.getParameter(HTTP_SERVER_CACHE_SECOND)).andReturn(60);
    expect(httpRequest.headers()).andReturn(headers).atLeastOnce();
    expect(context.write(isA(HttpResponse.class))).andReturn(new DefaultChannelPromise(channel)).atLeastOnce();
    expect(context.write(isA(ChunkedFile.class))).andReturn(new DefaultChannelPromise(channel)).atLeastOnce();
    expect(context.writeAndFlush(isA(LastHttpContent.class))).andReturn(new DefaultChannelPromise(channel))
            .atLeastOnce();

    HttpFileRequest request = new HttpFileRequest(httpRequest, new File(indexHtml.getAbsolutePath()),
            MimeType.valueOf("text/html"), true);

    replay(context, httpRequest, channel, configurationDao);
    HttpFileRequestHandler handler = new HttpFileRequestHandler(configurationDao);
    handler.channelRead0(context, request);
    verify(context, httpRequest, channel, configurationDao);
}