moneta 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +415 -0
  3. data/CHANGES +18 -0
  4. data/CONTRIBUTORS +2 -0
  5. data/Gemfile +144 -56
  6. data/README.md +21 -17
  7. data/lib/moneta/adapter.rb +52 -0
  8. data/lib/moneta/adapters/activerecord.rb +77 -68
  9. data/lib/moneta/adapters/activesupportcache.rb +22 -31
  10. data/lib/moneta/adapters/cassandra.rb +114 -116
  11. data/lib/moneta/adapters/client.rb +17 -18
  12. data/lib/moneta/adapters/couch.rb +31 -26
  13. data/lib/moneta/adapters/datamapper.rb +9 -5
  14. data/lib/moneta/adapters/daybreak.rb +15 -21
  15. data/lib/moneta/adapters/dbm.rb +6 -12
  16. data/lib/moneta/adapters/file.rb +21 -13
  17. data/lib/moneta/adapters/fog.rb +5 -6
  18. data/lib/moneta/adapters/gdbm.rb +6 -12
  19. data/lib/moneta/adapters/hbase.rb +10 -12
  20. data/lib/moneta/adapters/kyotocabinet.rb +22 -27
  21. data/lib/moneta/adapters/leveldb.rb +14 -20
  22. data/lib/moneta/adapters/lmdb.rb +19 -22
  23. data/lib/moneta/adapters/localmemcache.rb +7 -13
  24. data/lib/moneta/adapters/lruhash.rb +20 -20
  25. data/lib/moneta/adapters/memcached/dalli.rb +25 -33
  26. data/lib/moneta/adapters/memcached/native.rb +14 -20
  27. data/lib/moneta/adapters/memory.rb +5 -7
  28. data/lib/moneta/adapters/mongo.rb +59 -50
  29. data/lib/moneta/adapters/pstore.rb +21 -27
  30. data/lib/moneta/adapters/redis.rb +42 -37
  31. data/lib/moneta/adapters/restclient.rb +17 -25
  32. data/lib/moneta/adapters/riak.rb +8 -9
  33. data/lib/moneta/adapters/sdbm.rb +6 -12
  34. data/lib/moneta/adapters/sequel/mysql.rb +8 -8
  35. data/lib/moneta/adapters/sequel/postgres.rb +17 -17
  36. data/lib/moneta/adapters/sequel/postgres_hstore.rb +47 -47
  37. data/lib/moneta/adapters/sequel/sqlite.rb +9 -9
  38. data/lib/moneta/adapters/sequel.rb +56 -65
  39. data/lib/moneta/adapters/sqlite.rb +37 -35
  40. data/lib/moneta/adapters/tdb.rb +8 -14
  41. data/lib/moneta/adapters/tokyocabinet.rb +19 -17
  42. data/lib/moneta/adapters/tokyotyrant.rb +29 -30
  43. data/lib/moneta/adapters/yaml.rb +1 -5
  44. data/lib/moneta/config.rb +101 -0
  45. data/lib/moneta/expires.rb +0 -1
  46. data/lib/moneta/expires_support.rb +3 -4
  47. data/lib/moneta/pool.rb +27 -7
  48. data/lib/moneta/proxy.rb +29 -0
  49. data/lib/moneta/server.rb +21 -14
  50. data/lib/moneta/version.rb +1 -1
  51. data/lib/moneta/wrapper.rb +5 -0
  52. data/lib/moneta.rb +2 -0
  53. data/moneta.gemspec +1 -0
  54. data/spec/active_support/cache_moneta_store_spec.rb +13 -13
  55. data/spec/helper.rb +14 -3
  56. data/spec/moneta/adapters/activerecord/adapter_activerecord_existing_connection_spec.rb +3 -1
  57. data/spec/moneta/adapters/activerecord/adapter_activerecord_spec.rb +15 -7
  58. data/spec/moneta/adapters/activerecord/standard_activerecord_spec.rb +5 -2
  59. data/spec/moneta/adapters/activerecord/standard_activerecord_with_expires_spec.rb +5 -2
  60. data/spec/moneta/adapters/activesupportcache/adapter_activesupportcache_spec.rb +3 -3
  61. data/spec/moneta/adapters/activesupportcache/adapter_activesupportcache_with_default_expires_spec.rb +2 -2
  62. data/spec/moneta/adapters/client/client_helper.rb +4 -3
  63. data/spec/moneta/adapters/datamapper/adapter_datamapper_spec.rb +25 -8
  64. data/spec/moneta/adapters/datamapper/standard_datamapper_spec.rb +2 -2
  65. data/spec/moneta/adapters/datamapper/standard_datamapper_with_expires_spec.rb +2 -2
  66. data/spec/moneta/adapters/datamapper/standard_datamapper_with_repository_spec.rb +2 -2
  67. data/spec/moneta/adapters/faraday_helper.rb +3 -2
  68. data/spec/moneta/adapters/lruhash/adapter_lruhash_spec.rb +10 -6
  69. data/spec/moneta/adapters/memcached/dalli/adapter_memcached_dalli_spec.rb +13 -3
  70. data/spec/moneta/adapters/memcached/native/adapter_memcached_native_spec.rb +13 -3
  71. data/spec/moneta/adapters/mongo/adapter_mongo_spec.rb +17 -3
  72. data/spec/moneta/adapters/mongo/adapter_mongo_with_default_expires_spec.rb +4 -4
  73. data/spec/moneta/adapters/mongo/standard_mongo_spec.rb +1 -1
  74. data/spec/moneta/adapters/redis/adapter_redis_spec.rb +13 -3
  75. data/spec/moneta/adapters/redis/standard_redis_spec.rb +8 -1
  76. data/spec/moneta/adapters/sequel/adapter_sequel_spec.rb +4 -4
  77. data/spec/moneta/adapters/sequel/helper.rb +10 -5
  78. data/spec/moneta/adapters/sequel/standard_sequel_spec.rb +1 -1
  79. data/spec/moneta/adapters/sequel/standard_sequel_with_expires_spec.rb +1 -1
  80. data/spec/moneta/adapters/sqlite/adapter_sqlite_spec.rb +1 -1
  81. data/spec/moneta/adapters/sqlite/standard_sqlite_spec.rb +1 -1
  82. data/spec/moneta/adapters/sqlite/standard_sqlite_with_expires_spec.rb +1 -1
  83. data/spec/moneta/config_spec.rb +219 -0
  84. data/spec/moneta/proxies/pool/pool_spec.rb +31 -3
  85. data/spec/moneta/proxies/transformer/transformer_bson_spec.rb +3 -1
  86. data/spec/moneta/proxies/transformer/transformer_marshal_escape_spec.rb +2 -0
  87. data/spec/rack/session_moneta_spec.rb +44 -25
  88. data/spec/restserver.rb +3 -14
  89. metadata +25 -17
  90. data/.travis.yml +0 -146
  91. data/spec/moneta/adapters/memcached/dalli/adapter_memcached_dalli_with_default_expires_spec.rb +0 -15
  92. data/spec/moneta/adapters/memcached/native/adapter_memcached_native_with_default_expires_spec.rb +0 -15
  93. data/spec/moneta/adapters/redis/adapter_redis_with_default_expires_spec.rb +0 -10
  94. data/spec/moneta/proxies/proxy/proxy_redis_spec.rb +0 -13
  95. data/spec/support/mongo_helper.rb +0 -12
