|
| 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 | + |
| 20 | +import LeastConnectedLoadBalancingStrategy from '../../src/v1/internal/least-connected-load-balancing-strategy'; |
| 21 | +import Pool from '../../src/v1/internal/pool'; |
| 22 | + |
| 23 | +describe('LeastConnectedLoadBalancingStrategy', () => { |
| 24 | + |
| 25 | + it('should return null when no readers', () => { |
| 26 | + const knownReaders = []; |
| 27 | + const strategy = new LeastConnectedLoadBalancingStrategy(new DummyPool({})); |
| 28 | + |
| 29 | + expect(strategy.selectReader(knownReaders)).toBeNull(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should return null when no writers', () => { |
| 33 | + const knownWriters = []; |
| 34 | + const strategy = new LeastConnectedLoadBalancingStrategy(new DummyPool({})); |
| 35 | + |
| 36 | + expect(strategy.selectWriter(knownWriters)).toBeNull(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should return same reader when it is the only one available and has no connections', () => { |
| 40 | + const knownReaders = ['reader-1']; |
| 41 | + const strategy = new LeastConnectedLoadBalancingStrategy(new DummyPool({'reader-1': 0})); |
| 42 | + |
| 43 | + expect(strategy.selectReader(knownReaders)).toEqual('reader-1'); |
| 44 | + expect(strategy.selectReader(knownReaders)).toEqual('reader-1'); |
| 45 | + expect(strategy.selectReader(knownReaders)).toEqual('reader-1'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should return same writer when it is the only one available and has no connections', () => { |
| 49 | + const knownWriters = ['writer-1']; |
| 50 | + const strategy = new LeastConnectedLoadBalancingStrategy(new DummyPool({'writer-1': 0})); |
| 51 | + |
| 52 | + expect(strategy.selectWriter(knownWriters)).toEqual('writer-1'); |
| 53 | + expect(strategy.selectWriter(knownWriters)).toEqual('writer-1'); |
| 54 | + expect(strategy.selectWriter(knownWriters)).toEqual('writer-1'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should return same reader when it is the only one available and has active connections', () => { |
| 58 | + const knownReaders = ['reader-1']; |
| 59 | + const strategy = new LeastConnectedLoadBalancingStrategy(new DummyPool({'reader-1': 14})); |
| 60 | + |
| 61 | + expect(strategy.selectReader(knownReaders)).toEqual('reader-1'); |
| 62 | + expect(strategy.selectReader(knownReaders)).toEqual('reader-1'); |
| 63 | + expect(strategy.selectReader(knownReaders)).toEqual('reader-1'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should return same writer when it is the only one available and has active connections', () => { |
| 67 | + const knownWriters = ['writer-1']; |
| 68 | + const strategy = new LeastConnectedLoadBalancingStrategy(new DummyPool({'writer-1': 3})); |
| 69 | + |
| 70 | + expect(strategy.selectWriter(knownWriters)).toEqual('writer-1'); |
| 71 | + expect(strategy.selectWriter(knownWriters)).toEqual('writer-1'); |
| 72 | + expect(strategy.selectWriter(knownWriters)).toEqual('writer-1'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should return readers in round robin order when no active connections', () => { |
| 76 | + const knownReaders = ['reader-1', 'reader-2', 'reader-3']; |
| 77 | + const pool = new DummyPool({'reader-1': 0, 'reader-2': 0, 'reader-3': 0}); |
| 78 | + const strategy = new LeastConnectedLoadBalancingStrategy(pool); |
| 79 | + |
| 80 | + expect(strategy.selectReader(knownReaders)).toEqual('reader-1'); |
| 81 | + expect(strategy.selectReader(knownReaders)).toEqual('reader-2'); |
| 82 | + expect(strategy.selectReader(knownReaders)).toEqual('reader-3'); |
| 83 | + expect(strategy.selectReader(knownReaders)).toEqual('reader-1'); |
| 84 | + expect(strategy.selectReader(knownReaders)).toEqual('reader-2'); |
| 85 | + expect(strategy.selectReader(knownReaders)).toEqual('reader-3'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should return writers in round robin order when no active connections', () => { |
| 89 | + const knownWriters = ['writer-1', 'writer-2', 'writer-3', 'writer-4']; |
| 90 | + const pool = new DummyPool({'writer-1': 0, 'writer-2': 0, 'writer-3': 0, 'writer-4': 0}); |
| 91 | + const strategy = new LeastConnectedLoadBalancingStrategy(pool); |
| 92 | + |
| 93 | + expect(strategy.selectWriter(knownWriters)).toEqual('writer-1'); |
| 94 | + expect(strategy.selectWriter(knownWriters)).toEqual('writer-2'); |
| 95 | + expect(strategy.selectWriter(knownWriters)).toEqual('writer-3'); |
| 96 | + expect(strategy.selectWriter(knownWriters)).toEqual('writer-4'); |
| 97 | + expect(strategy.selectWriter(knownWriters)).toEqual('writer-1'); |
| 98 | + expect(strategy.selectWriter(knownWriters)).toEqual('writer-2'); |
| 99 | + expect(strategy.selectWriter(knownWriters)).toEqual('writer-3'); |
| 100 | + expect(strategy.selectWriter(knownWriters)).toEqual('writer-4'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should return least connected reader', () => { |
| 104 | + const knownReaders = ['reader-1', 'reader-2', 'reader-3']; |
| 105 | + const pool = new DummyPool({'reader-1': 7, 'reader-2': 3, 'reader-3': 8}); |
| 106 | + const strategy = new LeastConnectedLoadBalancingStrategy(pool); |
| 107 | + |
| 108 | + expect(strategy.selectReader(knownReaders)).toEqual('reader-2'); |
| 109 | + expect(strategy.selectReader(knownReaders)).toEqual('reader-2'); |
| 110 | + expect(strategy.selectReader(knownReaders)).toEqual('reader-2'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should return least connected writer', () => { |
| 114 | + const knownWriters = ['writer-1', 'writer-2', 'writer-3', 'writer-4']; |
| 115 | + const pool = new DummyPool({'writer-1': 5, 'writer-2': 4, 'writer-3': 6, 'writer-4': 2}); |
| 116 | + const strategy = new LeastConnectedLoadBalancingStrategy(pool); |
| 117 | + |
| 118 | + expect(strategy.selectWriter(knownWriters)).toEqual('writer-4'); |
| 119 | + expect(strategy.selectWriter(knownWriters)).toEqual('writer-4'); |
| 120 | + expect(strategy.selectWriter(knownWriters)).toEqual('writer-4'); |
| 121 | + }); |
| 122 | + |
| 123 | +}); |
| 124 | + |
| 125 | +class DummyPool extends Pool { |
| 126 | + |
| 127 | + constructor(activeConnections) { |
| 128 | + super(() => 42); |
| 129 | + this._activeConnections = activeConnections; |
| 130 | + } |
| 131 | + |
| 132 | + activeResourceCount(key) { |
| 133 | + return this._activeConnections[key]; |
| 134 | + } |
| 135 | +} |
0 commit comments