Search in sources :

Example 96 with EmbeddedChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project cassandra by apache.

the class EntireSSTableStreamingCorrectFilesCountTest method createMockNettyChannel.

private EmbeddedChannel createMockNettyChannel(ByteBuf serializedFile) {
    WritableByteChannel wbc = new WritableByteChannel() {

        private boolean isOpen = true;

        public int write(ByteBuffer src) {
            int size = src.limit();
            serializedFile.writeBytes(src);
            return size;
        }

        public boolean isOpen() {
            return isOpen;
        }

        public void close() {
            isOpen = false;
        }
    };
    return new EmbeddedChannel(new ChannelOutboundHandlerAdapter() {

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            ((SharedDefaultFileRegion) msg).transferTo(wbc, 0);
            super.write(ctx, msg, promise);
        }
    });
}
Also used : WritableByteChannel(java.nio.channels.WritableByteChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuffer(java.nio.ByteBuffer) RangesAtEndpoint(org.apache.cassandra.locator.RangesAtEndpoint) IOException(java.io.IOException)

Example 97 with EmbeddedChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project cassandra by apache.

the class EntireSSTableStreamingCorrectFilesCountTest method constructDataOutputStream.

private AsyncStreamingOutputPlus constructDataOutputStream() {
    // This is needed as Netty releases the ByteBuffers as soon as the channel is flushed
    ByteBuf serializedFile = Unpooled.buffer(8192);
    EmbeddedChannel channel = createMockNettyChannel(serializedFile);
    return new AsyncStreamingOutputPlus(channel) {

        public void flush() throws IOException {
        // NO-OP
        }
    };
}
Also used : AsyncStreamingOutputPlus(org.apache.cassandra.net.AsyncStreamingOutputPlus) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf)

Example 98 with EmbeddedChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project neo4j by neo4j.

the class FragmentedMessageDeliveryTest method testPermutation.

private void testPermutation(byte[] unfragmented, ByteBuf[] fragments) throws Exception {
    // Given
    channel = new EmbeddedChannel();
    BoltChannel boltChannel = newTestBoltChannel(channel);
    BoltStateMachine machine = mock(BoltStateMachine.class);
    SynchronousBoltConnection boltConnection = new SynchronousBoltConnection(machine);
    NullLogService logging = NullLogService.getInstance();
    var bookmarksParser = mock(BookmarksParser.class);
    var memoryTracker = mock(MemoryTracker.class);
    BoltProtocol boltProtocol = new BoltProtocolV4(boltChannel, (ch, s, messageWriter) -> boltConnection, (v, ch, hints, mem) -> machine, Config.defaults(), bookmarksParser, logging, mock(TransportThrottleGroup.class), mock(ChannelProtector.class), memoryTracker);
    boltProtocol.install();
    // When data arrives split up according to the current permutation
    for (ByteBuf fragment : fragments) {
        channel.writeInbound(fragment.readerIndex(0).retain());
    }
    // Then the session should've received the specified messages, and the protocol should be in a nice clean state
    try {
        RequestMessage run = new RunMessage("Mjölnir", EMPTY_MAP);
        verify(machine).process(eq(run), any(BoltResponseHandler.class));
    } catch (AssertionError e) {
        throw new AssertionError("Failed to handle fragmented delivery.\n" + "Messages: " + Arrays.toString(messages) + "\n" + "Chunk size: " + chunkSize + "\n" + "Serialized data delivered in fragments: " + describeFragments(fragments) + "\n" + "Unfragmented data: " + HexPrinter.hex(unfragmented) + "\n", e);
    }
}
Also used : SynchronousBoltConnection(org.neo4j.bolt.runtime.SynchronousBoltConnection) BoltProtocolV4(org.neo4j.bolt.v4.BoltProtocolV4) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) NullLogService(org.neo4j.logging.internal.NullLogService) BoltResponseHandler(org.neo4j.bolt.runtime.BoltResponseHandler) ByteBuf(io.netty.buffer.ByteBuf) RunMessage(org.neo4j.bolt.v4.messaging.RunMessage) ChannelProtector(org.neo4j.bolt.transport.pipeline.ChannelProtector) BoltProtocol(org.neo4j.bolt.BoltProtocol) BoltStateMachine(org.neo4j.bolt.runtime.statemachine.BoltStateMachine) BoltChannel(org.neo4j.bolt.BoltChannel) BoltTestUtil.newTestBoltChannel(org.neo4j.bolt.testing.BoltTestUtil.newTestBoltChannel) RequestMessage(org.neo4j.bolt.messaging.RequestMessage) TransportThrottleGroup(org.neo4j.bolt.transport.TransportThrottleGroup)

Example 99 with EmbeddedChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project neo4j by neo4j.

the class BoltChannelTest method shouldExposeClientConnectionInfo.

