3939import org .neo4j .driver .v1 .Config ;
4040import org .neo4j .driver .v1 .Driver ;
4141import org .neo4j .driver .v1 .GraphDatabase ;
42+ import org .neo4j .driver .v1 .Logger ;
43+ import org .neo4j .driver .v1 .Logging ;
4244import org .neo4j .driver .v1 .Record ;
4345import org .neo4j .driver .v1 .Session ;
4446import org .neo4j .driver .v1 .StatementResult ;
6163import static org .junit .jupiter .api .Assertions .assertThrows ;
6264import static org .junit .jupiter .api .Assertions .assertTrue ;
6365import static org .mockito .ArgumentMatchers .any ;
66+ import static org .mockito .ArgumentMatchers .startsWith ;
6467import static org .mockito .Mockito .mock ;
68+ import static org .mockito .Mockito .times ;
6569import static org .mockito .Mockito .verify ;
6670import static org .mockito .Mockito .when ;
6771import static org .neo4j .driver .v1 .Logging .none ;
@@ -729,16 +733,19 @@ void shouldRetryWriteTransactionUntilSuccess() throws Exception
729733 @ Test
730734 void shouldRetryWriteTransactionUntilSuccessWithWhenLeaderIsRemoved () throws Exception
731735 {
732- // This test simulates a router in a cluster that a leader is removed.
736+ // This test simulates a router in a cluster when a leader is removed.
733737 // The router first returns a RT with a writer inside.
734738 // However this writer is killed while the driver is running a tx with it.
735- // As a result, the writer server is removed from RT in the router's second reply.
736- // Finally, the router will return a RT with a reachable writer.
739+ // Then at the second time the router returns the same RT with the killed writer inside.
740+ // At the third round, the router removes the the writer server from RT reply.
741+ // Finally, the router returns a RT with a reachable writer.
737742 StubServer router = StubServer .start ( "acquire_endpoints_v3_leader_killed.script" , 9001 );
738743 StubServer brokenWriter = StubServer .start ( "dead_write_server.script" , 9004 );
739744 StubServer writer = StubServer .start ( "write_server.script" , 9008 );
740745
741- try ( Driver driver = newDriverWithSleeplessClock ( "bolt+routing://127.0.0.1:9001" );
746+ Logger logger = mock ( Logger .class );
747+ Config config = Config .builder ().withoutEncryption ().withLogging ( mockedLogging ( logger ) ).build ();
748+ try ( Driver driver = newDriverWithSleeplessClock ( "bolt+routing://127.0.0.1:9001" , config );
742749 Session session = driver .session () )
743750 {
744751 AtomicInteger invocations = new AtomicInteger ();
@@ -753,6 +760,8 @@ void shouldRetryWriteTransactionUntilSuccessWithWhenLeaderIsRemoved() throws Exc
753760 assertEquals ( 0 , brokenWriter .exitStatus () );
754761 assertEquals ( 0 , writer .exitStatus () );
755762 }
763+ verify ( logger , times ( 3 ) ).warn ( startsWith ( "Transaction failed and will be retried in" ), any ( SessionExpiredException .class ) );
764+ verify ( logger ).warn ( startsWith ( "Failed to obtain a connection towards address 127.0.0.1:9004" ), any ( SessionExpiredException .class ) );
756765 }
757766
758767 @ Test
@@ -1188,19 +1197,24 @@ void useSessionAfterDriverIsClosed() throws Exception
11881197 }
11891198 }
11901199
1191- private static Driver newDriverWithSleeplessClock ( String uriString )
1200+ private static Driver newDriverWithSleeplessClock ( String uriString , Config config )
11921201 {
11931202 DriverFactory driverFactory = new DriverFactoryWithClock ( new SleeplessClock () );
1194- return newDriver ( uriString , driverFactory );
1203+ return newDriver ( uriString , driverFactory , config );
1204+ }
1205+
1206+ private static Driver newDriverWithSleeplessClock ( String uriString )
1207+ {
1208+ return newDriverWithSleeplessClock ( uriString , config );
11951209 }
11961210
11971211 private static Driver newDriverWithFixedRetries ( String uriString , int retries )
11981212 {
11991213 DriverFactory driverFactory = new DriverFactoryWithFixedRetryLogic ( retries );
1200- return newDriver ( uriString , driverFactory );
1214+ return newDriver ( uriString , driverFactory , config );
12011215 }
12021216
1203- private static Driver newDriver ( String uriString , DriverFactory driverFactory )
1217+ private static Driver newDriver ( String uriString , DriverFactory driverFactory , Config config )
12041218 {
12051219 URI uri = URI .create ( uriString );
12061220 RoutingSettings routingConf = new RoutingSettings ( 1 , 1 , null );
@@ -1230,4 +1244,11 @@ private static List<String> readStrings( final String query, Session session )
12301244 return names ;
12311245 } );
12321246 }
1247+
1248+ private static Logging mockedLogging ( Logger logger )
1249+ {
1250+ Logging logging = mock ( Logging .class );
1251+ when ( logging .getLog ( any () ) ).thenReturn ( logger );
1252+ return logging ;
1253+ }
12331254}
0 commit comments