Search in sources :

Example 1 with DefaultByteBufHolder

use of io.netty.buffer.DefaultByteBufHolder in project netty by netty.

the class Http2DefaultFramesTest method testEqualOperation.

@SuppressWarnings("SimplifiableJUnitAssertion")
@Test
public void testEqualOperation() {
    // in this case, 'goAwayFrame' and 'unknownFrame' will also have an EMPTY_BUFFER data
    // so we want to check that 'dflt' will not consider them equal.
    DefaultHttp2GoAwayFrame goAwayFrame = new DefaultHttp2GoAwayFrame(1);
    DefaultHttp2UnknownFrame unknownFrame = new DefaultHttp2UnknownFrame((byte) 1, new Http2Flags((short) 1));
    DefaultByteBufHolder dflt = new DefaultByteBufHolder(Unpooled.EMPTY_BUFFER);
    try {
        // not using 'assertNotEquals' to be explicit about which object we are calling .equals() on
        assertFalse(dflt.equals(goAwayFrame));
        assertFalse(dflt.equals(unknownFrame));
    } finally {
        goAwayFrame.release();
        unknownFrame.release();
        dflt.release();
    }
}
Also used : DefaultByteBufHolder(io.netty.buffer.DefaultByteBufHolder) Test(org.junit.jupiter.api.Test)

Example 2 with DefaultByteBufHolder

use of io.netty.buffer.DefaultByteBufHolder in project netty by netty.

the class LoggingHandlerTest method shouldLogByteBufHolderDataRead.

@Test
public void shouldLogByteBufHolderDataRead() throws Exception {
    ByteBufHolder msg = new DefaultByteBufHolder(Unpooled.copiedBuffer("hello", CharsetUtil.UTF_8)) {

        @Override
        public String toString() {
            return "foobar";
        }
    };
    EmbeddedChannel channel = new EmbeddedChannel(new LoggingHandler());
    channel.writeInbound(msg);
    verify(appender).doAppend(argThat(new RegexLogMatcher(".+READ: foobar, 5B$", true)));
    ByteBufHolder handledMsg = channel.readInbound();
    assertThat(msg, is(sameInstance(handledMsg)));
    handledMsg.release();
    assertThat(channel.readInbound(), is(nullValue()));
}
Also used : ByteBufHolder(io.netty.buffer.ByteBufHolder) DefaultByteBufHolder(io.netty.buffer.DefaultByteBufHolder) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) DefaultByteBufHolder(io.netty.buffer.DefaultByteBufHolder) Test(org.junit.jupiter.api.Test)

Example 3 with DefaultByteBufHolder

use of io.netty.buffer.DefaultByteBufHolder in project rskj by rsksmart.

the class RskWebSocketJsonRpcHandlerTest method handlerPassesRequestToNextHandlerOnException.

@Test
public void handlerPassesRequestToNextHandlerOnException() throws Exception {
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(serializer.deserializeRequest(any())).thenThrow(new IOException());
    DefaultByteBufHolder msg = new DefaultByteBufHolder(Unpooled.copiedBuffer("raw".getBytes()));
    handler.channelRead(ctx, msg);
    verify(ctx, never()).writeAndFlush(any());
    verify(ctx, times(1)).fireChannelRead(msg);
}
Also used : ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) DefaultByteBufHolder(io.netty.buffer.DefaultByteBufHolder) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with DefaultByteBufHolder

use of io.netty.buffer.DefaultByteBufHolder in project netty by netty.

the class MessageAggregatorTest method testReadFlowManagement.

@SuppressWarnings("unchecked")
@Test
public void testReadFlowManagement() throws Exception {
    ReadCounter counter = new ReadCounter();
    ByteBufHolder first = message("first");
    ByteBufHolder chunk = message("chunk");
    ByteBufHolder last = message("last");
    MockMessageAggregator agg = spy(MockMessageAggregator.class);
    when(agg.isStartMessage(first)).thenReturn(true);
    when(agg.isContentMessage(chunk)).thenReturn(true);
    when(agg.isContentMessage(last)).thenReturn(true);
    when(agg.isLastContentMessage(last)).thenReturn(true);
    EmbeddedChannel embedded = new EmbeddedChannel(counter, agg);
    embedded.config().setAutoRead(false);
    assertFalse(embedded.writeInbound(first));
    assertFalse(embedded.writeInbound(chunk));
    assertTrue(embedded.writeInbound(last));
    // 2 reads issued from MockMessageAggregator
    assertEquals(3, counter.value);
    // 1 read issued from EmbeddedChannel constructor
    ByteBufHolder all = new DefaultByteBufHolder(Unpooled.wrappedBuffer(first.content().retain(), chunk.content().retain(), last.content().retain()));
    ByteBufHolder out = embedded.readInbound();
    assertEquals(all, out);
    assertTrue(all.release() && out.release());
    assertFalse(embedded.finish());
}
Also used : DefaultByteBufHolder(io.netty.buffer.DefaultByteBufHolder) ByteBufHolder(io.netty.buffer.ByteBufHolder) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) DefaultByteBufHolder(io.netty.buffer.DefaultByteBufHolder) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultByteBufHolder

use of io.netty.buffer.DefaultByteBufHolder in project rskj by rsksmart.

the class RskWebSocketJsonRpcHandlerTest method handlerDeserializesAndHandlesRequest.

@Test
public void handlerDeserializesAndHandlesRequest() throws Exception {
    String json = "{\"jsonrpc\":\"2.0\",\"id\":\"teste\",\"method\":\"eth_subscribe\",\"params\":[\"newHeads\"]}";
    Channel channel = mock(Channel.class);
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.channel()).thenReturn(channel);
    when(emitter.visit(any(EthSubscribeNewHeadsParams.class), eq(channel))).thenReturn(SAMPLE_SUBSCRIPTION_ID_1);
    DefaultByteBufHolder msg = new DefaultByteBufHolder(Unpooled.copiedBuffer(json.getBytes()));
    handler.channelRead(ctx, msg);
    verify(ctx, times(1)).writeAndFlush(new TextWebSocketFrame("{\"jsonrpc\":\"2.0\",\"id\":\"teste\",\"result\":\"0x3075\"}"));
    verify(ctx, never()).fireChannelRead(any());
}
Also used : Channel(io.netty.channel.Channel) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) DefaultByteBufHolder(io.netty.buffer.DefaultByteBufHolder) Test(org.junit.Test)

Aggregations

DefaultByteBufHolder (io.netty.buffer.DefaultByteBufHolder)5 Test (org.junit.jupiter.api.Test)3 ByteBufHolder (io.netty.buffer.ByteBufHolder)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 Test (org.junit.Test)2 Channel (io.netty.channel.Channel)1 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)1 IOException (java.io.IOException)1