Search in sources :

Example 1 with BoltProtocolFactory

use of org.neo4j.bolt.transport.BoltProtocolFactory in project neo4j by neo4j.

the class ProtocolHandshakerTest method shouldChooseFirstAvailableProtocol.

@Test
void shouldChooseFirstAvailableProtocol() {
    // Given
    BoltProtocol protocol = newBoltProtocol(1, 0);
    BoltProtocolFactory handlerFactory = newProtocolFactory(1, 0, protocol);
    var memoryTracker = mock(MemoryTracker.class, RETURNS_MOCKS);
    EmbeddedChannel channel = new EmbeddedChannel(new ProtocolHandshaker(handlerFactory, boltChannel, logProvider, false, true, mock(ChannelProtector.class), memoryTracker));
    // When
    ByteBuf input = // create handshake data
    Unpooled.wrappedBuffer(// preamble
    new byte[] { (byte) 0x60, (byte) 0x60, (byte) 0xB0, (byte) 0x17 }, // first choice - no protocol
    new byte[] { 0, 0, 0, 0 }, // second choice - protocol 1
    new byte[] { 0, 0, 0, 1 }, // third choice - no protocol
    new byte[] { 0, 0, 0, 0 }, // fourth choice - no protocol
    new byte[] { 0, 0, 0, 0 });
    channel.writeInbound(input);
    // Then
    assertEquals(1, channel.outboundMessages().size());
    assertByteBufEquals(Unpooled.buffer().writeInt(1), channel.readOutbound());
    assertThrows(NoSuchElementException.class, () -> channel.pipeline().remove(ProtocolHandshaker.class));
    assertTrue(channel.isActive());
    verify(protocol).install();
}
Also used : BoltProtocol(org.neo4j.bolt.BoltProtocol) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) BoltProtocolFactory(org.neo4j.bolt.transport.BoltProtocolFactory) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with BoltProtocolFactory

use of org.neo4j.bolt.transport.BoltProtocolFactory in project neo4j by neo4j.

the class ProtocolHandshakerTest method shouldHandleHandshakeFollowedImmediatelyByMessage.

@Test
void shouldHandleHandshakeFollowedImmediatelyByMessage() {
    // Given
    BoltProtocol protocol = newBoltProtocol(1, 0);
    BoltProtocolFactory handlerFactory = newProtocolFactory(1, 0, protocol);
    var memoryTracker = mock(MemoryTracker.class, RETURNS_MOCKS);
    EmbeddedChannel channel = new EmbeddedChannel(new ProtocolHandshaker(handlerFactory, boltChannel, logProvider, false, true, mock(ChannelProtector.class), memoryTracker));
    // When
    ByteBuf input = // create handshake data
    Unpooled.wrappedBuffer(// preamble
    new byte[] { (byte) 0x60, (byte) 0x60, (byte) 0xB0, (byte) 0x17 }, // first choice - no protocol
    new byte[] { 0, 0, 0, 0 }, // second choice - protocol 1
    new byte[] { 0, 0, 0, 1 }, // third choice - no protocol
    new byte[] { 0, 0, 0, 0 }, // fourth choice - no protocol
    new byte[] { 0, 0, 0, 0 }, // this is a message
    new byte[] { 1, 2, 3, 4 });
    channel.writeInbound(input);
    // Then
    assertEquals(1, channel.outboundMessages().size());
    assertByteBufEquals(Unpooled.buffer().writeInt(1), channel.readOutbound());
    assertEquals(1, channel.inboundMessages().size());
    assertByteBufEquals(Unpooled.wrappedBuffer(new byte[] { 1, 2, 3, 4 }), channel.readInbound());
    assertThrows(NoSuchElementException.class, () -> channel.pipeline().remove(ProtocolHandshaker.class));
    assertTrue(channel.isActive());
    verify(protocol).install();
}
Also used : BoltProtocol(org.neo4j.bolt.BoltProtocol) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) BoltProtocolFactory(org.neo4j.bolt.transport.BoltProtocolFactory) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with BoltProtocolFactory

use of org.neo4j.bolt.transport.BoltProtocolFactory in project neo4j by neo4j.

the class ProtocolHandshakerTest method shouldFailOutOfRangeProtocol.

@ParameterizedTest
@MethodSource("protocolVersionProviderOutOfRange")
void shouldFailOutOfRangeProtocol(int major, int minor) {
    // Given
    int noVersion = 0;
    BoltProtocol protocol = newBoltProtocol(major, minor);
    BoltProtocolFactory handlerFactory = newProtocolFactory(major, minor, protocol);
    var memoryTracker = mock(MemoryTracker.class);
    var scopedTracker = mock(MemoryTracker.class);
    when(memoryTracker.getScopedMemoryTracker()).thenReturn(scopedTracker);
    EmbeddedChannel channel = new EmbeddedChannel(new ProtocolHandshaker(handlerFactory, boltChannel, logProvider, false, true, mock(ChannelProtector.class), memoryTracker));
    // When
    ByteBuf input = // create handshake data
    Unpooled.wrappedBuffer(// preamble
    new byte[] { (byte) 0x60, (byte) 0x60, (byte) 0xB0, (byte) 0x17 }, // first choice - 4.0 no range
    new byte[] { 0, 0, 0, 4 }, // second choice - 4.4 -> 4.2 inclusive range of 2
    new byte[] { 0, 2, 4, 4 }, // No protocol
    new byte[] { 0, 0, 0, 0 }, // No protocol
    new byte[] { 0, 0, 0, 0 });
    channel.writeInbound(input);
    // Then
    assertEquals(1, channel.outboundMessages().size());
    assertByteBufEquals(Unpooled.buffer().writeInt(noVersion), channel.readOutbound());
    assertThrows(NoSuchElementException.class, () -> channel.pipeline().remove(ProtocolHandshaker.class));
    assertFalse(channel.isActive());
}
Also used : BoltProtocol(org.neo4j.bolt.BoltProtocol) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) BoltProtocolFactory(org.neo4j.bolt.transport.BoltProtocolFactory) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 4 with BoltProtocolFactory

