moneta 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +176 -0
  3. data/.travis.yml +57 -23
  4. data/CHANGES +12 -0
  5. data/Gemfile +89 -64
  6. data/README.md +40 -14
  7. data/feature_matrix.yaml +1 -0
  8. data/lib/action_dispatch/middleware/session/moneta_store.rb +1 -0
  9. data/lib/active_support/cache/moneta_store.rb +5 -5
  10. data/lib/moneta.rb +9 -1
  11. data/lib/moneta/adapters/activerecord.rb +35 -19
  12. data/lib/moneta/adapters/activesupportcache.rb +3 -7
  13. data/lib/moneta/adapters/cassandra.rb +24 -16
  14. data/lib/moneta/adapters/client.rb +13 -9
  15. data/lib/moneta/adapters/couch.rb +220 -80
  16. data/lib/moneta/adapters/datamapper.rb +1 -0
  17. data/lib/moneta/adapters/file.rb +9 -6
  18. data/lib/moneta/adapters/hbase.rb +1 -1
  19. data/lib/moneta/adapters/kyotocabinet.rb +8 -7
  20. data/lib/moneta/adapters/leveldb.rb +1 -1
  21. data/lib/moneta/adapters/lmdb.rb +3 -4
  22. data/lib/moneta/adapters/lruhash.rb +29 -62
  23. data/lib/moneta/adapters/memcached.rb +1 -0
  24. data/lib/moneta/adapters/memcached/dalli.rb +1 -1
  25. data/lib/moneta/adapters/memcached/native.rb +10 -8
  26. data/lib/moneta/adapters/mongo.rb +1 -0
  27. data/lib/moneta/adapters/mongo/base.rb +3 -3
  28. data/lib/moneta/adapters/mongo/moped.rb +12 -13
  29. data/lib/moneta/adapters/mongo/official.rb +7 -8
  30. data/lib/moneta/adapters/null.rb +1 -2
  31. data/lib/moneta/adapters/pstore.rb +3 -2
  32. data/lib/moneta/adapters/redis.rb +3 -3
  33. data/lib/moneta/adapters/restclient.rb +12 -3
  34. data/lib/moneta/adapters/riak.rb +2 -2
  35. data/lib/moneta/adapters/sequel.rb +112 -119
  36. data/lib/moneta/adapters/sqlite.rb +3 -3
  37. data/lib/moneta/adapters/tokyotyrant.rb +1 -1
  38. data/lib/moneta/builder.rb +0 -1
  39. data/lib/moneta/enumerable.rb +38 -0
  40. data/lib/moneta/expires.rb +12 -12
  41. data/lib/moneta/fallback.rb +84 -0
  42. data/lib/moneta/lock.rb +1 -1
  43. data/lib/moneta/logger.rb +2 -2
  44. data/lib/moneta/mixins.rb +12 -10
  45. data/lib/moneta/optionmerger.rb +0 -1
  46. data/lib/moneta/pool.rb +301 -31
  47. data/lib/moneta/proxy.rb +2 -2
  48. data/lib/moneta/server.rb +9 -12
  49. data/lib/moneta/shared.rb +1 -1
  50. data/lib/moneta/stack.rb +6 -6
  51. data/lib/moneta/synchronize.rb +3 -3
  52. data/lib/moneta/transformer.rb +19 -17
  53. data/lib/moneta/transformer/config.rb +6 -5
  54. data/lib/moneta/transformer/helper.rb +3 -3
  55. data/lib/moneta/transformer/helper/bson.rb +18 -15
  56. data/lib/moneta/utils.rb +3 -9
  57. data/lib/moneta/version.rb +1 -1
  58. data/lib/moneta/weak_each_key.rb +2 -4
  59. data/lib/rack/cache/moneta.rb +16 -13
  60. data/lib/rack/moneta_rest.rb +2 -2
  61. data/lib/rack/session/moneta.rb +3 -4
  62. data/moneta.gemspec +8 -0
  63. data/script/benchmarks +55 -32
  64. data/script/reconfigure-couchdb +13 -0
  65. data/script/start-hbase +1 -0
  66. data/script/start-services +2 -10
  67. data/spec/active_support/cache_moneta_store_spec.rb +3 -1
  68. data/spec/features/concurrent_create.rb +31 -10
  69. data/spec/features/concurrent_increment.rb +27 -19
  70. data/spec/features/increment.rb +41 -41
  71. data/spec/helper.rb +2 -42
  72. data/spec/moneta/adapters/activesupportcache/adapter_activesupportcache_spec.rb +4 -1
  73. data/spec/moneta/adapters/activesupportcache/adapter_activesupportcache_with_default_expires_spec.rb +4 -1
  74. data/spec/moneta/adapters/activesupportcache/standard_activesupportcache_spec.rb +14 -0
  75. data/spec/moneta/adapters/couch/adapter_couch_spec.rb +199 -2
  76. data/spec/moneta/adapters/couch/standard_couch_spec.rb +8 -2
  77. data/spec/moneta/adapters/couch/standard_couch_with_expires_spec.rb +7 -1
  78. data/spec/moneta/adapters/faraday_helper.rb +9 -0
  79. data/spec/moneta/adapters/lruhash/adapter_lruhash_spec.rb +2 -2
  80. data/spec/moneta/adapters/memcached/adapter_memcached_spec.rb +1 -1
  81. data/spec/moneta/adapters/memcached/dalli/adapter_memcached_dalli_spec.rb +1 -1
  82. data/spec/moneta/adapters/memcached/dalli/adapter_memcached_dalli_with_default_expires_spec.rb +1 -1
  83. data/spec/moneta/adapters/memcached/dalli/standard_memcached_dalli_spec.rb +1 -1
  84. data/spec/moneta/adapters/memcached/native/adapter_memcached_native_spec.rb +1 -1
  85. data/spec/moneta/adapters/memcached/native/adapter_memcached_native_with_default_expires_spec.rb +1 -1
  86. data/spec/moneta/adapters/memcached/native/standard_memcached_native_spec.rb +1 -1
  87. data/spec/moneta/adapters/memcached/standard_memcached_spec.rb +1 -1
  88. data/spec/moneta/adapters/{memcached/helper.rb → memcached_helper.rb} +0 -0
  89. data/spec/moneta/adapters/null/null_adapter_spec.rb +1 -1
  90. data/spec/moneta/adapters/restclient/adapter_restclient_spec.rb +7 -5
  91. data/spec/moneta/adapters/restclient/helper.rb +12 -0
  92. data/spec/moneta/adapters/restclient/standard_restclient_spec.rb +9 -6
  93. data/spec/moneta/adapters/riak/standard_riak_with_expires_spec.rb +4 -0
  94. data/spec/moneta/adapters/tokyotyrant/adapter_tokyotyrant_spec.rb +6 -2
  95. data/spec/moneta/adapters/tokyotyrant/helper.rb +12 -0
  96. data/spec/moneta/adapters/tokyotyrant/standard_tokyotyrant_spec.rb +5 -2
  97. data/spec/moneta/adapters/tokyotyrant/standard_tokyotyrant_with_expires_spec.rb +5 -1
  98. data/spec/moneta/proxies/enumerable/enumerable_spec.rb +26 -0
  99. data/spec/moneta/proxies/fallback/fallback_spec.rb +42 -0
  100. data/spec/moneta/proxies/pool/pool_spec.rb +319 -6
  101. data/spec/restserver.rb +40 -0
  102. metadata +122 -7
  103. data/script/install-kyotocabinet +0 -17
