mongo-lock 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/Gemfile.lock +1 -1
- data/README.md +52 -10
- data/lib/mongo-lock.rb +43 -28
- data/lib/mongo-lock/class_convenience_methods.rb +5 -5
- data/lib/mongo-lock/configuration.rb +29 -0
- data/lib/mongo-lock/{queries.rb → mongo_queries.rb} +5 -5
- data/lib/mongo-lock/railtie.rb +20 -0
- data/lib/mongo-lock/railties/mongo.rake +16 -0
- data/lib/mongo-lock/version.rb +1 -1
- data/spec/acquire_spec.rb +46 -13
- data/spec/acquired_spec.rb +4 -3
- data/spec/available_spec.rb +7 -7
- data/spec/clear_expired_spec.rb +84 -6
- data/spec/configuration_spec.rb +6 -6
- data/spec/ensure_indexes_spec.rb +11 -2
- data/spec/extend_by_spec.rb +4 -2
- data/spec/initialise_spec.rb +1 -2
- data/spec/rake_spec.rb +36 -0
- data/spec/release_all_spec.rb +48 -11
- data/spec/release_spec.rb +9 -9
- data/spec/spec_helper.rb +2 -1
- data/spec/support/mongo_helper.rb +6 -2
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
N2Y4MjU2MmNlNDI5ZjJhMWRlODA0NThmZTk3OWMyMGFlMGU4NWNjNg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MGZlYjEyMjY5ZTc3ZTkwNGI1OTE2MDBlNDVjYzZiMDQ2NWMxMTg2Ng==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NjA1ZjgzNWJiMGVjYmNiMjkzOGY3YWUxMzNhMTAyZDFlY2YxNzU2MzgzOTk3
|
10
|
+
MjBkY2E5NGM1ZmQwMzBiZWZlZjdkNzY0ODU5MjY0OTVkZWViMmJkOTY5OWY1
|
11
|
+
NDJhZThkZWNjYWEyOTdhZjI3YmE1YzhlZTAwMTBlYWM0OTQxNWU=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YjhiMjJmZmI3ZWJjMzFhZGZjMTAxMWRhN2RlZTY5ZGNmOTcwNzk0MTRiNjA0
|
14
|
+
OTUzOWJkZTM4MWU5OTdkNTYyOWFkZDYyNmZjZTczNWIwNThlNmM4Yzk4ZDVk
|
15
|
+
YjZjZTNlNTcyOTg1M2ZlOTgyNDM2OTYzMGM0MjdmNzU2MTJmNDI=
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -32,7 +32,7 @@ Or install it yourself as:
|
|
32
32
|
$ gem install mongo-lock
|
33
33
|
```
|
34
34
|
|
35
|
-
Build
|
35
|
+
Build your indexes on any collection that is going to hold locks:
|
36
36
|
|
37
37
|
```ruby
|
38
38
|
Mongo::Lock.ensure_indexes # Will use the collection provided to #configure
|
@@ -57,22 +57,16 @@ A lock has an owner. Mongo::Lock defaults to using an owner id of HOSTNAME:PID:T
|
|
57
57
|
Mongo::Lock makes no effort to help configure the MongoDB connection - that's
|
58
58
|
what the Mongo Ruby Driver is for.
|
59
59
|
|
60
|
-
Configuring Mongo::Lock with the Mongo Ruby Driver would look like this:
|
61
|
-
|
62
60
|
```ruby
|
63
61
|
Mongo::Lock.configure collection: Mongo::Connection.new("localhost").db("somedb").collection("locks")
|
64
62
|
```
|
65
63
|
|
66
64
|
Or using Mongoid:
|
67
65
|
|
68
|
-
```ruby
|
69
|
-
Mongo::Lock.configure collection: Mongoid.database.collection("locks")
|
70
|
-
```
|
71
|
-
|
72
66
|
You can add multiple collections with a hash that can be referenced later using symbols:
|
73
67
|
|
74
68
|
```ruby
|
75
|
-
Mongo::Lock.configure collections: { default:
|
69
|
+
Mongo::Lock.configure collections: { default: database.collection("locks"), other: database.collection("other_locks") }
|
76
70
|
Mongo::Lock.acquire('my_lock') # Locks in the default collection
|
77
71
|
Mongo::Lock.acquire('my_lock', collection: :other) # Locks in the other_locks collection
|
78
72
|
```
|
@@ -82,8 +76,8 @@ You can also configure using a block:
|
|
82
76
|
```ruby
|
83
77
|
Mongo::Lock.configure do |config|
|
84
78
|
config.collections: {
|
85
|
-
default:
|
86
|
-
other:
|
79
|
+
default: database.collection("locks"),
|
80
|
+
other: database.collection("other_locks")
|
87
81
|
}
|
88
82
|
end
|
89
83
|
```
|
@@ -183,6 +177,10 @@ lock.release
|
|
183
177
|
Mongo::Lock.release('my_key')
|
184
178
|
```
|
185
179
|
|
180
|
+
### Lock Key
|
181
|
+
|
182
|
+
The lock key is treated in the same way as [ActiveSupport::Cache's keys](http://guides.rubyonrails.org/caching_with_rails.html#cache-keys), except instead of responding to :cache_key or to :to_param it should respond to :lock_key or to :to_param. You can use Hashes and Arrays of values as cache keys.
|
183
|
+
|
186
184
|
### Options
|
187
185
|
|
188
186
|
When using Mongo::Lock#acquire, Mongo::Lock#release or Mongo::Lock#new after the key you may overide any of the following options:
|
@@ -235,6 +233,40 @@ lock = Mongo::Lock.new('my_key')
|
|
235
233
|
lock.available?
|
236
234
|
```
|
237
235
|
|
236
|
+
### Release all locks
|
237
|
+
|
238
|
+
You can release all locks across an entire collection or owner with the .release_all method.
|
239
|
+
|
240
|
+
```ruby
|
241
|
+
Mongo::Lock.release_all # Release all locks in all registered collections
|
242
|
+
Mongo::Lock.release_all collection: :my_locks # Release all locks in the collection registered as :my_locks
|
243
|
+
Mongo::Lock.release_all collection: my_collection # Release all locks in this instance of Mongo::Collection
|
244
|
+
Mongo::Lock.release_all collections: [c1,c2] # Release all locks in these instances of Mongo::Collection
|
245
|
+
Mongo::Lock.release_all collections: {a: ca, b: cb} # Release all locks in these instances of Mongo::Collection
|
246
|
+
Mongo::Lock.release_all owner: 'me' # Release all locks in all registered collections that belong to 'me'
|
247
|
+
```
|
248
|
+
|
249
|
+
### Clear expired locks
|
250
|
+
|
251
|
+
You can clear expire locks from the database with the .clear_expired method. If you have called .ensure_indexes mongo will do this for you automatically with a [time to live index](http://docs.mongodb.org/manual/tutorial/expire-data/).
|
252
|
+
|
253
|
+
```ruby
|
254
|
+
Mongo::Lock.clear_expired # Clear expired locks in all registered collections
|
255
|
+
Mongo::Lock.clear_expired collection: :my_locks # Clear expired locks in the collection registered as :my_locks
|
256
|
+
Mongo::Lock.clear_expired collection: my_collection # Clear expired locks in this instance of Mongo::Collection
|
257
|
+
Mongo::Lock.clear_expired collections: [c1,c2] # Clear expired locks in these instances of Mongo::Collection
|
258
|
+
Mongo::Lock.clear_expired collections: {a: ca, b: cb} # Clear expired locks in these instances of Mongo::Collection
|
259
|
+
```
|
260
|
+
|
261
|
+
### Check a key is already locked without acquiring it
|
262
|
+
|
263
|
+
```ruby
|
264
|
+
Mongo::Lock.available? 'my_key'
|
265
|
+
# Or
|
266
|
+
lock = Mongo::Lock.new('my_key')
|
267
|
+
lock.available?
|
268
|
+
```
|
269
|
+
|
238
270
|
### Failures
|
239
271
|
|
240
272
|
If Mongo::Lock#acquire cannot acquire a lock within its configuration limits it will return false.
|
@@ -279,6 +311,16 @@ rescue Mongo::Lock::LockNotAcquiredError => e
|
|
279
311
|
end
|
280
312
|
```
|
281
313
|
|
314
|
+
## Rake tasks
|
315
|
+
|
316
|
+
If you are running mongo-lock inside Rails it will add the following rake tasks for you.
|
317
|
+
|
318
|
+
```bash
|
319
|
+
bundle exec rake mongolock:clear_expired # Calls Mongo::Lock.clear_expired
|
320
|
+
bundle exec rake mongolock:release_all # Calls Mongo::Lock.release_all
|
321
|
+
bundle exec rake mongolock:ensure_indexes # Calls Mongo::Lock.ensure_indexes
|
322
|
+
```
|
323
|
+
|
282
324
|
## Contributors
|
283
325
|
|
284
326
|
Matthew Spence (msaspence)
|
data/lib/mongo-lock.rb
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
require 'mongo-lock/configuration'
|
2
|
-
require 'mongo-lock/
|
2
|
+
require 'mongo-lock/mongo_queries'
|
3
3
|
require 'mongo-lock/class_convenience_methods'
|
4
4
|
|
5
|
+
# If we are using Rails then we will include the Mongo::Lock railtie.
|
6
|
+
if defined?(Rails)
|
7
|
+
require "mongo-lock/railtie"
|
8
|
+
end
|
9
|
+
|
5
10
|
module Mongo
|
6
11
|
class Lock
|
7
12
|
|
@@ -39,34 +44,32 @@ module Mongo
|
|
39
44
|
end
|
40
45
|
end
|
41
46
|
|
42
|
-
|
43
|
-
def self.release_all options = {}
|
44
|
-
if options.include? :collection
|
45
|
-
Mongo::Lock::Queries.release_collection configuration.collection(options[:collection]), options[:owner]
|
46
|
-
else
|
47
|
-
configuration.collections.each_pair do |key,collection|
|
48
|
-
Mongo::Lock::Queries.release_collection collection, options[:owner]
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
47
|
def self.ensure_indexes
|
54
48
|
configuration.collections.each_pair do |key, collection|
|
55
|
-
Mongo::Lock::
|
49
|
+
Mongo::Lock::MongoQueries.ensure_indexes collection
|
56
50
|
end
|
57
51
|
end
|
58
52
|
|
59
|
-
def self.clear_expired
|
60
|
-
configuration.
|
61
|
-
|
53
|
+
def self.clear_expired options = {}
|
54
|
+
options = configuration.process_collection_options options
|
55
|
+
|
56
|
+
options[:collections].each do |collection|
|
57
|
+
Mongo::Lock::MongoQueries.clear_expired collection
|
62
58
|
end
|
63
59
|
end
|
64
60
|
|
61
|
+
def self.release_all options = {}
|
62
|
+
options = configuration.process_collection_options options
|
63
|
+
|
64
|
+
options[:collections].each do |collection|
|
65
|
+
Mongo::Lock::MongoQueries.release_collection collection, options[:owner]
|
66
|
+
end
|
67
|
+
end
|
65
68
|
|
66
69
|
def initialize key, options = {}
|
67
70
|
self.configuration = Configuration.new self.class.configuration.to_hash, options
|
68
|
-
self.key = key
|
69
|
-
self.query = Mongo::Lock::
|
71
|
+
self.key = retrieve_lock_key key
|
72
|
+
self.query = Mongo::Lock::MongoQueries.new self
|
70
73
|
acquire_if_acquired
|
71
74
|
end
|
72
75
|
|
@@ -77,13 +80,13 @@ module Mongo
|
|
77
80
|
yield self.configuration if block_given?
|
78
81
|
end
|
79
82
|
|
80
|
-
def acquire options = {}
|
83
|
+
def acquire options = {}, &block
|
81
84
|
options = inherit_options options
|
82
85
|
i = 1
|
83
86
|
time_spent = 0
|
84
87
|
|
85
88
|
loop do
|
86
|
-
result = try_acquire options, i, time_spent
|
89
|
+
result = try_acquire options, i, time_spent, &block
|
87
90
|
return result unless result.nil?
|
88
91
|
|
89
92
|
frequency = call_if_proc options[:frequency], i
|
@@ -93,7 +96,7 @@ module Mongo
|
|
93
96
|
end
|
94
97
|
end
|
95
98
|
|
96
|
-
def try_acquire options, i, time_spent
|
99
|
+
def try_acquire options, i, time_spent, &block
|
97
100
|
# If timeout has expired
|
98
101
|
if options[:timeout_in] && options[:timeout_in] < time_spent
|
99
102
|
return raise_or_false options
|
@@ -114,8 +117,16 @@ module Mongo
|
|
114
117
|
# If the lock was acquired
|
115
118
|
else
|
116
119
|
self.acquired = true
|
117
|
-
return
|
120
|
+
return call_block options, &block
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def call_block options, &block
|
125
|
+
if block_given?
|
126
|
+
yield self
|
127
|
+
release(options)
|
118
128
|
end
|
129
|
+
true
|
119
130
|
end
|
120
131
|
|
121
132
|
def release options = {}
|
@@ -150,12 +161,8 @@ module Mongo
|
|
150
161
|
def extend_by time, options = {}
|
151
162
|
options = inherit_options options
|
152
163
|
|
153
|
-
# Can't extend a lock that hasn't been acquired
|
154
|
-
if !acquired?
|
155
|
-
return raise_or_false options, NotExtendedError
|
156
|
-
|
157
|
-
# Can't extend a lock that has started
|
158
|
-
elsif expired?
|
164
|
+
# Can't extend a lock that hasn't been acquired or expired
|
165
|
+
if !acquired? || expired?
|
159
166
|
return raise_or_false options, NotExtendedError
|
160
167
|
|
161
168
|
else
|
@@ -209,6 +216,14 @@ module Mongo
|
|
209
216
|
|
210
217
|
# Utils
|
211
218
|
|
219
|
+
def retrieve_lock_key key
|
220
|
+
case
|
221
|
+
when key.respond_to?(:lock_key) then key.lock_key
|
222
|
+
when key.is_a?(Array) then key.map { |element| retrieve_lock_key(element) }.to_param
|
223
|
+
else key.to_param
|
224
|
+
end.to_s
|
225
|
+
end
|
226
|
+
|
212
227
|
def acquire_if_acquired
|
213
228
|
self.acquired = true if query.is_acquired?
|
214
229
|
end
|
@@ -2,14 +2,14 @@ module Mongo
|
|
2
2
|
class Lock
|
3
3
|
module ClassConvenienceMethods
|
4
4
|
|
5
|
-
def init_and_send key, options = {}, method
|
5
|
+
def init_and_send key, options = {}, method, &block
|
6
6
|
lock = Mongo::Lock.new(key, options)
|
7
|
-
lock.send(method)
|
7
|
+
lock.send(method, &block)
|
8
8
|
lock
|
9
9
|
end
|
10
10
|
|
11
|
-
def acquire key, options = {}
|
12
|
-
init_and_send key, options, :acquire
|
11
|
+
def acquire key, options = {}, &block
|
12
|
+
init_and_send key, options, :acquire, &block
|
13
13
|
end
|
14
14
|
|
15
15
|
def release key, options = {}
|
@@ -25,7 +25,7 @@ module Mongo
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def available? key, options = {}
|
28
|
-
|
28
|
+
Mongo::Lock.new(key, options).available?
|
29
29
|
end
|
30
30
|
|
31
31
|
end
|
@@ -50,6 +50,7 @@ module Mongo
|
|
50
50
|
|
51
51
|
def to_hash
|
52
52
|
{
|
53
|
+
collections: collections,
|
53
54
|
timeout_in: timeout_in,
|
54
55
|
limit: limit,
|
55
56
|
frequency: frequency,
|
@@ -67,6 +68,34 @@ module Mongo
|
|
67
68
|
end
|
68
69
|
end
|
69
70
|
|
71
|
+
def process_collection_options options
|
72
|
+
options = array_of_collections options
|
73
|
+
options = add_single_collection_to_collections options
|
74
|
+
options = use_registered_collections_if_empty options
|
75
|
+
options
|
76
|
+
end
|
77
|
+
|
78
|
+
def array_of_collections options
|
79
|
+
options[:collections] = options[:collections].try(:values) || options[:collections] || []
|
80
|
+
options
|
81
|
+
end
|
82
|
+
|
83
|
+
def add_single_collection_to_collections options
|
84
|
+
if options[:collection].is_a? Symbol
|
85
|
+
options[:collections] << self.collection(options[:collection])
|
86
|
+
elsif options[:collection]
|
87
|
+
options[:collections] << options[:collection]
|
88
|
+
end
|
89
|
+
options
|
90
|
+
end
|
91
|
+
|
92
|
+
def use_registered_collections_if_empty options
|
93
|
+
if options[:collections].empty?
|
94
|
+
options[:collections] = self.collections.values
|
95
|
+
end
|
96
|
+
options
|
97
|
+
end
|
98
|
+
|
70
99
|
end
|
71
100
|
end
|
72
101
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Mongo
|
2
2
|
class Lock
|
3
|
-
class
|
3
|
+
class MongoQueries
|
4
4
|
|
5
5
|
attr_accessor :lock
|
6
6
|
|
@@ -50,7 +50,7 @@ module Mongo
|
|
50
50
|
|
51
51
|
def find_and_modify options
|
52
52
|
operation = options[:insert] ? '$setOnInsert' : '$set'
|
53
|
-
existing_lock = collection.find_and_modify({
|
53
|
+
existing_lock = lock.configuration.collection.find_and_modify({
|
54
54
|
query: query,
|
55
55
|
update: {
|
56
56
|
operation => {
|
@@ -73,7 +73,7 @@ module Mongo
|
|
73
73
|
end
|
74
74
|
|
75
75
|
def remove options
|
76
|
-
collection.remove key: key, owner: options[:owner]
|
76
|
+
lock.configuration.collection.remove key: key, owner: options[:owner]
|
77
77
|
end
|
78
78
|
|
79
79
|
def is_acquired?
|
@@ -81,7 +81,7 @@ module Mongo
|
|
81
81
|
end
|
82
82
|
|
83
83
|
def find_already_acquired
|
84
|
-
collection.find({
|
84
|
+
lock.configuration.collection.find({
|
85
85
|
key: key,
|
86
86
|
owner: lock.configuration.owner,
|
87
87
|
expires_at: { '$gt' => Time.now }
|
@@ -89,7 +89,7 @@ module Mongo
|
|
89
89
|
end
|
90
90
|
|
91
91
|
def find_existing
|
92
|
-
collection.find(query).first
|
92
|
+
lock.configuration.collection.find(query).first
|
93
93
|
end
|
94
94
|
|
95
95
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "mongoid"
|
3
|
+
require "mongoid/config"
|
4
|
+
require "mongoid/railties/document"
|
5
|
+
require "rails"
|
6
|
+
require "rails/mongoid"
|
7
|
+
|
8
|
+
module Rails
|
9
|
+
module Mongo
|
10
|
+
module Lock
|
11
|
+
class Railtie < Rails::Railtie
|
12
|
+
|
13
|
+
rake_tasks do
|
14
|
+
load "mongoid/railties/mongo.rake"
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
namespace :mongolock do
|
2
|
+
desc "Remove all the expired locks from registered collections"
|
3
|
+
task :clear_expired => :environment do
|
4
|
+
::Mongo::Lock.clear_expired
|
5
|
+
end
|
6
|
+
|
7
|
+
desc "Release all expired locks from registered collections"
|
8
|
+
task :release_all => :environment do
|
9
|
+
::Mongo::Lock.release_all
|
10
|
+
end
|
11
|
+
|
12
|
+
desc "Release all expired locks from registered collections"
|
13
|
+
task :ensure_indexes => :environment do
|
14
|
+
::Mongo::Lock.ensure_indexes
|
15
|
+
end
|
16
|
+
end
|
data/lib/mongo-lock/version.rb
CHANGED
data/spec/acquire_spec.rb
CHANGED
@@ -22,6 +22,16 @@ describe Mongo::Lock do
|
|
22
22
|
|
23
23
|
end
|
24
24
|
|
25
|
+
context "when a block is provided" do
|
26
|
+
|
27
|
+
it "passes it to the new lock" do
|
28
|
+
block = Proc.new { |lock| }
|
29
|
+
expect_any_instance_of(Mongo::Lock).to receive(:acquire).with( &block)
|
30
|
+
lock = Mongo::Lock.acquire('my_lock', { limit: 3 }, &block)
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
25
35
|
end
|
26
36
|
|
27
37
|
describe '#acquire' do
|
@@ -32,13 +42,13 @@ describe Mongo::Lock do
|
|
32
42
|
|
33
43
|
it "acquires the lock" do
|
34
44
|
lock.acquire
|
35
|
-
expect(
|
45
|
+
expect(my_collection.find(key: 'my_lock').count).to be 1
|
36
46
|
end
|
37
47
|
|
38
48
|
it "sets the lock to expire" do
|
39
49
|
lock.acquire
|
40
|
-
expect(
|
41
|
-
expect(
|
50
|
+
expect(my_collection.find(key: 'my_lock').first['expires_at']).to be_within(1.second).of(10.seconds.from_now)
|
51
|
+
expect(my_collection.find(key: 'my_lock').first['ttl']).to be_within(1.second).of(10.seconds.from_now)
|
42
52
|
end
|
43
53
|
|
44
54
|
it "returns true" do
|
@@ -52,7 +62,7 @@ describe Mongo::Lock do
|
|
52
62
|
let(:lock) { Mongo::Lock.new 'my_lock' }
|
53
63
|
|
54
64
|
it "should call the Proc with the attempt number" do
|
55
|
-
|
65
|
+
my_collection.insert key: 'my_lock', owner: 'tobie', expires_at: 10.seconds.from_now
|
56
66
|
proc = Proc.new{ |x| x }
|
57
67
|
expect(proc).to receive(:call).with(1).and_return(0.01)
|
58
68
|
expect(proc).to receive(:call).with(2).and_return(0.01)
|
@@ -65,9 +75,9 @@ describe Mongo::Lock do
|
|
65
75
|
context "when the lock is unavailable" do
|
66
76
|
|
67
77
|
it "retries until it can acquire it" do
|
68
|
-
|
78
|
+
my_collection.insert key: 'my_lock', owner: 'tobie', expires_at: 0.1.seconds.from_now
|
69
79
|
lock.acquire frequency: 0.01, timeout_in: 0.2, limit: 20
|
70
|
-
expect(
|
80
|
+
expect(my_collection.find(key: 'my_lock', owner: 'spence').count).to be 1
|
71
81
|
end
|
72
82
|
|
73
83
|
end
|
@@ -75,12 +85,12 @@ describe Mongo::Lock do
|
|
75
85
|
context "when the lock is already acquired but by the same owner" do
|
76
86
|
|
77
87
|
before :each do
|
78
|
-
|
88
|
+
my_collection.insert key: 'my_lock', owner: 'spence', expires_at: 10.minutes.from_now
|
79
89
|
end
|
80
90
|
|
81
91
|
it "doesn't create a new lock" do
|
82
92
|
lock.acquire
|
83
|
-
expect(
|
93
|
+
expect(my_collection.find(key: 'my_lock').count).to be 1
|
84
94
|
end
|
85
95
|
|
86
96
|
it "returns true" do
|
@@ -101,7 +111,7 @@ describe Mongo::Lock do
|
|
101
111
|
let(:lock) { Mongo::Lock.new 'my_lock', owner: 'spence', timeout_in: 0.03, frequency: 0.01 }
|
102
112
|
|
103
113
|
it "should return false" do
|
104
|
-
|
114
|
+
my_collection.insert key: 'my_lock', owner: 'tobie', expires_at: 1.second.from_now
|
105
115
|
expect(lock.acquire).to be_false
|
106
116
|
end
|
107
117
|
|
@@ -112,7 +122,7 @@ describe Mongo::Lock do
|
|
112
122
|
let(:lock) { Mongo::Lock.new 'my_lock', owner: 'spence', timeout_in: 0.4, limit: 3, frequency: 0.01 }
|
113
123
|
|
114
124
|
it "should return false" do
|
115
|
-
|
125
|
+
my_collection.insert key: 'my_lock', owner: 'tobie', expires_at: 1.second.from_now
|
116
126
|
expect(lock.acquire).to be_false
|
117
127
|
end
|
118
128
|
|
@@ -127,7 +137,7 @@ describe Mongo::Lock do
|
|
127
137
|
let(:lock) { Mongo::Lock.new 'my_lock', owner: 'tobie', timeout_in: 0.4, limit: 3, frequency: 0.01, raise: true }
|
128
138
|
|
129
139
|
it "should raise Mongo::Lock::NotAcquiredError" do
|
130
|
-
|
140
|
+
my_collection.insert key: 'my_lock', owner: 'spence', expires_at: 1.second.from_now
|
131
141
|
expect{lock.acquire}.to raise_error Mongo::Lock::NotAcquiredError
|
132
142
|
end
|
133
143
|
|
@@ -138,7 +148,7 @@ describe Mongo::Lock do
|
|
138
148
|
let(:lock) { Mongo::Lock.new 'my_lock', owner: 'tobie', timeout_in: 0.3, limit: 3, frequency: 0.01, raise: true }
|
139
149
|
|
140
150
|
it "should raise Mongo::Lock::NotAcquiredError" do
|
141
|
-
|
151
|
+
my_collection.insert key: 'my_lock', owner: 'spence', expires_at: 1.second.from_now
|
142
152
|
expect{lock.acquire}.to raise_error Mongo::Lock::NotAcquiredError
|
143
153
|
end
|
144
154
|
|
@@ -151,12 +161,35 @@ describe Mongo::Lock do
|
|
151
161
|
let(:lock) { Mongo::Lock.new 'my_lock', owner: 'tobie', timeout_in: 0.2, limit: 11, frequency: 0.01, raise: true }
|
152
162
|
|
153
163
|
it "overrides the lock's" do
|
154
|
-
|
164
|
+
my_collection.insert key: 'my_lock', owner: 'spence', expires_at: 1.second.from_now
|
155
165
|
expect(lock.acquire timeout_in: 0.05, limit: 3, frequency: 0.02, raise: false).to be_false
|
156
166
|
end
|
157
167
|
|
158
168
|
end
|
159
169
|
|
170
|
+
context "when a block is provided" do
|
171
|
+
|
172
|
+
let(:lock) { Mongo::Lock.new 'my_lock', owner: 'tobie', timeout_in: 0.2, limit: 11, frequency: 0.01, raise: true }
|
173
|
+
|
174
|
+
it "should acquire the lock" do
|
175
|
+
lock.acquire do |lock|
|
176
|
+
expect(Mongo::Lock.available? 'my_lock', owner: 'spence').to be_false
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
it "should call the block" do
|
181
|
+
expect{ |block| lock.acquire &block }.to yield_with_args lock
|
182
|
+
end
|
183
|
+
|
184
|
+
it "should release the lock" do
|
185
|
+
lock.acquire do |lock|
|
186
|
+
# Do something
|
187
|
+
end
|
188
|
+
expect(Mongo::Lock.available?('my_lock', owner: 'spence')).to be_true
|
189
|
+
end
|
190
|
+
|
191
|
+
end
|
192
|
+
|
160
193
|
end
|
161
194
|
|
162
195
|
describe '.acquire!' do
|
data/spec/acquired_spec.rb
CHANGED
@@ -18,7 +18,7 @@ describe Mongo::Lock do
|
|
18
18
|
context "when the lock hasn't been acquired" do
|
19
19
|
|
20
20
|
it "returns false" do
|
21
|
-
|
21
|
+
my_collection.insert key: 'my_lock', owner: 'tobie', expires_at: 1.minute.from_now
|
22
22
|
lock.acquire
|
23
23
|
expect(lock.acquired?).to be_false
|
24
24
|
end
|
@@ -27,8 +27,9 @@ describe Mongo::Lock do
|
|
27
27
|
|
28
28
|
context "when the lock was acquired but has since expired" do
|
29
29
|
|
30
|
+
let(:lock) { Mongo::Lock.new 'my_lock', owner: 'spence', timeout_in: 0.01, frequency: 0.01, expire_in: 0.1 }
|
31
|
+
|
30
32
|
it "returns false" do
|
31
|
-
collection.insert key: 'my_lock', owner: 'spence', expires_at: 0.1.seconds.from_now
|
32
33
|
lock.acquire
|
33
34
|
sleep 0.2
|
34
35
|
expect(lock.acquired?).to be_false
|
@@ -39,7 +40,7 @@ describe Mongo::Lock do
|
|
39
40
|
context "when the lock was acquired but has since been released" do
|
40
41
|
|
41
42
|
it "returns false" do
|
42
|
-
|
43
|
+
my_collection.insert key: 'my_lock', owner: 'tobie', expires_at: 1.minute.ago
|
43
44
|
lock.acquire
|
44
45
|
lock.release
|
45
46
|
expect(lock.acquired?).to be_false
|
data/spec/available_spec.rb
CHANGED
@@ -4,8 +4,8 @@ describe Mongo::Lock do
|
|
4
4
|
|
5
5
|
describe '.available?' do
|
6
6
|
|
7
|
-
it "creates
|
8
|
-
expect(Mongo::Lock.available? 'my_lock').to
|
7
|
+
it "creates a new Mongo::Lock instance and returns whether it is available" do
|
8
|
+
expect(Mongo::Lock.available? 'my_lock').to be_true
|
9
9
|
end
|
10
10
|
|
11
11
|
it "calls #available?" do
|
@@ -16,8 +16,8 @@ describe Mongo::Lock do
|
|
16
16
|
context "when options are provided" do
|
17
17
|
|
18
18
|
it "passes them to the new lock" do
|
19
|
-
|
20
|
-
expect(
|
19
|
+
Mongo::Lock.acquire 'my_lock', { owner: 'spence' }
|
20
|
+
expect(Mongo::Lock.available?('my_lock', { owner: 'spence' })).to be_true
|
21
21
|
end
|
22
22
|
|
23
23
|
end
|
@@ -39,7 +39,7 @@ describe Mongo::Lock do
|
|
39
39
|
context "when the lock is expired" do
|
40
40
|
|
41
41
|
it "returns true" do
|
42
|
-
|
42
|
+
my_collection.insert key: 'my_lock', owner: 'tobie', expires_at: 1.minute.ago
|
43
43
|
expect(lock.available?).to be_true
|
44
44
|
end
|
45
45
|
|
@@ -48,7 +48,7 @@ describe Mongo::Lock do
|
|
48
48
|
context "when the lock is already acquired but by this owner" do
|
49
49
|
|
50
50
|
it "returns true" do
|
51
|
-
|
51
|
+
my_collection.insert key: 'my_lock', owner: 'spence', expires_at: 1.minute.from_now
|
52
52
|
expect(lock.available?).to be_true
|
53
53
|
end
|
54
54
|
|
@@ -57,7 +57,7 @@ describe Mongo::Lock do
|
|
57
57
|
context "when the lock is already acquired" do
|
58
58
|
|
59
59
|
it "returns false" do
|
60
|
-
|
60
|
+
my_collection.insert key: 'my_lock', owner: 'tobie', expires_at: 1.minute.from_now
|
61
61
|
expect(lock.available?).to be_false
|
62
62
|
end
|
63
63
|
|
data/spec/clear_expired_spec.rb
CHANGED
@@ -4,17 +4,95 @@ describe Mongo::Lock do
|
|
4
4
|
|
5
5
|
describe '.clear_expired' do
|
6
6
|
|
7
|
-
|
8
|
-
Mongo::Lock.configure collections: { default:
|
9
|
-
|
10
|
-
|
7
|
+
before :each do
|
8
|
+
Mongo::Lock.configure collections: { default: my_collection, other: other_collection }
|
9
|
+
end
|
10
|
+
|
11
|
+
let!(:locks) do
|
12
|
+
my_collection.insert key: 'tobies_lock', owner: 'tobie', expires_at: 1.minute.from_now, ttl: 1.minute.from_now
|
13
|
+
my_collection.insert key: 'spences_lock', owner: 'spence', expires_at: 1.minute.ago, ttl: 1.minute.ago
|
14
|
+
other_collection.insert key: 'spences_lock', owner: 'spence', expires_at: 1.minute.ago, ttl: 1.minute.ago
|
15
|
+
another_collection.insert key: 'spences_lock', owner: 'spence', expires_at: 1.minute.ago, ttl: 1.minute.ago
|
16
|
+
end
|
17
|
+
|
18
|
+
it "deletes expired locks in all reegistered collections" do
|
19
|
+
Mongo::Lock.configure collections: { default: my_collection, other: other_collection }
|
11
20
|
other_collection.insert owner: 'owner', key: 'my_lock', expires_at: 1.minute.from_now
|
12
|
-
other_collection.insert owner: 'owner', key: 'my_lock', expires_at: 1.minute.ago
|
13
21
|
Mongo::Lock.clear_expired
|
14
|
-
expect(
|
22
|
+
expect(my_collection.find().count).to be 1
|
15
23
|
expect(other_collection.find().count).to be 1
|
16
24
|
end
|
17
25
|
|
26
|
+
context "when a collection is provided" do
|
27
|
+
|
28
|
+
before do
|
29
|
+
Mongo::Lock.clear_expired collection: other_collection
|
30
|
+
end
|
31
|
+
|
32
|
+
it "does release locks in that collection" do
|
33
|
+
expect(other_collection.find({ key: 'spences_lock', owner: 'spence'}).count).to eql 0
|
34
|
+
end
|
35
|
+
|
36
|
+
it "doesn't release locks in other collections" do
|
37
|
+
expect(my_collection.find({ key: 'spences_lock', owner: 'spence'}).count).to eql 1
|
38
|
+
expect(my_collection.find({ key: 'tobies_lock', owner: 'tobie'}).count).to eql 1
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
context "when a collection symbol is provided" do
|
44
|
+
|
45
|
+
before do
|
46
|
+
Mongo::Lock.clear_expired collection: :other
|
47
|
+
end
|
48
|
+
|
49
|
+
it "does release locks in that collection" do
|
50
|
+
expect(other_collection.find({ key: 'spences_lock', owner: 'spence'}).count).to eql 0
|
51
|
+
end
|
52
|
+
|
53
|
+
it "doesn't release locks in other collections" do
|
54
|
+
expect(my_collection.find({ key: 'spences_lock', owner: 'spence'}).count).to eql 1
|
55
|
+
expect(my_collection.find({ key: 'tobies_lock', owner: 'tobie'}).count).to eql 1
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
context "when collections are provided" do
|
61
|
+
|
62
|
+
before do
|
63
|
+
Mongo::Lock.clear_expired collections: [another_collection, other_collection]
|
64
|
+
end
|
65
|
+
|
66
|
+
it "does release locks in those collection" do
|
67
|
+
expect(other_collection.find({ key: 'spences_lock', owner: 'spence'}).count).to eql 0
|
68
|
+
expect(another_collection.find({ key: 'spences_lock', owner: 'spence'}).count).to eql 0
|
69
|
+
end
|
70
|
+
|
71
|
+
it "doesn't release locks in other collections" do
|
72
|
+
expect(my_collection.find({ key: 'spences_lock', owner: 'spence'}).count).to eql 1
|
73
|
+
expect(my_collection.find({ key: 'tobies_lock', owner: 'tobie'}).count).to eql 1
|
74
|
+
end
|
75
|
+
|
76
|
+
context "when collections is provided as a hash" do
|
77
|
+
|
78
|
+
before do
|
79
|
+
Mongo::Lock.clear_expired collections: { another_collection: another_collection, other_collection: other_collection }
|
80
|
+
end
|
81
|
+
|
82
|
+
it "does release locks in those collection" do
|
83
|
+
expect(other_collection.find({ key: 'spences_lock', owner: 'spence'}).count).to eql 0
|
84
|
+
expect(another_collection.find({ key: 'spences_lock', owner: 'spence'}).count).to eql 0
|
85
|
+
end
|
86
|
+
|
87
|
+
it "doesn't release locks in other collections" do
|
88
|
+
expect(my_collection.find({ key: 'spences_lock', owner: 'spence'}).count).to eql 1
|
89
|
+
expect(my_collection.find({ key: 'tobies_lock', owner: 'tobie'}).count).to eql 1
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
18
96
|
end
|
19
97
|
|
20
98
|
end
|
data/spec/configuration_spec.rb
CHANGED
@@ -4,9 +4,9 @@ describe Mongo::Lock::Configuration do
|
|
4
4
|
|
5
5
|
subject { Mongo::Lock::Configuration.new({}, {}) }
|
6
6
|
|
7
|
-
let (:collections_with_default) { { a: 'a', b: 'b', default:
|
7
|
+
let (:collections_with_default) { { a: 'a', b: 'b', default: my_collection } }
|
8
8
|
let (:collections) { { a: 'a', b: 'b' } }
|
9
|
-
let (:
|
9
|
+
let (:my_collection) { 'default' }
|
10
10
|
|
11
11
|
describe '#initialize' do
|
12
12
|
|
@@ -21,7 +21,7 @@ describe Mongo::Lock::Configuration do
|
|
21
21
|
context "when provided with a default connection" do
|
22
22
|
|
23
23
|
it "stores it in the connections hash as :default" do
|
24
|
-
config = Mongo::Lock::Configuration.new({}, { collection:
|
24
|
+
config = Mongo::Lock::Configuration.new({}, { collection: my_collection, collections: collections })
|
25
25
|
expect(config.collections).to eql collections_with_default
|
26
26
|
end
|
27
27
|
|
@@ -40,7 +40,7 @@ describe Mongo::Lock::Configuration do
|
|
40
40
|
context "when provided with a default connection" do
|
41
41
|
|
42
42
|
it "stores it in the connections hash as :default" do
|
43
|
-
config = Mongo::Lock::Configuration.new({ collections: collections }, { collection:
|
43
|
+
config = Mongo::Lock::Configuration.new({ collections: collections }, { collection: my_collection})
|
44
44
|
expect(config.collections).to eql collections_with_default
|
45
45
|
end
|
46
46
|
|
@@ -87,8 +87,8 @@ describe Mongo::Lock::Configuration do
|
|
87
87
|
describe "#collection=" do
|
88
88
|
|
89
89
|
it "should set the default collection" do
|
90
|
-
subject.collection =
|
91
|
-
expect(subject.instance_variable_get('@collections')[:default]).to be
|
90
|
+
subject.collection = my_collection
|
91
|
+
expect(subject.instance_variable_get('@collections')[:default]).to be my_collection
|
92
92
|
end
|
93
93
|
|
94
94
|
end
|
data/spec/ensure_indexes_spec.rb
CHANGED
@@ -11,11 +11,20 @@ describe Mongo::Lock do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
it "should build ttl index for each collection" do
|
14
|
-
expect(
|
14
|
+
expect(my_collection.index_information['ttl_1']).to eql "v"=>1, "key"=> { "ttl"=>1 }, "ns"=>"mongo_lock_tests.locks", "name"=>"ttl_1", "expireAfterSeconds"=>0
|
15
15
|
end
|
16
16
|
|
17
17
|
it "should build an index on key and expires_at for each collection" do
|
18
|
-
expect(
|
18
|
+
expect(my_collection.index_information['key_1_owner_1_expires_at_1']).to eql "v"=>1, "key"=> { "key"=>1, "owner"=>1, "expires_at"=>1 }, "ns"=>"mongo_lock_tests.locks", "name"=>"key_1_owner_1_expires_at_1"
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should mean expired locks are automatically cleaned", :slow do
|
22
|
+
Mongo::Lock.acquire 'my_lock', owner: 'spence', expire_in: 0
|
23
|
+
Mongo::Lock.acquire 'other_lock', owner: 'spence', expire_in: 500
|
24
|
+
while my_collection.find(owner: 'spence').count != 1
|
25
|
+
sleep 1
|
26
|
+
end
|
27
|
+
expect(my_collection.find(owner: 'spence').count).to be 1
|
19
28
|
end
|
20
29
|
|
21
30
|
end
|
data/spec/extend_by_spec.rb
CHANGED
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Mongo::Lock do
|
4
4
|
|
5
|
-
let(:lock) { Mongo::Lock.new 'my_lock', owner: 'spence', timeout_in: 0.01, frequency: 0.01, expire_in:
|
5
|
+
let(:lock) { Mongo::Lock.new 'my_lock', owner: 'spence', timeout_in: 0.01, frequency: 0.01, expire_in: 1 }
|
6
6
|
|
7
7
|
describe '#extend_by' do
|
8
8
|
|
@@ -11,7 +11,7 @@ describe Mongo::Lock do
|
|
11
11
|
it "extends the lock" do
|
12
12
|
lock.acquire
|
13
13
|
lock.extend_by 60
|
14
|
-
expect(
|
14
|
+
expect(my_collection.find(owner: 'spence', key: 'my_lock').first['expires_at']).to be_within(1.second).of(60.seconds.from_now)
|
15
15
|
end
|
16
16
|
|
17
17
|
it "returns true" do
|
@@ -23,6 +23,8 @@ describe Mongo::Lock do
|
|
23
23
|
|
24
24
|
context "when the lock has expired" do
|
25
25
|
|
26
|
+
let(:lock) { Mongo::Lock.new 'my_lock', owner: 'spence', timeout_in: 0.01, frequency: 0.01, expire_in: 0.11 }
|
27
|
+
|
26
28
|
it "returns false" do
|
27
29
|
lock.acquire
|
28
30
|
sleep 0.11
|
data/spec/initialise_spec.rb
CHANGED
@@ -19,13 +19,12 @@ describe Mongo::Lock do
|
|
19
19
|
Mongo::Lock.configure limit: 3
|
20
20
|
lock = Mongo::Lock.new 'my_lock', limit: 4
|
21
21
|
expect(lock.configuration.limit).to be 4
|
22
|
-
|
23
22
|
end
|
24
23
|
|
25
24
|
context "when the key is already acquired by this owner" do
|
26
25
|
|
27
26
|
it "acquires that lock" do
|
28
|
-
|
27
|
+
my_collection.insert key: 'my_lock', owner: 'spence', expires_at: 1.minute.from_now
|
29
28
|
expect(lock.acquired?).to be_true
|
30
29
|
end
|
31
30
|
|
data/spec/rake_spec.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rake'
|
3
|
+
# load File.expand_path("../lib/railties/mongo.rake", __FILE__)
|
4
|
+
load 'mongo-lock/railties/mongo.rake'
|
5
|
+
task :environment do ; end
|
6
|
+
|
7
|
+
describe 'mongolock' do
|
8
|
+
|
9
|
+
describe 'mongolock:clear_expired' do
|
10
|
+
|
11
|
+
it "calls .clear_expired" do
|
12
|
+
expect(Mongo::Lock).to receive(:clear_expired)
|
13
|
+
Rake::Task['mongolock:clear_expired'].invoke
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
describe 'mongolock:release_all' do
|
19
|
+
|
20
|
+
it "calls .release_all" do
|
21
|
+
expect(Mongo::Lock).to receive(:release_all)
|
22
|
+
Rake::Task['mongolock:release_all'].invoke
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
describe 'mongolock:ensure_indexes' do
|
28
|
+
|
29
|
+
it "calls .ensure_indexes" do
|
30
|
+
expect(Mongo::Lock).to receive(:ensure_indexes)
|
31
|
+
Rake::Task['mongolock:ensure_indexes'].invoke
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
data/spec/release_all_spec.rb
CHANGED
@@ -5,19 +5,20 @@ describe Mongo::Lock do
|
|
5
5
|
describe '.release_all' do
|
6
6
|
|
7
7
|
before :each do
|
8
|
-
Mongo::Lock.configure collections: { default:
|
8
|
+
Mongo::Lock.configure collections: { default: my_collection, other: other_collection }
|
9
9
|
end
|
10
10
|
|
11
11
|
let!(:locks) do
|
12
|
-
|
13
|
-
|
12
|
+
my_collection.insert key: 'tobies_lock', owner: 'tobie'
|
13
|
+
my_collection.insert key: 'spences_lock', owner: 'spence'
|
14
14
|
other_collection.insert key: 'spences_lock', owner: 'spence'
|
15
|
+
another_collection.insert key: 'spences_lock', owner: 'spence'
|
15
16
|
end
|
16
17
|
|
17
18
|
it "removes all locks from the database" do
|
18
19
|
Mongo::Lock.release_all
|
19
|
-
expect(
|
20
|
-
expect(
|
20
|
+
expect(my_collection.count({ key: 'spences_lock', owner: 'spence'})).to eql 0
|
21
|
+
expect(my_collection.count({ key: 'tobies_lock', owner: 'tobie'})).to eql 0
|
21
22
|
expect(other_collection.count({ key: 'spences_lock', owner: 'spence'})).to eql 0
|
22
23
|
end
|
23
24
|
|
@@ -28,12 +29,12 @@ describe Mongo::Lock do
|
|
28
29
|
end
|
29
30
|
|
30
31
|
it "doesn't release locks belonging to other owners" do
|
31
|
-
expect(
|
32
|
+
expect(my_collection.find({ key: 'tobies_lock', owner: 'tobie'}).count).to eql 1
|
32
33
|
end
|
33
34
|
|
34
35
|
it "does release locks belonging to that owner" do
|
35
36
|
Mongo::Lock.release_all owner: 'spence'
|
36
|
-
expect(
|
37
|
+
expect(my_collection.find({ key: 'spences_lock', owner: 'spence'}).count).to eql 0
|
37
38
|
expect(other_collection.find({ key: 'spences_lock', owner: 'spence'}).count).to eql 0
|
38
39
|
end
|
39
40
|
|
@@ -50,8 +51,8 @@ describe Mongo::Lock do
|
|
50
51
|
end
|
51
52
|
|
52
53
|
it "doesn't release locks in other collections" do
|
53
|
-
expect(
|
54
|
-
expect(
|
54
|
+
expect(my_collection.find({ key: 'spences_lock', owner: 'spence'}).count).to eql 1
|
55
|
+
expect(my_collection.find({ key: 'tobies_lock', owner: 'tobie'}).count).to eql 1
|
55
56
|
end
|
56
57
|
|
57
58
|
end
|
@@ -67,8 +68,44 @@ describe Mongo::Lock do
|
|
67
68
|
end
|
68
69
|
|
69
70
|
it "doesn't release locks in other collections" do
|
70
|
-
expect(
|
71
|
-
expect(
|
71
|
+
expect(my_collection.find({ key: 'spences_lock', owner: 'spence'}).count).to eql 1
|
72
|
+
expect(my_collection.find({ key: 'tobies_lock', owner: 'tobie'}).count).to eql 1
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
context "when collections are provided" do
|
78
|
+
|
79
|
+
before do
|
80
|
+
Mongo::Lock.release_all collections: [another_collection, other_collection]
|
81
|
+
end
|
82
|
+
|
83
|
+
it "does release locks in those collection" do
|
84
|
+
expect(other_collection.find({ key: 'spences_lock', owner: 'spence'}).count).to eql 0
|
85
|
+
expect(another_collection.find({ key: 'spences_lock', owner: 'spence'}).count).to eql 0
|
86
|
+
end
|
87
|
+
|
88
|
+
it "doesn't release locks in other collections" do
|
89
|
+
expect(my_collection.find({ key: 'spences_lock', owner: 'spence'}).count).to eql 1
|
90
|
+
expect(my_collection.find({ key: 'tobies_lock', owner: 'tobie'}).count).to eql 1
|
91
|
+
end
|
92
|
+
|
93
|
+
context "when collections is provided as a hash" do
|
94
|
+
|
95
|
+
before do
|
96
|
+
Mongo::Lock.release_all collections: { another_collection: another_collection, other_collection: other_collection }
|
97
|
+
end
|
98
|
+
|
99
|
+
it "does release locks in those collection" do
|
100
|
+
expect(other_collection.find({ key: 'spences_lock', owner: 'spence'}).count).to eql 0
|
101
|
+
expect(another_collection.find({ key: 'spences_lock', owner: 'spence'}).count).to eql 0
|
102
|
+
end
|
103
|
+
|
104
|
+
it "doesn't release locks in other collections" do
|
105
|
+
expect(my_collection.find({ key: 'spences_lock', owner: 'spence'}).count).to eql 1
|
106
|
+
expect(my_collection.find({ key: 'tobies_lock', owner: 'tobie'}).count).to eql 1
|
107
|
+
end
|
108
|
+
|
72
109
|
end
|
73
110
|
|
74
111
|
end
|
data/spec/release_spec.rb
CHANGED
@@ -32,14 +32,14 @@ describe Mongo::Lock do
|
|
32
32
|
context "when lock is acquired" do
|
33
33
|
|
34
34
|
before :each do
|
35
|
-
|
35
|
+
my_collection.insert key: 'my_lock', owner: 'spence'
|
36
36
|
end
|
37
37
|
|
38
38
|
let(:lock) { Mongo::Lock.acquire 'my_lock', owner: 'spence' }
|
39
39
|
|
40
40
|
it "releases the lock" do
|
41
41
|
lock.release
|
42
|
-
expect(
|
42
|
+
expect(my_collection.find(key: 'my_lock', owner: 'spence').count).to be 0
|
43
43
|
end
|
44
44
|
|
45
45
|
it "returns true" do
|
@@ -68,14 +68,14 @@ describe Mongo::Lock do
|
|
68
68
|
let(:lock) { Mongo::Lock.new 'my_lock', timeout_in: 1, frequency: 0.01 }
|
69
69
|
|
70
70
|
it "returns false" do
|
71
|
-
|
71
|
+
my_collection.insert key: 'my_lock', owner: 'tobie', expires_at: 1.seconds.from_now
|
72
72
|
expect(lock.release timeout_in: 0.01).to be_false
|
73
73
|
end
|
74
74
|
|
75
75
|
it "doesn't release the lock" do
|
76
|
-
|
76
|
+
my_collection.insert key: 'my_lock', owner: 'tobie', expires_at: 1.seconds.from_now
|
77
77
|
lock.release timeout_in: 0.01
|
78
|
-
expect(
|
78
|
+
expect(my_collection.find(key: 'my_lock', owner: 'tobie').count).to be 1
|
79
79
|
end
|
80
80
|
|
81
81
|
end
|
@@ -106,7 +106,7 @@ describe Mongo::Lock do
|
|
106
106
|
it "releases the lock" do
|
107
107
|
lock
|
108
108
|
different_instance.release
|
109
|
-
expect(
|
109
|
+
expect(my_collection.find(key: 'my_lock', owner: 'spence').count).to be 0
|
110
110
|
end
|
111
111
|
|
112
112
|
it "returns true" do
|
@@ -123,7 +123,7 @@ describe Mongo::Lock do
|
|
123
123
|
context "when the lock isn't acquired and cant be" do
|
124
124
|
|
125
125
|
it "raises Mongo::Lock::NotReleasedError" do
|
126
|
-
|
126
|
+
my_collection.insert key: 'my_lock', owner: 'tobie', expires_at: 1.seconds.from_now
|
127
127
|
expect{ lock.release }.to raise_error Mongo::Lock::NotReleasedError
|
128
128
|
end
|
129
129
|
|
@@ -134,9 +134,9 @@ describe Mongo::Lock do
|
|
134
134
|
context "when options are provided" do
|
135
135
|
|
136
136
|
it "they override the defaults" do
|
137
|
-
|
137
|
+
my_collection.insert key: 'my_lock', owner: 'tobie', expires_at: 1.seconds.from_now
|
138
138
|
expect(lock.release owner: 'tobie').to be_true
|
139
|
-
expect(
|
139
|
+
expect(my_collection.find(key: 'my_lock', owner: 'tobie').count).to be 0
|
140
140
|
end
|
141
141
|
|
142
142
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -25,7 +25,8 @@ RSpec.configure do |config|
|
|
25
25
|
config.before :each do
|
26
26
|
database.drop_collection("locks")
|
27
27
|
database.drop_collection("other_locks")
|
28
|
-
|
28
|
+
database.drop_collection("another_locks")
|
29
|
+
Mongo::Lock.configure collection: my_collection
|
29
30
|
end
|
30
31
|
|
31
32
|
config.after :each do
|
@@ -8,12 +8,16 @@ module MongoHelper
|
|
8
8
|
@database ||= connection.db("mongo_lock_tests")
|
9
9
|
end
|
10
10
|
|
11
|
-
def
|
12
|
-
@
|
11
|
+
def my_collection
|
12
|
+
@my_collection ||= database.collection :locks
|
13
13
|
end
|
14
14
|
|
15
15
|
def other_collection
|
16
16
|
@other_collection ||= database.collection :other_locks
|
17
17
|
end
|
18
18
|
|
19
|
+
def another_collection
|
20
|
+
@another_collection ||= database.collection :another_locks
|
21
|
+
end
|
22
|
+
|
19
23
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo-lock
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Spence
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-03-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -113,7 +113,9 @@ files:
|
|
113
113
|
- lib/mongo-lock.rb
|
114
114
|
- lib/mongo-lock/class_convenience_methods.rb
|
115
115
|
- lib/mongo-lock/configuration.rb
|
116
|
-
- lib/mongo-lock/
|
116
|
+
- lib/mongo-lock/mongo_queries.rb
|
117
|
+
- lib/mongo-lock/railtie.rb
|
118
|
+
- lib/mongo-lock/railties/mongo.rake
|
117
119
|
- lib/mongo-lock/version.rb
|
118
120
|
- mongo-lock.gemspec
|
119
121
|
- mongoid.yml
|
@@ -127,6 +129,7 @@ files:
|
|
127
129
|
- spec/expired_spec.rb
|
128
130
|
- spec/extend_by_spec.rb
|
129
131
|
- spec/initialise_spec.rb
|
132
|
+
- spec/rake_spec.rb
|
130
133
|
- spec/release_all_spec.rb
|
131
134
|
- spec/release_spec.rb
|
132
135
|
- spec/spec_helper.rb
|
@@ -166,6 +169,7 @@ test_files:
|
|
166
169
|
- spec/expired_spec.rb
|
167
170
|
- spec/extend_by_spec.rb
|
168
171
|
- spec/initialise_spec.rb
|
172
|
+
- spec/rake_spec.rb
|
169
173
|
- spec/release_all_spec.rb
|
170
174
|
- spec/release_spec.rb
|
171
175
|
- spec/spec_helper.rb
|