moneta 1.1.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (208) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/.rubocop.yml +194 -0
  4. data/.travis.yml +65 -25
  5. data/CHANGES +36 -0
  6. data/CONTRIBUTORS +4 -2
  7. data/Gemfile +94 -65
  8. data/README.md +50 -25
  9. data/feature_matrix.yaml +3 -11
  10. data/lib/action_dispatch/middleware/session/moneta_store.rb +1 -0
  11. data/lib/active_support/cache/moneta_store.rb +5 -5
  12. data/lib/moneta.rb +20 -10
  13. data/lib/moneta/adapters/activerecord.rb +35 -19
  14. data/lib/moneta/adapters/activesupportcache.rb +3 -7
  15. data/lib/moneta/adapters/cassandra.rb +24 -16
  16. data/lib/moneta/adapters/client.rb +62 -21
  17. data/lib/moneta/adapters/couch.rb +225 -80
  18. data/lib/moneta/adapters/datamapper.rb +1 -0
  19. data/lib/moneta/adapters/file.rb +9 -6
  20. data/lib/moneta/adapters/hbase.rb +1 -1
  21. data/lib/moneta/adapters/kyotocabinet.rb +8 -7
  22. data/lib/moneta/adapters/leveldb.rb +1 -1
  23. data/lib/moneta/adapters/lmdb.rb +3 -4
  24. data/lib/moneta/adapters/lruhash.rb +29 -62
  25. data/lib/moneta/adapters/memcached.rb +1 -0
  26. data/lib/moneta/adapters/memcached/dalli.rb +1 -1
  27. data/lib/moneta/adapters/memcached/native.rb +10 -8
  28. data/lib/moneta/adapters/mongo.rb +256 -6
  29. data/lib/moneta/adapters/null.rb +1 -2
  30. data/lib/moneta/adapters/pstore.rb +3 -2
  31. data/lib/moneta/adapters/redis.rb +8 -4
  32. data/lib/moneta/adapters/restclient.rb +12 -3
  33. data/lib/moneta/adapters/riak.rb +2 -2
  34. data/lib/moneta/adapters/sequel.rb +137 -328
  35. data/lib/moneta/adapters/sequel/mysql.rb +66 -0
  36. data/lib/moneta/adapters/sequel/postgres.rb +80 -0
  37. data/lib/moneta/adapters/sequel/postgres_hstore.rb +240 -0
  38. data/lib/moneta/adapters/sequel/sqlite.rb +57 -0
  39. data/lib/moneta/adapters/sqlite.rb +25 -11
  40. data/lib/moneta/adapters/tokyotyrant.rb +1 -1
  41. data/lib/moneta/builder.rb +2 -3
  42. data/lib/moneta/create_support.rb +21 -0
  43. data/lib/moneta/dbm_adapter.rb +31 -0
  44. data/lib/moneta/{mixins.rb → defaults.rb} +3 -302
  45. data/lib/moneta/each_key_support.rb +27 -0
  46. data/lib/moneta/enumerable.rb +38 -0
  47. data/lib/moneta/expires.rb +12 -12
  48. data/lib/moneta/expires_support.rb +60 -0
  49. data/lib/moneta/fallback.rb +84 -0
  50. data/lib/moneta/hash_adapter.rb +68 -0
  51. data/lib/moneta/increment_support.rb +16 -0
  52. data/lib/moneta/lock.rb +7 -2
  53. data/lib/moneta/logger.rb +2 -2
  54. data/lib/moneta/nil_values.rb +35 -0
  55. data/lib/moneta/option_support.rb +51 -0
  56. data/lib/moneta/optionmerger.rb +0 -1
  57. data/lib/moneta/pool.rb +312 -30
  58. data/lib/moneta/proxy.rb +3 -3
  59. data/lib/moneta/server.rb +216 -65
  60. data/lib/moneta/shared.rb +13 -7
  61. data/lib/moneta/stack.rb +6 -6
  62. data/lib/moneta/synchronize.rb +3 -3
  63. data/lib/moneta/transformer.rb +68 -24
  64. data/lib/moneta/transformer/config.rb +63 -43
  65. data/lib/moneta/transformer/helper.rb +3 -3
  66. data/lib/moneta/transformer/helper/bson.rb +7 -14
  67. data/lib/moneta/utils.rb +3 -9
  68. data/lib/moneta/version.rb +1 -1
  69. data/lib/moneta/weak_each_key.rb +2 -4
  70. data/lib/rack/cache/moneta.rb +13 -11
  71. data/lib/rack/moneta_rest.rb +2 -2
  72. data/lib/rack/session/moneta.rb +3 -4
  73. data/moneta.gemspec +18 -4
  74. data/script/benchmarks +145 -46
  75. data/script/contributors +11 -6
  76. data/script/start-couchdb +27 -0
  77. data/script/start-hbase +3 -2
  78. data/script/start-services +3 -11
  79. data/spec/active_support/cache_moneta_store_spec.rb +30 -30
  80. data/spec/features/concurrent_create.rb +31 -10
  81. data/spec/features/concurrent_increment.rb +26 -19
  82. data/spec/features/create_expires.rb +15 -15
  83. data/spec/features/default_expires.rb +11 -12
  84. data/spec/features/expires.rb +215 -210
  85. data/spec/features/increment.rb +41 -41
  86. data/spec/features/store.rb +3 -3
  87. data/spec/helper.rb +23 -82
  88. data/spec/moneta/adapters/activerecord/standard_activerecord_spec.rb +1 -1
  89. data/spec/moneta/adapters/activerecord/standard_activerecord_with_expires_spec.rb +1 -1
  90. data/spec/moneta/adapters/activesupportcache/adapter_activesupportcache_spec.rb +4 -1
  91. data/spec/moneta/adapters/activesupportcache/adapter_activesupportcache_with_default_expires_spec.rb +4 -1
  92. data/spec/moneta/adapters/activesupportcache/standard_activesupportcache_spec.rb +14 -0
  93. data/spec/moneta/adapters/cassandra/standard_cassandra_spec.rb +1 -1
  94. data/spec/moneta/adapters/client/adapter_client_spec.rb +6 -6
  95. data/spec/moneta/adapters/client/client_helper.rb +24 -0
  96. data/spec/moneta/adapters/client/standard_client_tcp_spec.rb +8 -8
  97. data/spec/moneta/adapters/client/standard_client_unix_spec.rb +23 -7
  98. data/spec/moneta/adapters/couch/adapter_couch_spec.rb +199 -2
  99. data/spec/moneta/adapters/couch/standard_couch_spec.rb +9 -3
  100. data/spec/moneta/adapters/couch/standard_couch_with_expires_spec.rb +8 -2
  101. data/spec/moneta/adapters/daybreak/standard_daybreak_spec.rb +1 -1
  102. data/spec/moneta/adapters/daybreak/standard_daybreak_with_expires_spec.rb +1 -1
  103. data/spec/moneta/adapters/dbm/standard_dbm_spec.rb +1 -1
  104. data/spec/moneta/adapters/dbm/standard_dbm_with_expires_spec.rb +1 -1
  105. data/spec/moneta/adapters/faraday_helper.rb +9 -0
  106. data/spec/moneta/adapters/file/standard_file_spec.rb +2 -2
  107. data/spec/moneta/adapters/file/standard_file_with_expires_spec.rb +1 -1
  108. data/spec/moneta/adapters/gdbm/standard_gdbm_spec.rb +1 -1
  109. data/spec/moneta/adapters/gdbm/standard_gdbm_with_expires_spec.rb +1 -1
  110. data/spec/moneta/adapters/kyotocabinet/adapter_kyotocabinet_spec.rb +1 -1
  111. data/spec/moneta/adapters/kyotocabinet/standard_kyotocabinet_spec.rb +2 -2
  112. data/spec/moneta/adapters/kyotocabinet/standard_kyotocabinet_with_expires_spec.rb +2 -2
  113. data/spec/moneta/adapters/leveldb/standard_leveldb_spec.rb +1 -1
  114. data/spec/moneta/adapters/leveldb/standard_leveldb_with_expires_spec.rb +1 -1
  115. data/spec/moneta/adapters/lmdb/standard_lmdb_spec.rb +1 -1
  116. data/spec/moneta/adapters/lmdb/standard_lmdb_with_expires_spec.rb +1 -1
  117. data/spec/moneta/adapters/lruhash/adapter_lruhash_spec.rb +2 -2
  118. data/spec/moneta/adapters/lruhash/standard_lruhash_spec.rb +1 -1
  119. data/spec/moneta/adapters/lruhash/standard_lruhash_with_expires_spec.rb +1 -1
  120. data/spec/moneta/adapters/memcached/adapter_memcached_spec.rb +1 -1
  121. data/spec/moneta/adapters/memcached/dalli/adapter_memcached_dalli_spec.rb +1 -1
  122. data/spec/moneta/adapters/memcached/dalli/adapter_memcached_dalli_with_default_expires_spec.rb +1 -1
  123. data/spec/moneta/adapters/memcached/dalli/standard_memcached_dalli_spec.rb +1 -1
  124. data/spec/moneta/adapters/memcached/native/adapter_memcached_native_spec.rb +1 -1
  125. data/spec/moneta/adapters/memcached/native/adapter_memcached_native_with_default_expires_spec.rb +1 -1
  126. data/spec/moneta/adapters/memcached/native/standard_memcached_native_spec.rb +1 -1
  127. data/spec/moneta/adapters/memcached/standard_memcached_spec.rb +1 -1
  128. data/spec/moneta/adapters/{memcached/helper.rb → memcached_helper.rb} +0 -0
  129. data/spec/moneta/adapters/memory/standard_memory_spec.rb +1 -1
  130. data/spec/moneta/adapters/memory/standard_memory_with_compress_spec.rb +1 -1
  131. data/spec/moneta/adapters/memory/standard_memory_with_expires_spec.rb +1 -1
  132. data/spec/moneta/adapters/memory/standard_memory_with_json_key_serializer_spec.rb +1 -1
  133. data/spec/moneta/adapters/memory/standard_memory_with_json_serializer_spec.rb +1 -1
  134. data/spec/moneta/adapters/memory/standard_memory_with_json_value_serializer_spec.rb +2 -2
  135. data/spec/moneta/adapters/memory/standard_memory_with_prefix_spec.rb +39 -2
  136. data/spec/moneta/adapters/memory/standard_memory_with_snappy_compress_spec.rb +2 -2
  137. data/spec/moneta/adapters/mongo/adapter_mongo_spec.rb +18 -2
  138. data/spec/moneta/adapters/mongo/adapter_mongo_with_default_expires_spec.rb +5 -3
  139. data/spec/moneta/adapters/mongo/standard_mongo_spec.rb +2 -2
  140. data/spec/moneta/adapters/null/null_adapter_spec.rb +1 -1
  141. data/spec/moneta/adapters/pstore/standard_pstore_spec.rb +1 -1
  142. data/spec/moneta/adapters/pstore/standard_pstore_with_expires_spec.rb +1 -1
  143. data/spec/moneta/adapters/redis/standard_redis_spec.rb +1 -1
  144. data/spec/moneta/adapters/restclient/adapter_restclient_spec.rb +7 -5
  145. data/spec/moneta/adapters/restclient/helper.rb +12 -0
  146. data/spec/moneta/adapters/restclient/standard_restclient_spec.rb +9 -6
  147. data/spec/moneta/adapters/riak/standard_riak_with_expires_spec.rb +4 -0
  148. data/spec/moneta/adapters/sdbm/standard_sdbm_spec.rb +1 -1
  149. data/spec/moneta/adapters/sdbm/standard_sdbm_with_expires_spec.rb +1 -1
  150. data/spec/moneta/adapters/sequel/adapter_sequel_spec.rb +31 -79
  151. data/spec/moneta/adapters/sequel/helper.rb +75 -0
  152. data/spec/moneta/adapters/sequel/standard_sequel_spec.rb +5 -11
  153. data/spec/moneta/adapters/sequel/standard_sequel_with_expires_spec.rb +8 -9
  154. data/spec/moneta/adapters/sqlite/standard_sqlite_spec.rb +1 -1
  155. data/spec/moneta/adapters/sqlite/standard_sqlite_with_expires_spec.rb +1 -1
  156. data/spec/moneta/adapters/tdb/standard_tdb_spec.rb +1 -1
  157. data/spec/moneta/adapters/tdb/standard_tdb_with_expires_spec.rb +1 -1
  158. data/spec/moneta/adapters/tokyocabinet/standard_tokyocabinet_spec.rb +1 -1
  159. data/spec/moneta/adapters/tokyocabinet/standard_tokyocabinet_with_expires_spec.rb +1 -1
  160. data/spec/moneta/adapters/tokyotyrant/adapter_tokyotyrant_spec.rb +6 -2
  161. data/spec/moneta/adapters/tokyotyrant/helper.rb +12 -0
  162. data/spec/moneta/adapters/tokyotyrant/standard_tokyotyrant_spec.rb +5 -2
  163. data/spec/moneta/adapters/tokyotyrant/standard_tokyotyrant_with_expires_spec.rb +5 -1
  164. data/spec/moneta/adapters/yaml/standard_yaml_spec.rb +1 -1
  165. data/spec/moneta/adapters/yaml/standard_yaml_with_expires_spec.rb +1 -1
  166. data/spec/moneta/builder_spec.rb +22 -0
  167. data/spec/moneta/proxies/enumerable/enumerable_spec.rb +26 -0
  168. data/spec/moneta/proxies/expires/expires_file_spec.rb +1 -1
  169. data/spec/moneta/proxies/fallback/fallback_spec.rb +42 -0
  170. data/spec/moneta/proxies/pool/pool_spec.rb +319 -6
  171. data/spec/moneta/proxies/shared/shared_tcp_spec.rb +14 -4
  172. data/spec/moneta/proxies/shared/shared_unix_spec.rb +14 -4
  173. data/spec/moneta/proxies/transformer/transformer_bencode_spec.rb +1 -1
  174. data/spec/moneta/proxies/transformer/transformer_bert_spec.rb +3 -3
  175. data/spec/moneta/proxies/transformer/transformer_bson_spec.rb +2 -2
  176. data/spec/moneta/proxies/transformer/transformer_json_spec.rb +1 -1
  177. data/spec/moneta/proxies/transformer/transformer_key_marshal_spec.rb +1 -1
  178. data/spec/moneta/proxies/transformer/transformer_key_yaml_spec.rb +1 -1
  179. data/spec/moneta/proxies/transformer/transformer_marshal_base64_spec.rb +1 -1
  180. data/spec/moneta/proxies/transformer/transformer_marshal_escape_spec.rb +8 -4
  181. data/spec/moneta/proxies/transformer/transformer_marshal_hex_spec.rb +1 -1
  182. data/spec/moneta/proxies/transformer/transformer_marshal_hmac_spec.rb +1 -1
  183. data/spec/moneta/proxies/transformer/transformer_marshal_prefix_base64_spec.rb +33 -0
  184. data/spec/moneta/proxies/transformer/transformer_marshal_prefix_spec.rb +1 -1
  185. data/spec/moneta/proxies/transformer/transformer_marshal_qp_spec.rb +1 -1
  186. data/spec/moneta/proxies/transformer/transformer_marshal_spec.rb +1 -1
  187. data/spec/moneta/proxies/transformer/transformer_marshal_urlsafe_base64_spec.rb +1 -1
  188. data/spec/moneta/proxies/transformer/transformer_marshal_uuencode_spec.rb +1 -1
  189. data/spec/moneta/proxies/transformer/transformer_msgpack_spec.rb +1 -1
  190. data/spec/moneta/proxies/transformer/transformer_ox_spec.rb +1 -1
  191. data/spec/moneta/proxies/transformer/transformer_php_spec.rb +1 -1
  192. data/spec/moneta/proxies/transformer/transformer_tnet_spec.rb +1 -1
  193. data/spec/moneta/proxies/transformer/transformer_yaml_spec.rb +2 -2
  194. data/spec/moneta/proxies/weak_each_key/weak_each_key_spec.rb +0 -2
  195. data/spec/restserver.rb +55 -0
  196. data/spec/support/mongo_helper.rb +12 -0
  197. metadata +140 -32
  198. data/lib/moneta/adapters/mongo/base.rb +0 -103
  199. data/lib/moneta/adapters/mongo/moped.rb +0 -164
  200. data/lib/moneta/adapters/mongo/official.rb +0 -157
  201. data/script/install-kyotocabinet +0 -17
  202. data/spec/moneta/adapters/mongo/adapter_mongo_moped_spec.rb +0 -25
  203. data/spec/moneta/adapters/mongo/adapter_mongo_moped_with_default_expires_spec.rb +0 -12
  204. data/spec/moneta/adapters/mongo/adapter_mongo_official_spec.rb +0 -25
  205. data/spec/moneta/adapters/mongo/adapter_mongo_official_with_default_expires_spec.rb +0 -12
  206. data/spec/moneta/adapters/mongo/standard_mongo_moped_spec.rb +0 -7
  207. data/spec/moneta/adapters/mongo/standard_mongo_official_spec.rb +0 -7
  208. data/spec/quality_spec.rb +0 -51
