LeastConnectedLoadBalancingStrategy implements LoadBalancingStrategy { @Override public BoltServerAddress selectReader( BoltServerAddress[] knownReaders ) { return select( knownReaders, readersIndex, "reader" ); } LeastConnectedLoadBalancingStrategy( ConnectionPool connectionPool, Logging logging ); @Override BoltServerAddress selectReader( BoltServerAddress[] knownReaders ); @Override BoltServerAddress selectWriter( BoltServerAddress[] knownWriters );  }
@Test void shouldHandleEmptyReadersArray() { assertNull( strategy.selectReader( new BoltServerAddress[0] ) ); }
@Test void shouldHandleSingleReaderWithoutActiveConnections() { BoltServerAddress address = new BoltServerAddress( "reader", 9999 ); assertEquals( address, strategy.selectReader( new BoltServerAddress[]{address} ) ); }
@Test void shouldHandleSingleReaderWithActiveConnections() { BoltServerAddress address = new BoltServerAddress( "reader", 9999 ); when( connectionPool.inUseConnections( address ) ).thenReturn( 42 ); assertEquals( address, strategy.selectReader( new BoltServerAddress[]{address} ) ); }
@Test void shouldHandleMultipleReadersWithActiveConnections() { BoltServerAddress address1 = new BoltServerAddress( "reader", 1 ); BoltServerAddress address2 = new BoltServerAddress( "reader", 2 ); BoltServerAddress address3 = new BoltServerAddress( "reader", 3 ); when( connectionPool.inUseConnections( address1 ) ).thenReturn( 3 ); when( connectionPool.inUseConnections( address2 ) ).thenReturn( 4 ); when( connectionPool.inUseConnections( address3 ) ).thenReturn( 1 ); assertEquals( address3, strategy.selectReader( new BoltServerAddress[]{address1, address2, address3} ) ); }
@Test void shouldReturnDifferentReaderOnEveryInvocationWhenNoActiveConnections() { BoltServerAddress address1 = new BoltServerAddress( "reader", 1 ); BoltServerAddress address2 = new BoltServerAddress( "reader", 2 ); BoltServerAddress address3 = new BoltServerAddress( "reader", 3 ); assertEquals( address1, strategy.selectReader( new BoltServerAddress[]{address1, address2, address3} ) ); assertEquals( address2, strategy.selectReader( new BoltServerAddress[]{address1, address2, address3} ) ); assertEquals( address3, strategy.selectReader( new BoltServerAddress[]{address1, address2, address3} ) ); assertEquals( address1, strategy.selectReader( new BoltServerAddress[]{address1, address2, address3} ) ); assertEquals( address2, strategy.selectReader( new BoltServerAddress[]{address1, address2, address3} ) ); assertEquals( address3, strategy.selectReader( new BoltServerAddress[]{address1, address2, address3} ) ); }
@Test void shouldReturnDifferentWriterOnEveryInvocationWhenNoActiveConnections() { BoltServerAddress address1 = new BoltServerAddress( "writer", 1 ); BoltServerAddress address2 = new BoltServerAddress( "writer", 2 ); assertEquals( address1, strategy.selectReader( new BoltServerAddress[]{address1, address2} ) ); assertEquals( address2, strategy.selectReader( new BoltServerAddress[]{address1, address2} ) ); assertEquals( address1, strategy.selectReader( new BoltServerAddress[]{address1, address2} ) ); assertEquals( address2, strategy.selectReader( new BoltServerAddress[]{address1, address2} ) ); }