@@ -0,0 +1,12 @@
1
+ require_relative '../../../restserver.rb'
2
+
3
+ RSpec.shared_context :start_restserver do |port|
4
+ before :context do
5
+ @restserver_handle = start_restserver(port)
6
+ end
7
+
8
+ after :context do
9
+ stop_restserver(@restserver_handle)
10
+ @restserver_handle = nil
11
+ end
12
+ end
@@ -1,10 +1,13 @@
1
- describe 'standard_restclient', isolate: true, adapter: :RestClient do
2
- before :all do
3
- start_restserver
4
- end
1
+ require_relative '../faraday_helper.rb'
2
+ require_relative './helper.rb'
3
+
4
+ describe 'standard_restclient', adapter: :RestClient do
5
+ include_context :faraday_adapter
6
+ include_context :start_restserver, 11934
5
7
 
6
- moneta_store :RestClient,
7
- {url: 'http://localhost:8808/moneta/'}
8
+ moneta_store :RestClient do
9
+ { url: 'http://localhost:11934/moneta', adapter: faraday_adapter }
10
+ end
8
11
 
9
12
  moneta_specs STANDARD_SPECS.without_increment.without_create
10
13
  end
@@ -5,6 +5,10 @@ describe 'standard_riak_with_expires', unstable: true, adapter: :Riak do
5
5
  Riak.disable_list_keys_warnings = true
