Search in sources :

Example 1 with BoltConnection

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

the class ExecutorBoltSchedulerWithQueueTest 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());
    submitWork(threadPoolSize + queueSize);
    // register connection
    boltSchedulerWithQueue.created(newConnection);
    // send a job and wait for it to enter handleSchedulingError and block there
    CompletableFuture.runAsync(() -> boltSchedulerWithQueue.enqueued(newConnection, Jobs.noop()));
    Predicates.awaitForever(() -> handleSchedulingErrorCounter.get() > 0, 500, MILLISECONDS);
    // allow all threads to complete
    afterExecuteEvent.countDown();
    afterExecuteBarrier.await();
    // post a job
    boltSchedulerWithQueue.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 2 with BoltConnection

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

the class ExecutorBoltSchedulerWithQueueTest method newConnection.

private BoltConnection newConnection(String id) {
    BoltConnection result = mock(BoltConnection.class);
    when(result.id()).thenReturn(id);
    when(result.remoteAddress()).thenReturn(new InetSocketAddress("localhost", 32_000));
    return result;
}
Also used : BoltConnection(org.neo4j.bolt.runtime.BoltConnection) InetSocketAddress(java.net.InetSocketAddress)

Example 3 with BoltConnection

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

the class ExecutorBoltSchedulerTest method createdShouldAddConnectionToActiveConnections.

@Test
void createdShouldAddConnectionToActiveConnections() throws Throwable {
    String id = UUID.randomUUID().toString();
    BoltConnection connection = newConnection(id);
    boltScheduler.init();
    boltScheduler.start();
    boltScheduler.created(connection);
    verify(connection).id();
    assertTrue(boltScheduler.isRegistered(connection));
}
Also used : BoltConnection(org.neo4j.bolt.runtime.BoltConnection) Test(org.junit.jupiter.api.Test)

Example 4 with BoltConnection

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

the class ExecutorBoltSchedulerTest method destroyedShouldCancelActiveWorkItem.

@Test
void destroyedShouldCancelActiveWorkItem() throws Throwable {
    AtomicInteger processNextBatchCount = new AtomicInteger();
    String id = UUID.randomUUID().toString();
    BoltConnection connection = newConnection(id);
    AtomicBoolean exitCondition = new AtomicBoolean();
    when(connection.processNextBatch()).thenAnswer(inv -> {
        processNextBatchCount.incrementAndGet();
        return awaitExit(exitCondition);
    });
    boltScheduler.init();
    boltScheduler.start();
    boltScheduler.created(connection);
    boltScheduler.enqueued(connection, Jobs.noop());
    Predicates.await(() -> processNextBatchCount.get() > 0, 1, MINUTES);
    boltScheduler.closed(connection);
    Predicates.await(() -> !boltScheduler.isActive(connection), 1, MINUTES);
    assertFalse(boltScheduler.isActive(connection));
    assertEquals(1, processNextBatchCount.get());
    exitCondition.set(true);
}
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 5 with BoltConnection

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

the class ExecutorBoltSchedulerTest method createdWorkerThreadsShouldContainConnectorName.

@Test
void createdWorkerThreadsShouldContainConnectorName() throws Exception {
    AtomicInteger executeBatchCompletionCount = new AtomicInteger();
    AtomicReference<Thread> poolThread = new AtomicReference<>();
    AtomicReference<String> poolThreadName = new AtomicReference<>();
    String id = UUID.randomUUID().toString();
    BoltConnection connection = newConnection(id);
    when(connection.hasPendingJobs()).thenAnswer(inv -> {
        executeBatchCompletionCount.incrementAndGet();
        return false;
    });
    when(connection.processNextBatch()).thenAnswer(inv -> {
        poolThread.set(Thread.currentThread());
        poolThreadName.set(Thread.currentThread().getName());
        return true;
    });
    boltScheduler.init();
    boltScheduler.start();
    boltScheduler.created(connection);
    boltScheduler.enqueued(connection, Jobs.noop());
    Predicates.await(() -> executeBatchCompletionCount.get() > 0, 1, MINUTES);
    assertThat(poolThread.get().getName()).isNotEqualTo(poolThreadName.get());
    assertThat(poolThread.get().getName()).contains(String.format("[%s]", CONNECTOR_KEY));
    assertThat(poolThread.get().getName()).doesNotContain(String.format("[%s]", connection.remoteAddress()));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BoltConnection(org.neo4j.bolt.runtime.BoltConnection) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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