Skip to content

Commit

Permalink
Migrate Java IT tests to Testkit
Browse files Browse the repository at this point in the history
Migrated tests:
- `CausalClusteringIT.shouldNotReuseReadConnectionForWriteTransaction` -> `TestBookmarks.test_does_not_use_read_connection_for_write`
- `CausalClusteringIT.shouldExecuteReadAndWritesWhenDriverSuppliedWithAddressOfLeader` -> `Routing.test_should_get_rt_from_leader_w_and_r_via_leader_using_session_run`
- `CausalClusteringIT.shouldExecuteReadAndWritesWhenDriverSuppliedWithAddressOfFollower` -> `Routing.test_should_get_rt_from_follower_w_and_r_via_leader_using_session_run`
- `CausalClusteringIT.shouldDropBrokenOldConnections` -> `Routing.test_should_drop_connections_failing_liveness_check`
- `CausalClusteringIT.shouldRespectMaxConnectionPoolSizePerClusterMember` -> `Routing.test_should_enforce_pool_size_per_cluster_member`

Converted tests:
- `CausalClusteringIT.shouldExecuteReadAndWritesWhenRouterIsDiscovered` -> `GraphDatabaseTest.shouldNotFailRoutingDriverWhenThereIsWorkingUri`

New Testkit features:
- `Feature:API:Liveness.Check`
- `Temporary:DriverMaxConnectionPoolSize`
- `Temporary:ConnectionAcquisitionTimeout`
- `Temporary:GetConnectionPoolMetrics`

The `address` field has been added to `Summary` Testkit response.
  • Loading branch information
injectives committed Dec 6, 2021
1 parent bc95327 commit 1fdfe68
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 325 deletions.
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

0 comments on commit 1fdfe68

Please sign in to comment.