6
6
  end
7
7
 
8
+ let(:t_res) { 0.125 }
9
+ let(:min_ttl) { t_res }
10
+ use_timecop
11
+
8
12
  moneta_store :Riak, {bucket: 'standard_riak_with_expires', expires: true}
9
13
  moneta_specs STANDARD_SPECS.without_increment.with_expires.without_create
10
14
  end
@@ -1,6 +1,10 @@
1
- describe 'adapter_tokyotyrant', isolate: true, adapter: :TokyoTyrant do
1
+ require_relative './helper.rb'
2
+
3
+ describe 'adapter_tokyotyrant', adapter: :TokyoTyrant do
4
+ include_context :start_tokyotyrant, 10654
5
+
2
6
  moneta_build do
3
- Moneta::Adapters::TokyoTyrant.new
7
+ Moneta::Adapters::TokyoTyrant.new(port: 10654)
4
8
  end
5
9
 
6
10
  moneta_specs ADAPTER_SPECS
@@ -0,0 +1,12 @@
1
+ RSpec.shared_context :start_tokyotyrant do |port|
2
+ before :context do
3
+ @tokyotyrant = spawn("ttserver -port #{port} -le -log #{tempdir}/tokyotyrant#{port}.log #{tempdir}/tokyotyrant#{port}.tch")
4
+ sleep 0.5
5
+ end
6
+
7
+ after :context do
8
+ Process.kill("TERM", @tokyotyrant)
9
+ Process.wait(@tokyotyrant)
10
+ @tokyotyrant = nil
11
+ end
12
+ end
@@ -1,4 +1,7 @@
1
- describe 'standard_tokyotyrant', isolate: true, adapter: :TokyoTyrant do
2
- moneta_store :TokyoTyrant
1
+ require_relative './helper.rb'
2
+
3
+ describe 'standard_tokyotyrant', adapter: :TokyoTyrant do
4
+ include_context :start_tokyotyrant, 10655
5
+ moneta_store :TokyoTyrant, port: 10655
3
6
  moneta_specs STANDARD_SPECS
4
7
  end
@@ -1,8 +1,12 @@
1
+ require_relative './helper.rb'
2
+
1
3
  describe 'standard_tokyotyrant_with_expires', adapter: :TokyoTyrant do
2
4
  let(:t_res) { 0.125 }
3
5
  let(:min_ttl) { t_res }
4
6
  use_timecop
5
7
 
6
- moneta_store :TokyoTyrant, {expires: true}
8
+ include_context :start_tokyotyrant, 10656
9
+
10
+ moneta_store :TokyoTyrant, expires: true, port: 10656
7
11
  moneta_specs STANDARD_SPECS.with_expires
8
12
  end