use of org.neo4j.bolt.transport.BoltProtocolFactory in project neo4j by neo4j.

the class ProtocolHandshakerTest method shouldHandleProtocolRanges.

@ParameterizedTest
@MethodSource("protocolVersionProviderInRange")
void shouldHandleProtocolRanges(int major, int minor) {
    // Given
    int packedVersion = new BoltProtocolVersion(major, minor).toInt();
    BoltProtocol protocol = newBoltProtocol(major, minor);
    BoltProtocolFactory handlerFactory = newProtocolFactory(major, minor, protocol);
    var memoryTracker = mock(MemoryTracker.class, RETURNS_MOCKS);
    EmbeddedChannel channel = new EmbeddedChannel(new ProtocolHandshaker(handlerFactory, boltChannel, logProvider, false, true, mock(ChannelProtector.class), memoryTracker));
    // When
    ByteBuf input = // create handshake data
    Unpooled.wrappedBuffer(// preamble
    new byte[] { (byte) 0x60, (byte) 0x60, (byte) 0xB0, (byte) 0x17 }, // first choice - 4.0 no range
    new byte[] { 0, 0, 0, 4 }, // second choice - 4.3 -> 4.1 inclusive range of 2
    new byte[] { 0, 2, 3, 4 }, // No protocol
    new byte[] { 0, 0, 0, 0 }, // No protocol
    new byte[] { 0, 0, 0, 0 });
    channel.writeInbound(input);
    // Then
    assertEquals(1, channel.outboundMessages().size());
    assertByteBufEquals(Unpooled.buffer().writeInt(packedVersion), channel.readOutbound());
    assertThrows(NoSuchElementException.class, () -> channel.pipeline().remove(ProtocolHandshaker.class));
    assertTrue(channel.isActive());
}
Also used : BoltProtocol(org.neo4j.bolt.BoltProtocol) BoltProtocolVersion(org.neo4j.bolt.BoltProtocolVersion) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) BoltProtocolFactory(org.neo4j.bolt.transport.BoltProtocolFactory) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 5 with BoltProtocolFactory

use of org.neo4j.bolt.transport.BoltProtocolFactory in project neo4j by neo4j.

the class ProtocolHandshakerTest method shouldHandleFragmentedMessage.

@Test
void shouldHandleFragmentedMessage() {
    // Given
    BoltProtocol protocol = newBoltProtocol(1, 0);
    BoltProtocolFactory handlerFactory = newProtocolFactory(1, 0, protocol);
    var memoryTracker = mock(MemoryTracker.class, RETURNS_MOCKS);
    EmbeddedChannel channel = new EmbeddedChannel(new ProtocolHandshaker(handlerFactory, boltChannel, logProvider, false, true, mock(ChannelProtector.class), memoryTracker));
    // When
    channel.writeInbound(Unpooled.wrappedBuffer(new byte[] { (byte) 0x60, (byte) 0x60, (byte) 0xB0 }));
    assertEquals(0, channel.outboundMessages().size());
    channel.writeInbound(Unpooled.wrappedBuffer(new byte[] { (byte) 0x17, 0, 0, 0 }));
    assertEquals(0, channel.outboundMessages().size());
    channel.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0, 0 }));
    assertEquals(0, channel.outboundMessages().size());
    channel.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 1, 0, 0, 0 }));
    assertEquals(0, channel.outboundMessages().size());
    channel.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0, 0 }));
    assertEquals(0, channel.outboundMessages().size());
    channel.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0 }));
    // Then
    assertEquals(1, channel.outboundMessages().size());
    assertByteBufEquals(Unpooled.buffer().writeInt(1), channel.readOutbound());
    assertThrows(NoSuchElementException.class, () -> channel.pipeline().remove(ProtocolHandshaker.class));
    assertTrue(channel.isActive());
    verify(protocol).install();
}
Also used : BoltProtocol(org.neo4j.bolt.BoltProtocol) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) BoltProtocolFactory(org.neo4j.bolt.transport.BoltProtocolFactory) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

BoltProtocolFactory (org.neo4j.bolt.transport.BoltProtocolFactory)11 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 BoltProtocol (org.neo4j.bolt.BoltProtocol)10 ByteBuf (io.netty.buffer.ByteBuf)8 Test (org.junit.jupiter.api.Test)8 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 BoltProtocolVersion (org.neo4j.bolt.BoltProtocolVersion)2 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)1 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)1 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 BoltConnectionFactory (org.neo4j.bolt.runtime.BoltConnectionFactory)1 DefaultBoltConnectionFactory (org.neo4j.bolt.runtime.DefaultBoltConnectionFactory)1 BoltSchedulerProvider (org.neo4j.bolt.runtime.scheduling.BoltSchedulerProvider)1 CachedThreadPoolExecutorFactory (org.neo4j.bolt.runtime.scheduling.CachedThreadPoolExecutorFactory)1 ExecutorBoltSchedulerProvider (org.neo4j.bolt.runtime.scheduling.ExecutorBoltSchedulerProvider)1 NettyThreadFactory (org.neo4j.bolt.runtime.scheduling.NettyThreadFactory)1 BoltStateMachineFactory (org.neo4j.bolt.runtime.statemachine.BoltStateMachineFactory)1 BoltMemoryPool (org.neo4j.bolt.transport.BoltMemoryPool)1