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:net.holmes.core.service.http.HttpFileRequestHandlerTest.java

License:Open Source License

@Test
public void testExceptionCaughtHttpRequestException() throws Exception {
    ConfigurationDao configurationDao = createMock(ConfigurationDao.class);
    ChannelHandlerContext context = createMock(ChannelHandlerContext.class);
    Channel channel = createMock(Channel.class);

    expect(configurationDao.getParameter(HTTP_SERVER_CACHE_SECOND)).andReturn(60);
    expect(context.channel()).andReturn(channel).atLeastOnce();
    expect(channel.isActive()).andReturn(true).atLeastOnce();
    expect(channel.writeAndFlush(isA(Object.class))).andReturn(new DefaultChannelPromise(channel))
            .atLeastOnce();/*from   w w w.  j a  v  a2  s  .c o  m*/
    replay(context, channel, configurationDao);
    new HttpFileRequestHandler(configurationDao).exceptionCaught(context,
            new HttpFileRequestException("message", NOT_FOUND));
    verify(context, channel, configurationDao);
}

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

License:Open Source License

@Test
public void testExceptionCaughtIoException() throws Exception {
    ConfigurationDao configurationDao = createMock(ConfigurationDao.class);
    ChannelHandlerContext context = createMock(ChannelHandlerContext.class);
    Channel channel = createMock(Channel.class);

    expect(configurationDao.getParameter(HTTP_SERVER_CACHE_SECOND)).andReturn(60);
    expect(context.channel()).andReturn(channel).atLeastOnce();
    expect(channel.isActive()).andReturn(true).atLeastOnce();
    expect(channel.writeAndFlush(isA(Object.class))).andReturn(new DefaultChannelPromise(channel))
            .atLeastOnce();/*from w w  w  .  j  a v a 2 s  .c om*/
    replay(context, channel, configurationDao);
    new HttpFileRequestHandler(configurationDao).exceptionCaught(context, new IOException());
    verify(context, channel, configurationDao);
}

From source file:net.tomp2p.connection.Dispatcher.java

License:Apache License

@Override
protected void channelRead0(final ChannelHandlerContext ctx, final Message message) throws Exception {
    LOG.debug("received request {} from channel {}", message, ctx.channel());
    if (message.version() != p2pID) {
        LOG.error("Wrong version. We are looking for {} but we got {}, received: {}", p2pID, message.version(),
                message);/* ww w  .ja  v a2 s.  c o  m*/
        ctx.close();
        synchronized (peerBeanMaster.peerStatusListeners()) {
            for (PeerStatusListener peerStatusListener : peerBeanMaster.peerStatusListeners()) {
                peerStatusListener.peerFailed(message.sender(),
                        new PeerException(AbortCause.PEER_ERROR, "wrong P2P version"));
            }
        }
        return;
    }

    if (!message.isRequest()) {
        LOG.debug("handing message to the next handler {}", message);
        ctx.fireChannelRead(message);
        return;
    }

    Responder responder = new DirectResponder(ctx, message);
    final DispatchHandler myHandler = associatedHandler(message);
    if (myHandler != null) {
        boolean isUdp = ctx.channel() instanceof DatagramChannel;
        LOG.debug("about to respond to {}", message);
        PeerConnection peerConnection = new PeerConnection(message.sender(),
                new DefaultChannelPromise(ctx.channel()).setSuccess(), heartBeatMillis);
        myHandler.forwardMessage(message, isUdp ? null : peerConnection, responder);
    } else {
        if (LOG.isWarnEnabled()) {
            printWarnMessage(message);
        }

        Message responseMessage = DispatchHandler.createResponseMessage(message, Type.UNKNOWN_ID,
                peerBeanMaster.serverPeerAddress());
        response(ctx, responseMessage);
    }
}

From source file:org.apache.bookkeeper.proto.ForceLedgerProcessorV3Test.java

License:Apache License

