LeastConnectedLoadBalancingStrategy implements LoadBalancingStrategy { @Override public BoltServerAddress selectWriter( BoltServerAddress[] knownWriters ) { return select( knownWriters, writersIndex, "writer" ); } LeastConnectedLoadBalancingStrategy( ConnectionPool connectionPool, Logging logging ); @Override BoltServerAddress selectReader( BoltServerAddress[] knownReaders ); @Override BoltServerAddress selectWriter( BoltServerAddress[] knownWriters );  }
@Test void shouldHandleEmptyWritersArray() { assertNull( strategy.selectWriter( new BoltServerAddress[0] ) ); }
@Test void shouldHandleSingleWriterWithoutActiveConnections() { BoltServerAddress address = new BoltServerAddress( "writer", 9999 ); assertEquals( address, strategy.selectWriter( new BoltServerAddress[]{address} ) ); }
@Test void shouldHandleSingleWriterWithActiveConnections() { BoltServerAddress address = new BoltServerAddress( "writer", 9999 ); when( connectionPool.inUseConnections( address ) ).thenReturn( 24 ); assertEquals( address, strategy.selectWriter( new BoltServerAddress[]{address} ) ); }
@Test void shouldHandleMultipleWritersWithActiveConnections() { BoltServerAddress address1 = new BoltServerAddress( "writer", 1 ); BoltServerAddress address2 = new BoltServerAddress( "writer", 2 ); BoltServerAddress address3 = new BoltServerAddress( "writer", 3 ); BoltServerAddress address4 = new BoltServerAddress( "writer", 4 ); when( connectionPool.inUseConnections( address1 ) ).thenReturn( 5 ); when( connectionPool.inUseConnections( address2 ) ).thenReturn( 6 ); when( connectionPool.inUseConnections( address3 ) ).thenReturn( 0 ); when( connectionPool.inUseConnections( address4 ) ).thenReturn( 1 ); assertEquals( address3, strategy.selectWriter( new BoltServerAddress[]{address1, address2, address3, address4} ) ); }