elasticsearch-transport 7.1.0 → 7.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +3 -16
- data/{LICENSE.txt → LICENSE} +0 -0
- data/README.md +18 -19
- data/Rakefile +3 -16
- data/elasticsearch-transport.gemspec +7 -19
- data/lib/elasticsearch/transport/client.rb +27 -24
- data/lib/elasticsearch/transport/redacted.rb +3 -16
- data/lib/elasticsearch/transport/transport/base.rb +81 -26
- data/lib/elasticsearch/transport/transport/connections/collection.rb +3 -16
- data/lib/elasticsearch/transport/transport/connections/connection.rb +3 -16
- data/lib/elasticsearch/transport/transport/connections/selector.rb +20 -21
- data/lib/elasticsearch/transport/transport/errors.rb +3 -16
- data/lib/elasticsearch/transport/transport/http/curb.rb +28 -24
- data/lib/elasticsearch/transport/transport/http/faraday.rb +19 -18
- data/lib/elasticsearch/transport/transport/http/manticore.rb +27 -25
- data/lib/elasticsearch/transport/transport/loggable.rb +3 -16
- data/lib/elasticsearch/transport/transport/response.rb +3 -16
- data/lib/elasticsearch/transport/transport/serializer/multi_json.rb +3 -16
- data/lib/elasticsearch/transport/transport/sniffer.rb +5 -17
- data/lib/elasticsearch/transport/version.rb +4 -17
- data/lib/elasticsearch/transport.rb +3 -16
- data/lib/elasticsearch-transport.rb +3 -16
- data/spec/elasticsearch/connections/collection_spec.rb +241 -0
- data/spec/elasticsearch/connections/selector_spec.rb +161 -0
- data/spec/elasticsearch/transport/base_spec.rb +186 -24
- data/spec/elasticsearch/transport/client_spec.rb +350 -19
- data/spec/elasticsearch/transport/sniffer_spec.rb +3 -16
- data/spec/spec_helper.rb +6 -0
- data/test/integration/transport_test.rb +3 -16
- data/test/profile/client_benchmark_test.rb +3 -16
- data/test/test_helper.rb +3 -16
- data/test/unit/connection_test.rb +3 -16
- data/test/unit/response_test.rb +3 -16
- data/test/unit/serializer_test.rb +3 -16
- data/test/unit/transport_base_test.rb +3 -16
- data/test/unit/transport_curb_test.rb +4 -17
- data/test/unit/transport_faraday_test.rb +3 -16
- data/test/unit/transport_manticore_test.rb +30 -27
- metadata +42 -16
- data/test/unit/connection_collection_test.rb +0 -147
- data/test/unit/connection_selector_test.rb +0 -81
@@ -1,19 +1,6 @@
|
|
1
|
-
# Licensed to Elasticsearch B.V
|
2
|
-
#
|
3
|
-
#
|
4
|
-
# ownership. Elasticsearch B.V. licenses this file to you under
|
5
|
-
# the Apache License, Version 2.0 (the "License"); you may
|
6
|
-
# not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing,
|
12
|
-
# software distributed under the License is distributed on an
|
13
|
-
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14
|
-
# KIND, either express or implied. See the License for the
|
15
|
-
# specific language governing permissions and limitations
|
16
|
-
# under the License.
|
1
|
+
# Licensed to Elasticsearch B.V under one or more agreements.
|
2
|
+
# Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
|
3
|
+
# See the LICENSE file in the project root for more information
|
17
4
|
|
18
5
|
require 'test_helper'
|
19
6
|
|
@@ -56,32 +43,42 @@ else
|
|
56
43
|
|
57
44
|
should "set body for GET request" do
|
58
45
|
@transport.connections.first.connection.expects(:get).
|
59
|
-
with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}'
|
46
|
+
with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}',
|
47
|
+
:headers => {"Content-Type" => "application/json",
|
48
|
+
"User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
|
60
49
|
@transport.perform_request 'GET', '/', {}, '{"foo":"bar"}'
|
61
50
|
end
|
62
51
|
|
63
52
|
should "set body for PUT request" do
|
64
53
|
@transport.connections.first.connection.expects(:put).
|
65
|
-
with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}'
|
54
|
+
with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}',
|
55
|
+
:headers => {"Content-Type" => "application/json",
|
56
|
+
"User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
|
66
57
|
@transport.perform_request 'PUT', '/', {}, {:foo => 'bar'}
|
67
58
|
end
|
68
59
|
|
69
60
|
should "serialize the request body" do
|
70
61
|
@transport.connections.first.connection.expects(:post).
|
71
|
-
with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}'
|
62
|
+
with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}',
|
63
|
+
:headers => {"Content-Type" => "application/json",
|
64
|
+
"User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
|
72
65
|
@transport.perform_request 'POST', '/', {}, {'foo' => 'bar'}
|
73
66
|
end
|
74
67
|
|
75
68
|
should "set custom headers for PUT request" do
|
76
69
|
@transport.connections.first.connection.expects(:put).
|
77
|
-
with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}',
|
70
|
+
with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}',
|
71
|
+
:headers => {"Content-Type" => "application/json",
|
72
|
+
"User-Agent" => @transport.send(:user_agent_header)}})
|
78
73
|
.returns(stub_everything)
|
79
74
|
@transport.perform_request 'PUT', '/', {}, '{"foo":"bar"}', {"Content-Type" => "application/x-ndjson"}
|
80
75
|
end
|
81
76
|
|
82
77
|
should "not serialize a String request body" do
|
83
78
|
@transport.connections.first.connection.expects(:post).
|
84
|
-
with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}'
|
79
|
+
with('http://127.0.0.1:8080//', {:body => '{"foo":"bar"}',
|
80
|
+
:headers => {"Content-Type" => "application/json",
|
81
|
+
"User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
|
85
82
|
@transport.serializer.expects(:dump).never
|
86
83
|
@transport.perform_request 'POST', '/', {}, '{"foo":"bar"}'
|
87
84
|
end
|
@@ -93,7 +90,8 @@ else
|
|
93
90
|
|
94
91
|
transport = Manticore.new :hosts => [ { :host => 'localhost', :port => 8080 } ], :options => options
|
95
92
|
|
96
|
-
transport.connections.first.connection.stub("http://localhost:8080//", :body => "\"\"", :headers => {"
|
93
|
+
transport.connections.first.connection.stub("http://localhost:8080//", :body => "\"\"", :headers => {"Content-Type" => "application/x-ndjson",
|
94
|
+
"User-Agent" => @transport.send(:user_agent_header)}, :code => 200 )
|
97
95
|
|
98
96
|
response = transport.perform_request 'GET', '/', {}
|
99
97
|
assert_equal response.status, 200
|
@@ -113,11 +111,16 @@ else
|
|
113
111
|
end
|
114
112
|
|
115
113
|
should "handle HTTP methods" do
|
116
|
-
@transport.connections.first.connection.expects(:delete).with('http://127.0.0.1:8080//', {
|
117
|
-
|
118
|
-
@transport.connections.first.connection.expects(:
|
119
|
-
|
120
|
-
@transport.connections.first.connection.expects(:
|
114
|
+
@transport.connections.first.connection.expects(:delete).with('http://127.0.0.1:8080//', { headers: {"Content-Type" => "application/json",
|
115
|
+
"User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
|
116
|
+
@transport.connections.first.connection.expects(:head).with('http://127.0.0.1:8080//', { headers: {"Content-Type" => "application/json",
|
117
|
+
"User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
|
118
|
+
@transport.connections.first.connection.expects(:get).with('http://127.0.0.1:8080//', { headers: {"Content-Type" => "application/json",
|
119
|
+
"User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
|
120
|
+
@transport.connections.first.connection.expects(:put).with('http://127.0.0.1:8080//', { headers: {"Content-Type" => "application/json",
|
121
|
+
"User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
|
122
|
+
@transport.connections.first.connection.expects(:post).with('http://127.0.0.1:8080//', { headers: {"Content-Type" => "application/json",
|
123
|
+
"User-Agent" => @transport.send(:user_agent_header)}}).returns(stub_everything)
|
121
124
|
|
122
125
|
%w| HEAD GET PUT POST DELETE |.each { |method| @transport.perform_request method, '/' }
|
123
126
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: elasticsearch-transport
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 7.
|
4
|
+
version: 7.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Karel Minarik
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-02-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: multi_json
|
@@ -30,14 +30,20 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
33
|
+
version: '0.14'
|
34
|
+
- - "<"
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '1'
|
34
37
|
type: :runtime
|
35
38
|
prerelease: false
|
36
39
|
version_requirements: !ruby/object:Gem::Requirement
|
37
40
|
requirements:
|
38
41
|
- - ">="
|
39
42
|
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
43
|
+
version: '0.14'
|
44
|
+
- - "<"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '1'
|
41
47
|
- !ruby/object:Gem::Dependency
|
42
48
|
name: bundler
|
43
49
|
requirement: !ruby/object:Gem::Requirement
|
@@ -192,6 +198,20 @@ dependencies:
|
|
192
198
|
- - ">="
|
193
199
|
- !ruby/object:Gem::Version
|
194
200
|
version: '0'
|
201
|
+
- !ruby/object:Gem::Dependency
|
202
|
+
name: httpclient
|
203
|
+
requirement: !ruby/object:Gem::Requirement
|
204
|
+
requirements:
|
205
|
+
- - ">="
|
206
|
+
- !ruby/object:Gem::Version
|
207
|
+
version: '0'
|
208
|
+
type: :development
|
209
|
+
prerelease: false
|
210
|
+
version_requirements: !ruby/object:Gem::Requirement
|
211
|
+
requirements:
|
212
|
+
- - ">="
|
213
|
+
- !ruby/object:Gem::Version
|
214
|
+
version: '0'
|
195
215
|
- !ruby/object:Gem::Dependency
|
196
216
|
name: hashie
|
197
217
|
requirement: !ruby/object:Gem::Requirement
|
@@ -280,16 +300,22 @@ dependencies:
|
|
280
300
|
name: simplecov
|
281
301
|
requirement: !ruby/object:Gem::Requirement
|
282
302
|
requirements:
|
283
|
-
- - "
|
303
|
+
- - "~>"
|
284
304
|
- !ruby/object:Gem::Version
|
285
|
-
version: '0'
|
305
|
+
version: '0.17'
|
306
|
+
- - "<"
|
307
|
+
- !ruby/object:Gem::Version
|
308
|
+
version: '0.18'
|
286
309
|
type: :development
|
287
310
|
prerelease: false
|
288
311
|
version_requirements: !ruby/object:Gem::Requirement
|
289
312
|
requirements:
|
290
|
-
- - "
|
313
|
+
- - "~>"
|
291
314
|
- !ruby/object:Gem::Version
|
292
|
-
version: '0'
|
315
|
+
version: '0.17'
|
316
|
+
- - "<"
|
317
|
+
- !ruby/object:Gem::Version
|
318
|
+
version: '0.18'
|
293
319
|
- !ruby/object:Gem::Dependency
|
294
320
|
name: simplecov-rcov
|
295
321
|
requirement: !ruby/object:Gem::Requirement
|
@@ -335,18 +361,18 @@ dependencies:
|
|
335
361
|
description: 'Ruby client for Elasticsearch. See the `elasticsearch` gem for full
|
336
362
|
integration.
|
337
363
|
|
338
|
-
|
364
|
+
'
|
339
365
|
email:
|
340
366
|
- karel.minarik@elasticsearch.org
|
341
367
|
executables: []
|
342
368
|
extensions: []
|
343
369
|
extra_rdoc_files:
|
344
370
|
- README.md
|
345
|
-
- LICENSE
|
371
|
+
- LICENSE
|
346
372
|
files:
|
347
373
|
- ".gitignore"
|
348
374
|
- Gemfile
|
349
|
-
- LICENSE
|
375
|
+
- LICENSE
|
350
376
|
- README.md
|
351
377
|
- Rakefile
|
352
378
|
- elasticsearch-transport.gemspec
|
@@ -367,6 +393,8 @@ files:
|
|
367
393
|
- lib/elasticsearch/transport/transport/serializer/multi_json.rb
|
368
394
|
- lib/elasticsearch/transport/transport/sniffer.rb
|
369
395
|
- lib/elasticsearch/transport/version.rb
|
396
|
+
- spec/elasticsearch/connections/collection_spec.rb
|
397
|
+
- spec/elasticsearch/connections/selector_spec.rb
|
370
398
|
- spec/elasticsearch/transport/base_spec.rb
|
371
399
|
- spec/elasticsearch/transport/client_spec.rb
|
372
400
|
- spec/elasticsearch/transport/sniffer_spec.rb
|
@@ -374,8 +402,6 @@ files:
|
|
374
402
|
- test/integration/transport_test.rb
|
375
403
|
- test/profile/client_benchmark_test.rb
|
376
404
|
- test/test_helper.rb
|
377
|
-
- test/unit/connection_collection_test.rb
|
378
|
-
- test/unit/connection_selector_test.rb
|
379
405
|
- test/unit/connection_test.rb
|
380
406
|
- test/unit/response_test.rb
|
381
407
|
- test/unit/serializer_test.rb
|
@@ -403,11 +429,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
403
429
|
- !ruby/object:Gem::Version
|
404
430
|
version: '0'
|
405
431
|
requirements: []
|
406
|
-
rubygems_version: 3.0.
|
432
|
+
rubygems_version: 3.0.6
|
407
433
|
signing_key:
|
408
434
|
specification_version: 4
|
409
435
|
summary: Ruby client for Elasticsearch.
|
410
436
|
test_files:
|
437
|
+
- spec/elasticsearch/connections/collection_spec.rb
|
438
|
+
- spec/elasticsearch/connections/selector_spec.rb
|
411
439
|
- spec/elasticsearch/transport/base_spec.rb
|
412
440
|
- spec/elasticsearch/transport/client_spec.rb
|
413
441
|
- spec/elasticsearch/transport/sniffer_spec.rb
|
@@ -415,8 +443,6 @@ test_files:
|
|
415
443
|
- test/integration/transport_test.rb
|
416
444
|
- test/profile/client_benchmark_test.rb
|
417
445
|
- test/test_helper.rb
|
418
|
-
- test/unit/connection_collection_test.rb
|
419
|
-
- test/unit/connection_selector_test.rb
|
420
446
|
- test/unit/connection_test.rb
|
421
447
|
- test/unit/response_test.rb
|
422
448
|
- test/unit/serializer_test.rb
|
@@ -1,147 +0,0 @@
|
|
1
|
-
# Licensed to Elasticsearch B.V. under one or more contributor
|
2
|
-
# license agreements. See the NOTICE file distributed with
|
3
|
-
# this work for additional information regarding copyright
|
4
|
-
# ownership. Elasticsearch B.V. licenses this file to you under
|
5
|
-
# the Apache License, Version 2.0 (the "License"); you may
|
6
|
-
# not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing,
|
12
|
-
# software distributed under the License is distributed on an
|
13
|
-
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14
|
-
# KIND, either express or implied. See the License for the
|
15
|
-
# specific language governing permissions and limitations
|
16
|
-
# under the License.
|
17
|
-
|
18
|
-
require 'test_helper'
|
19
|
-
|
20
|
-
class Elasticsearch::Transport::Transport::Connections::CollectionTest < Minitest::Test
|
21
|
-
include Elasticsearch::Transport::Transport::Connections
|
22
|
-
|
23
|
-
context "Connection collection" do
|
24
|
-
|
25
|
-
should "have empty array as default connections array" do
|
26
|
-
assert_equal [], Collection.new.connections
|
27
|
-
end
|
28
|
-
|
29
|
-
should "have default selector class" do
|
30
|
-
assert_not_nil Collection.new.selector
|
31
|
-
end
|
32
|
-
|
33
|
-
should "initialize a custom selector class" do
|
34
|
-
c = Collection.new :selector_class => Selector::Random
|
35
|
-
assert_instance_of Selector::Random, c.selector
|
36
|
-
end
|
37
|
-
|
38
|
-
should "take a custom selector instance" do
|
39
|
-
c = Collection.new :selector => Selector::Random.new
|
40
|
-
assert_instance_of Selector::Random, c.selector
|
41
|
-
end
|
42
|
-
|
43
|
-
should "get connection from selector" do
|
44
|
-
c = Collection.new
|
45
|
-
c.selector.expects(:select).returns('OK')
|
46
|
-
assert_equal 'OK', c.get_connection
|
47
|
-
end
|
48
|
-
|
49
|
-
should "return an array of hosts" do
|
50
|
-
c = Collection.new :connections => [ Connection.new(:host => 'foo'), Connection.new(:host => 'bar') ]
|
51
|
-
assert_equal ['foo', 'bar'], c.hosts
|
52
|
-
end
|
53
|
-
|
54
|
-
should "be enumerable" do
|
55
|
-
c = Collection.new :connections => [ Connection.new(:host => 'foo'), Connection.new(:host => 'bar') ]
|
56
|
-
|
57
|
-
assert_equal ['FOO', 'BAR'], c.map { |i| i.host.upcase }
|
58
|
-
assert_equal 'foo', c[0].host
|
59
|
-
assert_equal 'bar', c[1].host
|
60
|
-
assert_equal 2, c.size
|
61
|
-
end
|
62
|
-
|
63
|
-
should "add connections" do
|
64
|
-
c = Collection.new :connections => [ Connection.new(:host => { :protocol => 'http', :host => 'foo', :port => 1}) ]
|
65
|
-
assert_equal 1, c.size
|
66
|
-
|
67
|
-
c.add([ Connection.new(:host => { :protocol => 'http', :host => 'bar', :port => 1 }),
|
68
|
-
Connection.new(:host => { :protocol => 'http', :host => 'bam', :port => 1 }) ])
|
69
|
-
assert_equal 3, c.size
|
70
|
-
end
|
71
|
-
|
72
|
-
should "add connection" do
|
73
|
-
c = Collection.new :connections => [ Connection.new(:host => { :protocol => 'http', :host => 'foo', :port => 1}) ]
|
74
|
-
assert_equal 1, c.size
|
75
|
-
|
76
|
-
c.add(Connection.new(:host => { :protocol => 'http', :host => 'bar', :port => 1 }))
|
77
|
-
assert_equal 2, c.size
|
78
|
-
end
|
79
|
-
|
80
|
-
should "remove connections" do
|
81
|
-
c = Collection.new :connections => [
|
82
|
-
Connection.new(:host => { :protocol => 'http', :host => 'foo', :port => 1 }),
|
83
|
-
Connection.new(:host => { :protocol => 'http', :host => 'bar', :port => 1 })
|
84
|
-
]
|
85
|
-
assert_equal 2, c.size
|
86
|
-
|
87
|
-
c.remove([c.first])
|
88
|
-
assert_equal 1, c.size
|
89
|
-
|
90
|
-
c.remove(c)
|
91
|
-
assert_equal 0, c.size
|
92
|
-
end
|
93
|
-
|
94
|
-
should "remove connection" do
|
95
|
-
c = Collection.new :connections => [
|
96
|
-
Connection.new(:host => { :protocol => 'http', :host => 'foo', :port => 1 }),
|
97
|
-
Connection.new(:host => { :protocol => 'http', :host => 'bar', :port => 1 })
|
98
|
-
]
|
99
|
-
assert_equal 2, c.size
|
100
|
-
|
101
|
-
c.remove(c.first)
|
102
|
-
assert_equal 1, c.size
|
103
|
-
end
|
104
|
-
|
105
|
-
context "with the dead pool" do
|
106
|
-
setup do
|
107
|
-
@collection = Collection.new :connections => [ Connection.new(:host => 'foo'), Connection.new(:host => 'bar') ]
|
108
|
-
@collection[1].dead!
|
109
|
-
end
|
110
|
-
|
111
|
-
should "not iterate over dead connections" do
|
112
|
-
assert_equal 1, @collection.size
|
113
|
-
assert_equal ['FOO'], @collection.map { |c| c.host.upcase }
|
114
|
-
assert_equal @collection.connections, @collection.alive
|
115
|
-
end
|
116
|
-
|
117
|
-
should "have dead connections collection" do
|
118
|
-
assert_equal 1, @collection.dead.size
|
119
|
-
assert_equal ['BAR'], @collection.dead.map { |c| c.host.upcase }
|
120
|
-
end
|
121
|
-
|
122
|
-
should "not return dead connections, when alive connections exist" do
|
123
|
-
assert_equal 1, @collection.size
|
124
|
-
@collection.all.size.times { refute @collection.get_connection.dead? }
|
125
|
-
end
|
126
|
-
|
127
|
-
should "resurrect dead connection with least failures when no alive is available" do
|
128
|
-
c1 = Connection.new(:host => { :protocol => 'http', :host => 'foo', :port => 123 }).dead!.dead!
|
129
|
-
c2 = Connection.new(:host => { :protocol => 'http', :host => 'bar', :port => 123 }).dead!
|
130
|
-
|
131
|
-
@collection = Collection.new :connections => [ c1, c2 ]
|
132
|
-
|
133
|
-
assert_equal 0, @collection.size
|
134
|
-
assert_not_nil @collection.get_connection
|
135
|
-
assert_equal 1, @collection.size
|
136
|
-
assert_equal c2, @collection.first
|
137
|
-
end
|
138
|
-
|
139
|
-
should "return all connections" do
|
140
|
-
assert_equal 2, @collection.all.size
|
141
|
-
end
|
142
|
-
|
143
|
-
end
|
144
|
-
|
145
|
-
end
|
146
|
-
|
147
|
-
end
|
@@ -1,81 +0,0 @@
|
|
1
|
-
# Licensed to Elasticsearch B.V. under one or more contributor
|
2
|
-
# license agreements. See the NOTICE file distributed with
|
3
|
-
# this work for additional information regarding copyright
|
4
|
-
# ownership. Elasticsearch B.V. licenses this file to you under
|
5
|
-
# the Apache License, Version 2.0 (the "License"); you may
|
6
|
-
# not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing,
|
12
|
-
# software distributed under the License is distributed on an
|
13
|
-
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14
|
-
# KIND, either express or implied. See the License for the
|
15
|
-
# specific language governing permissions and limitations
|
16
|
-
# under the License.
|
17
|
-
|
18
|
-
require 'test_helper'
|
19
|
-
|
20
|
-
class Elasticsearch::Transport::Transport::Connections::SelectorTest < Minitest::Test
|
21
|
-
include Elasticsearch::Transport::Transport::Connections::Selector
|
22
|
-
|
23
|
-
class DummyStrategySelector
|
24
|
-
include Elasticsearch::Transport::Transport::Connections::Selector::Base
|
25
|
-
end
|
26
|
-
|
27
|
-
class BackupStrategySelector
|
28
|
-
include Elasticsearch::Transport::Transport::Connections::Selector::Base
|
29
|
-
|
30
|
-
def select(options={})
|
31
|
-
connections.reject do |c|
|
32
|
-
c.host[:attributes] && c.host[:attributes][:backup]
|
33
|
-
end.send( defined?(RUBY_VERSION) && RUBY_VERSION > '1.9' ? :sample : :choice)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
context "Connection selector" do
|
38
|
-
|
39
|
-
should "be initialized with connections" do
|
40
|
-
assert_equal [1, 2], Random.new(:connections => [1, 2]).connections
|
41
|
-
end
|
42
|
-
|
43
|
-
should "have the abstract select method" do
|
44
|
-
assert_raise(NoMethodError) { DummyStrategySelector.new.select }
|
45
|
-
end
|
46
|
-
|
47
|
-
context "in random strategy" do
|
48
|
-
setup do
|
49
|
-
@selector = Random.new :connections => ['A', 'B', 'C']
|
50
|
-
end
|
51
|
-
|
52
|
-
should "pick a connection" do
|
53
|
-
assert_not_nil @selector.select
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
context "in round-robin strategy" do
|
58
|
-
setup do
|
59
|
-
@selector = RoundRobin.new :connections => ['A', 'B', 'C']
|
60
|
-
end
|
61
|
-
|
62
|
-
should "rotate over connections" do
|
63
|
-
assert_equal 'A', @selector.select
|
64
|
-
assert_equal 'B', @selector.select
|
65
|
-
assert_equal 'C', @selector.select
|
66
|
-
assert_equal 'A', @selector.select
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
context "with a custom strategy" do
|
71
|
-
|
72
|
-
should "return proper connection" do
|
73
|
-
selector = BackupStrategySelector.new :connections => [ stub(:host => { :hostname => 'host1' }),
|
74
|
-
stub(:host => { :hostname => 'host2', :attributes => { :backup => true }}) ]
|
75
|
-
10.times { assert_equal 'host1', selector.select.host[:hostname] }
|
76
|
-
end
|
77
|
-
|
78
|
-
end
|
79
|
-
|
80
|
-
end
|
81
|
-
end
|