Search in sources :

Example 21 with BoltConnection

use of org.neo4j.bolt.runtime.BoltConnection in project neo4j by neo4j.

the class ExecutorBoltSchedulerConcurrencyTest method shouldInvokeHandleSchedulingErrorIfNoThreadsAvailable.

@Test
void shouldInvokeHandleSchedulingErrorIfNoThreadsAvailable() throws Throwable {
    AtomicInteger handleSchedulingErrorCounter = new AtomicInteger(0);
    BoltConnection newConnection = newConnection(UUID.randomUUID().toString());
    doAnswer(newCountingAnswer(handleSchedulingErrorCounter)).when(newConnection).handleSchedulingError(any());
    blockAllThreads();
    // register connection
    boltScheduler.created(newConnection);
    // send a job and wait for it to enter handleSchedulingError and block there
    CompletableFuture.runAsync(() -> boltScheduler.enqueued(newConnection, Jobs.noop()));
    Predicates.awaitForever(() -> handleSchedulingErrorCounter.get() > 0, 500, MILLISECONDS);
    // verify that handleSchedulingError is called once
    assertEquals(1, handleSchedulingErrorCounter.get());
    // allow all threads to complete
    afterExecuteEvent.countDown();
    afterExecuteBarrier.await();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BoltConnection(org.neo4j.bolt.runtime.BoltConnection) Test(org.junit.jupiter.api.Test)

Example 22 with BoltConnection

use of org.neo4j.bolt.runtime.BoltConnection in project neo4j by neo4j.

the class ExecutorBoltSchedulerConcurrencyTest method blockAllThreads.

private void blockAllThreads() throws InterruptedException {
    for (int i = 0; i < maxPoolSize; i++) {
        BoltConnection connection = newConnection(UUID.randomUUID().toString());
        boltScheduler.created(connection);
        boltScheduler.enqueued(connection, Jobs.noop());
    }
    beforeExecuteEvent.countDown();
    beforeExecuteBarrier.await();
}
Also used : BoltConnection(org.neo4j.bolt.runtime.BoltConnection)

Example 23 with BoltConnection

use of org.neo4j.bolt.runtime.BoltConnection in project neo4j by neo4j.

the class ExecutorBoltSchedulerConcurrencyTest method shouldNotScheduleNewJobIfHandlingSchedulingError.

@Test
void shouldNotScheduleNewJobIfHandlingSchedulingError() throws Throwable {
    AtomicInteger handleSchedulingErrorCounter = new AtomicInteger(0);
    AtomicBoolean exitCondition = new AtomicBoolean();
    BoltConnection newConnection = newConnection(UUID.randomUUID().toString());
    doAnswer(newBlockingAnswer(handleSchedulingErrorCounter, exitCondition)).when(newConnection).handleSchedulingError(any());
    blockAllThreads();
    // register connection
    boltScheduler.created(newConnection);
    // send a job and wait for it to enter handleSchedulingError and block there
    CompletableFuture.runAsync(() -> boltScheduler.enqueued(newConnection, Jobs.noop()));
    Predicates.awaitForever(() -> handleSchedulingErrorCounter.get() > 0, 500, MILLISECONDS);
    // allow all threads to complete
    afterExecuteEvent.countDown();
    afterExecuteBarrier.await();
    // post a job
    boltScheduler.enqueued(newConnection, Jobs.noop());
    // exit handleSchedulingError
    exitCondition.set(true);
    // verify that handleSchedulingError is called once and processNextBatch never.
    assertEquals(1, handleSchedulingErrorCounter.get());
    verify(newConnection, never()).processNextBatch();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BoltConnection(org.neo4j.bolt.runtime.BoltConnection) Test(org.junit.jupiter.api.Test)

Example 24 with BoltConnection

use of org.neo4j.bolt.runtime.BoltConnection in project neo4j by neo4j.

the class BoltRequestMessageReaderTest method shouldHandleErrorThatCausesFailureMessage.

@Test
void shouldHandleErrorThatCausesFailureMessage() throws Exception {
    Unpacker unpacker = mock(Unpacker.class);
    BoltIOException error = new BoltIOException(Status.General.UnknownError, "Hello");
    when(unpacker.unpackStructHeader()).thenThrow(error);
    BoltStateMachine stateMachine = mock(BoltStateMachine.class);
    BoltConnection connection = new SynchronousBoltConnection(stateMachine);
    BoltResponseHandler externalErrorResponseHandler = responseHandlerMock();
    BoltRequestMessageReader reader = new TestBoltRequestMessageReader(connection, externalErrorResponseHandler, emptyList(), mock(ChannelProtector.class));
    reader.read(unpacker);
    verify(stateMachine).handleExternalFailure(Neo4jError.from(error), externalErrorResponseHandler);
}
Also used : ChannelProtector(org.neo4j.bolt.transport.pipeline.ChannelProtector) BoltStateMachine(org.neo4j.bolt.runtime.statemachine.BoltStateMachine) BoltConnection(org.neo4j.bolt.runtime.BoltConnection) SynchronousBoltConnection(org.neo4j.bolt.runtime.SynchronousBoltConnection) SynchronousBoltConnection(org.neo4j.bolt.runtime.SynchronousBoltConnection) BoltResponseHandler(org.neo4j.bolt.runtime.BoltResponseHandler) Unpacker(org.neo4j.bolt.packstream.Neo4jPack.Unpacker) Test(org.junit.jupiter.api.Test)

Example 25 with BoltConnection

use of org.neo4j.bolt.runtime.BoltConnection in project neo4j by neo4j.

the class BoltRequestMessageReaderTest method shouldDecodeKnownMessage.

@Test
void shouldDecodeKnownMessage() throws Exception {
    Unpacker unpacker = mock(Unpacker.class);
    when(unpacker.unpackStructSignature()).thenReturn('a');
    RequestMessage message = mock(RequestMessage.class);
    BoltResponseHandler responseHandler = responseHandlerMock();
    RequestMessageDecoder decoder = new TestRequestMessageDecoder('a', responseHandler, message);
    BoltStateMachine stateMachine = mock(BoltStateMachine.class);
    BoltConnection connection = new SynchronousBoltConnection(stateMachine);
    BoltRequestMessageReader reader = new TestBoltRequestMessageReader(connection, responseHandlerMock(), singletonList(decoder), mock(ChannelProtector.class));
    reader.read(unpacker);
    verify(stateMachine).process(message, responseHandler);
}
Also used : ChannelProtector(org.neo4j.bolt.transport.pipeline.ChannelProtector) BoltStateMachine(org.neo4j.bolt.runtime.statemachine.BoltStateMachine) BoltConnection(org.neo4j.bolt.runtime.BoltConnection) SynchronousBoltConnection(org.neo4j.bolt.runtime.SynchronousBoltConnection) SynchronousBoltConnection(org.neo4j.bolt.runtime.SynchronousBoltConnection) BoltResponseHandler(org.neo4j.bolt.runtime.BoltResponseHandler) Unpacker(org.neo4j.bolt.packstream.Neo4jPack.Unpacker) Test(org.junit.jupiter.api.Test)

Aggregations

BoltConnection (org.neo4j.bolt.runtime.BoltConnection)31 Test (org.junit.jupiter.api.Test)24 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 InetSocketAddress (java.net.InetSocketAddress)3 BoltResponseMessageWriter (org.neo4j.bolt.messaging.BoltResponseMessageWriter)3 SynchronousBoltConnection (org.neo4j.bolt.runtime.SynchronousBoltConnection)3 BoltStateMachine (org.neo4j.bolt.runtime.statemachine.BoltStateMachine)3 ChannelProtector (org.neo4j.bolt.transport.pipeline.ChannelProtector)3 AssertableLogProvider (org.neo4j.logging.AssertableLogProvider)3 Bootstrap (io.netty.bootstrap.Bootstrap)2 Channel (io.netty.channel.Channel)2 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelInboundHandler (io.netty.channel.ChannelInboundHandler)2 SocketChannel (io.netty.channel.socket.SocketChannel)2 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)2 ServerSocket (java.net.ServerSocket)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2