|
| 1 | +/* |
| 2 | + * Copyright (c) 2002-2017 "Neo Technology," |
| 3 | + * Network Engine for Objects in Lund AB [http://neotechnology.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +package org.neo4j.driver.internal.cluster; |
| 20 | + |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Test; |
| 23 | +import org.mockito.Mock; |
| 24 | + |
| 25 | +import org.neo4j.driver.internal.net.BoltServerAddress; |
| 26 | +import org.neo4j.driver.internal.spi.ConnectionPool; |
| 27 | + |
| 28 | +import static org.junit.Assert.assertEquals; |
| 29 | +import static org.junit.Assert.assertNull; |
| 30 | +import static org.mockito.Mockito.when; |
| 31 | +import static org.mockito.MockitoAnnotations.initMocks; |
| 32 | + |
| 33 | +public class LeastConnectedLoadBalancingStrategyTest |
| 34 | +{ |
| 35 | + @Mock |
| 36 | + private ConnectionPool connectionPool; |
| 37 | + private LeastConnectedLoadBalancingStrategy strategy; |
| 38 | + |
| 39 | + @Before |
| 40 | + public void setUp() throws Exception |
| 41 | + { |
| 42 | + initMocks( this ); |
| 43 | + strategy = new LeastConnectedLoadBalancingStrategy( connectionPool ); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void shouldHandleEmptyReadersArray() |
| 48 | + { |
| 49 | + assertNull( strategy.selectReader( new BoltServerAddress[0] ) ); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void shouldHandleEmptyWritersArray() |
| 54 | + { |
| 55 | + assertNull( strategy.selectWriter( new BoltServerAddress[0] ) ); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void shouldHandleSingleReaderWithoutActiveConnections() |
| 60 | + { |
| 61 | + BoltServerAddress address = new BoltServerAddress( "reader", 9999 ); |
| 62 | + |
| 63 | + assertEquals( address, strategy.selectReader( new BoltServerAddress[]{address} ) ); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void shouldHandleSingleWriterWithoutActiveConnections() |
| 68 | + { |
| 69 | + BoltServerAddress address = new BoltServerAddress( "writer", 9999 ); |
| 70 | + |
| 71 | + assertEquals( address, strategy.selectWriter( new BoltServerAddress[]{address} ) ); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void shouldHandleSingleReaderWithActiveConnections() |
| 76 | + { |
| 77 | + BoltServerAddress address = new BoltServerAddress( "reader", 9999 ); |
| 78 | + when( connectionPool.activeConnections( address ) ).thenReturn( 42 ); |
| 79 | + |
| 80 | + assertEquals( address, strategy.selectReader( new BoltServerAddress[]{address} ) ); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void shouldHandleSingleWriterWithActiveConnections() |
| 85 | + { |
| 86 | + BoltServerAddress address = new BoltServerAddress( "writer", 9999 ); |
| 87 | + when( connectionPool.activeConnections( address ) ).thenReturn( 24 ); |
| 88 | + |
| 89 | + assertEquals( address, strategy.selectWriter( new BoltServerAddress[]{address} ) ); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void shouldHandleMultipleReadersWithActiveConnections() |
| 94 | + { |
| 95 | + BoltServerAddress address1 = new BoltServerAddress( "reader", 1 ); |
| 96 | + BoltServerAddress address2 = new BoltServerAddress( "reader", 2 ); |
| 97 | + BoltServerAddress address3 = new BoltServerAddress( "reader", 3 ); |
| 98 | + |
| 99 | + when( connectionPool.activeConnections( address1 ) ).thenReturn( 3 ); |
| 100 | + when( connectionPool.activeConnections( address2 ) ).thenReturn( 4 ); |
| 101 | + when( connectionPool.activeConnections( address3 ) ).thenReturn( 1 ); |
| 102 | + |
| 103 | + assertEquals( address3, strategy.selectReader( new BoltServerAddress[]{address1, address2, address3} ) ); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void shouldHandleMultipleWritersWithActiveConnections() |
| 108 | + { |
| 109 | + BoltServerAddress address1 = new BoltServerAddress( "writer", 1 ); |
| 110 | + BoltServerAddress address2 = new BoltServerAddress( "writer", 2 ); |
| 111 | + BoltServerAddress address3 = new BoltServerAddress( "writer", 3 ); |
| 112 | + BoltServerAddress address4 = new BoltServerAddress( "writer", 4 ); |
| 113 | + |
| 114 | + when( connectionPool.activeConnections( address1 ) ).thenReturn( 5 ); |
| 115 | + when( connectionPool.activeConnections( address2 ) ).thenReturn( 6 ); |
| 116 | + when( connectionPool.activeConnections( address3 ) ).thenReturn( 0 ); |
| 117 | + when( connectionPool.activeConnections( address4 ) ).thenReturn( 1 ); |
| 118 | + |
| 119 | + assertEquals( address3, |
| 120 | + strategy.selectWriter( new BoltServerAddress[]{address1, address2, address3, address4} ) ); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void shouldReturnDifferentReaderOnEveryInvocationWhenNoActiveConnections() |
| 125 | + { |
| 126 | + BoltServerAddress address1 = new BoltServerAddress( "reader", 1 ); |
| 127 | + BoltServerAddress address2 = new BoltServerAddress( "reader", 2 ); |
| 128 | + BoltServerAddress address3 = new BoltServerAddress( "reader", 3 ); |
| 129 | + |
| 130 | + assertEquals( address1, strategy.selectReader( new BoltServerAddress[]{address1, address2, address3} ) ); |
| 131 | + assertEquals( address2, strategy.selectReader( new BoltServerAddress[]{address1, address2, address3} ) ); |
| 132 | + assertEquals( address3, strategy.selectReader( new BoltServerAddress[]{address1, address2, address3} ) ); |
| 133 | + |
| 134 | + assertEquals( address1, strategy.selectReader( new BoltServerAddress[]{address1, address2, address3} ) ); |
| 135 | + assertEquals( address2, strategy.selectReader( new BoltServerAddress[]{address1, address2, address3} ) ); |
| 136 | + assertEquals( address3, strategy.selectReader( new BoltServerAddress[]{address1, address2, address3} ) ); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void shouldReturnDifferentWriterOnEveryInvocationWhenNoActiveConnections() |
| 141 | + { |
| 142 | + BoltServerAddress address1 = new BoltServerAddress( "writer", 1 ); |
| 143 | + BoltServerAddress address2 = new BoltServerAddress( "writer", 2 ); |
| 144 | + |
| 145 | + assertEquals( address1, strategy.selectReader( new BoltServerAddress[]{address1, address2} ) ); |
| 146 | + assertEquals( address2, strategy.selectReader( new BoltServerAddress[]{address1, address2} ) ); |
| 147 | + |
| 148 | + assertEquals( address1, strategy.selectReader( new BoltServerAddress[]{address1, address2} ) ); |
| 149 | + assertEquals( address2, strategy.selectReader( new BoltServerAddress[]{address1, address2} ) ); |
| 150 | + } |
| 151 | +} |
0 commit comments