@@ -0,0 +1,75 @@
1
+ RSpec.shared_context :sequel do
2
+ def mysql_uri database=nil
3
+ database ||= mysql_database1
4
+ if defined?(JRUBY_VERSION)
5
+ "jdbc:mysql://localhost/#{database}?user=#{mysql_username}&useSSL=false"
6
+ else
7
+ "mysql2://#{mysql_username}:@localhost/#{database}"
8
+ end
9
+ end
10
+
11
+ def sqlite_uri file_name
12
+ "#{defined?(JRUBY_VERSION) && 'jdbc:'}sqlite://" + File.join(tempdir, file_name)
13
+ end
14
+
15
+ def postgres_options database=nil
16
+ database ||= postgres_database1
17
+ if defined?(JRUBY_VERSION)
18
+ {db: "jdbc:postgresql://localhost/#{database}?user=#{postgres_username}"}
19
+ else
20
+ {
21
+ db: "postgres://localhost/#{database}",
22
+ user: postgres_username
23
+ }
24
+ end
25
+ end
26
+
27
+ def postgres_hstore_options database=nil
28
+ postgres_options(database).merge \
29
+ table: 'hstore_table1',
30
+ hstore: 'row'
31
+ end
32
+
33
+ def h2_uri
34
+ "jdbc:h2:" + tempdir
35
+ end
36
+ end
37
+
38
+ RSpec.shared_examples :adapter_sequel do |specs, optimize: true|
39
+ shared_examples :each_key_server do
40
+ context "with each_key server" do
41
+ let(:opts) do
42
+ base_opts.merge(
43
+ servers: {each_key: {}},
44
+ each_key_server: :each_key
45
+ )
46
+ end
47
+
48
+ moneta_specs specs
49
+ end
50
+
51
+ context "without each_key server" do
52
+ let(:opts) { base_opts }
53
+ moneta_specs specs
54
+ end
55
+ end
56
+
57
+ if optimize
58
+ context 'with backend optimizations' do
59
+ let(:base_opts) { {table: "adapter_sequel"} }
60
+
61
+ include_examples :each_key_server
62
+ end
63
+ end
64
+
65
+ context 'without backend optimizations' do
66
+ let(:base_opts) do
67
+ {
68
+ table: "adapter_sequel",
69
+ optimize: false
70
+ }
71
+ end
72
+
73
+ include_examples :each_key_server
74
+ end
75
+ end
@@ -1,14 +1,8 @@
1
+ require_relative './helper.rb'
2
+
1
3
  describe 'standard_sequel', adapter: :Sequel do
