Skip to content

Commit

Permalink
Fix flaky test in ExecutorBoltSchedulerTest
Browse files Browse the repository at this point in the history
Fixed the flaky test in `ExecutorBoltSchedulerTest#createdWorkerThreadsShouldContainConnectorName` due to thread race condition.
The test requires `ExecutorBoltScheduler#executeBatch` to be finished before the test verification code.
While before the test could only ensure `BoltConnection#processNextBatch` finishes.
```
try
{
    return connection.processNextBatch(); // before the test only ensure this line finished
}
finally
{
    currentThread.setName( originalName ); // we need this to be also finished too
}
```
  • Loading branch information
Zhen Li committed Apr 11, 2018
1 parent 88f317f commit bfaea9c
Showing 1 changed file with 7 additions and 5 deletions.
Expand Up @@ -36,8 +36,6 @@

import org.neo4j.bolt.BoltKernelExtension;
import org.neo4j.bolt.testing.Jobs;
import org.neo4j.bolt.v1.runtime.BoltStateMachine;
import org.neo4j.bolt.v1.runtime.Job;
import org.neo4j.function.Predicates;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.logging.LogService;
Expand Down Expand Up @@ -260,25 +258,29 @@ public void destroyedShouldCancelActiveWorkItem() throws Throwable
@Test
public void createdWorkerThreadsShouldContainConnectorName() throws Exception
{
AtomicInteger processNextBatchCount = new AtomicInteger();
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() );
processNextBatchCount.incrementAndGet();
return true;
} );

boltScheduler.start();
boltScheduler.created( connection );
boltScheduler.enqueued( connection, Jobs.noop() );

Predicates.await( () -> processNextBatchCount.get() > 0, 1, MINUTES );
Predicates.await( () -> executeBatchCompletionCount.get() > 0, 1, MINUTES );

assertThat( poolThread.get().getName(), not( equalTo( poolThreadName.get() ) ) );
assertThat( poolThread.get().getName(), containsString( String.format( "[%s]", CONNECTOR_KEY ) ) );
Expand Down

0 comments on commit bfaea9c

Please sign in to comment.