39
39
import org .neo4j .driver .v1 .Config ;
40
40
import org .neo4j .driver .v1 .Driver ;
41
41
import org .neo4j .driver .v1 .GraphDatabase ;
42
+ import org .neo4j .driver .v1 .Logger ;
43
+ import org .neo4j .driver .v1 .Logging ;
42
44
import org .neo4j .driver .v1 .Record ;
43
45
import org .neo4j .driver .v1 .Session ;
44
46
import org .neo4j .driver .v1 .StatementResult ;
61
63
import static org .junit .jupiter .api .Assertions .assertThrows ;
62
64
import static org .junit .jupiter .api .Assertions .assertTrue ;
63
65
import static org .mockito .ArgumentMatchers .any ;
66
+ import static org .mockito .ArgumentMatchers .startsWith ;
64
67
import static org .mockito .Mockito .mock ;
68
+ import static org .mockito .Mockito .times ;
65
69
import static org .mockito .Mockito .verify ;
66
70
import static org .mockito .Mockito .when ;
67
71
import static org .neo4j .driver .v1 .Logging .none ;
@@ -729,16 +733,19 @@ void shouldRetryWriteTransactionUntilSuccess() throws Exception
729
733
@ Test
730
734
void shouldRetryWriteTransactionUntilSuccessWithWhenLeaderIsRemoved () throws Exception
731
735
{
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.
733
737
// The router first returns a RT with a writer inside.
734
738
// 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.
737
742
StubServer router = StubServer .start ( "acquire_endpoints_v3_leader_killed.script" , 9001 );
738
743
StubServer brokenWriter = StubServer .start ( "dead_write_server.script" , 9004 );
739
744
StubServer writer = StubServer .start ( "write_server.script" , 9008 );
740
745
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 );
742
749
Session session = driver .session () )
743
750
{
744
751
AtomicInteger invocations = new AtomicInteger ();
@@ -753,6 +760,8 @@ void shouldRetryWriteTransactionUntilSuccessWithWhenLeaderIsRemoved() throws Exc
753
760
assertEquals ( 0 , brokenWriter .exitStatus () );
754
761
assertEquals ( 0 , writer .exitStatus () );
755
762
}
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 ) );
756
765
}
757
766
758
767
@ Test
@@ -1188,19 +1197,24 @@ void useSessionAfterDriverIsClosed() throws Exception
1188
1197
}
1189
1198
}
1190
1199
1191
- private static Driver newDriverWithSleeplessClock ( String uriString )
1200
+ private static Driver newDriverWithSleeplessClock ( String uriString , Config config )
1192
1201
{
1193
1202
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 );
1195
1209
}
1196
1210
1197
1211
private static Driver newDriverWithFixedRetries ( String uriString , int retries )
1198
1212
{
1199
1213
DriverFactory driverFactory = new DriverFactoryWithFixedRetryLogic ( retries );
1200
- return newDriver ( uriString , driverFactory );
1214
+ return newDriver ( uriString , driverFactory , config );
1201
1215
}
1202
1216
1203
- private static Driver newDriver ( String uriString , DriverFactory driverFactory )
1217
+ private static Driver newDriver ( String uriString , DriverFactory driverFactory , Config config )
1204
1218
{
1205
1219
URI uri = URI .create ( uriString );
1206
1220
RoutingSettings routingConf = new RoutingSettings ( 1 , 1 , null );
@@ -1230,4 +1244,11 @@ private static List<String> readStrings( final String query, Session session )
1230
1244
return names ;
1231
1245
} );
1232
1246
}
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
+ }
1233
1254
}
0 commit comments