@Test
void shouldExposeClientConnectionInfo() {
    EmbeddedChannel channel = new EmbeddedChannel();
    BoltChannel boltChannel = new BoltChannel("bolt-42", "my-bolt", channel, ChannelProtector.NULL);
    ClientConnectionInfo info1 = boltChannel.info();
    assertEquals("bolt-42", info1.connectionId());
    assertEquals("bolt", info1.protocol());
    assertEquals(SocketAddress.format(channel.remoteAddress()), info1.clientAddress());
    boltChannel.updateUser("Tom", "my-driver");
    ClientConnectionInfo info2 = boltChannel.info();
    assertEquals("bolt-42", info2.connectionId());
    assertEquals("bolt", info2.protocol());
    assertEquals(SocketAddress.format(channel.remoteAddress()), info2.clientAddress());
    assertThat(info2.asConnectionDetails()).contains("my-driver");
}
Also used : ClientConnectionInfo(org.neo4j.internal.kernel.api.connectioninfo.ClientConnectionInfo) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Test(org.junit.jupiter.api.Test)

Example 100 with EmbeddedChannel

use of org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel in project neo4j by neo4j.

the class DefaultBoltProtocolFactoryTest method shouldCreateBoltProtocol.

@ParameterizedTest(name = "V{0}.{1}")
@CsvSource({ "3, 0", "4, 0", "4, 1", "4, 2", "4, 3" })
void shouldCreateBoltProtocol(int majorVersion, int minorVersion) throws Throwable {
    EmbeddedChannel channel = new EmbeddedChannel();
    BoltChannel boltChannel = newTestBoltChannel(channel);
    BoltProtocolVersion boltProtocolVersion = new BoltProtocolVersion(majorVersion, minorVersion);
    BoltStateMachineFactory stateMachineFactory = mock(BoltStateMachineFactory.class);
    BoltStateMachine stateMachine = mock(BoltStateMachine.class);
    var channelProtector = mock(ChannelProtector.class);
    var memoryTracker = mock(MemoryTracker.class, RETURNS_MOCKS);
    when(stateMachineFactory.newStateMachine(boltProtocolVersion, boltChannel, MapValue.EMPTY, memoryTracker)).thenReturn(stateMachine);
    BoltConnectionFactory connectionFactory = mock(BoltConnectionFactory.class);
    BoltConnection connection = mock(BoltConnection.class);
    when(connectionFactory.newConnection(eq(boltChannel), eq(stateMachine), any())).thenReturn(connection);
    BoltProtocolFactory factory = new DefaultBoltProtocolFactory(connectionFactory, stateMachineFactory, Config.defaults(), NullLogService.getInstance(), new TestDatabaseIdRepository(), CustomBookmarkFormatParser.DEFAULT, mock(TransportThrottleGroup.class), Clocks.fakeClock(), Duration.ZERO);
    BoltProtocol protocol = factory.create(boltProtocolVersion, boltChannel, channelProtector, memoryTracker);
    protocol.install();
    // handler with correct version is created
    assertEquals(boltProtocolVersion, protocol.version());
    // it uses the expected worker
    verify(connectionFactory).newConnection(eq(boltChannel), any(BoltStateMachine.class), any(BoltResponseMessageWriter.class));
    verify(memoryTracker, times(5)).allocateHeap(anyLong());
    // and halts this same worker when closed
    verify(connection, never()).stop();
    channel.close();
    verify(connection).stop();
    channel.finishAndReleaseAll();
}
Also used : BoltResponseMessageWriter(org.neo4j.bolt.messaging.BoltResponseMessageWriter) BoltConnectionFactory(org.neo4j.bolt.runtime.BoltConnectionFactory) BoltStateMachineFactory(org.neo4j.bolt.runtime.statemachine.BoltStateMachineFactory) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) TestDatabaseIdRepository(org.neo4j.kernel.database.TestDatabaseIdRepository) BoltProtocol(org.neo4j.bolt.BoltProtocol) BoltStateMachine(org.neo4j.bolt.runtime.statemachine.BoltStateMachine) BoltConnection(org.neo4j.bolt.runtime.BoltConnection) BoltProtocolVersion(org.neo4j.bolt.BoltProtocolVersion) BoltChannel(org.neo4j.bolt.BoltChannel) BoltTestUtil.newTestBoltChannel(org.neo4j.bolt.testing.BoltTestUtil.newTestBoltChannel) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)1027 Test (org.junit.jupiter.api.Test)515 ByteBuf (io.netty.buffer.ByteBuf)356 Test (org.junit.Test)342 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)85 HttpResponse (io.netty.handler.codec.http.HttpResponse)73 HttpRequest (io.netty.handler.codec.http.HttpRequest)69 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)64 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)60 DefaultLastHttpContent (io.netty.handler.codec.http.DefaultLastHttpContent)55 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)50 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)49 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)46 EmbeddedChannel (org.apache.flink.shaded.netty4.io.netty.channel.embedded.EmbeddedChannel)42 IOException (java.io.IOException)38 InetSocketAddress (java.net.InetSocketAddress)38 Executable (org.junit.jupiter.api.function.Executable)36 ArrayList (java.util.ArrayList)35 Before (org.junit.Before)32 ChannelHandler (io.netty.channel.ChannelHandler)27