@@ -7,9 +7,19 @@ describe 'adapter_memcached_native', isolate: true, unstable: defined?(JRUBY_VER
7
7
 
8
8
  include_context :start_memcached, 11214
9
9
 
10
- moneta_build do
11
- Moneta::Adapters::MemcachedNative.new(server: "127.0.0.1:11214")
10
+ describe 'without default expires' do
11
+ moneta_build do
12
+ Moneta::Adapters::MemcachedNative.new(server: "127.0.0.1:11214")
13
+ end
14
+
15
+ moneta_specs ADAPTER_SPECS.with_native_expires
12
16
  end
13
17
 
14
- moneta_specs ADAPTER_SPECS.with_native_expires
18
+ describe 'with default expires' do
19
+ moneta_build do
20
+ Moneta::Adapters::MemcachedNative.new(server: '127.0.0.1:11214', expires: min_ttl)
21
+ end
22
+
23
+ moneta_specs NATIVE_EXPIRY_SPECS.with_default_expires
24
+ end
15
25
  end
@@ -2,11 +2,13 @@ describe 'adapter_mongo', adapter: :Mongo do
2
2
  let(:t_res) { 0.125 }
3
3
  let(:min_ttl) { t_res }
4
4
 
5
+ let(:database) { File.basename(__FILE__, '.rb') }
6
+
5
7
  moneta_build do
6
- Moneta::Adapters::Mongo.new(mongo_config(
7
- db: File.basename(__FILE__, '.rb'),
8
+ Moneta::Adapters::Mongo.new(
9
+ database: database,
8
10
  collection: 'adapter_mongo'
9
- ))
11
+ )
10
12
  end
11
13
 
12
14
  moneta_specs ADAPTER_SPECS.with_each_key.with_native_expires.simplevalues_only
@@ -24,4 +26,16 @@ describe 'adapter_mongo', adapter: :Mongo do
24
26
  i.should be > 0 # Indicates that it took at least one sleep to expire
25
27
  query.count.should == 0
26
28
  end
29
+
30
+ it 'uses the database specified via the :database option' do
31
+ expect(store.config.database).to eq database
32
+ end
33
+
34
+ it 'uses the database specified via the :db option' do
35
+ store = Moneta::Adapters::Mongo.new(
36
+ db: database,
37
+ collection: 'adapter_mongo'
38
+ )
39
+ expect(store.config.database).to eq database
40
+ end
27
41
  end
@@ -1,13 +1,13 @@
1
1
  describe 'adapter_mongo_with_default_expires', isolate: true, adapter: :Mongo do
2
2
  let(:t_res) { 0.125 }
3
- let(:min_ttl) { t_res }
3
+ let(:min_ttl) { t_res * 4 }
4
4
 
5
5
  moneta_build do
6
- Moneta::Adapters::Mongo.new(mongo_config(
7
- db: File.basename(__FILE__, '.rb'),
6
+ Moneta::Adapters::Mongo.new(
7
+ database: File.basename(__FILE__, '.rb'),
8
8
  collection: 'adapter_mongo_with_default_expires',
9
9
  expires: min_ttl
10
- ))
10
+ )
11
11
  end
12
12
 
13
13
  moneta_specs ADAPTER_SPECS.with_each_key.with_expires.with_default_expires.simplevalues_only
@@ -2,6 +2,6 @@ describe 'standard_mongo', adapter: :Mongo do
2
2
  let(:t_res) { 0.125 }
3
3
  let(:min_ttl) { t_res }
4
4
 
5
- moneta_store :Mongo, MongoHelper::mongo_config(db: File.basename(__FILE__, '.rb'), collection: 'standard_mongo')
5
+ moneta_store :Mongo, {database: File.basename(__FILE__, '.rb'), collection: 'standard_mongo'}
6
6
  moneta_specs STANDARD_SPECS.with_native_expires.with_each_key
7
7
  end
@@ -2,9 +2,19 @@ describe 'adapter_redis', adapter: :Redis do
2
2
  let(:t_res) { 0.125 }
3
3
  let(:min_ttl) { t_res }
4
4
 
5
- moneta_build do
6
- Moneta::Adapters::Redis.new(db: 6)
5
+ describe 'without default expiry' do
6
+ moneta_build do
7
+ Moneta::Adapters::Redis.new(host: redis_host, port: redis_port, db: 6)
8
+ end
9
+
10
+ moneta_specs ADAPTER_SPECS.with_each_key.with_native_expires
7
11
  end
8
12
 
9
- moneta_specs ADAPTER_SPECS.with_each_key.with_native_expires
13
+ describe 'with default expiry' do
14
+ moneta_build do
15
+ Moneta::Adapters::Redis.new(host: redis_host, port: redis_port, db: 6, expires: min_ttl)
16
+ end
17
+
18
+ moneta_specs NATIVE_EXPIRY_SPECS.with_default_expires
19
+ end
10
20
  end
@@ -2,6 +2,13 @@ describe 'standard_redis', adapter: :Redis do
2
2
  let(:t_res) { 0.125 }
3
3
  let(:min_ttl) { t_res }
4
4
 
5
- moneta_store :Redis, db: 4
5
+ moneta_store :Redis do
6
+ {
7
+ db: 4,
8
+ host: redis_host,
9
+ port: redis_port
10
+ }
11
+ end
12
+
6
13
  moneta_specs STANDARD_SPECS.with_native_expires.with_each_key
7
14
  end
@@ -5,7 +5,7 @@ describe ':Sequel adapter', adapter: :Sequel do
5
5
 
6
6
  specs = ADAPTER_SPECS.with_each_key.with_values(:nil)
7
7
 
8
- context 'with MySQL backend' do
8
+ context 'with MySQL backend', mysql: true do
9
9
  moneta_build do
10
10
  Moneta::Adapters::Sequel.new(opts.merge(db: mysql_uri))
11
11
  end
@@ -13,7 +13,7 @@ describe ':Sequel adapter', adapter: :Sequel do
13
13
  include_examples :adapter_sequel, specs
14
14
  end
15
15
 
16
- context "with SQLite backend" do
16
+ context "with SQLite backend", sqlite: true do
17
17
  moneta_build do
18
18
  Moneta::Adapters::Sequel.new(opts.merge(db: sqlite_uri('adapter_sequel.db')))
19
19
  end
@@ -21,7 +21,7 @@ describe ':Sequel adapter', adapter: :Sequel do
21
21
  include_examples :adapter_sequel, specs.without_concurrent
22
22
  end
23
23
 
24
- context "with Postgres backend" do
24
+ context "with Postgres backend", postgres: true do
25
25
  moneta_build do
26
26
  Moneta::Adapters::Sequel.new(opts.merge(postgres_options))
27
27
  end
@@ -37,7 +37,7 @@ describe ':Sequel adapter', adapter: :Sequel do
37
37
  include_examples :adapter_sequel, specs, optimize: false
38
38
  end
39
39
 
40
- context "with Postgres HStore backend" do
40
+ context "with Postgres HStore backend", postgres: true do
41
41
  moneta_build do
42
42
  Moneta::Adapters::Sequel.new(postgres_hstore_options)
43
43
  end
@@ -2,9 +2,11 @@ RSpec.shared_context :sequel do
2
2
  def mysql_uri database=nil
3
3
  database ||= mysql_database1
4
4
  if defined?(JRUBY_VERSION)
5
- "jdbc:mysql://localhost/#{database}?user=#{mysql_username}&useSSL=false"
5
+ uri = "jdbc:mysql://#{mysql_host}:#{mysql_port}/#{database}?user=#{mysql_username}&useSSL=false"
6
+ uri += "&password=#{mysql_password}" if mysql_password
7
+ uri
6
8
  else
7
- "mysql2://#{mysql_username}:@localhost/#{database}"
9
+ "mysql2://#{mysql_username}:#{mysql_password}@#{mysql_host}:#{mysql_port}/#{database}"
8
10
  end
9
11
  end
10
12
 
@@ -15,11 +17,14 @@ RSpec.shared_context :sequel do
15
17
  def postgres_options database=nil
16
18
  database ||= postgres_database1
17
19
  if defined?(JRUBY_VERSION)
18
- {db: "jdbc:postgresql://localhost/#{database}?user=#{postgres_username}"}
20
+ uri = "jdbc:postgresql://localhost/#{database}?user=#{postgres_username}"
21
+ uri += "&password=#{postgres_password}" if postgres_password
22
+ {db: uri}
19
23
  else
20
24
  {
21
25
  db: "postgres://localhost/#{database}",
22
- user: postgres_username
26
+ user: postgres_username,
27
+ password: postgres_password
23
28
  }
24
29
  end
25
30
  end
@@ -33,7 +38,7 @@ RSpec.shared_context :sequel do
33
38
  def h2_uri
34
39
  "jdbc:h2:" + tempdir
35
40
  end
36
- end
41
+ end
37
42
 
38
43
  RSpec.shared_examples :adapter_sequel do |specs, optimize: true|
39
44
  shared_examples :each_key_server do
@@ -1,6 +1,6 @@
1
1
  require_relative './helper.rb'
2
2
 
3
- describe 'standard_sequel', adapter: :Sequel do
3
+ describe 'standard_sequel', adapter: :Sequel, postgres: true do
4
4
  include_context :sequel
5
5
 
6
6
  moneta_store(:Sequel) { postgres_options.merge(table: "standard_sequel") }
@@ -1,6 +1,6 @@
1
1
  require_relative './helper.rb'
2
2
 
3
- describe 'standard_sequel_with_expires', adapter: :Sequel do
3
+ describe 'standard_sequel_with_expires', adapter: :Sequel, postgres: true do
4
4
  include_context :sequel
5
5
 
6
6
  let(:t_res) { 0.125 }
@@ -1,4 +1,4 @@
1
- describe 'adapter_sqlite', unsupported: defined?(JRUBY_VERSION), adapter: :Sqlite do
1
+ describe 'adapter_sqlite', unsupported: defined?(JRUBY_VERSION), adapter: :Sqlite, sqlite: true do
2
2
  moneta_build do
3
3
  Moneta::Adapters::Sqlite.new(file: File.join(tempdir, "adapter_sqlite"))
4
4
  end
@@ -1,4 +1,4 @@
1
- describe 'standard_sqlite', unsupported: defined?(JRUBY_VERSION), adapter: :Sqlite do
1
+ describe 'standard_sqlite', unsupported: defined?(JRUBY_VERSION), adapter: :Sqlite, sqlite: true do
2
2
  moneta_store :Sqlite do
3
3
  {file: File.join(tempdir, "standard_sqlite")}
4
4
  end
@@ -1,4 +1,4 @@
1
- describe 'standard_sqlite_with_expires', unsupported: defined?(JRUBY_VERSION), adapter: :Sqlite do
1
+ describe 'standard_sqlite_with_expires', unsupported: defined?(JRUBY_VERSION), adapter: :Sqlite, sqlite: true do
2
2
  let(:t_res) { 0.125 }
3
3
  let(:min_ttl) { 1 }
4
4
  use_timecop
@@ -0,0 +1,219 @@
1
+ describe Moneta::Config do
2
+ describe 'without any configuration' do
3
+ it 'does not set the config attribute' do
4
+ klass = Class.new do
5
+ include ::Moneta::Config
6
+
7
+ def initialize(**options)
8
+ configure(**options)
9
+ end
10
+ end
11
+
12
+ instance = klass.new(k: 'v')
13
+ expect(instance.config).to be nil
14
+ end
15
+ end
16
+
17
+ describe 'basic functionality' do
18
+ subject :klass do
19
+ Class.new do
20
+ include ::Moneta::Config
21
+
22
+ config :a
23
+ config :b
24
+
25
+ def initialize(**options)
26
+ configure(**options)
27
+ end
28
+ end
29
+ end
30
+
31
+ it 'sets all config values to nil by default' do
32
+ instance = klass.new
33
+ expect(instance.config.a).to eq nil
34
+ expect(instance.config.b).to be nil
35
+ end
36
+
37
+ it 'sets config values with values provided to #configure' do
38
+ instance = klass.new(a: 1)
39
+ expect(instance.config.a).to eq 1
40
+ expect(instance.config.b).to be nil
41
+ end
42
+
43
+ it 'freezes the config' do
44
+ instance = klass.new
45
+ expect(instance.config.frozen?).to be true
46
+ end
47
+ end
48
+
49
+ describe 'with required arguments' do
50
+ subject :klass do
51
+ Class.new do
52
+ include ::Moneta::Config
53
+
54
+ config :a, required: true
55
+ config :b, default: 'x', required: true
56
+
57
+ def initialize(**options)
58
+ configure(**options)
59
+ end
60
+ end
61
+ end
62
+
63
+ it 'raises an ArgumentError if #configure is called without one of the required arguments' do
64
+ expect { klass.new(a: 1) }.to raise_error ArgumentError, 'b is required'
65
+ expect { klass.new(b: 1) }.to raise_error ArgumentError, 'a is required'
66
+ end
67
+ end
68
+
69
+ describe 'with defaults' do
70
+ subject :klass do
71
+ Class.new do
72
+ include ::Moneta::Config
73
+
74
+ config :a, default: 't'
75
+ config :b, default: 's'
76
+
77
+ def initialize(**options)
78
+ configure(**options)
79
+ end
80
+ end
81
+ end
82
+
83
+ it 'uses the defaults if no argument is provided' do
84
+ instance = klass.new(a: 1)
85
+
86
+ expect(instance.config.a).to eq 1
87
+ expect(instance.config.b).to eq 's'
88
+ end
89
+
90
+ it 'allows falsy values to override truthy defaults' do
91
+ instance = klass.new(a: nil, b: false)
92
+
93
+ expect(instance.config.a).to be nil
94
+ expect(instance.config.b).to be false
95
+ end
96
+ end
97
+
98
+ describe 'with coercion' do
99
+ describe 'using a symbol' do
100
+ subject :klass do
101
+ Class.new do
102
+ include ::Moneta::Config
103
+
104
+ config :a, coerce: :to_s
105
+
106
+ def initialize(**options)
107
+ configure(**options)
108
+ end
109
+ end
110
+ end
111
+
112
+ it "uses the symbol's to_proc property" do
113
+ instance = klass.new(a: :x)
114
+ expect(instance.config.a).to eq 'x'
115
+ end
116
+ end
117
+
118
+ describe 'using a lambda' do
119
+ subject :klass do
120
+ Class.new do
121
+ include ::Moneta::Config
122
+
123
+ config :a, coerce: lambda { |a| a.to_sym }
124
+
125
+ def initialize(**options)
126
+ configure(**options)
127
+ end
128
+ end
129
+ end
130
+
131
+ it "calls the lambda" do
132
+ instance = klass.new(a: 'x')
133
+ expect(instance.config.a).to eq :x
134
+ end
135
+ end
136
+ end
137
+
138
+ describe 'with a block' do
139
+ subject :klass do
140
+ Class.new do
141
+ include ::Moneta::Config
142
+
143
+ config :a do |a:, b:|
144
+ { a: a, b: b, test: @test }
145
+ end
146
+
147
+ config :b, default: 'b default'
148
+
149
+ def initialize(test: nil, **options)
150
+ @test = test
151
+ configure(**options)
152
+ end
153
+ end
154
+ end
155
+
156
+ it 'calls the block after all arguments and defaults have been processed' do
157
+ instance1 = klass.new(a: 'a value')
158
+ expect(instance1.config.a).to include(a: 'a value', b: 'b default')
159
+
160
+ instance2 = klass.new(b: 'b value')
161
+ expect(instance2.config.a).to include(a: nil, b: 'b value')
162
+ end
163
+
164
+ it 'calls the block using instance_exec' do
165
+ instance = klass.new(test: 'test value')
166
+ expect(instance.config.a).to include(test: 'test value')
167
+ end
168
+ end
169
+
170
+ describe 'with inheritance' do
171
+ subject :klass do
172
+ Class.new do
173
+ include ::Moneta::Config
174
+
175
+ config :a
176
+
177
+ def initialize(**options)
178
+ configure(**options)
179
+ end
180
+ end
181
+ end
182
+
183
+ it 'does not allow subclasses to override superclass config' do
184
+ expect do
185
+ Class.new(klass) do
186
+ config :a
187
+ end
188
+ end.to raise_error ArgumentError, 'a is already a config option'
189
+ end
190
+
191
+ it 'does not affect the superclass when additional config is added to the subclass' do
192
+ klass2 = Class.new(klass) do
193
+ config :b
194
+ end
195
+
196
+ instance1 = klass.new(a: 1, b: 2)
197
+ expect(instance1.config.to_h).to eq(a: 1)
198
+
199
+ instance2 = klass2.new(a: 1, b: 2)
200
+ expect(instance2.config.to_h).to eq(a: 1, b: 2)
201
+ end
202
+
203
+ it 'is possible for two subclasses to have the same additional config' do
204
+ klass2 = Class.new(klass) do
205
+ config :b
206
+ end
207
+
208
+ klass3 = Class.new(klass) do
209
+ config :b
210
+ end
211
+
212
+ instance2 = klass2.new(a: 2, b: 1)
213
+ expect(instance2.config.to_h).to eq(a: 2, b: 1)
214
+
215
+ instance3 = klass3.new(a: 1, b: 2)
216
+ expect(instance3.config.to_h).to eq(a: 1, b: 2)
217
+ end
218
+ end
219
+ end
@@ -1,6 +1,6 @@
1
1
  require 'timeout'
2
2
 
3
- describe "pool", proxy: :Pool do
3
+ describe "pool", proxy: :Pool, broken: ::Gem::Version.new(RUBY_ENGINE_VERSION) < ::Gem::Version.new('2.4.0') do
4
4
  describe "Moneta::Pool" do
5
5
  # To test the pool, we create the store once and pass the same object around
6
6
  # whenever a new store is requested.
@@ -153,7 +153,7 @@ describe "pool", proxy: :Pool do
153
153
  describe "closing stores after TTL expiry" do
154
154
  let(:num) { max || min + 10 }
155
155
 
156
- it "closes available stores after ttl" do
156
+ it "closes unneeded stores after ttl" do
157
157
  stores.each do |store|
158
158
  allow(store).to receive(:close)
159
159
  end
@@ -169,6 +169,32 @@ describe "pool", proxy: :Pool do
169
169
  end
170
170
  end
171
171
 
172
+ shared_examples :ttl_with_nonzero_min do |ttl:, min:, max: nil|
173
+ describe "TTL check" do
174
+ let(:num) { max || min + 10 }
175
+
176
+ # This is testing that a very specific bug is fixed - see
177
+ # https://github.com/moneta-rb/moneta/issues/197. A better long-term
178
+ # solution would be to have more granular tests of the functions in the
179
+ # PoolManager
180
+ it "doesn't cause a busy-loop when there are available stores" do
181
+ # Check a store in and out - now the manager needs to decide whether to close
182
+ # stores after ttl seconds.
183
+ store = subject.check_out
184
+ subject.check_in(store)
185
+ sleep ttl
186
+ expect(subject.stats[:available]).to be > 0
187
+
188
+ # needs to be less than the TTL, but otherwise not important.
189
+ sleep_time = ttl / 2.0
190
+ sleep sleep_time
191
+
192
+ # during the sleep, the pool manager should have been idle.
193
+ expect(subject.stats[:idle_time]).to be >= sleep_time
194
+ end
195
+ end
196
+ end
197
+
172
198
  shared_examples :timeout do |timeout, max:|
173
199
  describe "raising an error after timeout" do
174
200
  let(:num) { max }
@@ -233,6 +259,7 @@ describe "pool", proxy: :Pool do
233
259
  include_examples :min, 10
234
260
  include_examples :max, 20, timeout: 4
235
261
  include_examples :ttl, 1, min: 10, max: 20
262
+ include_examples :ttl_with_nonzero_min, ttl: 1, min: 10, max: 20
236
263
  include_examples :timeout, 4, max: 20
237
264
  end
238
265
 
@@ -243,6 +270,7 @@ describe "pool", proxy: :Pool do
243
270
  include_examples :min, 10
244
271
  include_examples :max, 10, timeout: 4
245
272
  include_examples :ttl, 2, min: 10, max: 10
273
+ include_examples :ttl_with_nonzero_min, ttl: 2, min: 10, max: 10
246
274
  include_examples :timeout, 4, max: 10
247
275
  end
248
276
 
@@ -292,7 +320,7 @@ describe "pool", proxy: :Pool do
292
320
  end
293
321
 
294
322
  it "raises a ShutdownError if the pool is stopped while waiting for a store" do
295
- # Exaust the pool
323
+ # Exhaust the pool
296
324
  store = stores.first
297
325
  allow(store).to receive(:close).once
298
326
  expect(subject.check_out).to eq store
@@ -1,4 +1,6 @@
1
- describe 'transformer_bson', proxy: :Transformer do
1
+ # Currently broken in JRuby 9.3 - see https://github.com/jruby/jruby/issues/6941
2
+
3
+ describe 'transformer_bson', proxy: :Transformer, broken: defined?(JRUBY_VERSION) && ::Gem::Version.new(JRUBY_VERSION) >= ::Gem::Version.new('9.3.0.0') do
2
4
  moneta_build do
3
5
  Moneta.build do
4
6
  use :Transformer, key: :bson, value: :bson
@@ -1,3 +1,5 @@
1
+ require 'uri'
2
+
1
3
  describe 'transformer_marshal_escape', proxy: :Transformer do
2
4
  moneta_build do
3
5
  Moneta.build do
@@ -4,30 +4,50 @@ require 'rack/mock'
4
4
  require 'thread'
5
5
 
6
6
  describe Rack::Session::Moneta do
7
- session_key = Rack::Session::Moneta::DEFAULT_OPTIONS[:key]
8
- session_match = /#{session_key}=([0-9a-fA-F]+);/
9
- incrementor = lambda do |env|
10
- env["rack.session"]["counter"] ||= 0
11
- env["rack.session"]["counter"] += 1
12
- Rack::Response.new(env["rack.session"].inspect).to_a
7
+ use_timecop
8
+
9
+ let(:session_key) { Rack::Session::Moneta::DEFAULT_OPTIONS[:key] }
10
+ let(:session_match) { /#{session_key}=([0-9a-fA-F]+);/ }
11
+
12
+ let :incrementor_proc do
13
+ lambda do |env|
14
+ env["rack.session"]["counter"] ||= 0
15
+ env["rack.session"]["counter"] += 1
16
+ Rack::Response.new(env["rack.session"].inspect).to_a
17
+ end
18
+ end
19
+
20
+ let :drop_session do
21
+ Rack::Lint.new(proc do |env|
22
+ env['rack.session.options'][:drop] = true
23
+ incrementor_proc.call(env)
24
+ end)
25
+ end
26
+
27
+ let :renew_session do
28
+ Rack::Lint.new(proc do |env|
29
+ env['rack.session.options'][:renew] = true
30
+ incrementor_proc.call(env)
31
+ end)
32
+ end
33
+
34
+ let :defer_session do
35
+ Rack::Lint.new(proc do |env|
36
+ env['rack.session.options'][:defer] = true
37
+ incrementor_proc.call(env)
38
+ end)
39
+ end
40
+
41
+ let :skip_session do
42
+ Rack::Lint.new(proc do |env|
43
+ env['rack.session.options'][:skip] = true
44
+ incrementor_proc.call(env)
45
+ end)
46
+ end
47
+
48
+ let :incrementor do
49
+ Rack::Lint.new(incrementor_proc)
13
50
  end
14
- drop_session = Rack::Lint.new(proc do |env|
15
- env['rack.session.options'][:drop] = true
16
- incrementor.call(env)
17
- end)
18
- renew_session = Rack::Lint.new(proc do |env|
19
- env['rack.session.options'][:renew] = true
20
- incrementor.call(env)
21
- end)
22
- defer_session = Rack::Lint.new(proc do |env|
23
- env['rack.session.options'][:defer] = true
24
- incrementor.call(env)
25
- end)
26
- skip_session = Rack::Lint.new(proc do |env|
27
- env['rack.session.options'][:skip] = true
28
- incrementor.call(env)
29
- end)
30
- incrementor = Rack::Lint.new(incrementor)
31
51
 
32
52
  it 'supports different constructors' do
33
53
  Rack::Session::Moneta.new(incrementor, :store => :Memory)
@@ -96,8 +116,7 @@ describe Rack::Session::Moneta do
96
116
  res = Rack::MockRequest.new(pool).get('/', "HTTP_COOKIE" => cookie)
97
117
  res["Set-Cookie"].should == cookie
98
118
  res.body.should include '"counter"=>2'
99
- puts 'Sleeping to expire session' if $DEBUG
100
- sleep 4
119
+ advance 4
101
120
  res = Rack::MockRequest.new(pool).get('/', "HTTP_COOKIE" => cookie)
102
121
  res["Set-Cookie"].should_not == cookie
103
122
  res.body.should include '"counter"=>1'
data/spec/restserver.rb CHANGED
@@ -1,18 +1,7 @@
1
1
  require 'faraday'
2
2
  require 'rack'
3
3
  require 'rack/moneta_rest'
4
-
5
- MONETA_RESTSERVER_LOGGER =
6
- if defined?(JRUBY_VERSION)
7
- require 'rjack-logback'
8
- RJack::Logback.config_console( :level => :off )
9
- require 'fishwife'
10
- nil
11
- else
12
- require 'webrick'
13
- # Keep webrick quiet
14
- WEBrick::Log.new($stderr, WEBrick::BasicLog::ERROR)
15
- end
4
+ require 'webrick'
16
5
 
17
6
  class MonetaRestServerShutdown < StandardError; end
18
7
 
@@ -25,10 +14,10 @@ def start_restserver(port)
25
14
  end
26
15
  end,
27
16
  :environment => 'none',
28
- :server => defined?(JRUBY_VERSION) ? :Fishwife : :webrick,
17
+ :server => :webrick,
29
18
  :Port => port,
30
19
  :AccessLog => [],
31
- :Logger => MONETA_RESTSERVER_LOGGER
20
+ :Logger => WEBrick::Log.new($stderr, WEBrick::BasicLog::ERROR)
32
21
  )
33
22
 
34
23
  Thread.start { server.start }