2
- moneta_store :Sequel do
3
- {
4
- db: if defined?(JRUBY_VERSION)
5
- "jdbc:mysql://localhost/#{mysql_database1}?user=#{mysql_username}"
6
- else
7
- "mysql2://#{mysql_username}:@localhost/#{mysql_database1}"
8
- end,
9
- table: "simple_sequel"
10
- }
11
- end
4
+ include_context :sequel
12
5
 
13
- moneta_specs STANDARD_SPECS
6
+ moneta_store(:Sequel) { postgres_options.merge(table: "standard_sequel") }
7
+ moneta_specs STANDARD_SPECS.with_each_key
14
8
  end
@@ -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
- db: if defined?(JRUBY_VERSION)
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
@@ -3,5 +3,5 @@ describe 'standard_sqlite', unsupported: defined?(JRUBY_VERSION), adapter: :Sqli
3
3
  {file: File.join(tempdir, "standard_sqlite")}
4
4
  end
5
5
 
6
- moneta_specs STANDARD_SPECS.without_concurrent
6
+ moneta_specs STANDARD_SPECS.without_concurrent.with_each_key
7
7
  end
@@ -11,5 +11,5 @@ describe 'standard_sqlite_with_expires', unsupported: defined?(JRUBY_VERSION), a
11
11
  }
