elasticsearch-transport-pixlee 1.0.13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/Gemfile +16 -0
  4. data/LICENSE.txt +13 -0
  5. data/README.md +441 -0
  6. data/Rakefile +80 -0
  7. data/elasticsearch-transport.gemspec +74 -0
  8. data/lib/elasticsearch-transport.rb +1 -0
  9. data/lib/elasticsearch/transport.rb +30 -0
  10. data/lib/elasticsearch/transport/client.rb +195 -0
  11. data/lib/elasticsearch/transport/transport/base.rb +261 -0
  12. data/lib/elasticsearch/transport/transport/connections/collection.rb +93 -0
  13. data/lib/elasticsearch/transport/transport/connections/connection.rb +121 -0
  14. data/lib/elasticsearch/transport/transport/connections/selector.rb +63 -0
  15. data/lib/elasticsearch/transport/transport/errors.rb +73 -0
  16. data/lib/elasticsearch/transport/transport/http/curb.rb +87 -0
  17. data/lib/elasticsearch/transport/transport/http/faraday.rb +60 -0
  18. data/lib/elasticsearch/transport/transport/http/manticore.rb +124 -0
  19. data/lib/elasticsearch/transport/transport/response.rb +21 -0
  20. data/lib/elasticsearch/transport/transport/serializer/multi_json.rb +36 -0
  21. data/lib/elasticsearch/transport/transport/sniffer.rb +46 -0
  22. data/lib/elasticsearch/transport/version.rb +5 -0
  23. data/test/integration/client_test.rb +144 -0
  24. data/test/integration/transport_test.rb +73 -0
  25. data/test/profile/client_benchmark_test.rb +125 -0
  26. data/test/test_helper.rb +76 -0
  27. data/test/unit/client_test.rb +274 -0
  28. data/test/unit/connection_collection_test.rb +88 -0
  29. data/test/unit/connection_selector_test.rb +64 -0
  30. data/test/unit/connection_test.rb +100 -0
  31. data/test/unit/response_test.rb +15 -0
  32. data/test/unit/serializer_test.rb +16 -0
  33. data/test/unit/sniffer_test.rb +145 -0
  34. data/test/unit/transport_base_test.rb +478 -0
  35. data/test/unit/transport_curb_test.rb +97 -0
  36. data/test/unit/transport_faraday_test.rb +140 -0
  37. data/test/unit/transport_manticore_test.rb +118 -0
  38. metadata +408 -0
