File: collection_spec.rb

package info (click to toggle)
ruby-elasticsearch 7.17.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,712 kB
  • sloc: ruby: 44,120; sh: 16; makefile: 2
file content (266 lines) | stat: -rw-r--r-- 7,027 bytes parent folder | download | duplicates (2)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# 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::Collection do

  describe '#initialize' do

    let(:collection) do
      described_class.new
    end

    it 'has an empty list of connections as a default' do
      expect(collection.connections).to be_empty
    end

    it 'has a default selector class' do
      expect(collection.selector).not_to be_nil
    end

    context 'when a selector class is specified' do

      let(:collection) do
        described_class.new(selector_class: Elasticsearch::Transport::Transport::Connections::Selector::Random)
      end

      it 'sets the selector' do
        expect(collection.selector).to be_a(Elasticsearch::Transport::Transport::Connections::Selector::Random)
      end
    end
  end

  describe '#get_connection' do

    let(:collection) do
      described_class.new(selector_class: Elasticsearch::Transport::Transport::Connections::Selector::Random)
    end

    before do
      expect(collection.selector).to receive(:select).and_return('OK')
    end

    it 'uses the selector to select a connection' do
      expect(collection.get_connection).to eq('OK')
    end
  end

  describe '#hosts' do

    let(:collection) do
      described_class.new(connections: [ double('connection', host: 'A'),
                                         double('connection', host: 'B') ])
    end

    it 'returns a list of hosts' do
      expect(collection.hosts).to eq([ 'A', 'B'])
    end
  end

  describe 'enumerable' do

    let(:collection) do
      described_class.new(connections: [ double('connection', host: 'A', dead?: false),
                                         double('connection', host: 'B', dead?: false) ])
    end

    describe '#map' do

      it 'responds to the method' do
        expect(collection.map { |c| c.host.downcase }).to eq(['a', 'b'])
      end
    end

    describe '#[]' do

      it 'responds to the method' do
        expect(collection[0].host).to eq('A')
        expect(collection[1].host).to eq('B')
      end
    end

    describe '#size' do

      it 'responds to the method' do
        expect(collection.size).to eq(2)
      end
    end

    context 'when a connection is marked as dead' do

      let(:collection) do
        described_class.new(connections: [ double('connection', host: 'A', dead?: true),
                                           double('connection', host: 'B', dead?: false) ])
      end

      it 'does not enumerate the dead connections' do
        expect(collection.size).to eq(1)
        expect(collection.collect { |c| c.host }).to eq(['B'])
      end

      context '#alive' do

        it 'enumerates the alive connections' do
          expect(collection.alive.collect { |c| c.host }).to eq(['B'])
        end
      end

      context '#dead' do

        it 'enumerates the alive connections' do
          expect(collection.dead.collect { |c| c.host }).to eq(['A'])
        end
      end
    end
  end

  describe '#add' do

    let(:collection) do
      described_class.new(connections: [ double('connection', host: 'A', dead?: false),
                                         double('connection', host: 'B', dead?: false) ])
    end

    context 'when an array is provided' do

      before do
        collection.add([double('connection', host: 'C', dead?: false),
                        double('connection', host: 'D', dead?: false)])
      end

      it 'adds the connections' do
        expect(collection.size).to eq(4)
      end
    end

    context 'when an element is provided' do

      before do
        collection.add(double('connection', host: 'C', dead?: false))
      end

      it 'adds the connection' do
        expect(collection.size).to eq(3)
      end
    end
  end

  describe '#remove' do

    let(:connections) do
      [ double('connection', host: 'A', dead?: false),
        double('connection', host: 'B', dead?: false) ]
    end

    let(:collection) do
      described_class.new(connections: connections)
    end

    context 'when an array is provided' do

      before do
        collection.remove(connections)
      end

      it 'removes the connections' do
        expect(collection.size).to eq(0)
      end
    end

    context 'when an element is provided' do

      let(:connections) do
        [ double('connection', host: 'A', dead?: false),
          double('connection', host: 'B', dead?: false) ]
      end

      before do
        collection.remove(connections.first)
      end

      it 'removes the connection' do
        expect(collection.size).to eq(1)
      end
    end
  end

  describe '#get_connection' do

    context 'when all connections are dead' do

      let(:connection_a) do
        Elasticsearch::Transport::Transport::Connections::Connection.new(host: { host: 'A' })
      end

      let(:connection_b) do
        Elasticsearch::Transport::Transport::Connections::Connection.new(host: { host: 'B' })
      end

      let(:collection) do
        described_class.new(connections: [connection_a, connection_b])
      end

      before do
        connection_a.dead!.dead!
        connection_b.dead!
      end

      it 'returns the connection with the least failures' do
        expect(collection.get_connection.host[:host]).to eq('B')
      end
    end

    context 'when multiple threads are used' do

      let(:connections) do
        20.times.collect do |i|
          Elasticsearch::Transport::Transport::Connections::Connection.new(host: { host: i })
        end
      end

      let(:collection) do
        described_class.new(connections: connections)
      end

      it 'allows threads to select connections in parallel' do
       expect(10.times.collect do
          threads = []
          20.times do
            threads << Thread.new do
              collection.get_connection
            end
          end
          threads.map { |t| t.join }
          collection.get_connection.host[:host]
        end).to eq((0..9).to_a)
      end

      it 'always returns a connection' do
        threads = 20.times.map do
          Thread.new do
            20.times.map do
              collection.get_connection.dead!
            end
          end
        end

        expect(threads.flat_map(&:value).size).to eq(400)
      end
    end
  end
end