Skip to content

Commit 45c76a8

Browse files
committed
Handle executors better in tests
1 parent 24e87be commit 45c76a8

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

driver/src/test/java/org/neo4j/driver/v1/integration/SessionIT.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.concurrent.CountDownLatch;
3232
import java.util.concurrent.ExecutionException;
3333
import java.util.concurrent.ExecutorService;
34+
import java.util.concurrent.Executors;
3435
import java.util.concurrent.Future;
3536
import java.util.concurrent.TimeUnit;
3637
import java.util.concurrent.atomic.AtomicInteger;
@@ -62,7 +63,6 @@
6263
import org.neo4j.driver.v1.util.TestNeo4j;
6364

6465
import static java.lang.String.format;
65-
import static java.util.concurrent.Executors.newSingleThreadExecutor;
6666
import static org.hamcrest.CoreMatchers.containsString;
6767
import static org.hamcrest.CoreMatchers.equalTo;
6868
import static org.hamcrest.CoreMatchers.instanceOf;
@@ -98,6 +98,7 @@ public class SessionIT
9898
public ExpectedException exception = ExpectedException.none();
9999

100100
private Driver driver;
101+
private ExecutorService executor;
101102

102103
@After
103104
public void tearDown()
@@ -106,6 +107,10 @@ public void tearDown()
106107
{
107108
driver.close();
108109
}
110+
if ( executor != null )
111+
{
112+
executor.shutdownNow();
113+
}
109114
}
110115

111116
@Test
@@ -1451,9 +1456,12 @@ private static void assertDeadlockDetectedError( ExecutionException e )
14511456
assertEquals( "Neo.TransientError.Transaction.DeadlockDetected", errorCode );
14521457
}
14531458

1454-
private static <T> Future<T> executeInDifferentThread( Callable<T> callable )
1459+
private <T> Future<T> executeInDifferentThread( Callable<T> callable )
14551460
{
1456-
ExecutorService executor = newSingleThreadExecutor( daemon( "test-thread-" ) );
1461+
if ( executor == null )
1462+
{
1463+
executor = Executors.newCachedThreadPool( daemon( getClass().getSimpleName() + "-thread-" ) );
1464+
}
14571465
return executor.submit( callable );
14581466
}
14591467

@@ -1470,7 +1478,7 @@ private static void await( CountDownLatch latch )
14701478
}
14711479
}
14721480

1473-
private static abstract class NodeIdUpdater
1481+
private abstract class NodeIdUpdater
14741482
{
14751483
final Future<Void> update( final Driver driver, final int nodeId, final int newNodeId,
14761484
final AtomicReference<Session> usedSessionRef, final CountDownLatch latchToWait )

driver/src/test/java/org/neo4j/driver/v1/stress/SessionPoolingStressIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import java.util.concurrent.atomic.AtomicBoolean;
3636
import java.util.concurrent.atomic.AtomicReference;
3737

38-
import org.neo4j.driver.v1.AuthTokens;
3938
import org.neo4j.driver.v1.Config;
4039
import org.neo4j.driver.v1.Driver;
4140
import org.neo4j.driver.v1.Session;
@@ -45,6 +44,7 @@
4544
import static java.util.Arrays.asList;
4645
import static org.junit.Assert.assertTrue;
4746
import static org.neo4j.driver.v1.GraphDatabase.driver;
47+
import static org.neo4j.driver.v1.util.DaemonThreadFactory.daemon;
4848

4949
public class SessionPoolingStressIT
5050
{
@@ -87,7 +87,7 @@ protected void failed( Throwable e, Description description )
8787
@Before
8888
public void setUp() throws Exception
8989
{
90-
executor = Executors.newFixedThreadPool( N_THREADS );
90+
executor = Executors.newFixedThreadPool( N_THREADS, daemon( getClass().getSimpleName() + "-thread-" ) );
9191
}
9292

9393
@After
@@ -122,7 +122,7 @@ public void shouldWorkFine() throws Throwable
122122

123123
stop.set( true );
124124
executor.shutdown();
125-
assertTrue( executor.awaitTermination( 20, TimeUnit.SECONDS ) );
125+
assertTrue( executor.awaitTermination( 30, TimeUnit.SECONDS ) );
126126

127127
Throwable failure = failureReference.get();
128128
if ( failure != null )

0 commit comments

Comments
 (0)