@Test
public void testForceLedger() throws Exception {
    when(channel.voidPromise()).thenReturn(mock(ChannelPromise.class));
    when(channel.writeAndFlush(any())).thenReturn(mock(ChannelPromise.class));
    doAnswer(invocationOnMock -> {/*from  w  w  w .  ja  v a 2 s  .c om*/
        WriteCallback wc = invocationOnMock.getArgument(1);

        wc.writeComplete(0, request.getForceLedgerRequest().getLedgerId(), Bookie.METAENTRY_ID_FORCE_LEDGER,
                null, null);
        return null;
    }).when(bookie).forceLedger(eq(request.getForceLedgerRequest().getLedgerId()), any(WriteCallback.class),
            same(channel));

    ChannelPromise promise = new DefaultChannelPromise(channel);
    AtomicReference<Object> writtenObject = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    doAnswer(invocationOnMock -> {
        writtenObject.set(invocationOnMock.getArgument(0));
        latch.countDown();
        return promise;
    }).when(channel).writeAndFlush(any());

    processor.run();

    verify(bookie, times(1)).forceLedger(eq(request.getForceLedgerRequest().getLedgerId()),
            any(WriteCallback.class), same(channel));
    verify(channel, times(1)).writeAndFlush(any(Response.class));

    latch.await();

    assertTrue(writtenObject.get() instanceof Response);
    Response response = (Response) writtenObject.get();
    assertEquals(StatusCode.EOK, response.getStatus());
}

From source file:org.apache.bookkeeper.proto.ReadEntryProcessorTest.java

License:Apache License

private void testAsynchronousRequest(boolean result, int errorCode) throws Exception {
    SettableFuture<Boolean> fenceResult = SettableFuture.create();
    when(bookie.fenceLedger(anyLong(), any())).thenReturn(fenceResult);

    ChannelPromise promise = new DefaultChannelPromise(channel);
    AtomicReference<Object> writtenObject = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    doAnswer(invocationOnMock -> {//  w w  w. j a  va2 s .  c  o m
        writtenObject.set(invocationOnMock.getArgument(0));
        latch.countDown();
        return promise;
    }).when(channel).writeAndFlush(any(Response.class), any());

    ExecutorService service = Executors.newCachedThreadPool();
    long ledgerId = System.currentTimeMillis();
    ReadRequest request = new ReadRequest(BookieProtocol.CURRENT_PROTOCOL_VERSION, ledgerId, 1,
            BookieProtocol.FLAG_DO_FENCING, new byte[] {});
    ReadEntryProcessor processor = ReadEntryProcessor.create(request, channel, requestProcessor, service);
    processor.run();

    fenceResult.set(result);
    latch.await();
    verify(channel, times(1)).writeAndFlush(any(Response.class), any());

    assertTrue(writtenObject.get() instanceof Response);
    Response response = (Response) writtenObject.get();
    assertEquals(1, response.getEntryId());
    assertEquals(ledgerId, response.getLedgerId());
    assertEquals(BookieProtocol.READENTRY, response.getOpCode());
    assertEquals(errorCode, response.getErrorCode());
}

From source file:org.apache.bookkeeper.proto.ReadEntryProcessorTest.java

License:Apache License

private void testSynchronousRequest(boolean result, int errorCode) throws Exception {
    SettableFuture<Boolean> fenceResult = SettableFuture.create();
    when(bookie.fenceLedger(anyLong(), any())).thenReturn(fenceResult);
    ChannelPromise promise = new DefaultChannelPromise(channel);
    AtomicReference<Object> writtenObject = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    doAnswer(invocationOnMock -> {/* w  ww  .  j a v  a 2 s .co m*/
        writtenObject.set(invocationOnMock.getArgument(0));
        latch.countDown();
        return promise;
    }).when(channel).writeAndFlush(any(Response.class), any());

    long ledgerId = System.currentTimeMillis();
    ReadRequest request = new ReadRequest(BookieProtocol.CURRENT_PROTOCOL_VERSION, ledgerId, 1,
            BookieProtocol.FLAG_DO_FENCING, new byte[] {});
    ReadEntryProcessor processor = ReadEntryProcessor.create(request, channel, requestProcessor, null);
    fenceResult.set(result);
    processor.run();

    latch.await();
    verify(channel, times(1)).writeAndFlush(any(Response.class), any());

    assertTrue(writtenObject.get() instanceof Response);
    Response response = (Response) writtenObject.get();
    assertEquals(1, response.getEntryId());
    assertEquals(ledgerId, response.getLedgerId());
    assertEquals(BookieProtocol.READENTRY, response.getOpCode());
    assertEquals(errorCode, response.getErrorCode());
}

From source file:org.apache.bookkeeper.proto.ReadEntryProcessorTest.java

License:Apache License

