Skip to content

Commit

Permalink
No unbounded Future#get() in tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lutovich committed Nov 21, 2017
1 parent 0be2121 commit a20ef2e
Show file tree
Hide file tree
Showing 22 changed files with 209 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ public final Driver newInstance( URI uri, AuthToken authToken, RoutingSettings r
{
InternalDriver driver = createDriver( uri, address, connectionPool, config, newRoutingSettings,
eventExecutorGroup, securityPlan, retryLogic );
Futures.getBlocking( driver.verifyConnectivity() );
Futures.blockingGet( driver.verifyConnectivity() );
return driver;
}
catch ( Throwable driverError )
{
// we need to close the connection pool if driver creation threw exception
try
{
Futures.getBlocking( connectionPool.close() );
Futures.blockingGet( connectionPool.close() );
}
catch ( Throwable closeError )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@

import static java.util.Collections.emptyMap;
import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.neo4j.driver.internal.util.Futures.blockingGet;
import static org.neo4j.driver.internal.util.Futures.completionErrorCause;
import static org.neo4j.driver.internal.util.Futures.failedFuture;
import static org.neo4j.driver.internal.util.Futures.getBlocking;
import static org.neo4j.driver.v1.Values.value;

public class ExplicitTransaction implements Transaction
Expand Down Expand Up @@ -151,7 +151,7 @@ public void failure()
@Override
public void close()
{
getBlocking( closeAsync() );
blockingGet( closeAsync() );
}

CompletionStage<Void> closeAsync()
Expand Down Expand Up @@ -275,7 +275,7 @@ public CompletionStage<StatementResultCursor> runAsync( String statementTemplate
@Override
public StatementResult run( Statement statement )
{
StatementResultCursor cursor = getBlocking( run( statement, false ) );
StatementResultCursor cursor = blockingGet( run( statement, false ) );
return new InternalStatementResult( cursor );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.neo4j.driver.v1.Session;

import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.neo4j.driver.internal.util.Futures.getBlocking;
import static org.neo4j.driver.internal.util.Futures.blockingGet;

public class InternalDriver implements Driver
{
Expand Down Expand Up @@ -100,7 +100,7 @@ private Session newSession( AccessMode mode, Bookmark bookmark )
@Override
public void close()
{
getBlocking( closeAsync() );
blockingGet( closeAsync() );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.neo4j.driver.v1.summary.ResultSummary;
import org.neo4j.driver.v1.util.Function;

import static org.neo4j.driver.internal.util.Futures.getBlocking;
import static org.neo4j.driver.internal.util.Futures.blockingGet;

public class InternalStatementResult implements StatementResult
{
Expand All @@ -45,7 +45,7 @@ public List<String> keys()
{
if ( keys == null )
{
getBlocking( cursor.peekAsync() );
blockingGet( cursor.peekAsync() );
keys = cursor.keys();
}
return keys;
Expand All @@ -54,13 +54,13 @@ public List<String> keys()
@Override
public boolean hasNext()
{
return getBlocking( cursor.peekAsync() ) != null;
return blockingGet( cursor.peekAsync() ) != null;
}

@Override
public Record next()
{
Record record = getBlocking( cursor.nextAsync() );
Record record = blockingGet( cursor.nextAsync() );
if ( record == null )
{
throw new NoSuchRecordException( "No more records" );
Expand All @@ -71,13 +71,13 @@ public Record next()
@Override
public Record single()
{
return getBlocking( cursor.singleAsync() );
return blockingGet( cursor.singleAsync() );
}

@Override
public Record peek()
{
Record record = getBlocking( cursor.peekAsync() );
Record record = blockingGet( cursor.peekAsync() );
if ( record == null )
{
throw new NoSuchRecordException( "Cannot peek past the last record" );
Expand All @@ -88,25 +88,25 @@ public Record peek()
@Override
public List<Record> list()
{
return getBlocking( cursor.listAsync() );
return blockingGet( cursor.listAsync() );
}

@Override
public <T> List<T> list( Function<Record, T> mapFunction )
{
return getBlocking( cursor.listAsync( mapFunction ) );
return blockingGet( cursor.listAsync( mapFunction ) );
}

@Override
public ResultSummary consume()
{
return getBlocking( cursor.consumeAsync() );
return blockingGet( cursor.consumeAsync() );
}

@Override
public ResultSummary summary()
{
return getBlocking( cursor.summaryAsync() );
return blockingGet( cursor.summaryAsync() );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected void finalize() throws Throwable

private void logLeakIfNeeded()
{
Boolean isOpen = Futures.getBlocking( currentConnectionIsOpen() );
Boolean isOpen = Futures.blockingGet( currentConnectionIsOpen() );
if ( isOpen )
{
logger.error( "Neo4j Session object leaked, please ensure that your application" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
import org.neo4j.driver.v1.types.TypeSystem;

import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.neo4j.driver.internal.util.Futures.blockingGet;
import static org.neo4j.driver.internal.util.Futures.failedFuture;
import static org.neo4j.driver.internal.util.Futures.getBlocking;
import static org.neo4j.driver.v1.Values.value;

public class NetworkSession implements Session
Expand Down Expand Up @@ -133,7 +133,7 @@ public CompletionStage<StatementResultCursor> runAsync( String statementText, Va
@Override
public StatementResult run( Statement statement )
{
StatementResultCursor cursor = getBlocking( runAsync( statement, false ) );
StatementResultCursor cursor = blockingGet( runAsync( statement, false ) );
return new InternalStatementResult( cursor );
}

Expand All @@ -153,7 +153,7 @@ public boolean isOpen()
@Override
public void close()
{
getBlocking( closeAsync() );
blockingGet( closeAsync() );
}

@Override
Expand Down Expand Up @@ -190,7 +190,7 @@ public CompletionStage<Void> closeAsync()
@Override
public Transaction beginTransaction()
{
return getBlocking( beginTransactionAsync( mode ) );
return blockingGet( beginTransactionAsync( mode ) );
}

@Deprecated
Expand Down Expand Up @@ -249,7 +249,7 @@ public String lastBookmark()
@Override
public void reset()
{
getBlocking( resetAsync() );
blockingGet( resetAsync() );
}

private CompletionStage<Void> resetAsync()
Expand Down Expand Up @@ -289,7 +289,7 @@ private <T> T transaction( AccessMode mode, TransactionWork<T> work )
// event loop thread will bock and wait for itself to read some data
return retryLogic.retry( () ->
{
try ( Transaction tx = getBlocking( beginTransactionAsync( mode ) ) )
try ( Transaction tx = blockingGet( beginTransactionAsync( mode ) ) )
{
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static <T> CompletableFuture<T> failedFuture( Throwable error )
return result;
}

public static <V> V getBlocking( CompletionStage<V> stage )
public static <V> V blockingGet( CompletionStage<V> stage )
{
EventLoopGroupFactory.assertNotInEventLoopThread();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.neo4j.driver.internal.util.Futures.getBlocking;
import static org.neo4j.driver.v1.AccessMode.READ;
import static org.neo4j.driver.v1.AccessMode.WRITE;
import static org.neo4j.driver.v1.util.TestUtil.await;

public class DirectConnectionProviderTest
{
Expand All @@ -49,8 +49,8 @@ public void acquiresConnectionsFromThePool()
ConnectionPool pool = poolMock( address, connection1, connection2 );
DirectConnectionProvider provider = new DirectConnectionProvider( address, pool );

assertSame( connection1, getBlocking( provider.acquireConnection( READ ) ) );
assertSame( connection2, getBlocking( provider.acquireConnection( WRITE ) ) );
assertSame( connection1, await( provider.acquireConnection( READ ) ) );
assertSame( connection2, await( provider.acquireConnection( WRITE ) ) );
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.neo4j.driver.internal.util.Futures.getBlocking;
import static org.neo4j.driver.v1.util.TestUtil.await;
import static org.neo4j.driver.v1.util.TestUtil.connectionMock;

public class ExplicitTransactionTest
Expand Down Expand Up @@ -235,7 +235,7 @@ public void shouldReleaseConnectionWhenBeginFails()

try
{
getBlocking( tx.beginAsync( Bookmark.from( "SomeBookmark" ) ) );
await( tx.beginAsync( Bookmark.from( "SomeBookmark" ) ) );
fail( "Exception expected" );
}
catch ( RuntimeException e )
Expand All @@ -251,7 +251,7 @@ public void shouldNotReleaseConnectionWhenBeginSucceeds()
{
Connection connection = connectionWithBegin( handler -> handler.onSuccess( emptyMap() ) );
ExplicitTransaction tx = new ExplicitTransaction( connection, mock( NetworkSession.class ) );
getBlocking( tx.beginAsync( Bookmark.from( "SomeBookmark" ) ) );
await( tx.beginAsync( Bookmark.from( "SomeBookmark" ) ) );

verify( connection, never() ).release();
}
Expand All @@ -270,7 +270,7 @@ private static ExplicitTransaction beginTx( Connection connection, NetworkSessio
Bookmark initialBookmark )
{
ExplicitTransaction tx = new ExplicitTransaction( connection, session );
return getBlocking( tx.beginAsync( initialBookmark ) );
return await( tx.beginAsync( initialBookmark ) );
}

private static Connection connectionWithBegin( Consumer<ResponseHandler> beginBehaviour )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.neo4j.driver.internal.util.Futures.getBlocking;
import static org.neo4j.driver.v1.util.TestUtil.await;

public class InternalDriverTest
{
Expand All @@ -37,7 +37,7 @@ public void shouldCloseSessionFactory()
SessionFactory sessionFactory = sessionFactoryMock();
InternalDriver driver = newDriver( sessionFactory );

assertNull( getBlocking( driver.closeAsync() ) );
assertNull( await( driver.closeAsync() ) );
verify( sessionFactory ).close();
}

Expand All @@ -47,9 +47,9 @@ public void shouldNotCloseSessionFactoryMultipleTimes()
SessionFactory sessionFactory = sessionFactoryMock();
InternalDriver driver = newDriver( sessionFactory );

assertNull( getBlocking( driver.closeAsync() ) );
assertNull( getBlocking( driver.closeAsync() ) );
assertNull( getBlocking( driver.closeAsync() ) );
assertNull( await( driver.closeAsync() ) );
assertNull( await( driver.closeAsync() ) );
assertNull( await( driver.closeAsync() ) );

verify( sessionFactory ).close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@
import static org.mockito.Mockito.when;
import static org.neo4j.driver.internal.logging.DevNullLogging.DEV_NULL_LOGGING;
import static org.neo4j.driver.internal.util.Futures.failedFuture;
import static org.neo4j.driver.internal.util.Futures.getBlocking;
import static org.neo4j.driver.v1.AccessMode.READ;
import static org.neo4j.driver.v1.AccessMode.WRITE;
import static org.neo4j.driver.v1.util.TestUtil.await;
import static org.neo4j.driver.v1.util.TestUtil.connectionMock;

public class NetworkSessionTest
Expand Down Expand Up @@ -211,7 +211,7 @@ public void releasesOpenConnectionUsedForRunWhenSessionIsClosed()

session.run( query );

getBlocking( session.closeAsync() );
await( session.closeAsync() );

InOrder inOrder = inOrder( connection );
inOrder.verify( connection ).runAndFlush( eq( "RETURN 1" ), any(), any(), any() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.neo4j.driver.internal.util.Futures.getBlocking;
import static org.neo4j.driver.v1.util.TestUtil.await;

public class ResultCursorsHolderTest
{
Expand All @@ -41,7 +41,7 @@ public void shouldReturnNoErrorWhenNoCursorStages()
{
ResultCursorsHolder holder = new ResultCursorsHolder();

Throwable error = getBlocking( holder.retrieveNotConsumedError() );
Throwable error = await( holder.retrieveNotConsumedError() );
assertNull( error );
}

Expand Down Expand Up @@ -71,7 +71,7 @@ public void shouldReturnNoErrorWhenCursorStagesHaveNoErrors()
holder.add( cursorWithoutError() );
holder.add( cursorWithoutError() );

Throwable error = getBlocking( holder.retrieveNotConsumedError() );
Throwable error = await( holder.retrieveNotConsumedError() );
assertNull( error );
}

Expand All @@ -85,7 +85,7 @@ public void shouldNotReturnStageErrors()
holder.add( cursorWithoutError() );
holder.add( Futures.failedFuture( new IOException( "Failed to do IO" ) ) );

Throwable error = getBlocking( holder.retrieveNotConsumedError() );
Throwable error = await( holder.retrieveNotConsumedError() );
assertNull( error );
}

Expand All @@ -100,7 +100,7 @@ public void shouldReturnErrorWhenOneCursorFailed()
holder.add( cursorWithError( error ) );
holder.add( cursorWithoutError() );

Throwable retrievedError = getBlocking( holder.retrieveNotConsumedError() );
Throwable retrievedError = await( holder.retrieveNotConsumedError() );
assertEquals( error, retrievedError );
}

Expand All @@ -117,7 +117,7 @@ public void shouldReturnFirstError()
holder.add( cursorWithError( error2 ) );
holder.add( cursorWithError( error3 ) );

assertEquals( error1, getBlocking( holder.retrieveNotConsumedError() ) );
assertEquals( error1, await( holder.retrieveNotConsumedError() ) );
}

private CompletionStage<InternalStatementResultCursor> cursorWithoutError()
Expand Down
Loading

0 comments on commit a20ef2e

Please sign in to comment.