hot_tub 0.2.6 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.travis.yml +5 -5
- data/Gemfile +0 -4
- data/HISTORY.md +12 -4
- data/LICENSE.txt +1 -1
- data/README.md +94 -70
- data/hot_tub.gemspec +4 -2
- data/lib/hot_tub.rb +8 -13
- data/lib/hot_tub/known_clients.rb +41 -23
- data/lib/hot_tub/pool.rb +235 -98
- data/lib/hot_tub/reaper.rb +37 -0
- data/lib/hot_tub/sessions.rb +157 -0
- data/lib/hot_tub/version.rb +1 -1
- data/spec/helpers/moc_client.rb +10 -6
- data/spec/helpers/moc_pool.rb +23 -0
- data/spec/hot_tub_spec.rb +20 -0
- data/spec/pool_spec.rb +189 -178
- data/spec/reaper_mixin_spec.rb +29 -0
- data/spec/reaper_spec.rb +28 -0
- data/spec/sessions_spec.rb +223 -0
- data/spec/spec_helper.rb +1 -0
- metadata +34 -27
- data/lib/hot_tub/session.rb +0 -103
- data/spec/session_spec.rb +0 -213
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
describe HotTub::Reaper::Mixin do
|
3
|
+
before(:each) do
|
4
|
+
@pool = MocMixinPool.new
|
5
|
+
end
|
6
|
+
describe '#reaper' do
|
7
|
+
it "should be defined" do
|
8
|
+
@pool.should respond_to(:reaper)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
describe '#reap_timeout' do
|
12
|
+
it "should be defined" do
|
13
|
+
@pool.should respond_to(:reap_timeout)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
describe '#shutdown' do
|
17
|
+
it "should be defined" do
|
18
|
+
@pool.should respond_to(:shutdown)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
describe '#reap!' do
|
22
|
+
it "should be defined" do
|
23
|
+
@pool.should respond_to(:reap!)
|
24
|
+
end
|
25
|
+
it "should raise NoMethodError if called" do
|
26
|
+
lambda {@pool.reap!}.should raise_error(NoMethodError)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/spec/reaper_spec.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe HotTub::Reaper do
|
4
|
+
before(:each) do
|
5
|
+
@pool = MocReaperPool.new
|
6
|
+
@reaper = @pool.reaper
|
7
|
+
end
|
8
|
+
it "should be a HotTub::Reaper Thread" do
|
9
|
+
@reaper.should be_a(HotTub::Reaper)
|
10
|
+
@reaper.should be_a(Thread)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should reap!" do
|
14
|
+
@pool.reaped.should be_false
|
15
|
+
@pool.lets_reap = true
|
16
|
+
@reaper.wakeup
|
17
|
+
sleep(0.01)
|
18
|
+
@pool.reaped.should be_true
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should sleep after reap!" do
|
22
|
+
@pool.reaped.should be_false
|
23
|
+
@pool.lets_reap = true
|
24
|
+
@reaper.wakeup
|
25
|
+
sleep(0.01)
|
26
|
+
@reaper.status.should eql('sleep')
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,223 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'hot_tub/sessions'
|
3
|
+
require 'uri'
|
4
|
+
require 'time'
|
5
|
+
describe HotTub::Sessions do
|
6
|
+
|
7
|
+
context 'initialized without a block' do
|
8
|
+
it "should raise error if block is not supplied" do
|
9
|
+
lambda {HotTub::Sessions.new}.should raise_error(ArgumentError)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
context 'initialized with a block' do
|
13
|
+
before(:each) do
|
14
|
+
@url = "https://www.somewebsite.com"
|
15
|
+
@uri = URI(@url)
|
16
|
+
@sessions = HotTub::Sessions.new { |url| MocClient.new(url) }
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '#to_url' do
|
20
|
+
context "passed URL string" do
|
21
|
+
it "should return key with URI scheme-domain" do
|
22
|
+
@sessions.send(:to_key,@url).should eql("#{@uri.scheme}://#{@uri.host}:#{@uri.port}")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "passed URI" do
|
27
|
+
it "should return key with URI scheme-domain" do
|
28
|
+
@sessions.send(:to_key,@uri).should eql("#{@uri.scheme}://#{@uri.host}:#{@uri.port}")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "invalid argument" do
|
33
|
+
it "should raise an ArgumentError" do
|
34
|
+
lambda { @sessions.send(:to_key, nil) }.should raise_error(ArgumentError)
|
35
|
+
end
|
36
|
+
it "should raise URI::InvalidURIError with bad url" do
|
37
|
+
lambda { @sessions.send(:to_key,"bad url") }.should raise_error(URI::InvalidURIError)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe '#sessions' do
|
43
|
+
context 'HotTub::Pool as client' do
|
44
|
+
it "should add a new client for the url" do
|
45
|
+
with_pool_options = HotTub::Sessions.new { |url| HotTub::Pool.new(:size => 13) { MocClient.new(url) } }
|
46
|
+
with_pool_options.sessions(@url)
|
47
|
+
sessions = with_pool_options.instance_variable_get(:@sessions)
|
48
|
+
sessions.size.should eql(1)
|
49
|
+
sessions.each_value {|v| v.should be_a( HotTub::Pool)}
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'other clients' do
|
54
|
+
it "should add a new client for the url" do
|
55
|
+
no_pool = HotTub::Sessions.new { |url| Excon.new(url) }
|
56
|
+
no_pool.sessions(@url)
|
57
|
+
sessions = no_pool.instance_variable_get(:@sessions)
|
58
|
+
sessions.size.should eql(1)
|
59
|
+
sessions.each_value {|v| v.should be_a(Excon::Connection)}
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context "passed URL string" do
|
64
|
+
it "should set key with URI scheme-domain" do
|
65
|
+
@sessions.sessions(@url)
|
66
|
+
sessions = @sessions.instance_variable_get(:@sessions)
|
67
|
+
sessions["#{@uri.scheme}://#{@uri.host}:#{@uri.port}"].should be_a(MocClient)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
context "passed URI" do
|
71
|
+
it "should set key with URI scheme-domain" do
|
72
|
+
@sessions.sessions(@uri)
|
73
|
+
sessions = @sessions.instance_variable_get(:@sessions)
|
74
|
+
sessions["#{@uri.scheme}://#{@uri.host}:#{@uri.port}"].should be_a(MocClient)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context "with_pool" do
|
79
|
+
it "should initialize a new HotTub::Pool" do
|
80
|
+
session_with_pool = HotTub::Sessions.new({:with_pool => true}) { |url| MocClient.new(url) }
|
81
|
+
pool = session_with_pool.sessions(@url)
|
82
|
+
pool.should be_a(HotTub::Pool)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe '#run' do
|
88
|
+
it "should work" do
|
89
|
+
url = HotTub::Server.url
|
90
|
+
sessions = HotTub::Sessions.new { |url| Excon.new(url) }
|
91
|
+
result = nil
|
92
|
+
sessions.run(url) do |conn|
|
93
|
+
result = conn.get.status
|
94
|
+
end
|
95
|
+
result.should eql(200)
|
96
|
+
end
|
97
|
+
|
98
|
+
context "with_pool" do
|
99
|
+
it "should pass run to pool" do
|
100
|
+
url = HotTub::Server.url
|
101
|
+
session_with_pool = HotTub::Sessions.new({:with_pool => true}) { |url|
|
102
|
+
uri = URI.parse(url)
|
103
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
104
|
+
http.use_ssl = false
|
105
|
+
http.start
|
106
|
+
http
|
107
|
+
}
|
108
|
+
result = nil
|
109
|
+
session_with_pool.run(url) do |conn|
|
110
|
+
uri = URI.parse(url)
|
111
|
+
result = conn.get(uri.path).code
|
112
|
+
end
|
113
|
+
result.should eql('200')
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe '#clean!' do
|
119
|
+
it "should clean all sessions" do
|
120
|
+
sessions = HotTub::Sessions.new(:clean => lambda { |clnt| clnt.clean}) { |url| MocClient.new(url) }
|
121
|
+
sessions.sessions('foo')
|
122
|
+
sessions.sessions('bar')
|
123
|
+
sessions.clean!
|
124
|
+
sessions.instance_variable_get(:@sessions).each_pair do |k,v|
|
125
|
+
v.cleaned?.should be_true
|
126
|
+
end
|
127
|
+
end
|
128
|
+
context "with_pool" do
|
129
|
+
it "should clean all pools in sessions" do
|
130
|
+
sessions = HotTub::Sessions.new(:with_pool => true, :clean => lambda { |clnt| clnt.clean}) { |url| MocClient.new(url) }
|
131
|
+
sessions.sessions('foo')
|
132
|
+
sessions.sessions('bar')
|
133
|
+
sessions.clean!
|
134
|
+
sessions.instance_variable_get(:@sessions).each_pair do |k,v|
|
135
|
+
v.instance_variable_get(:@pool).each do |c|
|
136
|
+
c.cleaned?.should be_true
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
describe '#drain!' do
|
144
|
+
it "should drain all sessions" do
|
145
|
+
sessions = HotTub::Sessions.new { |url| MocClient.new(url) }
|
146
|
+
sessions.sessions('foo')
|
147
|
+
sessions.sessions('bar')
|
148
|
+
sessions.drain!
|
149
|
+
sessions.instance_variable_get(:@sessions).empty?.should be_true
|
150
|
+
end
|
151
|
+
context "with_pool" do
|
152
|
+
it "should drain all pools in sessions" do
|
153
|
+
sessions = HotTub::Sessions.new(:with_pool => true) { |url| MocClient.new(url) }
|
154
|
+
sessions.sessions('foo')
|
155
|
+
sessions.sessions('bar')
|
156
|
+
sessions.drain!
|
157
|
+
sessions.instance_variable_get(:@sessions).empty?.should be_true
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
describe '#reap!' do
|
163
|
+
it "should clean all sessions" do
|
164
|
+
sessions = HotTub::Sessions.new(:reap => lambda { |clnt| clnt.reap}) { |url| MocClient.new(url) }
|
165
|
+
sessions.sessions('foo')
|
166
|
+
sessions.sessions('bar')
|
167
|
+
sessions.reap!
|
168
|
+
sessions.instance_variable_get(:@sessions).each_pair do |k,v|
|
169
|
+
v.reaped?.should be_true
|
170
|
+
end
|
171
|
+
end
|
172
|
+
context "with_pool" do
|
173
|
+
it "should clean all pools in sessions" do
|
174
|
+
sessions = HotTub::Sessions.new(:with_pool => true, :reap => lambda { |clnt| clnt.reap}) { |url| MocClient.new(url) }
|
175
|
+
sessions.sessions('foo')
|
176
|
+
sessions.sessions('bar')
|
177
|
+
sessions.reap!
|
178
|
+
sessions.instance_variable_get(:@sessions).each_pair do |k,v|
|
179
|
+
v.instance_variable_get(:@pool).each do |c|
|
180
|
+
c.reaped?.should be_true
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
186
|
+
context 'integration tests' do
|
187
|
+
context 'threads' do
|
188
|
+
it "should work" do
|
189
|
+
url = HotTub::Server.url
|
190
|
+
url2 = HotTub::Server2.url
|
191
|
+
session = HotTub::Sessions.new(:with_pool => true) { |url|
|
192
|
+
uri = URI.parse(url)
|
193
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
194
|
+
http.use_ssl = false
|
195
|
+
http.start
|
196
|
+
http
|
197
|
+
}
|
198
|
+
failed = false
|
199
|
+
start_time = Time.now
|
200
|
+
stop_time = nil
|
201
|
+
threads = []
|
202
|
+
lambda {
|
203
|
+
10.times.each do
|
204
|
+
threads << Thread.new do
|
205
|
+
session.run(url) { |clnt| Thread.current[:result] = clnt.get(URI.parse(url).path).code }
|
206
|
+
session.run(url2) { |clnt| Thread.current[:result] = clnt.get(URI.parse(url).path).code }
|
207
|
+
end
|
208
|
+
end
|
209
|
+
threads.each do |t|
|
210
|
+
t.join
|
211
|
+
end
|
212
|
+
stop_time = Time.now
|
213
|
+
}.should_not raise_error # make sure we're thread safe
|
214
|
+
# Some extra checks just to make sure...
|
215
|
+
results = threads.collect{ |t| t[:result]}
|
216
|
+
results.length.should eql(10) # make sure all threads are present
|
217
|
+
results.uniq.should eql([results.first]) # make sure we got the same results
|
218
|
+
session.instance_variable_get(:@sessions).keys.length.should eql(2) # make sure sessions were created
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,20 +1,32 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hot_tub
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.3.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Joshua Mckinney
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-03-07 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: thread_safe
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
14
27
|
- !ruby/object:Gem::Dependency
|
15
28
|
name: rspec
|
16
29
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
30
|
requirements:
|
19
31
|
- - ! '>='
|
20
32
|
- !ruby/object:Gem::Version
|
@@ -22,7 +34,6 @@ dependencies:
|
|
22
34
|
type: :development
|
23
35
|
prerelease: false
|
24
36
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
37
|
requirements:
|
27
38
|
- - ! '>='
|
28
39
|
- !ruby/object:Gem::Version
|
@@ -30,7 +41,6 @@ dependencies:
|
|
30
41
|
- !ruby/object:Gem::Dependency
|
31
42
|
name: sinatra
|
32
43
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
44
|
requirements:
|
35
45
|
- - ! '>='
|
36
46
|
- !ruby/object:Gem::Version
|
@@ -38,7 +48,6 @@ dependencies:
|
|
38
48
|
type: :development
|
39
49
|
prerelease: false
|
40
50
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
51
|
requirements:
|
43
52
|
- - ! '>='
|
44
53
|
- !ruby/object:Gem::Version
|
@@ -46,7 +55,6 @@ dependencies:
|
|
46
55
|
- !ruby/object:Gem::Dependency
|
47
56
|
name: puma
|
48
57
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
58
|
requirements:
|
51
59
|
- - ~>
|
52
60
|
- !ruby/object:Gem::Version
|
@@ -54,7 +62,6 @@ dependencies:
|
|
54
62
|
type: :development
|
55
63
|
prerelease: false
|
56
64
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
65
|
requirements:
|
59
66
|
- - ~>
|
60
67
|
- !ruby/object:Gem::Version
|
@@ -62,7 +69,6 @@ dependencies:
|
|
62
69
|
- !ruby/object:Gem::Dependency
|
63
70
|
name: excon
|
64
71
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
72
|
requirements:
|
67
73
|
- - ! '>='
|
68
74
|
- !ruby/object:Gem::Version
|
@@ -70,13 +76,12 @@ dependencies:
|
|
70
76
|
type: :development
|
71
77
|
prerelease: false
|
72
78
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
79
|
requirements:
|
75
80
|
- - ! '>='
|
76
81
|
- !ruby/object:Gem::Version
|
77
82
|
version: '0'
|
78
|
-
description: A
|
79
|
-
|
83
|
+
description: A dynamic thread-safe pooling gem, when you need more than a standard
|
84
|
+
static pool.
|
80
85
|
email:
|
81
86
|
- joshmckin@gmail.com
|
82
87
|
executables: []
|
@@ -95,47 +100,49 @@ files:
|
|
95
100
|
- lib/hot_tub.rb
|
96
101
|
- lib/hot_tub/known_clients.rb
|
97
102
|
- lib/hot_tub/pool.rb
|
98
|
-
- lib/hot_tub/
|
103
|
+
- lib/hot_tub/reaper.rb
|
104
|
+
- lib/hot_tub/sessions.rb
|
99
105
|
- lib/hot_tub/version.rb
|
100
106
|
- spec/helpers/moc_client.rb
|
107
|
+
- spec/helpers/moc_pool.rb
|
101
108
|
- spec/helpers/server.rb
|
109
|
+
- spec/hot_tub_spec.rb
|
102
110
|
- spec/pool_spec.rb
|
103
|
-
- spec/
|
111
|
+
- spec/reaper_mixin_spec.rb
|
112
|
+
- spec/reaper_spec.rb
|
113
|
+
- spec/sessions_spec.rb
|
104
114
|
- spec/spec_helper.rb
|
105
115
|
homepage: https://github.com/JoshMcKin/hot_tub
|
106
116
|
licenses:
|
107
117
|
- MIT
|
118
|
+
metadata: {}
|
108
119
|
post_install_message:
|
109
120
|
rdoc_options: []
|
110
121
|
require_paths:
|
111
122
|
- lib
|
112
123
|
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
-
none: false
|
114
124
|
requirements:
|
115
125
|
- - ! '>='
|
116
126
|
- !ruby/object:Gem::Version
|
117
127
|
version: '0'
|
118
|
-
segments:
|
119
|
-
- 0
|
120
|
-
hash: -3131859420697003965
|
121
128
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
|
-
none: false
|
123
129
|
requirements:
|
124
130
|
- - ! '>='
|
125
131
|
- !ruby/object:Gem::Version
|
126
132
|
version: '0'
|
127
|
-
segments:
|
128
|
-
- 0
|
129
|
-
hash: -3131859420697003965
|
130
133
|
requirements: []
|
131
134
|
rubyforge_project: hot_tub
|
132
|
-
rubygems_version:
|
135
|
+
rubygems_version: 2.2.2
|
133
136
|
signing_key:
|
134
|
-
specification_version:
|
135
|
-
summary: A
|
137
|
+
specification_version: 4
|
138
|
+
summary: A dynamic thread-safe pooling gem.
|
136
139
|
test_files:
|
137
140
|
- spec/helpers/moc_client.rb
|
141
|
+
- spec/helpers/moc_pool.rb
|
138
142
|
- spec/helpers/server.rb
|
143
|
+
- spec/hot_tub_spec.rb
|
139
144
|
- spec/pool_spec.rb
|
140
|
-
- spec/
|
145
|
+
- spec/reaper_mixin_spec.rb
|
146
|
+
- spec/reaper_spec.rb
|
147
|
+
- spec/sessions_spec.rb
|
141
148
|
- spec/spec_helper.rb
|