@Test
public void testNonFenceRequest() throws Exception {
    ChannelPromise promise = new DefaultChannelPromise(channel);
    AtomicReference<Object> writtenObject = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    doAnswer(invocationOnMock -> {//from  w w  w .  j  av  a  2s .co m
        writtenObject.set(invocationOnMock.getArgument(0));
        latch.countDown();
        return promise;
    }).when(channel).writeAndFlush(any(Response.class), any());

    long ledgerId = System.currentTimeMillis();
    ReadRequest request = new ReadRequest(BookieProtocol.CURRENT_PROTOCOL_VERSION, ledgerId, 1, (short) 0,
            new byte[] {});
    ReadEntryProcessor processor = ReadEntryProcessor.create(request, channel, requestProcessor, null);
    processor.run();

    latch.await();
    verify(channel, times(1)).writeAndFlush(any(Response.class), any());

    assertTrue(writtenObject.get() instanceof Response);
    Response response = (Response) writtenObject.get();
    assertEquals(1, response.getEntryId());
    assertEquals(ledgerId, response.getLedgerId());
    assertEquals(BookieProtocol.READENTRY, response.getOpCode());
    assertEquals(BookieProtocol.EOK, response.getErrorCode());
}

From source file:org.apache.bookkeeper.proto.WriteEntryProcessorV3Test.java

License:Apache License

@Test
public void testNoneHighPriorityWritesOnReadOnlyBookie() throws Exception {
    when(bookie.isReadOnly()).thenReturn(true);
    when(channel.voidPromise()).thenReturn(mock(ChannelPromise.class));

    ChannelPromise promise = new DefaultChannelPromise(channel);
    AtomicReference<Object> writtenObject = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    doAnswer(invocationOnMock -> {/*from   ww  w .j a va  2  s  .  com*/
        writtenObject.set(invocationOnMock.getArgument(0));
        latch.countDown();
        return promise;
    }).when(channel).writeAndFlush(any());

    processor.run();

    verify(channel, times(1)).writeAndFlush(any(Response.class));

    latch.await();

    assertTrue(writtenObject.get() instanceof Response);
    Response response = (Response) writtenObject.get();
    assertEquals(StatusCode.EREADONLY, response.getStatus());
}

From source file:org.apache.bookkeeper.proto.WriteEntryProcessorV3Test.java

License:Apache License

@Test
public void testHighPriorityWritesOnReadOnlyBookieWhenHighPriorityWritesDisallowed() throws Exception {
    reinitRequest(100);/*from  w  w  w . ja v  a2  s. com*/

    when(bookie.isReadOnly()).thenReturn(true);
    when(bookie.isAvailableForHighPriorityWrites()).thenReturn(false);
    when(channel.voidPromise()).thenReturn(mock(ChannelPromise.class));

    ChannelPromise promise = new DefaultChannelPromise(channel);
    AtomicReference<Object> writtenObject = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    doAnswer(invocationOnMock -> {
        writtenObject.set(invocationOnMock.getArgument(0));
        latch.countDown();
        return promise;
    }).when(channel).writeAndFlush(any());

    processor.run();

    verify(channel, times(1)).writeAndFlush(any(Response.class));

    latch.await();

    assertTrue(writtenObject.get() instanceof Response);
    Response response = (Response) writtenObject.get();
    assertEquals(StatusCode.EREADONLY, response.getStatus());
}

From source file:org.apache.bookkeeper.proto.WriteEntryProcessorV3Test.java

License:Apache License

@Test
public void testHighPriorityWritesOnReadOnlyBookieWhenHighPriorityWritesAllowed() throws Exception {
    reinitRequest(BookieProtocol.FLAG_HIGH_PRIORITY);

    when(bookie.isReadOnly()).thenReturn(true);
    when(bookie.isAvailableForHighPriorityWrites()).thenReturn(true);
    when(channel.voidPromise()).thenReturn(mock(ChannelPromise.class));

    doAnswer(invocationOnMock -> {//from   w  ww .  j a v a 2s  . c om
        WriteCallback wc = invocationOnMock.getArgument(2);

        wc.writeComplete(0, request.getAddRequest().getLedgerId(), request.getAddRequest().getEntryId(), null,
                null);
        return null;
    }).when(bookie).addEntry(any(ByteBuf.class), eq(false), any(WriteCallback.class), same(channel),
            eq(new byte[0]));

    ChannelPromise promise = new DefaultChannelPromise(channel);
    AtomicReference<Object> writtenObject = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    doAnswer(invocationOnMock -> {
        writtenObject.set(invocationOnMock.getArgument(0));
        latch.countDown();
        return promise;
    }).when(channel).writeAndFlush(any());

    processor.run();

    verify(bookie, times(1)).addEntry(any(ByteBuf.class), eq(false), any(WriteCallback.class), same(channel),
            eq(new byte[0]));
    verify(channel, times(1)).writeAndFlush(any(Response.class));

    latch.await();

    assertTrue(writtenObject.get() instanceof Response);
    Response response = (Response) writtenObject.get();
    assertEquals(StatusCode.EOK, response.getStatus());
}