@@ -0,0 +1,125 @@
1
+ require 'test_helper'
2
+
3
+ class Elasticsearch::Transport::ClientProfilingTest < Elasticsearch::Test::ProfilingTest
4
+ startup do
5
+ Elasticsearch::Extensions::Test::Cluster.start if ENV['SERVER'] and not Elasticsearch::Extensions::Test::Cluster.running?
6
+ end
7
+
8
+ context "Elasticsearch client benchmark" do
9
+ setup do
10
+ @port = (ENV['TEST_CLUSTER_PORT'] || 9250).to_i
11
+ client = Elasticsearch::Client.new host: "localhost:#{@port}", adapter: ::Faraday.default_adapter
12
+ client.perform_request 'DELETE', '/ruby_test_benchmark/' rescue nil
13
+ client.perform_request 'POST', '/ruby_test_benchmark/', {index: {number_of_shards: 1, number_of_replicas: 0}}
14
+ 100.times do client.perform_request 'POST', '/ruby_test_benchmark_search/test/', {}, {foo: 'bar'}; end
15
+ client.perform_request 'POST', '/ruby_test_benchmark_search/_refresh'
16
+ end
17
+ teardown do
18
+ client = Elasticsearch::Client.new host: "localhost:#{@port}"
19
+ client.perform_request 'DELETE', '/ruby_test_benchmark/'
20
+ client.perform_request 'DELETE', '/ruby_test_benchmark_search/'
21
+ end
22
+
23
+ context "with a single-node cluster and the default adapter" do
24
+ setup do
25
+ @client = Elasticsearch::Client.new hosts: "localhost:#{@port}", adapter: ::Faraday.default_adapter
26
+ end
27
+
28
+ measure "get the cluster info", count: 1_000 do
29
+ @client.perform_request 'GET', ''
30
+ end
31
+
32
+ measure "index a document" do
33
+ @client.perform_request 'POST', '/ruby_test_benchmark/test/', {}, {foo: 'bar'}
34
+ end
35
+
36
+ measure "search" do
37
+ @client.perform_request 'POST', '/ruby_test_benchmark_search/test/_search', {}, {query: {match: {foo: 'bar'}}}
38
+ end
39
+ end
40
+
41
+ context "with a two-node cluster and the default adapter" do
42
+ setup do
43
+ @client = Elasticsearch::Client.new hosts: ["localhost:#{@port}", "localhost:#{@port+1}"], adapter: ::Faraday.default_adapter
44
+ end
45
+
46
+ measure "get the cluster info", count: 1_000 do
47
+ @client.perform_request 'GET', ''
48
+ end
49
+
50
+ measure "index a document"do
51
+ @client.perform_request 'POST', '/ruby_test_benchmark/test/', {}, {foo: 'bar'}
52
+ end
53
+
54
+ measure "search" do
55
+ @client.perform_request 'POST', '/ruby_test_benchmark_search/test/_search', {}, {query: {match: {foo: 'bar'}}}
56
+ end
57
+ end
58
+
59
+ context "with a single-node cluster and the Curb client" do
60
+ setup do
61
+ require 'curb'
62
+ require 'elasticsearch/transport/transport/http/curb'
63
+ @client = Elasticsearch::Client.new host: "localhost:#{@port}",
64
+ transport_class: Elasticsearch::Transport::Transport::HTTP::Curb
65
+ end
66
+
67
+ measure "get the cluster info", count: 1_000 do
68
+ @client.perform_request 'GET', ''
69
+ end
70
+
71
+ measure "index a document" do
72
+ @client.perform_request 'POST', '/ruby_test_benchmark/test/', {}, {foo: 'bar'}
73
+ end
74
+
75
+ measure "search" do
76
+ @client.perform_request 'POST', '/ruby_test_benchmark_search/test/_search', {}, {query: {match: {foo: 'bar'}}}
77
+ end
78
+ end
79
+
80
+ context "with a single-node cluster and the Typhoeus client" do
81
+ require 'typhoeus'
82
+ require 'typhoeus/adapters/faraday'
83
+
84
+ setup do
85
+ transport = Elasticsearch::Transport::Transport::HTTP::Faraday.new \
86
+ :hosts => [ { :host => 'localhost', :port => @port } ] do |f|
87
+ f.adapter :typhoeus
88
+ end
89
+
90
+ @client = Elasticsearch::Client.new transport: transport
91
+ end
92
+
93
+ measure "get the cluster info", count: 1_000 do
94
+ @client.perform_request 'GET', ''
95
+ end
96
+
97
+ measure "index a document" do
98
+ @client.perform_request 'POST', '/ruby_test_benchmark/test/', {}, {foo: 'bar'}
99
+ end
100
+
101
+ measure "search" do
102
+ @client.perform_request 'POST', '/ruby_test_benchmark_search/test/_search', {}, {query: {match: {foo: 'bar'}}}
103
+ end
104
+ end
105
+
106
+ context "with a single-node cluster and the Patron adapter" do
107
+ setup do
108
+ require 'patron'
109
+ @client = Elasticsearch::Client.new host: "localhost:#{@port}", adapter: :patron
110
+ end
111
+
112
+ measure "get the cluster info", count: 1_000 do
113
+ @client.perform_request 'GET', ''
114
+ end
115
+
116
+ measure "index a document" do
117
+ @client.perform_request 'POST', '/ruby_test_benchmark/test/', {}, {foo: 'bar'}
118
+ end
119
+
120
+ measure "search" do
121
+ @client.perform_request 'POST', '/ruby_test_benchmark_search/test/_search', {}, {query: {match: {foo: 'bar'}}}
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,76 @@
1
+ RUBY_1_8 = defined?(RUBY_VERSION) && RUBY_VERSION < '1.9'
2
+ JRUBY = defined?(JRUBY_VERSION)
3
+
4
+ if RUBY_1_8 and not ENV['BUNDLE_GEMFILE']
5
+ require 'rubygems'
6
+ gem 'test-unit'
7
+ end
8
+
9
+ require 'rubygems' if RUBY_1_8
10
+
11
+ if ENV['COVERAGE'] && ENV['CI'].nil? && !RUBY_1_8
12
+ require 'simplecov'
13
+ SimpleCov.start { add_filter "/test|test_/" }
14
+ end
15
+
16
+ if ENV['CI'] && !RUBY_1_8
17
+ require 'simplecov'
18
+ require 'simplecov-rcov'
19
+ SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
20
+ SimpleCov.start { add_filter "/test|test_" }
21
+ end
22
+
23
+ # Register `at_exit` handler for integration tests shutdown.
24
+ # MUST be called before requiring `test/unit`.
25
+ if defined?(RUBY_VERSION) && RUBY_VERSION > '1.9'
26
+ at_exit { Elasticsearch::Test::IntegrationTestCase.__run_at_exit_hooks }
27
+ end
28
+
29
+ require 'test/unit'
30
+ require 'shoulda-context'
31
+ require 'mocha/setup'
32
+ require 'ansi/code'
33
+ require 'turn' unless ENV["TM_FILEPATH"] || ENV["NOTURN"] || RUBY_1_8
34
+
35
+ require 'require-prof' if ENV["REQUIRE_PROF"]
36
+ require 'elasticsearch-transport'
37
+ require 'logger'
38
+
39
+ require 'hashie'
40
+
41
+ RequireProf.print_timing_infos if ENV["REQUIRE_PROF"]
42
+
43
+ if defined?(RUBY_VERSION) && RUBY_VERSION > '1.9'
44
+ require 'elasticsearch/extensions/test/cluster'
45
+ require 'elasticsearch/extensions/test/startup_shutdown'
46
+ require 'elasticsearch/extensions/test/profiling' unless JRUBY
47
+ end
48
+
49
+ class Test::Unit::TestCase
50
+ def setup
51
+ end
52
+
53
+ def teardown
54
+ end
55
+ end
56
+
57
+ module Elasticsearch
58
+ module Test
59
+ class IntegrationTestCase < ::Test::Unit::TestCase
60
+ extend Elasticsearch::Extensions::Test::StartupShutdown
61
+
62
+ shutdown { Elasticsearch::Extensions::Test::Cluster.stop if ENV['SERVER'] && started? && Elasticsearch::Extensions::Test::Cluster.running? }
63
+ context "IntegrationTest" do; should "noop on Ruby 1.8" do; end; end if RUBY_1_8
64
+ end if defined?(RUBY_VERSION) && RUBY_VERSION > '1.9'
65
+ end
66
+
67
+ module Test
68
+ class ProfilingTest < ::Test::Unit::TestCase
69
+ extend Elasticsearch::Extensions::Test::StartupShutdown
70
+ extend Elasticsearch::Extensions::Test::Profiling
71
+
72
+ shutdown { Elasticsearch::Extensions::Test::Cluster.stop if ENV['SERVER'] && started? && Elasticsearch::Extensions::Test::Cluster.running? }
73
+ context "IntegrationTest" do; should "noop on Ruby 1.8" do; end; end if RUBY_1_8
74
+ end unless RUBY_1_8 || JRUBY
75
+ end
76
+ end
@@ -0,0 +1,274 @@
1
+ require 'test_helper'
2
+
3
+ class Elasticsearch::Transport::ClientTest < Test::Unit::TestCase
4
+
5
+ class DummyTransport
6
+ def initialize(*); end
7
+ end
8
+
9
+ context "Client" do
10
+ setup do
11
+ Elasticsearch::Transport::Client::DEFAULT_TRANSPORT_CLASS.any_instance.stubs(:__build_connections)
12
+ @client = Elasticsearch::Transport::Client.new
13
+ end
14
+
15
+ should "be aliased as Elasticsearch::Client" do
16
+ assert_nothing_raised do
17
+ assert_instance_of(Elasticsearch::Transport::Client, Elasticsearch::Client.new)
18
+ end
19
+ end
20
+
21
+ should "have default transport" do
22
+ assert_instance_of Elasticsearch::Transport::Client::DEFAULT_TRANSPORT_CLASS, @client.transport
23
+ end
24
+
25
+ should "instantiate custom transport class" do
26
+ client = Elasticsearch::Transport::Client.new :transport_class => DummyTransport
27
+ assert_instance_of DummyTransport, client.transport
28
+ end
29
+
30
+ should "take custom transport instance" do
31
+ client = Elasticsearch::Transport::Client.new :transport => DummyTransport.new
32
+ assert_instance_of DummyTransport, client.transport
33
+ end
34
+
35
+ should "delegate performing requests to transport" do
36
+ assert_respond_to @client, :perform_request
37
+ @client.transport.expects(:perform_request)
38
+ @client.perform_request 'GET', '/'
39
+ end
40
+
41
+ should "send GET request as POST with the send_get_body_as option" do
42
+ transport = DummyTransport.new
43
+ client = Elasticsearch::Transport::Client.new :transport => transport, :send_get_body_as => 'POST'
44
+ transport.expects(:perform_request).with 'POST', '/', {}, '{"foo":"bar"}'
45
+ client.perform_request 'GET', '/', {}, '{"foo":"bar"}'
46
+ end
47
+
48
+ should "have default logger for transport" do
49
+ client = Elasticsearch::Transport::Client.new :log => true
50
+ assert_respond_to client.transport.logger, :info
51
+ end
52
+
53
+ should "have default tracer for transport" do
54
+ client = Elasticsearch::Transport::Client.new :trace => true
55
+ assert_respond_to client.transport.tracer, :info
56
+ end
57
+
58
+ should "initialize the default transport class" do
59
+ Elasticsearch::Transport::Client::DEFAULT_TRANSPORT_CLASS.any_instance.
60
+ unstub(:__build_connections)
61
+
62
+ client = Elasticsearch::Client.new
63
+ assert_match /Faraday/, client.transport.connections.first.connection.headers['User-Agent']
64
+ end
65
+
66
+ should "pass options to the transport" do
67
+ client = Elasticsearch::Transport::Client.new :transport_options => { :foo => 'bar' }
68
+ assert_equal 'bar', client.transport.options[:transport_options][:foo]
69
+ end
70
+
71
+ should "merge request_timeout to the transport options" do
72
+ client = Elasticsearch::Transport::Client.new :request_timeout => 120
73
+ assert_equal 120, client.transport.options[:transport_options][:request][:timeout]
74
+ end
75
+
76
+ context "when passed hosts" do
77
+ should "have localhost by default" do
78
+ c = Elasticsearch::Transport::Client.new
79
+ assert_equal 'localhost', c.transport.hosts.first[:host]
80
+ end
81
+
82
+ should "take :hosts, :host, :url or :urls" do
83
+ c1 = Elasticsearch::Transport::Client.new :hosts => ['foobar']
84
+ c2 = Elasticsearch::Transport::Client.new :host => 'foobar'
85
+ c3 = Elasticsearch::Transport::Client.new :url => 'foobar'
86
+ c4 = Elasticsearch::Transport::Client.new :urls => 'foo,bar'
87
+
88
+ assert_equal 'foobar', c1.transport.hosts[0][:host]
89
+ assert_equal 'foobar', c2.transport.hosts[0][:host]
90
+ assert_equal 'foobar', c3.transport.hosts[0][:host]
91
+ assert_equal 'foo', c4.transport.hosts[0][:host]
92
+ assert_equal 'bar', c4.transport.hosts[1][:host]
93
+ end
94
+
95
+ end
96
+
97
+ context "when the URL is set in the environment variable" do
98
+ setup { ENV['ELASTICSEARCH_URL'] = 'foobar' }
99
+ teardown { ENV.delete('ELASTICSEARCH_URL') }
100
+
101
+ should "use a single host" do
102
+ c = Elasticsearch::Transport::Client.new
103
+
104
+ assert_equal 1, c.transport.hosts.size
105
+ assert_equal 'foobar', c.transport.hosts.first[:host]
106
+ end
107
+
108
+ should "use multiple hosts" do
109
+ ENV['ELASTICSEARCH_URL'] = 'foo, bar'
110
+ c = Elasticsearch::Transport::Client.new
111
+
112
+ assert_equal 2, c.transport.hosts.size
113
+ assert_equal 'foo', c.transport.hosts[0][:host]
114
+ assert_equal 'bar', c.transport.hosts[1][:host]
115
+ end
116
+ end
117
+
118
+ context "extracting hosts" do
119
+ should "extract from string" do
120
+ hosts = @client.__extract_hosts 'myhost'
121
+
122
+ assert_equal 'myhost', hosts[0][:host]
123
+ assert_nil hosts[0][:port]
124
+ end
125
+
126
+ should "extract from hash" do
127
+ hosts = @client.__extract_hosts( { :host => 'myhost', :scheme => 'https' } )
128
+ assert_equal 'myhost', hosts[0][:host]
129
+ assert_equal 'https', hosts[0][:scheme]
130
+ assert_nil hosts[0][:port]
131
+ end
132
+
133
+ should "extract from hash with a port passed as a string" do
134
+ hosts = @client.__extract_hosts( { :host => 'myhost', :scheme => 'https', :port => '443' } )
135
+ assert_equal 443, hosts[0][:port]
136
+ end
137
+
138
+ should "extract from hash with a port passed as an integer" do
139
+ hosts = @client.__extract_hosts( { :host => 'myhost', :scheme => 'https', :port => 443 } )
140
+ assert_equal 443, hosts[0][:port]
141
+ end
142
+
143
+ should "extract from Hashie::Mash" do
144
+ hosts = @client.__extract_hosts( Hashie::Mash.new(:host => 'myhost', :scheme => 'https') )
145
+ assert_equal 'myhost', hosts[0][:host]
146
+ assert_equal 'https', hosts[0][:scheme]
147
+ end
148
+
149
+ should "extract from array" do
150
+ hosts = @client.__extract_hosts ['myhost']
151
+
152
+ assert_equal 'myhost', hosts[0][:host]
153
+ end
154
+
155
+ should "extract from array with multiple hosts" do
156
+ hosts = @client.__extract_hosts ['host1', 'host2']
157
+
158
+ assert_equal 'host1', hosts[0][:host]
159
+ assert_equal 'host2', hosts[1][:host]
160
+ end
161
+
162
+ should "extract from array with ports" do
163
+ hosts = @client.__extract_hosts ['host1:1000', 'host2:2000']
164
+
165
+ assert_equal 2, hosts.size
166
+
167
+ assert_equal 'host1', hosts[0][:host]
168
+ assert_equal 1000, hosts[0][:port]
169
+
170
+ assert_equal 'host2', hosts[1][:host]
171
+ assert_equal 2000, hosts[1][:port]
172
+ end
173
+
174
+ should "extract path" do
175
+ hosts = @client.__extract_hosts 'https://myhost:8080/api'
176
+
177
+ assert_equal '/api', hosts[0][:path]
178
+ end
179
+
180
+ should "extract scheme (protocol)" do
181
+ hosts = @client.__extract_hosts 'https://myhost:8080'
182
+
183
+ assert_equal 'https', hosts[0][:scheme]
184
+ assert_equal 'myhost', hosts[0][:host]
185
+ assert_equal 8080, hosts[0][:port]
186
+ end
187
+
188
+ should "extract credentials" do
189
+ hosts = @client.__extract_hosts 'http://USERNAME:PASSWORD@myhost:8080'
190
+
191
+ assert_equal 'http', hosts[0][:scheme]
192
+ assert_equal 'USERNAME', hosts[0][:user]
193
+ assert_equal 'PASSWORD', hosts[0][:password]
194
+ assert_equal 'myhost', hosts[0][:host]
195
+ assert_equal 8080, hosts[0][:port]
196
+ end
197
+
198
+ should "pass hashes over" do
199
+ hosts = @client.__extract_hosts [{:host => 'myhost', :port => '1000', :foo => 'bar'}]
200
+
201
+ assert_equal 'myhost', hosts[0][:host]
202
+ assert_equal 1000, hosts[0][:port]
203
+ assert_equal 'bar', hosts[0][:foo]
204
+ end
205
+
206
+ should "use URL instance" do
207
+ require 'uri'
208
+ hosts = @client.__extract_hosts URI.parse('https://USERNAME:PASSWORD@myhost:4430')
209
+
210
+ assert_equal 'https', hosts[0][:scheme]
211
+ assert_equal 'USERNAME', hosts[0][:user]
212
+ assert_equal 'PASSWORD', hosts[0][:password]
213
+ assert_equal 'myhost', hosts[0][:host]
214
+ assert_equal 4430, hosts[0][:port]
215
+ end
216
+
217
+ should "split comma-separated URLs" do
218
+ hosts = @client.__extract_hosts 'foo, bar'
219
+
220
+ assert_equal 2, hosts.size
221
+
222
+ assert_equal 'foo', hosts[0][:host]
223
+ assert_equal 'bar', hosts[1][:host]
224
+ end
225
+
226
+ should "raise error for incompatible argument" do
227
+ assert_raise ArgumentError do
228
+ @client.__extract_hosts 123
229
+ end
230
+ end
231
+
232
+ should "randomize hosts" do
233
+ hosts = [ {:host => 'host1'}, {:host => 'host2'}, {:host => 'host3'}, {:host => 'host4'}, {:host => 'host5'}]
234
+
235
+ Array.any_instance.expects(:shuffle!).twice
236
+
237
+ @client.__extract_hosts(hosts, :randomize_hosts => true)
238
+ assert_same_elements hosts, @client.__extract_hosts(hosts, :randomize_hosts => true)
239
+ end
240
+ end
241
+
242
+ context "detecting adapter for Faraday" do
243
+ setup do
244
+ Elasticsearch::Transport::Client::DEFAULT_TRANSPORT_CLASS.any_instance.unstub(:__build_connections)
245
+ begin; Object.send(:remove_const, :Typhoeus); rescue NameError; end
246
+ begin; Object.send(:remove_const, :Patron); rescue NameError; end
247
+ end
248
+
249
+ should "use the default adapter" do
250
+ c = Elasticsearch::Transport::Client.new
251
+ handlers = c.transport.connections.all.first.connection.builder.handlers
252
+
253
+ assert_includes handlers, Faraday::Adapter::NetHttp
254
+ end
255
+
256
+ should "use the adapter from arguments" do
257
+ c = Elasticsearch::Transport::Client.new :adapter => :typhoeus
258
+ handlers = c.transport.connections.all.first.connection.builder.handlers
259
+
260
+ assert_includes handlers, Faraday::Adapter::Typhoeus
261
+ end
262
+
263
+ should "detect the adapter" do
264
+ require 'patron'; load 'patron.rb'
265
+
266
+ c = Elasticsearch::Transport::Client.new
267
+ handlers = c.transport.connections.all.first.connection.builder.handlers
268
+
269
+ assert_includes handlers, Faraday::Adapter::Patron
270
+ end unless JRUBY
271
+ end
272
+
273
+ end
274
+ end