1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
require 'spec_helper'
describe Elasticsearch::Transport::Transport::Connections::Selector do
before do
class BackupStrategySelector
include Elasticsearch::Transport::Transport::Connections::Selector::Base
def select(options={})
connections.reject do |c|
c.host[:attributes] && c.host[:attributes][:backup]
end.sample
end
end
end
after do
Object.send(:remove_const, :BackupStrategySelector)
end
let(:backup_strategy_selector) do
BackupStrategySelector.new
end
describe 'the Random selector' do
let(:connections) do
[1, 2]
end
let(:selector) do
described_class::Random.new(connections: connections)
end
it 'is initialized with connections' do
expect(selector.connections).to eq(connections)
end
describe '#select' do
let(:connections) do
(0..19).to_a
end
it 'returns a connection' do
expect(selector.select).to be_a(Integer)
end
context 'when multiple threads are used' do
it 'allows threads to select connections in parallel' do
expect(10.times.collect do
threads = []
20.times do
threads << Thread.new do
selector.select
end
end
threads.map { |t| t.join }
selector.select
end).to all(be_a(Integer))
end
end
end
end
describe 'the RoundRobin selector' do
let(:connections) do
['A', 'B', 'C']
end
let(:selector) do
described_class::RoundRobin.new(connections: connections)
end
it 'is initialized with connections' do
expect(selector.connections).to eq(connections)
end
describe '#select' do
it 'rotates over the connections' do
expect(selector.select).to eq('A')
expect(selector.select).to eq('B')
expect(selector.select).to eq('C')
expect(selector.select).to eq('A')
end
context 'when multiple threads are used' do
let(:connections) do
(0..19).to_a
end
it 'returns a connection' do
expect(selector.select).to be_a(Integer)
end
it 'allows threads to select connections in parallel' do
expect(10.times.collect do
threads = []
20.times do
threads << Thread.new do
selector.select
end
end
threads.map { |t| t.join }
selector.select
end).to eq((0..9).to_a)
end
end
end
end
describe 'a custom selector' do
let(:connections) do
[ double(host: { hostname: 'host1' }),
double(host: { hostname: 'host2', attributes: { backup: true } }) ]
end
let(:selector) do
BackupStrategySelector.new(connections: connections)
end
it 'is initialized with connections' do
expect(selector.connections).to eq(connections)
end
describe '#select' do
it 'applies the custom strategy' do
10.times { expect(selector.select.host[:hostname]).to eq('host1') }
end
end
end
context 'when the Base module is included in a class' do
before do
class ExampleSelector
include Elasticsearch::Transport::Transport::Connections::Selector::Base
end
end
after do
Object.send(:remove_const, :ExampleSelector)
end
it 'requires the #select method to be redefined' do
expect {
ExampleSelector.new.select
}.to raise_exception(NoMethodError)
end
end
end
|