12
12
  end
13
13
 
14
- moneta_specs STANDARD_SPECS.with_expires.without_concurrent
14
+ moneta_specs STANDARD_SPECS.with_expires.without_concurrent.with_each_key
15
15
  end
@@ -3,5 +3,5 @@ describe 'standard_tdb', unsupported: defined?(JRUBY_VERSION), adapter: :TDB do
3
3
  {file: File.join(tempdir, "simple_tdb")}
4
4
  end
5
5
 
6
- moneta_specs STANDARD_SPECS.without_multiprocess
6
+ moneta_specs STANDARD_SPECS.without_multiprocess.with_each_key
7
7
  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
@@ -3,5 +3,5 @@ describe 'standard_tokyocabinet', unsupported: defined?(JRUBY_VERSION), adapter:
3
3
  {file: File.join(tempdir, "simple_tokyocabinet")}
4
4
  end
5
5
 
6
- moneta_specs STANDARD_SPECS.without_multiprocess
6
+ moneta_specs STANDARD_SPECS.without_multiprocess.with_each_key
7
7
  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
- 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
@@ -5,5 +5,5 @@ describe 'standard_yaml', adapter: :YAML do
5
5
 
6
6
  moneta_loader{ |value| value }
7
7
 
8
- moneta_specs STANDARD_SPECS.without_marshallable_value.without_concurrent
8
+ moneta_specs STANDARD_SPECS.without_marshallable_value.without_concurrent.with_each_key
9
9
  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
@@ -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
- 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