### Question:
TypicallyConnectToRandomReadReplicaStrategy extends UpstreamDatabaseSelectionStrategy { @Override public Optional<MemberId> upstreamDatabase() { if ( counter.shouldReturnCoreMemberId() ) { return randomCoreMember(); } else { List<MemberId> readReplicaMembers = new ArrayList<>( topologyService.localReadReplicas().members().keySet() ); Collections.shuffle( readReplicaMembers ); List<MemberId> coreMembers = new ArrayList<>( topologyService.localCoreServers().members().keySet() ); Collections.shuffle( coreMembers ); return Stream.concat( readReplicaMembers.stream(), coreMembers.stream() ).filter( not( myself::equals ) ).findFirst(); } } TypicallyConnectToRandomReadReplicaStrategy();  TypicallyConnectToRandomReadReplicaStrategy( int connectToCoreInterval ); @Override Optional<MemberId> upstreamDatabase(); static final String IDENTITY; }### Answer:
@Test public void shouldConnectToCoreOneInTenTimesByDefault() { MemberId theCoreMemberId = new MemberId( UUID.randomUUID() ); TopologyService topologyService = fakeTopologyService( fakeCoreTopology( theCoreMemberId ), fakeReadReplicaTopology( memberIDs( 100 ) ) ); Config config = mock( Config.class ); when( config.get( CausalClusteringSettings.database ) ).thenReturn( "default" ); TypicallyConnectToRandomReadReplicaStrategy connectionStrategy = new TypicallyConnectToRandomReadReplicaStrategy( 2 ); connectionStrategy.inject( topologyService, config, NullLogProvider.getInstance(), myself ); List<MemberId> responses = new ArrayList<>(); for ( int i = 0; i < 3; i++ ) { for ( int j = 0; j < 2; j++ ) { responses.add( connectionStrategy.upstreamDatabase().get() ); } assertThat( responses, hasItem( theCoreMemberId ) ); responses.clear(); } }
@Test public void filtersSelf() { String groupName = "groupName"; Config config = Config.defaults(); TypicallyConnectToRandomReadReplicaStrategy typicallyConnectToRandomReadReplicaStrategy = new TypicallyConnectToRandomReadReplicaStrategy(); typicallyConnectToRandomReadReplicaStrategy.inject( new TopologyServiceThatPrioritisesItself( myself, groupName ), config, NullLogProvider.getInstance(), myself ); Optional<MemberId> found = typicallyConnectToRandomReadReplicaStrategy.upstreamDatabase(); assertTrue( found.isPresent() ); assertNotEquals( myself, found ); }
@Test public void onCounterTriggerFiltersSelf() { TypicallyConnectToRandomReadReplicaStrategy connectionStrategy = new TypicallyConnectToRandomReadReplicaStrategy( 1 ); MemberId otherCoreMember = new MemberId( new UUID( 12, 34 ) ); TopologyService topologyService = fakeTopologyService( fakeCoreTopology( myself, otherCoreMember ), fakeReadReplicaTopology( memberIDs( 2 ) ) ); connectionStrategy.inject( topologyService, Config.defaults(), NullLogProvider.getInstance(), myself ); Optional<MemberId> found = connectionStrategy.upstreamDatabase(); assertTrue( found.isPresent() ); assertNotEquals( myself, found.get() ); }
@Test public void randomCoreDoesNotReturnSameCoreTwice() { TypicallyConnectToRandomReadReplicaStrategy connectionStrategy = new TypicallyConnectToRandomReadReplicaStrategy( 1 ); MemberId firstOther = new MemberId( new UUID( 12, 34 ) ); MemberId secondOther = new MemberId( new UUID( 56, 78 ) ); TopologyService topologyService = fakeTopologyService( fakeCoreTopology( myself, firstOther, secondOther ), fakeReadReplicaTopology( memberIDs( 2 ) ) ); connectionStrategy.inject( topologyService, Config.defaults(), NullLogProvider.getInstance(), myself ); List<MemberId> found = IntStream.range( 0, 20 ) .mapToObj( i -> connectionStrategy.upstreamDatabase() ) .filter( Optional::isPresent ) .map( Optional::get ) .collect( Collectors.toList() ); assertFalse( found.contains( myself ) ); assertTrue( found.contains( firstOther ) ); assertTrue( found.contains( secondOther ) ); }