moneta 1.1.1 → 1.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.rubocop.yml +194 -0
- data/.travis.yml +65 -25
- data/CHANGES +36 -0
- data/CONTRIBUTORS +4 -2
- data/Gemfile +94 -65
- data/README.md +50 -25
- data/feature_matrix.yaml +3 -11
- data/lib/action_dispatch/middleware/session/moneta_store.rb +1 -0
- data/lib/active_support/cache/moneta_store.rb +5 -5
- data/lib/moneta.rb +18 -10
- data/lib/moneta/adapters/activerecord.rb +35 -19
- data/lib/moneta/adapters/activesupportcache.rb +3 -7
- data/lib/moneta/adapters/cassandra.rb +24 -16
- data/lib/moneta/adapters/client.rb +62 -21
- data/lib/moneta/adapters/couch.rb +225 -80
- data/lib/moneta/adapters/datamapper.rb +1 -0
- data/lib/moneta/adapters/file.rb +9 -6
- data/lib/moneta/adapters/hbase.rb +1 -1
- data/lib/moneta/adapters/kyotocabinet.rb +8 -7
- data/lib/moneta/adapters/leveldb.rb +1 -1
- data/lib/moneta/adapters/lmdb.rb +3 -4
- data/lib/moneta/adapters/lruhash.rb +29 -62
- data/lib/moneta/adapters/memcached.rb +1 -0
- data/lib/moneta/adapters/memcached/dalli.rb +1 -1
- data/lib/moneta/adapters/memcached/native.rb +10 -8
- data/lib/moneta/adapters/mongo.rb +264 -6
- data/lib/moneta/adapters/null.rb +1 -2
- data/lib/moneta/adapters/pstore.rb +3 -2
- data/lib/moneta/adapters/redis.rb +8 -4
- data/lib/moneta/adapters/restclient.rb +12 -3
- data/lib/moneta/adapters/riak.rb +2 -2
- data/lib/moneta/adapters/sequel.rb +68 -494
- data/lib/moneta/adapters/sequel/mysql.rb +66 -0
- data/lib/moneta/adapters/sequel/postgres.rb +80 -0
- data/lib/moneta/adapters/sequel/postgres_hstore.rb +240 -0
- data/lib/moneta/adapters/sequel/sqlite.rb +57 -0
- data/lib/moneta/adapters/sqlite.rb +10 -10
- data/lib/moneta/adapters/tokyotyrant.rb +1 -1
- data/lib/moneta/builder.rb +2 -3
- data/lib/moneta/create_support.rb +21 -0
- data/lib/moneta/dbm_adapter.rb +31 -0
- data/lib/moneta/{mixins.rb → defaults.rb} +3 -302
- data/lib/moneta/each_key_support.rb +27 -0
- data/lib/moneta/enumerable.rb +38 -0
- data/lib/moneta/expires.rb +12 -12
- data/lib/moneta/expires_support.rb +60 -0
- data/lib/moneta/fallback.rb +84 -0
- data/lib/moneta/hash_adapter.rb +68 -0
- data/lib/moneta/increment_support.rb +16 -0
- data/lib/moneta/lock.rb +7 -2
- data/lib/moneta/logger.rb +2 -2
- data/lib/moneta/nil_values.rb +35 -0
- data/lib/moneta/option_support.rb +51 -0
- data/lib/moneta/optionmerger.rb +0 -1
- data/lib/moneta/pool.rb +312 -30
- data/lib/moneta/proxy.rb +3 -3
- data/lib/moneta/server.rb +216 -65
- data/lib/moneta/shared.rb +13 -7
- data/lib/moneta/stack.rb +6 -6
- data/lib/moneta/synchronize.rb +3 -3
- data/lib/moneta/transformer.rb +68 -24
- data/lib/moneta/transformer/config.rb +63 -43
- data/lib/moneta/transformer/helper.rb +3 -3
- data/lib/moneta/transformer/helper/bson.rb +7 -14
- data/lib/moneta/utils.rb +3 -9
- data/lib/moneta/version.rb +1 -1
- data/lib/moneta/weak_each_key.rb +2 -4
- data/lib/rack/cache/moneta.rb +13 -11
- data/lib/rack/moneta_rest.rb +2 -2
- data/lib/rack/session/moneta.rb +3 -4
- data/moneta.gemspec +18 -4
- data/script/benchmarks +61 -34
- data/script/contributors +11 -6
- data/script/start-couchdb +27 -0
- data/script/start-hbase +3 -2
- data/script/start-services +3 -11
- data/spec/active_support/cache_moneta_store_spec.rb +30 -30
- data/spec/features/concurrent_create.rb +31 -10
- data/spec/features/concurrent_increment.rb +26 -19
- data/spec/features/create_expires.rb +15 -15
- data/spec/features/default_expires.rb +11 -12
- data/spec/features/expires.rb +215 -210
- data/spec/features/increment.rb +41 -41
- data/spec/features/store.rb +3 -3
- data/spec/helper.rb +21 -82
- data/spec/moneta/adapters/activerecord/standard_activerecord_spec.rb +1 -1
- data/spec/moneta/adapters/activerecord/standard_activerecord_with_expires_spec.rb +1 -1
- data/spec/moneta/adapters/activesupportcache/adapter_activesupportcache_spec.rb +4 -1
- data/spec/moneta/adapters/activesupportcache/adapter_activesupportcache_with_default_expires_spec.rb +4 -1
- data/spec/moneta/adapters/activesupportcache/standard_activesupportcache_spec.rb +14 -0
- data/spec/moneta/adapters/cassandra/standard_cassandra_spec.rb +1 -1
- data/spec/moneta/adapters/client/adapter_client_spec.rb +6 -6
- data/spec/moneta/adapters/client/client_helper.rb +24 -0
- data/spec/moneta/adapters/client/standard_client_tcp_spec.rb +8 -8
- data/spec/moneta/adapters/client/standard_client_unix_spec.rb +23 -7
- data/spec/moneta/adapters/couch/adapter_couch_spec.rb +199 -2
- data/spec/moneta/adapters/couch/standard_couch_spec.rb +9 -3
- data/spec/moneta/adapters/couch/standard_couch_with_expires_spec.rb +8 -2
- data/spec/moneta/adapters/daybreak/standard_daybreak_spec.rb +1 -1
- data/spec/moneta/adapters/daybreak/standard_daybreak_with_expires_spec.rb +1 -1
- data/spec/moneta/adapters/dbm/standard_dbm_spec.rb +1 -1
- data/spec/moneta/adapters/dbm/standard_dbm_with_expires_spec.rb +1 -1
- data/spec/moneta/adapters/faraday_helper.rb +9 -0
- data/spec/moneta/adapters/file/standard_file_spec.rb +2 -2
- data/spec/moneta/adapters/file/standard_file_with_expires_spec.rb +1 -1
- data/spec/moneta/adapters/gdbm/standard_gdbm_spec.rb +1 -1
- data/spec/moneta/adapters/gdbm/standard_gdbm_with_expires_spec.rb +1 -1
- data/spec/moneta/adapters/kyotocabinet/adapter_kyotocabinet_spec.rb +1 -1
- data/spec/moneta/adapters/kyotocabinet/standard_kyotocabinet_spec.rb +2 -2
- data/spec/moneta/adapters/kyotocabinet/standard_kyotocabinet_with_expires_spec.rb +2 -2
- data/spec/moneta/adapters/leveldb/standard_leveldb_spec.rb +1 -1
- data/spec/moneta/adapters/leveldb/standard_leveldb_with_expires_spec.rb +1 -1
- data/spec/moneta/adapters/lmdb/standard_lmdb_spec.rb +1 -1
- data/spec/moneta/adapters/lmdb/standard_lmdb_with_expires_spec.rb +1 -1
- data/spec/moneta/adapters/lruhash/adapter_lruhash_spec.rb +2 -2
- data/spec/moneta/adapters/lruhash/standard_lruhash_spec.rb +1 -1
- data/spec/moneta/adapters/lruhash/standard_lruhash_with_expires_spec.rb +1 -1
- data/spec/moneta/adapters/memcached/adapter_memcached_spec.rb +1 -1
- data/spec/moneta/adapters/memcached/dalli/adapter_memcached_dalli_spec.rb +1 -1
- data/spec/moneta/adapters/memcached/dalli/adapter_memcached_dalli_with_default_expires_spec.rb +1 -1
- data/spec/moneta/adapters/memcached/dalli/standard_memcached_dalli_spec.rb +1 -1
- data/spec/moneta/adapters/memcached/native/adapter_memcached_native_spec.rb +1 -1
- data/spec/moneta/adapters/memcached/native/adapter_memcached_native_with_default_expires_spec.rb +1 -1
- data/spec/moneta/adapters/memcached/native/standard_memcached_native_spec.rb +1 -1
- data/spec/moneta/adapters/memcached/standard_memcached_spec.rb +1 -1
- data/spec/moneta/adapters/{memcached/helper.rb → memcached_helper.rb} +0 -0
- data/spec/moneta/adapters/memory/standard_memory_spec.rb +1 -1
- data/spec/moneta/adapters/memory/standard_memory_with_compress_spec.rb +1 -1
- data/spec/moneta/adapters/memory/standard_memory_with_expires_spec.rb +1 -1
- data/spec/moneta/adapters/memory/standard_memory_with_json_key_serializer_spec.rb +1 -1
- data/spec/moneta/adapters/memory/standard_memory_with_json_serializer_spec.rb +1 -1
- data/spec/moneta/adapters/memory/standard_memory_with_json_value_serializer_spec.rb +2 -2
- data/spec/moneta/adapters/memory/standard_memory_with_prefix_spec.rb +39 -2
- data/spec/moneta/adapters/memory/standard_memory_with_snappy_compress_spec.rb +2 -2
- data/spec/moneta/adapters/mongo/adapter_mongo_spec.rb +32 -2
- data/spec/moneta/adapters/mongo/adapter_mongo_with_default_expires_spec.rb +5 -3
- data/spec/moneta/adapters/mongo/standard_mongo_spec.rb +2 -2
- data/spec/moneta/adapters/null/null_adapter_spec.rb +1 -1
- data/spec/moneta/adapters/pstore/standard_pstore_spec.rb +1 -1
- data/spec/moneta/adapters/pstore/standard_pstore_with_expires_spec.rb +1 -1
- data/spec/moneta/adapters/redis/standard_redis_spec.rb +1 -1
- data/spec/moneta/adapters/restclient/adapter_restclient_spec.rb +7 -5
- data/spec/moneta/adapters/restclient/helper.rb +12 -0
- data/spec/moneta/adapters/restclient/standard_restclient_spec.rb +9 -6
- data/spec/moneta/adapters/riak/standard_riak_with_expires_spec.rb +4 -0
- data/spec/moneta/adapters/sdbm/standard_sdbm_spec.rb +1 -1
- data/spec/moneta/adapters/sdbm/standard_sdbm_with_expires_spec.rb +1 -1
- data/spec/moneta/adapters/sequel/adapter_sequel_spec.rb +7 -34
- data/spec/moneta/adapters/sequel/helper.rb +37 -0
- data/spec/moneta/adapters/sequel/standard_sequel_spec.rb +5 -11
- data/spec/moneta/adapters/sequel/standard_sequel_with_expires_spec.rb +8 -9
- data/spec/moneta/adapters/sqlite/standard_sqlite_spec.rb +1 -1
- data/spec/moneta/adapters/sqlite/standard_sqlite_with_expires_spec.rb +1 -1
- data/spec/moneta/adapters/tdb/standard_tdb_spec.rb +1 -1
- data/spec/moneta/adapters/tdb/standard_tdb_with_expires_spec.rb +1 -1
- data/spec/moneta/adapters/tokyocabinet/standard_tokyocabinet_spec.rb +1 -1
- data/spec/moneta/adapters/tokyocabinet/standard_tokyocabinet_with_expires_spec.rb +1 -1
- data/spec/moneta/adapters/tokyotyrant/adapter_tokyotyrant_spec.rb +6 -2
- data/spec/moneta/adapters/tokyotyrant/helper.rb +12 -0
- data/spec/moneta/adapters/tokyotyrant/standard_tokyotyrant_spec.rb +5 -2
- data/spec/moneta/adapters/tokyotyrant/standard_tokyotyrant_with_expires_spec.rb +5 -1
- data/spec/moneta/adapters/yaml/standard_yaml_spec.rb +1 -1
- data/spec/moneta/adapters/yaml/standard_yaml_with_expires_spec.rb +1 -1
- data/spec/moneta/builder_spec.rb +22 -0
- data/spec/moneta/proxies/enumerable/enumerable_spec.rb +26 -0
- data/spec/moneta/proxies/expires/expires_file_spec.rb +1 -1
- data/spec/moneta/proxies/fallback/fallback_spec.rb +42 -0
- data/spec/moneta/proxies/pool/pool_spec.rb +319 -6
- data/spec/moneta/proxies/shared/shared_tcp_spec.rb +14 -4
- data/spec/moneta/proxies/shared/shared_unix_spec.rb +14 -4
- data/spec/moneta/proxies/transformer/transformer_bencode_spec.rb +1 -1
- data/spec/moneta/proxies/transformer/transformer_bert_spec.rb +3 -3
- data/spec/moneta/proxies/transformer/transformer_bson_spec.rb +2 -2
- data/spec/moneta/proxies/transformer/transformer_json_spec.rb +1 -1
- data/spec/moneta/proxies/transformer/transformer_key_marshal_spec.rb +1 -1
- data/spec/moneta/proxies/transformer/transformer_key_yaml_spec.rb +1 -1
- data/spec/moneta/proxies/transformer/transformer_marshal_base64_spec.rb +1 -1
- data/spec/moneta/proxies/transformer/transformer_marshal_escape_spec.rb +8 -4
- data/spec/moneta/proxies/transformer/transformer_marshal_hex_spec.rb +1 -1
- data/spec/moneta/proxies/transformer/transformer_marshal_hmac_spec.rb +1 -1
- data/spec/moneta/proxies/transformer/transformer_marshal_prefix_base64_spec.rb +33 -0
- data/spec/moneta/proxies/transformer/transformer_marshal_prefix_spec.rb +1 -1
- data/spec/moneta/proxies/transformer/transformer_marshal_qp_spec.rb +1 -1
- data/spec/moneta/proxies/transformer/transformer_marshal_spec.rb +1 -1
- data/spec/moneta/proxies/transformer/transformer_marshal_urlsafe_base64_spec.rb +1 -1
- data/spec/moneta/proxies/transformer/transformer_marshal_uuencode_spec.rb +1 -1
- data/spec/moneta/proxies/transformer/transformer_msgpack_spec.rb +1 -1
- data/spec/moneta/proxies/transformer/transformer_ox_spec.rb +1 -1
- data/spec/moneta/proxies/transformer/transformer_php_spec.rb +1 -1
- data/spec/moneta/proxies/transformer/transformer_tnet_spec.rb +1 -1
- data/spec/moneta/proxies/transformer/transformer_yaml_spec.rb +2 -2
- data/spec/moneta/proxies/weak_each_key/weak_each_key_spec.rb +0 -2
- data/spec/restserver.rb +55 -0
- metadata +136 -32
- data/lib/moneta/adapters/mongo/base.rb +0 -103
- data/lib/moneta/adapters/mongo/moped.rb +0 -164
- data/lib/moneta/adapters/mongo/official.rb +0 -157
- data/script/install-kyotocabinet +0 -17
- data/spec/moneta/adapters/mongo/adapter_mongo_moped_spec.rb +0 -25
- data/spec/moneta/adapters/mongo/adapter_mongo_moped_with_default_expires_spec.rb +0 -12
- data/spec/moneta/adapters/mongo/adapter_mongo_official_spec.rb +0 -25
- data/spec/moneta/adapters/mongo/adapter_mongo_official_with_default_expires_spec.rb +0 -12
- data/spec/moneta/adapters/mongo/standard_mongo_moped_spec.rb +0 -7
- data/spec/moneta/adapters/mongo/standard_mongo_official_spec.rb +0 -7
- data/spec/quality_spec.rb +0 -51
@@ -1,19 +1,18 @@
|
|
1
|
+
require_relative './helper.rb'
|
2
|
+
|
1
3
|
describe 'standard_sequel_with_expires', adapter: :Sequel do
|
4
|
+
include_context :sequel
|
5
|
+
|
2
6
|
let(:t_res) { 0.125 }
|
3
7
|
let(:min_ttl) { t_res }
|
4
8
|
use_timecop
|
5
9
|
|
6
10
|
moneta_store :Sequel do
|
7
|
-
|
8
|
-
|
9
|
-
"jdbc:mysql://localhost/#{mysql_database1}?user=#{mysql_username}"
|
10
|
-
else
|
11
|
-
"mysql2://#{mysql_username}:@localhost/#{mysql_database1}"
|
12
|
-
end,
|
13
|
-
table: "simple_sequel_with_expires",
|
11
|
+
postgres_options.merge(
|
12
|
+
table: "standard_sequel_with_expires",
|
14
13
|
expires: true
|
15
|
-
|
14
|
+
)
|
16
15
|
end
|
17
16
|
|
18
|
-
moneta_specs STANDARD_SPECS.with_expires
|
17
|
+
moneta_specs STANDARD_SPECS.with_expires.with_each_key
|
19
18
|
end
|
@@ -7,5 +7,5 @@ describe 'standard_tdb_with_expires', unsupported: defined?(JRUBY_VERSION), adap
|
|
7
7
|
{file: File.join(tempdir, "simple_tdb_with_expires"), expires: true}
|
8
8
|
end
|
9
9
|
|
10
|
-
moneta_specs STANDARD_SPECS.without_multiprocess.with_expires
|
10
|
+
moneta_specs STANDARD_SPECS.without_multiprocess.with_expires.with_each_key
|
11
11
|
end
|
@@ -7,5 +7,5 @@ describe 'standard_tokyocabinet_with_expires', unsupported: defined?(JRUBY_VERSI
|
|
7
7
|
{file: File.join(tempdir, "simple_tokyocabinet_with_expires"), expires: true}
|
8
8
|
end
|
9
9
|
|
10
|
-
moneta_specs STANDARD_SPECS.without_multiprocess.with_expires
|
10
|
+
moneta_specs STANDARD_SPECS.without_multiprocess.with_expires.with_each_key
|
11
11
|
end
|
@@ -1,6 +1,10 @@
|
|
1
|
-
|
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
|
-
|
2
|
-
|
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
|
-
|
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
|
@@ -9,5 +9,5 @@ describe 'standard_yaml_with_expires', adapter: :YAML do
|
|
9
9
|
|
10
10
|
moneta_loader{ |value| value }
|
11
11
|
|
12
|
-
moneta_specs STANDARD_SPECS.without_marshallable_value.with_expires.without_concurrent
|
12
|
+
moneta_specs STANDARD_SPECS.without_marshallable_value.with_expires.without_concurrent.with_each_key
|
13
13
|
end
|
data/spec/moneta/builder_spec.rb
CHANGED
@@ -25,4 +25,26 @@ describe Moneta::Builder do
|
|
25
25
|
end.build
|
26
26
|
end.to raise_error /Please check/
|
27
27
|
end
|
28
|
+
|
29
|
+
it 'dups options before passing them to each middleware' do
|
30
|
+
my_adapter = Class.new do
|
31
|
+
def initialize(options)
|
32
|
+
throw "a is missing" unless options.delete(:a)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
my_middleware = Class.new do
|
37
|
+
def initialize(backend, options)
|
38
|
+
throw "a is missing" unless options.delete(:a)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
options = { a: 1 }
|
43
|
+
Moneta::Builder.new do
|
44
|
+
use my_middleware, options
|
45
|
+
adapter my_adapter, options
|
46
|
+
end.build
|
47
|
+
|
48
|
+
expect(options).to include(a: 1)
|
49
|
+
end
|
28
50
|
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
|
@@ -13,7 +13,7 @@ describe 'expires_file', proxy: :Expires do
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
moneta_specs STANDARD_SPECS.with_expires.stringvalues_only
|
16
|
+
moneta_specs STANDARD_SPECS.with_expires.stringvalues_only.with_each_key
|
17
17
|
|
18
18
|
it 'deletes expired value in underlying file storage' do
|
19
19
|
store.store('foo', 'bar', expires: 2)
|
@@ -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
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
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
|