Search in sources :

Example 1 with BoltProtocolVersion

use of org.neo4j.bolt.BoltProtocolVersion in project neo4j by neo4j.

the class BoltStateMachineFactoryImplTest method shouldCreateBoltStateMachinesV41.

@Test
void shouldCreateBoltStateMachinesV41() {
    BoltStateMachineFactoryImpl factory = newBoltFactory();
    var memoryTracker = mock(MemoryTracker.class, RETURNS_MOCKS);
    BoltStateMachine boltStateMachine = factory.newStateMachine(new BoltProtocolVersion(4, 1), CHANNEL, MapValue.EMPTY, memoryTracker);
    assertNotNull(boltStateMachine);
    assertThat(boltStateMachine).isInstanceOf(BoltStateMachineV41.class);
    verify(memoryTracker).getScopedMemoryTracker();
    verify(memoryTracker, times(3)).allocateHeap(anyLong());
    verifyNoMoreInteractions(memoryTracker);
}
Also used : BoltStateMachine(org.neo4j.bolt.runtime.statemachine.BoltStateMachine) BoltProtocolVersion(org.neo4j.bolt.BoltProtocolVersion) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with BoltProtocolVersion

use of org.neo4j.bolt.BoltProtocolVersion in project neo4j by neo4j.

the class BoltStateMachineFactoryImplTest method shouldCreateBoltStateMachinesV4.

@Test
void shouldCreateBoltStateMachinesV4() {
    BoltStateMachineFactoryImpl factory = newBoltFactory();
    var memoryTracker = mock(MemoryTracker.class, RETURNS_MOCKS);
    BoltStateMachine boltStateMachine = factory.newStateMachine(new BoltProtocolVersion(4, 0), CHANNEL, MapValue.EMPTY, memoryTracker);
    assertNotNull(boltStateMachine);
    assertThat(boltStateMachine).isInstanceOf(BoltStateMachineV4.class);
    verify(memoryTracker).getScopedMemoryTracker();
    verify(memoryTracker, times(3)).allocateHeap(anyLong());
    verifyNoMoreInteractions(memoryTracker);
}
Also used : BoltStateMachine(org.neo4j.bolt.runtime.statemachine.BoltStateMachine) BoltProtocolVersion(org.neo4j.bolt.BoltProtocolVersion) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with BoltProtocolVersion

use of org.neo4j.bolt.BoltProtocolVersion in project neo4j by neo4j.

the class BoltStateMachineFactoryImplTest method shouldThrowExceptionIfVersionIsUnknown.

@ParameterizedTest(name = "V{0}")
@ValueSource(ints = { 999, -1, 1, 2 })
void shouldThrowExceptionIfVersionIsUnknown(int protocolVersion) {
    BoltStateMachineFactoryImpl factory = newBoltFactory();
    BoltProtocolVersion boltProtocolVersion = new BoltProtocolVersion(protocolVersion, 0);
    var memoryTracker = mock(MemoryTracker.class);
    IllegalArgumentException error = assertThrows(IllegalArgumentException.class, () -> factory.newStateMachine(boltProtocolVersion, CHANNEL, MapValue.EMPTY, memoryTracker));
    assertThat(error.getMessage()).startsWith("Failed to create a state machine for protocol version");
    verifyNoMoreInteractions(memoryTracker);
}
Also used : BoltProtocolVersion(org.neo4j.bolt.BoltProtocolVersion) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with BoltProtocolVersion

use of org.neo4j.bolt.BoltProtocolVersion in project neo4j by neo4j.

the class BoltStateMachineFactoryImplTest method shouldCreateBoltStateMachinesV43.

