Skip to content

Commit 12f6c3a

Browse files
committed
No unbounded Future#get() in tests
Production code uses unbounded `Futures#blockingGet()` that ignores interrupts and waits until task completes. This is not desired in tests because we do not expect async calls to take long time there. Commit makes all tests use bounded wait from `TestUtil` class instead.
1 parent 0698fcb commit 12f6c3a

22 files changed

+209
-213
lines changed

driver/src/main/java/org/neo4j/driver/internal/DriverFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ public final Driver newInstance( URI uri, AuthToken authToken, RoutingSettings r
7979
{
8080
InternalDriver driver = createDriver( uri, address, connectionPool, config, newRoutingSettings,
8181
eventExecutorGroup, securityPlan, retryLogic );
82-
Futures.getBlocking( driver.verifyConnectivity() );
82+
Futures.blockingGet( driver.verifyConnectivity() );
8383
return driver;
8484
}
8585
catch ( Throwable driverError )
8686
{
8787
// we need to close the connection pool if driver creation threw exception
8888
try
8989
{
90-
Futures.getBlocking( connectionPool.close() );
90+
Futures.blockingGet( connectionPool.close() );
9191
}
9292
catch ( Throwable closeError )
9393
{

driver/src/main/java/org/neo4j/driver/internal/ExplicitTransaction.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@
4848

4949
import static java.util.Collections.emptyMap;
5050
import static java.util.concurrent.CompletableFuture.completedFuture;
51+
import static org.neo4j.driver.internal.util.Futures.blockingGet;
5152
import static org.neo4j.driver.internal.util.Futures.completionErrorCause;
5253
import static org.neo4j.driver.internal.util.Futures.failedFuture;
53-
import static org.neo4j.driver.internal.util.Futures.getBlocking;
5454
import static org.neo4j.driver.v1.Values.value;
5555

5656
public class ExplicitTransaction implements Transaction
@@ -150,7 +150,7 @@ public void failure()
150150
@Override
151151
public void close()
152152
{
153-
getBlocking( closeAsync() );
153+
blockingGet( closeAsync() );
154154
}
155155

156156
CompletionStage<Void> closeAsync()
@@ -274,7 +274,7 @@ public CompletionStage<StatementResultCursor> runAsync( String statementTemplate
274274
@Override
275275
public StatementResult run( Statement statement )
276276
{
277-
StatementResultCursor cursor = getBlocking( run( statement, false ) );
277+
StatementResultCursor cursor = blockingGet( run( statement, false ) );
278278
return new InternalStatementResult( cursor );
279279
}
280280

driver/src/main/java/org/neo4j/driver/internal/InternalDriver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.neo4j.driver.v1.Session;
3030

3131
import static java.util.concurrent.CompletableFuture.completedFuture;
32-
import static org.neo4j.driver.internal.util.Futures.getBlocking;
32+
import static org.neo4j.driver.internal.util.Futures.blockingGet;
3333

3434
public class InternalDriver implements Driver
3535
{
@@ -104,7 +104,7 @@ private Session newSession( AccessMode mode, Bookmark bookmark )
104104
@Override
105105
public void close()
106106
{
107-
getBlocking( closeAsync() );
107+
blockingGet( closeAsync() );
108108
}
109109

110110
@Override

driver/src/main/java/org/neo4j/driver/internal/InternalStatementResult.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import org.neo4j.driver.v1.summary.ResultSummary;
2929
import org.neo4j.driver.v1.util.Function;
3030

31-
import static org.neo4j.driver.internal.util.Futures.getBlocking;
31+
import static org.neo4j.driver.internal.util.Futures.blockingGet;
3232

3333
public class InternalStatementResult implements StatementResult
3434
{
@@ -45,7 +45,7 @@ public List<String> keys()
4545
{
4646
if ( keys == null )
4747
{
48-
getBlocking( cursor.peekAsync() );
48+
blockingGet( cursor.peekAsync() );
4949
keys = cursor.keys();
5050
}
5151
return keys;
@@ -54,13 +54,13 @@ public List<String> keys()
5454
@Override
5555
public boolean hasNext()
5656
{
57-
return getBlocking( cursor.peekAsync() ) != null;
57+
return blockingGet( cursor.peekAsync() ) != null;
5858
}
5959

6060
@Override
6161
public Record next()
6262
{
63-
Record record = getBlocking( cursor.nextAsync() );
63+
Record record = blockingGet( cursor.nextAsync() );
6464
if ( record == null )
6565
{
6666
throw new NoSuchRecordException( "No more records" );
@@ -71,13 +71,13 @@ public Record next()
7171
@Override
7272
public Record single()
7373
{
74-
return getBlocking( cursor.singleAsync() );
74+
return blockingGet( cursor.singleAsync() );
7575
}
7676

7777
@Override
7878
public Record peek()
7979
{
80-
Record record = getBlocking( cursor.peekAsync() );
80+
Record record = blockingGet( cursor.peekAsync() );
8181
if ( record == null )
8282
{
8383
throw new NoSuchRecordException( "Cannot peek past the last record" );
@@ -88,25 +88,25 @@ public Record peek()
8888
@Override
8989
public List<Record> list()
9090
{
91-
return getBlocking( cursor.listAsync() );
91+
return blockingGet( cursor.listAsync() );
9292
}
9393

9494
@Override
9595
public <T> List<T> list( Function<Record, T> mapFunction )
9696
{
97-
return getBlocking( cursor.listAsync( mapFunction ) );
97+
return blockingGet( cursor.listAsync( mapFunction ) );
9898
}
9999

100100
@Override
101101
public ResultSummary consume()
102102
{
103-
return getBlocking( cursor.consumeAsync() );
103+
return blockingGet( cursor.consumeAsync() );
104104
}
105105

106106
@Override
107107
public ResultSummary summary()
108108
{
109-
return getBlocking( cursor.summaryAsync() );
109+
return blockingGet( cursor.summaryAsync() );
110110
}
111111

112112
@Override

driver/src/main/java/org/neo4j/driver/internal/LeakLoggingNetworkSession.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected void finalize() throws Throwable
4646

4747
private void logLeakIfNeeded()
4848
{
49-
Boolean isOpen = Futures.getBlocking( currentConnectionIsOpen() );
49+
Boolean isOpen = Futures.blockingGet( currentConnectionIsOpen() );
5050
if ( isOpen )
5151
{
5252
logger.error( "Neo4j Session object leaked, please ensure that your application" +

driver/src/main/java/org/neo4j/driver/internal/NetworkSession.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
import org.neo4j.driver.v1.types.TypeSystem;
4848

4949
import static java.util.concurrent.CompletableFuture.completedFuture;
50+
import static org.neo4j.driver.internal.util.Futures.blockingGet;
5051
import static org.neo4j.driver.internal.util.Futures.failedFuture;
51-
import static org.neo4j.driver.internal.util.Futures.getBlocking;
5252
import static org.neo4j.driver.v1.Values.value;
5353

5454
public class NetworkSession implements Session
@@ -132,7 +132,7 @@ public CompletionStage<StatementResultCursor> runAsync( String statementText, Va
132132
@Override
133133
public StatementResult run( Statement statement )
134134
{
135-
StatementResultCursor cursor = getBlocking( runAsync( statement, false ) );
135+
StatementResultCursor cursor = blockingGet( runAsync( statement, false ) );
136136
return new InternalStatementResult( cursor );
137137
}
138138

@@ -152,7 +152,7 @@ public boolean isOpen()
152152
@Override
153153
public void close()
154154
{
155-
getBlocking( closeAsync() );
155+
blockingGet( closeAsync() );
156156
}
157157

158158
@Override
@@ -189,7 +189,7 @@ public CompletionStage<Void> closeAsync()
189189
@Override
190190
public Transaction beginTransaction()
191191
{
192-
return getBlocking( beginTransactionAsync( mode ) );
192+
return blockingGet( beginTransactionAsync( mode ) );
193193
}
194194

195195
@Deprecated
@@ -248,7 +248,7 @@ public String lastBookmark()
248248
@Override
249249
public void reset()
250250
{
251-
getBlocking( resetAsync() );
251+
blockingGet( resetAsync() );
252252
}
253253

254254
private CompletionStage<Void> resetAsync()
@@ -288,7 +288,7 @@ private <T> T transaction( AccessMode mode, TransactionWork<T> work )
288288
// event loop thread will bock and wait for itself to read some data
289289
return retryLogic.retry( () ->
290290
{
291-
try ( Transaction tx = getBlocking( beginTransactionAsync( mode ) ) )
291+
try ( Transaction tx = blockingGet( beginTransactionAsync( mode ) ) )
292292
{
293293
try
294294
{

driver/src/main/java/org/neo4j/driver/internal/util/Futures.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static <T> CompletableFuture<T> failedFuture( Throwable error )
7373
return result;
7474
}
7575

76-
public static <V> V getBlocking( CompletionStage<V> stage )
76+
public static <V> V blockingGet( CompletionStage<V> stage )
7777
{
7878
EventLoopGroupFactory.assertNotInEventLoopThread();
7979

driver/src/test/java/org/neo4j/driver/internal/DirectConnectionProviderTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
import static org.mockito.Mockito.mock;
3333
import static org.mockito.Mockito.verify;
3434
import static org.mockito.Mockito.when;
35-
import static org.neo4j.driver.internal.util.Futures.getBlocking;
3635
import static org.neo4j.driver.v1.AccessMode.READ;
3736
import static org.neo4j.driver.v1.AccessMode.WRITE;
37+
import static org.neo4j.driver.v1.util.TestUtil.await;
3838

3939
public class DirectConnectionProviderTest
4040
{
@@ -48,8 +48,8 @@ public void acquiresConnectionsFromThePool()
4848
ConnectionPool pool = poolMock( address, connection1, connection2 );
4949
DirectConnectionProvider provider = new DirectConnectionProvider( address, pool );
5050

51-
assertSame( connection1, getBlocking( provider.acquireConnection( READ ) ) );
52-
assertSame( connection2, getBlocking( provider.acquireConnection( WRITE ) ) );
51+
assertSame( connection1, await( provider.acquireConnection( READ ) ) );
52+
assertSame( connection2, await( provider.acquireConnection( WRITE ) ) );
5353
}
5454

5555
@Test

driver/src/test/java/org/neo4j/driver/internal/ExplicitTransactionTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import static org.mockito.Mockito.mock;
4040
import static org.mockito.Mockito.never;
4141
import static org.mockito.Mockito.verify;
42-
import static org.neo4j.driver.internal.util.Futures.getBlocking;
42+
import static org.neo4j.driver.v1.util.TestUtil.await;
4343
import static org.neo4j.driver.v1.util.TestUtil.connectionMock;
4444

4545
public class ExplicitTransactionTest
@@ -235,7 +235,7 @@ public void shouldReleaseConnectionWhenBeginFails()
235235

236236
try
237237
{
238-
getBlocking( tx.beginAsync( Bookmark.from( "SomeBookmark" ) ) );
238+
await( tx.beginAsync( Bookmark.from( "SomeBookmark" ) ) );
239239
fail( "Exception expected" );
240240
}
241241
catch ( RuntimeException e )
@@ -251,7 +251,7 @@ public void shouldNotReleaseConnectionWhenBeginSucceeds()
251251
{
252252
Connection connection = connectionWithBegin( handler -> handler.onSuccess( emptyMap() ) );
253253
ExplicitTransaction tx = new ExplicitTransaction( connection, mock( NetworkSession.class ) );
254-
getBlocking( tx.beginAsync( Bookmark.from( "SomeBookmark" ) ) );
254+
await( tx.beginAsync( Bookmark.from( "SomeBookmark" ) ) );
255255

256256
verify( connection, never() ).release();
257257
}
@@ -270,7 +270,7 @@ private static ExplicitTransaction beginTx( Connection connection, NetworkSessio
270270
Bookmark initialBookmark )
271271
{
272272
ExplicitTransaction tx = new ExplicitTransaction( connection, session );
273-
return getBlocking( tx.beginAsync( initialBookmark ) );
273+
return await( tx.beginAsync( initialBookmark ) );
274274
}
275275

276276
private static Connection connectionWithBegin( Consumer<ResponseHandler> beginBehaviour )

driver/src/test/java/org/neo4j/driver/internal/InternalDriverTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import static org.mockito.Mockito.verify;
2929
import static org.mockito.Mockito.when;
3030
import static org.neo4j.driver.internal.logging.DevNullLogging.DEV_NULL_LOGGING;
31-
import static org.neo4j.driver.internal.util.Futures.getBlocking;
31+
import static org.neo4j.driver.v1.util.TestUtil.await;
3232

3333
public class InternalDriverTest
3434
{
@@ -38,7 +38,7 @@ public void shouldCloseSessionFactory()
3838
SessionFactory sessionFactory = sessionFactoryMock();
3939
InternalDriver driver = newDriver( sessionFactory );
4040

41-
assertNull( getBlocking( driver.closeAsync() ) );
41+
assertNull( await( driver.closeAsync() ) );
4242
verify( sessionFactory ).close();
4343
}
4444

@@ -48,9 +48,9 @@ public void shouldNotCloseSessionFactoryMultipleTimes()
4848
SessionFactory sessionFactory = sessionFactoryMock();
4949
InternalDriver driver = newDriver( sessionFactory );
5050

51-
assertNull( getBlocking( driver.closeAsync() ) );
52-
assertNull( getBlocking( driver.closeAsync() ) );
53-
assertNull( getBlocking( driver.closeAsync() ) );
51+
assertNull( await( driver.closeAsync() ) );
52+
assertNull( await( driver.closeAsync() ) );
53+
assertNull( await( driver.closeAsync() ) );
5454

5555
verify( sessionFactory ).close();
5656
}

0 commit comments

Comments
 (0)