LVS-JSONService 0.3.8 → 0.3.9
Sign up to get free protection for your applications and to get access to all the features.
- data/.specification +1 -1
- data/LVS-JSONService.gemspec +1 -1
- data/VERSION +1 -1
- data/lib/lvs/json_service/connection_manager.rb +24 -5
- data/spec/lvs/json_service/connection_manager_spec.rb +1 -254
- metadata +1 -1
data/.specification
CHANGED
data/LVS-JSONService.gemspec
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.9
|
@@ -3,7 +3,7 @@ module LVS
|
|
3
3
|
class ConnectionManager
|
4
4
|
def self.get_connection(host, port, options)
|
5
5
|
@@connections ||= {}
|
6
|
-
key = create_key(host, port
|
6
|
+
key = create_key(host, port)
|
7
7
|
connection = @@connections[key]
|
8
8
|
if connection.nil? || !connection.started?
|
9
9
|
connection = create_connection(host, port, options)
|
@@ -14,10 +14,29 @@ module LVS
|
|
14
14
|
|
15
15
|
def self.reset_connection(host, port, options)
|
16
16
|
@@connections ||= {}
|
17
|
-
key = create_key(host, port
|
17
|
+
key = create_key(host, port)
|
18
|
+
begin
|
19
|
+
LVS::JsonService::Logger.debug "Disconnecting from #{host}:#{port}"
|
20
|
+
@@connections[key].finish
|
21
|
+
rescue IOError
|
22
|
+
# Do nothing
|
23
|
+
end
|
18
24
|
@@connections.delete(key)
|
19
25
|
end
|
20
26
|
|
27
|
+
def self.close_all_connections
|
28
|
+
LVS::JsonService::Logger.debug "Requesting to close all (#{@@connections.size}) connections"
|
29
|
+
@@connections.each do |key, connection|
|
30
|
+
begin
|
31
|
+
LVS::JsonService::Logger.debug "Disconnecting from #{host}:#{port}"
|
32
|
+
connection.finish
|
33
|
+
rescue IOError
|
34
|
+
# Do nothing
|
35
|
+
end
|
36
|
+
end
|
37
|
+
reset_all_connections
|
38
|
+
end
|
39
|
+
|
21
40
|
def self.reset_all_connections
|
22
41
|
@@connections = {}
|
23
42
|
end
|
@@ -38,15 +57,15 @@ module LVS
|
|
38
57
|
|
39
58
|
http.open_timeout = options[:timeout] || 1
|
40
59
|
http.read_timeout = options[:timeout] || 1
|
41
|
-
LVS::JsonService::Logger.debug "Connecting"
|
60
|
+
LVS::JsonService::Logger.debug "Connecting to #{host}:#{port}"
|
42
61
|
http.start
|
43
62
|
http
|
44
63
|
end
|
45
64
|
|
46
65
|
private
|
47
66
|
|
48
|
-
def self.create_key(host, port
|
49
|
-
"#{host}:#{port}
|
67
|
+
def self.create_key(host, port)
|
68
|
+
key = "#{host}:#{port}"
|
50
69
|
end
|
51
70
|
end
|
52
71
|
end
|
@@ -38,257 +38,4 @@ describe LVS::JsonService::ConnectionManager do
|
|
38
38
|
http_yahoo.should_not eql(http_yahoo_subsequent)
|
39
39
|
end
|
40
40
|
|
41
|
-
end
|
42
|
-
|
43
|
-
__END__
|
44
|
-
|
45
|
-
describe ".http_request_with_timeout" do
|
46
|
-
before :each do
|
47
|
-
@mock_post = mock(:post, :null_object => true)
|
48
|
-
Net::HTTP::Post.stub!(:new).and_return(@mock_post)
|
49
|
-
|
50
|
-
@mock_http = MockNetHttp.new
|
51
|
-
@connection = @mock_http.connection
|
52
|
-
Net::HTTP.stub!(:new).and_return(@mock_http)
|
53
|
-
end
|
54
|
-
|
55
|
-
it "should set Net::HTTP#open_timeout" do
|
56
|
-
@mock_http.should_receive(:open_timeout=).with(@options[:timeout])
|
57
|
-
do_request
|
58
|
-
end
|
59
|
-
|
60
|
-
it "should set Net::HTTP#read_timeout" do
|
61
|
-
@mock_http.should_receive(:read_timeout=).with(@options[:timeout])
|
62
|
-
do_request
|
63
|
-
end
|
64
|
-
|
65
|
-
it "should default to a timeout of 1 if not set" do
|
66
|
-
@options = {:timeout => nil}
|
67
|
-
@mock_http.should_receive(:read_timeout=).with(1)
|
68
|
-
do_request
|
69
|
-
end
|
70
|
-
|
71
|
-
it "should pass through the domain and port to Net::HTTP" do
|
72
|
-
Net::HTTP.should_receive(:new).with(@domain, @port).and_return(@mock_http)
|
73
|
-
do_request
|
74
|
-
end
|
75
|
-
|
76
|
-
it "should create a Net::HTTP::Post object" do
|
77
|
-
Net::HTTP::Post.should_receive(:new).with(@path).and_return(@mock_post)
|
78
|
-
do_request
|
79
|
-
end
|
80
|
-
|
81
|
-
it "should assign the JSON parameters to a Net::HTTP::Post object" do
|
82
|
-
@mock_post.should_receive(:form_data=).with({ "object_request" => @args.to_json })
|
83
|
-
do_request
|
84
|
-
end
|
85
|
-
|
86
|
-
it "should send one request to Net::HTTP#start" do
|
87
|
-
@connection.should_receive(:request).once.with(@mock_post)
|
88
|
-
do_request
|
89
|
-
end
|
90
|
-
|
91
|
-
it "should return the response from the service" do
|
92
|
-
response = "some response"
|
93
|
-
@connection.should_receive(:request).and_return(response)
|
94
|
-
do_request.should == response
|
95
|
-
end
|
96
|
-
|
97
|
-
describe "with 2 retries of Timeout::Error" do
|
98
|
-
|
99
|
-
before :each do
|
100
|
-
@options = {:retries => 2}
|
101
|
-
end
|
102
|
-
|
103
|
-
describe "with subsequent success" do
|
104
|
-
|
105
|
-
it "should post the request 2 times" do
|
106
|
-
@connection.should_receive(:request).with(@mock_post).exactly(1).times.ordered.and_raise(Timeout::Error.new(nil))
|
107
|
-
@connection.should_receive(:request).with(@mock_post).exactly(1).times.ordered
|
108
|
-
do_request
|
109
|
-
end
|
110
|
-
|
111
|
-
it "should return the response from the service" do
|
112
|
-
response = "some response"
|
113
|
-
@connection.should_receive(:request).with(@mock_post).exactly(1).times.ordered.and_raise(Timeout::Error.new(nil))
|
114
|
-
@connection.should_receive(:request).with(@mock_post).exactly(1).times.ordered.and_return(response)
|
115
|
-
do_request.should == response
|
116
|
-
end
|
117
|
-
|
118
|
-
end
|
119
|
-
|
120
|
-
it "should log the retry attempt" do
|
121
|
-
@connection.stub!(:request).and_raise(Timeout::Error.new(nil))
|
122
|
-
LVS::JsonService::Logger.stub!(:debug)
|
123
|
-
LVS::JsonService::Logger.should_receive(:debug).at_least(1).times.
|
124
|
-
with("Retrying #{@url} due to TimeoutError")
|
125
|
-
do_request_catching_errors
|
126
|
-
end
|
127
|
-
|
128
|
-
describe "with subseqent failure" do
|
129
|
-
|
130
|
-
before :each do
|
131
|
-
@connection.stub!(:request).and_raise(Timeout::Error.new(nil))
|
132
|
-
end
|
133
|
-
|
134
|
-
it "should post the request 3 times (original + 2 retries)" do
|
135
|
-
@connection.should_receive(:request).with(@mock_post).exactly(3).times.and_raise(Timeout::Error.new(nil))
|
136
|
-
do_request_catching_errors
|
137
|
-
end
|
138
|
-
|
139
|
-
it "should raise an LVS::JsonService::TimeoutError exception" do
|
140
|
-
lambda {
|
141
|
-
do_request
|
142
|
-
}.should raise_error(LVS::JsonService::TimeoutError)
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
end
|
147
|
-
|
148
|
-
describe "with 2 retries of Errno::ECONNREFUSED" do
|
149
|
-
|
150
|
-
before :each do
|
151
|
-
@options = {:retries => 2}
|
152
|
-
ClassWithRequest.stub!(:sleep)
|
153
|
-
end
|
154
|
-
|
155
|
-
it "should sleep for 1 second before each timeout" do
|
156
|
-
@connection.stub!(:request).and_raise(Errno::ECONNREFUSED)
|
157
|
-
ClassWithRequest.should_receive(:sleep).with(1)
|
158
|
-
do_request_catching_errors
|
159
|
-
end
|
160
|
-
|
161
|
-
describe "with subsequent success" do
|
162
|
-
|
163
|
-
it "should post the request 2 times" do
|
164
|
-
@connection.should_receive(:request).with(@mock_post).exactly(1).times.ordered.and_raise(Errno::ECONNREFUSED)
|
165
|
-
@connection.should_receive(:request).with(@mock_post).exactly(1).times.ordered
|
166
|
-
do_request
|
167
|
-
end
|
168
|
-
|
169
|
-
it "should return the response from the service" do
|
170
|
-
response = "some response"
|
171
|
-
@connection.should_receive(:request).with(@mock_post).exactly(1).times.ordered.and_raise(Errno::ECONNREFUSED)
|
172
|
-
@connection.should_receive(:request).with(@mock_post).exactly(1).times.ordered.and_return(response)
|
173
|
-
do_request.should == response
|
174
|
-
end
|
175
|
-
end
|
176
|
-
|
177
|
-
describe "with subsequent failure" do
|
178
|
-
|
179
|
-
before :each do
|
180
|
-
@connection.stub!(:request).and_raise(Errno::ECONNREFUSED)
|
181
|
-
end
|
182
|
-
|
183
|
-
it "should post the request 3 times (original + 2 retries)" do
|
184
|
-
@connection.should_receive(:request).with(@mock_post).exactly(3).times.and_raise(Errno::ECONNREFUSED)
|
185
|
-
do_request_catching_errors
|
186
|
-
end
|
187
|
-
|
188
|
-
it "should raise an LVS::JsonService::BackendUnavailableError exception" do
|
189
|
-
lambda {
|
190
|
-
do_request
|
191
|
-
}.should raise_error(LVS::JsonService::BackendUnavailableError)
|
192
|
-
end
|
193
|
-
|
194
|
-
end
|
195
|
-
|
196
|
-
it "should log the retry attempt" do
|
197
|
-
@connection.stub!(:request).and_raise(Errno::ECONNREFUSED)
|
198
|
-
LVS::JsonService::Logger.stub!(:debug)
|
199
|
-
LVS::JsonService::Logger.should_receive(:debug).at_least(1).times.
|
200
|
-
with("Retrying #{@url} due to Errno::ECONNREFUSED")
|
201
|
-
do_request_catching_errors
|
202
|
-
end
|
203
|
-
end
|
204
|
-
|
205
|
-
it "should raise LVS::JsonService::NotFoundError if Net::HTTPNotFound is raised" do
|
206
|
-
@connection.stub!(:request).and_return(Net::HTTPNotFound.new(404, 1.1, "Not Found"))
|
207
|
-
lambda {
|
208
|
-
do_request
|
209
|
-
}.should raise_error(LVS::JsonService::NotFoundError)
|
210
|
-
end
|
211
|
-
|
212
|
-
it "should raise LVS::JsonService::NotModified if HTTPNotModified is raised" do
|
213
|
-
@connection.stub!(:request).and_return(Net::HTTPNotModified.new(304, 1.1, "Not Modified"))
|
214
|
-
lambda {
|
215
|
-
do_request
|
216
|
-
}.should raise_error(LVS::JsonService::NotModified)
|
217
|
-
end
|
218
|
-
|
219
|
-
def do_request
|
220
|
-
ClassWithRequest.http_request_with_timeout(@url, @args, @options)
|
221
|
-
end
|
222
|
-
|
223
|
-
def do_request_catching_errors
|
224
|
-
do_request
|
225
|
-
rescue LVS::JsonService::Error
|
226
|
-
end
|
227
|
-
|
228
|
-
end
|
229
|
-
|
230
|
-
describe ".run_remote_request" do
|
231
|
-
before :each do
|
232
|
-
@response = load_fixture('response.yml')
|
233
|
-
end
|
234
|
-
|
235
|
-
it "should call http_request_with_timeout with service, args and options" do
|
236
|
-
ClassWithRequest.should_receive(:http_request_with_timeout).
|
237
|
-
with(@url, @args, @options).
|
238
|
-
and_return(@response)
|
239
|
-
ClassWithRequest.run_remote_request(@url, @args, @options)
|
240
|
-
end
|
241
|
-
|
242
|
-
it "should return the parsed JSON result" do
|
243
|
-
expected_result = [
|
244
|
-
{"id"=>1100, "description"=>"Handball (ABP)"},
|
245
|
-
{"id"=>978400, "description"=>"Casino Roulette"}
|
246
|
-
]
|
247
|
-
ClassWithRequest.stub!(:http_request_with_timeout).and_return(@response)
|
248
|
-
ClassWithRequest.run_remote_request(@url, @args, @options).should == expected_result
|
249
|
-
end
|
250
|
-
|
251
|
-
it "should raise an error if the response contains PCode" do
|
252
|
-
error_response = load_fixture('error_response.yml')
|
253
|
-
ClassWithRequest.stub!(:http_request_with_timeout).
|
254
|
-
and_return(error_response)
|
255
|
-
|
256
|
-
lambda {
|
257
|
-
ClassWithRequest.run_remote_request(@url, @args, @options)
|
258
|
-
}.should raise_error(LVS::JsonService::Error)
|
259
|
-
end
|
260
|
-
end
|
261
|
-
|
262
|
-
end
|
263
|
-
|
264
|
-
def load_fixture (file)
|
265
|
-
response_file = File.join(File.dirname(__FILE__), '..', '..', 'fixtures', file)
|
266
|
-
YAML.load_file(response_file)
|
267
|
-
end
|
268
|
-
|
269
|
-
class MockNetHttp
|
270
|
-
|
271
|
-
attr_accessor :connection
|
272
|
-
|
273
|
-
def initialize(*args)
|
274
|
-
@connection = mock(:connection)
|
275
|
-
end
|
276
|
-
|
277
|
-
def start
|
278
|
-
yield @connection if block_given?
|
279
|
-
end
|
280
|
-
|
281
|
-
def method_missing(*args)
|
282
|
-
return self
|
283
|
-
end
|
284
|
-
|
285
|
-
end
|
286
|
-
|
287
|
-
class ClassWithRequest
|
288
|
-
include LVS::JsonService::Request
|
289
|
-
|
290
|
-
def self.require_ssl?
|
291
|
-
false
|
292
|
-
end
|
293
|
-
end
|
294
|
-
|
41
|
+
end
|