Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Java IT tests to Testkit #1097

Merged
merged 1 commit into from
Dec 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,20 @@ public long acquired()
return this.acquired.get();
}


@Override
public String toString()
{
return format( "%s=[created=%s, closed=%s, creating=%s, failedToCreate=%s, acquiring=%s, acquired=%s, " +
"timedOutToAcquire=%s, inUse=%s, idle=%s, " +
"totalAcquisitionTime=%s, totalConnectionTime=%s, totalInUseTime=%s, totalInUseCount=%s]",
id(), created(), closed(), creating(), failedToCreate(), acquiring(), acquired(),
timedOutToAcquire(), inUse(), idle(),
totalAcquisitionTime(), totalConnectionTime(), totalInUseTime(), totalInUseCount() );
"timedOutToAcquire=%s, inUse=%s, idle=%s, " +
"totalAcquisitionTime=%s, totalConnectionTime=%s, totalInUseTime=%s, totalInUseCount=%s]",
id(), created(), closed(), creating(), failedToCreate(), acquiring(), acquired(),
timedOutToAcquire(), inUse(), idle(),
totalAcquisitionTime(), totalConnectionTime(), totalInUseTime(), totalInUseCount() );
}

// This method is for purposes testing only
public BoltServerAddress getAddress()
{
return address;
}
}
41 changes: 34 additions & 7 deletions driver/src/test/java/org/neo4j/driver/GraphDatabaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.io.IOException;
import java.net.ServerSocket;
import java.net.URI;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.neo4j.driver.exceptions.ServiceUnavailableException;
Expand Down Expand Up @@ -73,7 +75,7 @@ void shouldLogWhenUnableToCreateRoutingDriver()
when( logging.getLog( any( Class.class ) ) ).thenReturn( logger );
InternalDriver driver = mock( InternalDriver.class );
doThrow( ServiceUnavailableException.class ).when( driver ).verifyConnectivity();
DriverFactory driverFactory = new MockSupplyingDriverFactory( driver );
DriverFactory driverFactory = new MockSupplyingDriverFactory( Arrays.asList( driver, driver ) );
Config config = Config.builder()
.withLogging( logging )
.build();
Expand All @@ -85,10 +87,35 @@ void shouldLogWhenUnableToCreateRoutingDriver()
assertThrows( ServiceUnavailableException.class, () -> GraphDatabase.routingDriver( routingUris, AuthTokens.none(), config, driverFactory ) );

verify( logger ).warn( eq( "Unable to create routing driver for URI: neo4j://localhost:9001" ),
any( Throwable.class ) );
any( Throwable.class ) );

verify( logger ).warn( eq( "Unable to create routing driver for URI: neo4j://localhost:9002" ),
any( Throwable.class ) );
any( Throwable.class ) );
}

@Test
void shouldNotFailRoutingDriverWhenThereIsWorkingUri()
{
Logging logging = mock( Logging.class );
Logger logger = mock( Logger.class );
when( logging.getLog( any( Class.class ) ) ).thenReturn( logger );
InternalDriver failingDriver = mock( InternalDriver.class );
doThrow( ServiceUnavailableException.class ).when( failingDriver ).verifyConnectivity();
InternalDriver workingDriver = mock( InternalDriver.class );
DriverFactory driverFactory = new MockSupplyingDriverFactory( Arrays.asList( failingDriver, workingDriver ) );
Config config = Config.builder()
.withLogging( logging )
.build();

List<URI> routingUris = asList(
URI.create( "neo4j://localhost:9001" ),
URI.create( "neo4j://localhost:9002" ) );

Driver driver = GraphDatabase.routingDriver( routingUris, AuthTokens.none(), config, driverFactory );

verify( logger ).warn( eq( "Unable to create routing driver for URI: neo4j://localhost:9001" ),
any( Throwable.class ) );
assertEquals( driver, workingDriver );
}

@Test
Expand Down Expand Up @@ -184,19 +211,19 @@ private static Config createConfig( boolean encrypted, int timeoutMillis )

private static class MockSupplyingDriverFactory extends DriverFactory
{
private final InternalDriver driver;
private final Iterator<InternalDriver> driverIterator;

private MockSupplyingDriverFactory( InternalDriver driver )
private MockSupplyingDriverFactory( List<InternalDriver> drivers )
{
this.driver = driver;
driverIterator = drivers.iterator();
}

@Override
protected InternalDriver createRoutingDriver( SecurityPlan securityPlan, BoltServerAddress address, ConnectionPool connectionPool,
EventExecutorGroup eventExecutorGroup, RoutingSettings routingSettings, RetryLogic retryLogic,
MetricsProvider metricsProvider, Config config )
{
return driver;
return driverIterator.next();
}
}
}
Loading