@@ -0,0 +1,26 @@
1
+ describe 'enumerable', proxy: :Enumerable do
2
+ moneta_build do
3
+ Moneta.build do
4
+ use :Enumerable
5
+ adapter :Memory
6
+ end
7
+ end
8
+
9
+ moneta_specs STANDARD_SPECS.without_transform.returnsame.without_persist.with_each_key
10
+
11
+ it 'includes the enumerable interface' do
12
+ expect(store).to be_a Enumerable
13
+ expect(Enumerable.instance_methods).to all satisfy { |m| store.respond_to? m }
14
+ end
15
+
16
+ it 'allows enumeration over key-value pairs' do
17
+ moneta_property_of(keys: 100, values: 100) do |keys:, values:|
18
+ pairs = keys.zip(values)
19
+ store.merge!(pairs)
20
+ expect(store.to_a).to contain_exactly(*pairs)
21
+ expect(store.each.to_a).to contain_exactly(*pairs)
22
+ expect(store.each_pair.to_a).to contain_exactly(*pairs)
23
+ store.clear
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,42 @@
1
+ describe 'fallback', proxy: :Fallback do
2
+ context 'when the adapter is working' do
3
+ moneta_build do
4
+ Moneta.build do
5
+ use :Fallback
6
+ adapter :Memory
7
+ end
8
+ end
9
+
10
+ moneta_specs STANDARD_SPECS.without_transform.returnsame.without_persist.with_each_key
11
+ end
12
+
13
+ context 'when the adapter is broken' do
14
+ moneta_build do
15
+ Moneta.build do
16
+ use :Fallback #, rescue: [IOError, NoMethodError]
17
+ adapter(Class.new do
18
+ include Moneta::Defaults
19
+
20
+ def load(key, options = {})
21
+ raise IOError, "deliberate error for load"
22
+ end
23
+
24
+ def store(key, value, options = {})
25
+ raise IOError, "deliberate error for store"
26
+ end
27
+
28
+ def delete(key, options = {})
29
+ raise IOError, "deliberate error for delete"
30
+ end
31
+
32
+ def clear(options = {})
33
+ raise IOError, "deliberate error for clear"
34
+ end
35
+ end.new)
36
+ end
37
+ end
38
+
39
+ # Null adapter behaviour
40
+ moneta_specs MonetaSpecs.new(specs: [:null, :not_increment, :not_create, :not_persist])
41
+ end
42
+ end
@@ -1,12 +1,325 @@
1
+ require 'timeout'
2
+
1
3
  describe "pool", proxy: :Pool do
2
- moneta_build do
3
- tempdir = self.tempdir
4
- Moneta.build do
5
- use :Pool do
6
- adapter :File, dir: File.join(tempdir, "pool")
4
+ describe "Moneta::Pool" do
5
+ # To test the pool, we create the store once and pass the same object around
6
+ # whenever a new store is requested.
7
+ before :context do
8
+ tempdir = @pool_tempdir = Dir.mktmpdir
9
+ @pool_store = Moneta.build do
10
+ use :Pool, max: 2, timeout: 5 do
11
+ adapter :File, dir: tempdir
12
+ end
7
13
  end
8
14
  end
15
+
16
+ # Tell the manager to close all open stores
17
+ after :context do
18
+ @pool_store.stop
19
+ FileUtils.remove_dir(@pool_tempdir)
20
+ end
21
+
22
+ moneta_build { @pool_store }
23
+ moneta_specs ADAPTER_SPECS.with_each_key
24
+
25
+ it "raises an error on check-out the builder fails" do
26
+ store = Moneta.build do
27
+ use :Pool do
28
+ adapter(Class.new do
29
+ def initialize(options = {})
30
+ raise "boom"
31
+ end
32
+ end)
33
+ end
34
+ end
35
+
36
+ expect { store['x'] }.to raise_error "boom"
37
+ end
9
38
  end
10
39
 