@Test
void shouldCreateBoltStateMachinesV43() {
    BoltStateMachineFactoryImpl factory = newBoltFactory();
    var memoryTracker = mock(MemoryTracker.class, RETURNS_MOCKS);
    BoltStateMachine boltStateMachine = factory.newStateMachine(new BoltProtocolVersion(4, 3), CHANNEL, MapValue.EMPTY, memoryTracker);
    assertNotNull(boltStateMachine);
    assertThat(boltStateMachine).isInstanceOf(BoltStateMachineV43.class);
    verify(memoryTracker).getScopedMemoryTracker();
    verify(memoryTracker, times(3)).allocateHeap(anyLong());
    verifyNoMoreInteractions(memoryTracker);
}
Also used : BoltStateMachine(org.neo4j.bolt.runtime.statemachine.BoltStateMachine) BoltProtocolVersion(org.neo4j.bolt.BoltProtocolVersion) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with BoltProtocolVersion

use of org.neo4j.bolt.BoltProtocolVersion in project neo4j by neo4j.

the class ProtocolHandshaker method performHandshake.

private boolean performHandshake() {
    try (var handshakeMemoryTracker = memoryTracker.getScopedMemoryTracker()) {
        ArrayList<BoltProtocolVersion> suggestions = new ArrayList<BoltProtocolVersion>();
        for (int i = 0; i < 4; i++) {
            int rawBytes = handshakeBuffer.getInt((i + 1) * Integer.BYTES);
            int major = BoltProtocolVersion.getMajorFromRawBytes(rawBytes);
            int minor = BoltProtocolVersion.getMinorFromRawBytes(rawBytes);
            int range = BoltProtocolVersion.getRangeFromRawBytes(rawBytes);
            handshakeMemoryTracker.allocateHeap(BoltProtocolVersion.SHALLOW_SIZE * (range + 1));
            for (// Range is inclusive thus the use of <=
            int j = 0; // Range is inclusive thus the use of <=
            j <= range; // Range is inclusive thus the use of <=
            j++) {
                int newMinor = Math.max(minor - j, 0);
                BoltProtocolVersion suggestion = new BoltProtocolVersion(major, newMinor);
                protocol = boltProtocolFactory.create(suggestion, boltChannel, channelProtector, memoryTracker);
                if (protocol != null) {
                    break;
                }
                suggestions.add(suggestion);
            }
            if (protocol != null) {
                break;
            }
        }
        if (protocol == null) {
            log.debug("Failed Bolt handshake: Bolt versions suggested by client '%s' are not supported by this server.", Arrays.toString(suggestions.toArray()));
        }
    }
    return protocol != null;
}
Also used : BoltProtocolVersion(org.neo4j.bolt.BoltProtocolVersion) ArrayList(java.util.ArrayList)

Aggregations

BoltProtocolVersion (org.neo4j.bolt.BoltProtocolVersion)17 Test (org.junit.jupiter.api.Test)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 BoltStateMachine (org.neo4j.bolt.runtime.statemachine.BoltStateMachine)6 BoltProtocol (org.neo4j.bolt.BoltProtocol)5 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)3 ByteBuf (io.netty.buffer.ByteBuf)2 BoltChannel (org.neo4j.bolt.BoltChannel)2 BoltConnectionFactory (org.neo4j.bolt.runtime.BoltConnectionFactory)2 BoltStateMachineFactory (org.neo4j.bolt.runtime.statemachine.BoltStateMachineFactory)2 BoltTestUtil.newTestBoltChannel (org.neo4j.bolt.testing.BoltTestUtil.newTestBoltChannel)2 BoltProtocolFactory (org.neo4j.bolt.transport.BoltProtocolFactory)2 TestDatabaseIdRepository (org.neo4j.kernel.database.TestDatabaseIdRepository)2 ArrayList (java.util.ArrayList)1 CsvSource (org.junit.jupiter.params.provider.CsvSource)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1 ValueSource (org.junit.jupiter.params.provider.ValueSource)1 BoltResponseMessageWriter (org.neo4j.bolt.messaging.BoltResponseMessageWriter)1 BoltConnection (org.neo4j.bolt.runtime.BoltConnection)1 ChannelProtector (org.neo4j.bolt.transport.pipeline.ChannelProtector)1