-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool_spec.rb
299 lines (269 loc) · 8.84 KB
/
pool_spec.rb
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
require 'spec_helper'
describe HotTub::Pool do
context 'default settings' do
before(:each) do
@pool = HotTub::Pool.new { MocClient.new }
end
it "should have :size of 5" do
@pool.instance_variable_get(:@size).should eql(5)
end
it "should have :blocking_timeout of 0.5" do
@pool.instance_variable_get(:@blocking_timeout).should eql(10)
end
it "should have set the client" do
@pool.instance_variable_get(:@client_block).call.should be_a(MocClient)
end
it "should be true" do
@pool.instance_variable_get(:@never_block).should be_true
end
end
context 'custom settings' do
before(:each) do
@pool = HotTub::Pool.new(:size => 10, :blocking_timeout => 1.0, :never_block => false) { MocClient.new }
end
it "should have :size of 5" do
@pool.instance_variable_get(:@size).should eql(10)
end
it "should have :blocking_timeout of 0.5" do
@pool.instance_variable_get(:@blocking_timeout).should eql(1.0)
end
it "should be true" do
@pool.instance_variable_get(:@never_block).should be_false
end
end
describe '#run' do
before(:each) do
@pool = HotTub::Pool.new { MocClient.new}
end
it "should remove connection from pool when doing work" do
@pool.run do |connection|
@connection = connection
@fetched = @pool.instance_variable_get(:@pool).select{|c| c.object_id == @connection.object_id}.length.should eql(0) # not in pool because its doing work
end
end
it "should return the connection after use" do
@connection = nil
@pool.run do |connection|
@connection = connection
end
# returned to pool after work was done
@pool.instance_variable_get(:@pool).pop.should eql(@connection)
end
it "should work" do
result = nil
@pool.run{|clnt| result = clnt.get}
result.should_not be_nil
end
context 'block not given' do
it "should raise ArgumentError" do
lambda { @pool.run }.should raise_error(ArgumentError)
end
end
end
describe '#close_all' do
before(:each) do
@pool = HotTub::Pool.new(:size => 5) { MocClient.new }
end
it "should reset out" do
@pool.instance_variable_set(:@out, [MocClient.new,MocClient.new,MocClient.new])
@pool.instance_variable_get(:@out).length.should eql(3)
@pool.send(:_current_size).should eql(3)
@pool.close_all
@pool.instance_variable_get(:@out).length.should eql(0)
@pool.send(:_current_size).should eql(0)
end
it "should reset pool" do
@pool.instance_variable_set(:@pool, [MocClient.new,MocClient.new,MocClient.new])
@pool.instance_variable_get(:@pool).length.should eql(3)
@pool.send(:_current_size).should eql(3)
@pool.close_all
@pool.instance_variable_get(:@pool).length.should eql(0)
@pool.send(:_current_size).should eql(0)
end
end
context 'private methods' do
before(:each) do
@url = "http://www.testurl123.com/"
@pool = HotTub::Pool.new { MocClient.new}
end
describe '#client' do
it "should raise HotTub::BlockingTimeout if an available is not found in time"do
@pool.instance_variable_set(:@never_block,false)
@pool.instance_variable_set(:@blocking_timeout, 0.1)
@pool.stub(:raise_alarm?).and_return(true)
lambda { puts @pool.send(:pop) }.should raise_error(HotTub::BlockingTimeout)
end
it "should return an instance of the client" do
@pool.send(:client).should be_instance_of(MocClient)
end
end
describe 'add?' do
it "should be true if @pool_data[:length] is less than desired pool size and
the pool is empty?"do
@pool.instance_variable_set(:@pool,[])
@pool.send(:_add?).should be_true
end
it "should be false pool has reached pool_size" do
@pool.instance_variable_set(:@size, 5)
@pool.instance_variable_set(:@pool,["connection","connection","connection","connection","connection"])
@pool.send(:_add?).should be_false
end
end
describe '#_add' do
it "should add connections for supplied url"do
pre_add_length = @pool.instance_variable_get(:@pool).length
@pool.send(:_add)
@pool.instance_variable_get(:@pool).length.should be > pre_add_length
end
end
describe '#push' do
context "connection is registered" do
it "should push connection back to pool" do
@pool.send(:_add)
clnt = @pool.instance_variable_get(:@pool).pop
@pool.send(:push,clnt)
@pool.instance_variable_get(:@pool).include?(clnt).should be_true
end
end
context "client is nil" do
it "should not push connection back to pool" do
@pool.send(:push,nil)
@pool.instance_variable_get(:@pool).include?(nil).should be_false
end
end
end
end
context ':never_block' do
context 'is true' do
it "should add connections to pool as necessary" do
pool = HotTub::Pool.new({:size => 1}) { MocClient.new }
threads = []
5.times.each do
threads << Thread.new do
pool.run{|connection| connection.get }
end
end
sleep(0.01)
threads.each do |t|
t.join
end
(pool.send(:_current_size) > 1).should be_true
end
end
context 'is false' do
it "should not add connections to pool beyond specified size" do
pool = HotTub::Pool.new({:size => 1, :never_block => false, :blocking_timeout => 100}) { MocClient.new }
threads = []
2.times.each do
threads << Thread.new do
pool.run{|connection| connection.get }
end
end
sleep(0.01)
threads.each do |t|
t.join
end
pool.send(:_current_size).should eql(1)
end
end
end
describe '#reap' do
context 'current_size is greater than :size' do
it "should remove a connection from the pool" do
pool = HotTub::Pool.new({:size => 1}) { MocClient.new }
pool.instance_variable_set(:@last_activity,(Time.now - 601))
pool.instance_variable_set(:@pool, [MocClient.new,MocClient.new])
pool.send(:_reap?).should be_true
pool.instance_variable_get(:@reaper).wakeup # run the reaper thread
sleep(0.1) # let results
pool.send(:_current_size).should eql(1)
pool.instance_variable_get(:@pool).length.should eql(1)
end
end
end
context 'thread safety' do
it "should work" do
pool = HotTub::Pool.new({:size => 10}) { MocClient.new }
failed = false
lambda {
threads = []
20.times.each do
threads << Thread.new do
pool.run{|connection| connection.get }
end
end
sleep(0.01)
threads.each do |t|
t.join
end
}.should_not raise_error
(pool.instance_variable_get(:@pool).length >= 10).should be_true # make sure work got done
end
end
context "other http client" do
before(:each) do
@pool = HotTub::Pool.new(:clean => lambda {|clnt| clnt.clean}) {MocClient.new}
end
it "should clean connections" do
@pool.run do |clnt|
clnt.cleaned?.should be_true
end
end
end
context 'Net::Http' do
before(:each) do
@pool = HotTub::Pool.new(:size => 10) {
uri = URI.parse(HotTub::Server.url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = false
http.start
http
}
end
it "should work" do
result = nil
@pool.run{|clnt|
uri = URI.parse(HotTub::Server.url)
result = clnt.head(uri.path).code
}
result.should eql('200')
end
context 'threads' do
it "should work" do
failed = false
threads = []
lambda {
15.times.each do
threads << Thread.new do
@pool.run{|connection|
uri = URI.parse(HotTub::Server.url)
Thread.current[:status] = connection.head(uri.path).code
}
end
end
sleep(0.01)
threads.each do |t|
t.join
end
}.should_not raise_error
# Reuse and run reaper
@pool.instance_variable_set(:@last_activity,(Time.now - 601))
lambda {
10.times.each do
threads << Thread.new do
uri = URI.parse(HotTub::Server.url)
@pool.run{|connection| Thread.current[:status] = connection.head(uri.path).code }
end
end
sleep(0.01)
threads.each do |t|
t.join
end
}.should_not raise_error
results = threads.collect{ |t| t[:status]}
results.length.should eql(25) # make sure all threads are present
results.uniq.should eql(['200']) # make sure all returned status 200
end
end
end
end