11
- moneta_specs ADAPTER_SPECS.with_each_key
40
+ describe "Moneta::Pool::PoolManager" do
41
+ let :builder do
42
+ double('builder').tap do |builder|
43
+ i = -1
44
+ allow(builder).to receive(:build) do
45
+ [stores[i += 1]]
46
+ end
47
+ end
48
+ end
49
+
50
+ let(:stores) { (0...num).map { |i| double("store#{i}") } }
51
+
52
+ after { subject.kill! }
53
+
54
+ shared_examples :no_ttl do
55
+ context "with one store" do
56
+ let(:num) { 1 }
57
+
58
+ it "never expires the store" do
59
+ store = stores.first
60
+ expect(builder).to receive(:build).once
61
+ expect(subject.check_out).to be store
62
+ expect(subject.stats).to include(stores: 1, available: 0)
63
+ expect(subject.check_in(store)).to eq nil
64
+ expect(subject.stats).to include(stores: 1, available: 1)
65
+ sleep 1
66
+ expect(subject.stats).to include(stores: 1, available: 1)
67
+ expect(subject.check_out).to be store
68
+ expect(subject.check_in(store)).to eq nil
69
+ end
70
+ end
71
+ end
72
+
73
+ shared_examples :no_max do
74
+ context "with 1,000 stores" do
75
+ let(:num) { 1_000 }
76
+
77
+ it "never blocks" do
78
+ # Check out 1000 stores in 1000 threads
79
+ threads = (0...num).map do
80
+ Thread.new { subject.check_out }
81
+ end
82
+ expect(threads.map(&:value)).to contain_exactly(*stores)
83
+ expect(subject.stats).to include(stores: num, available: 0, waiting: 0)
84
+
85
+ # Check in the first 50
86
+ expect(stores.take(50).map { |store| subject.check_in(store) }).to all(be nil)
87
+ expect(subject.stats).to include(stores: num, available: 50, waiting: 0)
88
+
89
+ # Now check those 50 out again
90
+ threads = (0...50).map do
91
+ Thread.new { subject.check_out }
92
+ end
93
+ expect(threads.map(&:value)).to contain_exactly(*stores.take(50))
94
+
95
+ # Finally check in all stores
96
+ expect(stores.map { |store| subject.check_in(store) }).to all(be nil)
97
+ end
98
+ end
99
+ end
100
+
101
+ shared_examples :min do |min|
102
+ describe "initial number of stores" do
103
+ let(:num) { min }
104
+
105
+ it "starts with #{min} available stores" do
106
+ expect(subject.stats).to include(stores: min, available: min)
107
+ expect((0...min).map { subject.check_out }).to contain_exactly(*stores)
108
+ end
109
+ end
110
+ end
111
+
112
+ shared_examples :max do |max, timeout: 5|
113
+ describe "maximum number of stores" do
114
+ let(:num) { max }
115
+
116
+ after do
117
+ expect(stores.map { |store| subject.check_in(store) }).to all be_nil
118
+ end
119
+
120
+ it "blocks after #{max} stores have been created" do
121
+ # All stores are checked out
122
+ expect(max.times.map { subject.check_out }).to contain_exactly(*stores)
123
+
124
+ # Launch threads that make checkout requests (in order). These will
125
+ # all block.
126
+ threads = max.times.map { Thread.new { subject.check_out } }
127
+ Timeout.timeout(timeout) { Thread.pass until subject.stats[:waiting] == max }
128
+ expect(threads).to all be_alive
129
+
130
+ # Check all stores except the first one back in. This will cause all
131
+ # but one of the threads to return
132
+ expect(stores.drop(1).map { |store| subject.check_in(store) }).to all be_nil
133
+ Timeout.timeout(timeout) { Thread.pass until threads.select(&:alive?).length == 1 }
134
+ expect(subject.stats).to include(waiting: 1)
135
+
136
+ # Ensure that the stores that were checked back in are the ones that
137
+ # were given to the waiting threads.
138
+ alive, dead = threads.partition(&:alive?)
139
+ expect(dead.map(&:value)).to contain_exactly(*stores.drop(1))
140
+
141
+ # Check the last store back in and make sure it goes to the last
142
+ # waiting thread.
143
+ last_thread = alive.first
144
+ expect(subject.check_in(stores.first)).to eq nil
145
+ Timeout.timeout(timeout) { Thread.pass while last_thread.alive? }
146
+ expect(subject.stats).to include(waiting: 0)
147
+ expect(last_thread.value).to be stores.first
148
+ end
149
+ end
150
+ end
151
+
152
+ shared_examples :ttl do |ttl, min: 0, max: nil|
153
+ describe "closing stores after TTL expiry" do
154
+ let(:num) { max || min + 10 }
155
+
156
+ it "closes available stores after ttl" do
157
+ stores.each do |store|
158
+ allow(store).to receive(:close)
159
+ end
160
+
161
+ Timeout.timeout(5) { Thread.pass until subject.stats[:stores] == min }
162
+ expect(stores.length.times.map { subject.check_out }).to contain_exactly(*stores)
163
+ expect(subject.stats).to include(stores: num, available: 0)
164
+ expect(stores.map { |store| subject.check_in(store) }).to all be_nil
165
+ expect(subject.stats).to include(stores: num, available: num)
166
+ sleep ttl
167
+ expect(subject.stats).to include(stores: min, available: min)
168
+ end
169
+ end
170
+ end
171
+
172
+ shared_examples :timeout do |timeout, max:|
173
+ describe "raising an error after timeout" do
174
+ let(:num) { max }
175
+
176
+ it "raises a timeout error after waiting too long" do
177
+ expect((0...num).map { subject.check_out }).to contain_exactly(*stores)
178
+ # One extra checkout request in a separate thread
179
+ t = Thread.new do
180
+ Thread.current.report_on_exception = false if Thread.current.respond_to? :report_on_exception
181
+ subject.check_out
182
+ end
183
+ Timeout.timeout(timeout) { Thread.pass until subject.stats[:waiting] == 1 }
184
+ expect(subject.stats[:longest_wait]).to be_a Time
185
+ expect(t).to be_alive
186
+ sleep timeout
187
+ Timeout.timeout(timeout) { Thread.pass while t.alive? }
188
+ expect { t.value }.to raise_error Moneta::Pool::TimeoutError
189
+ expect(subject.stats).to include(waiting: 0, longest_wait: nil)
190
+ expect(stores.map { |store| subject.check_in store }).to all be_nil
191
+ end
192
+ end
193
+ end
194
+
195
+ context "with default arguments" do
196
+ subject { Moneta::Pool::PoolManager.new(builder) }
197
+ after { subject.kill! }
198
+
199
+ include_examples :no_ttl
200
+ include_examples :no_max
201
+ include_examples :min, 0
202
+ end
203
+
204
+ context "with max: 10, timeout: 4" do
205
+ subject { Moneta::Pool::PoolManager.new(builder, max: 10, timeout: 4) }
206
+ after { subject.kill! }
207
+
208
+ include_examples :no_ttl
209
+ include_examples :max, 10, timeout: 4
210
+ include_examples :min, 0
211
+ include_examples :timeout, 4, max: 10
212
+ end
213
+
214
+ context "with min: 10" do
215
+ subject { Moneta::Pool::PoolManager.new(builder, min: 10) }
216
+ after { subject.kill! }
217
+
218
+ include_examples :no_max
219
+ include_examples :min, 10
220
+ end
221
+
222
+ context "with ttl: 1" do
223
+ subject { Moneta::Pool::PoolManager.new(builder, ttl: 1) }
224
+ after { subject.kill! }
225
+
226
+ include_examples :ttl, 1, min: 0
227
+ end
228
+
229
+ context "with min: 10, max: 20, ttl: 1, timeout: 4" do
230
+ subject { Moneta::Pool::PoolManager.new(builder, min: 10, max: 20, ttl: 1, timeout: 4) }
231
+ after { subject.kill! }
232
+
233
+ include_examples :min, 10
234
+ include_examples :max, 20, timeout: 4
235
+ include_examples :ttl, 1, min: 10, max: 20
236
+ include_examples :timeout, 4, max: 20
237
+ end
238
+
239
+ context "with min: 10, max: 10, ttl: 2, timeout: 4" do
240
+ subject { Moneta::Pool::PoolManager.new(builder, min: 10, max: 10, ttl: 2, timeout: 4) }
241
+ after { subject.kill! }
242
+
243
+ include_examples :min, 10
244
+ include_examples :max, 10, timeout: 4
245
+ include_examples :ttl, 2, min: 10, max: 10
246
+ include_examples :timeout, 4, max: 10
247
+ end
248
+
249
+ describe '#check_out' do
250
+ subject { Moneta::Pool::PoolManager.new(builder, max: 1, timeout: 5) }
251
+ after { subject.kill! }
252
+
253
+ let(:num) { 1 }
254
+
255
+ it 'yields the store to requesters first come, first served' do
256
+ store = stores.first
257
+ procs = (0...10)
258
+ .map { |i| double("proc#{i}") }
259
+ .each { |p| expect(p).to receive(:call).with(store).ordered }
260
+
261
+ # Each thread stops immediately after starting. We can then ensure
262
+ # that each checkout is called in order.
263
+ threads = procs.map do |p|
264
+ Thread.new do
265
+ Thread.stop
266
+ p.call(subject.check_out)
267
+ end
268
+ end
269
+
270
+ Timeout.timeout(5) { Thread.pass until threads.all?(&:stop?) }
271
+
272
+ # The first thread should return immediately after waking
273
+ threads.first.wakeup
274
+ Timeout.timeout(5) { threads.first.join }
275
+
276
+ # Wait for the remaining threads to block, one at a time.
277
+ threads.drop(1).each_with_index do |thread, i|
278
+ thread.wakeup
279
+ Timeout.timeout(5) { Thread.pass until subject.stats[:waiting] == i + 1 }
280
+ end
281
+
282
+ # The remaining threads should be waiting for the store to be checked back in
283
+ expect(threads.drop(1)).to all be_alive
284
+ expect(subject.stats).to include(waiting: 9)
285
+
286
+ Timeout.timeout(5) do
287
+ threads.each do |t|
288
+ t.join
289
+ subject.check_in(store)
290
+ end
291
+ end
292
+ end
293
+
294
+ it "raises a ShutdownError if the pool is stopped while waiting for a store" do
295
+ # Exaust the pool
296
+ store = stores.first
297
+ allow(store).to receive(:close).once
298
+ expect(subject.check_out).to eq store
299
+
300
+ # Simulate a new thread requesting a check-out
301
+ t1 = Thread.new do
302
+ Thread.current.report_on_exception = false if Thread.current.respond_to? :report_on_exception
303
+ subject.check_out
304
+ end
305
+ Timeout.timeout(5) { Thread.pass until subject.stats[:waiting] > 0 }
306
+ expect(t1).to be_alive
307
+
308
+ # Meanwhile in another thread, the pool is stopped.
309
+ t2 = Thread.new { subject.stop }
310
+
311
+ # The requesting thread should error out immediately
312
+ expect { t1.value }.to raise_error Moneta::Pool::ShutdownError
313
+
314
+ # In this thread we return the store to the pool, allowing graceful shutdown to complete.
315
+ subject.check_in(store)
316
+ expect(t2.value).to be_nil
317
+ end
318
+
319
+ it "raises a ShutdownError if a the pool is stopped before requesting a store" do
320
+ subject.stop
321
+ expect { subject.check_out }.to raise_error Moneta::Pool::ShutdownError
322
+ end
323
+ end
324
+ end
12
325
  end