mongo-lock 1.1.4 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/Gemfile.lock +41 -38
- data/README.md +7 -4
- data/lib/mongo-lock.rb +22 -37
- data/lib/mongo-lock/configuration.rb +38 -4
- data/lib/mongo-lock/drivers/base.rb +41 -0
- data/lib/mongo-lock/drivers/mongo.rb +99 -0
- data/lib/mongo-lock/drivers/moped.rb +62 -0
- data/lib/mongo-lock/send_with_raise_methods.rb +28 -0
- data/lib/mongo-lock/version.rb +1 -1
- data/mongo-lock.gemspec +4 -3
- data/spec/configuration_spec.rb +66 -0
- data/spec/configure_spec.rb +8 -2
- data/spec/examples/acquire_example.rb +219 -0
- data/spec/examples/acquired_example.rb +54 -0
- data/spec/examples/available_example.rb +70 -0
- data/spec/examples/clear_expired_example.rb +100 -0
- data/spec/examples/ensure_indexes_example.rb +38 -0
- data/spec/examples/expired_example.rb +41 -0
- data/spec/examples/extend_by_example.rb +137 -0
- data/spec/examples/release_all_example.rb +117 -0
- data/spec/examples/release_example.rb +166 -0
- data/spec/initialise_spec.rb +2 -0
- data/spec/mongo_driver_spec.rb +22 -0
- data/spec/moped_driver_spec.rb +22 -0
- data/spec/rake_spec.rb +1 -1
- data/spec/spec_helper.rb +2 -7
- data/spec/support/mongo_helper.rb +41 -0
- metadata +58 -23
- data/lib/mongo-lock/mongo_queries.rb +0 -97
- data/spec/acquire_spec.rb +0 -217
- data/spec/acquired_spec.rb +0 -53
- data/spec/available_spec.rb +0 -68
- data/spec/clear_expired_spec.rb +0 -98
- data/spec/ensure_indexes_spec.rb +0 -34
- data/spec/expired_spec.rb +0 -39
- data/spec/extend_by_spec.rb +0 -135
- data/spec/release_all_spec.rb +0 -115
- data/spec/release_spec.rb +0 -164
@@ -0,0 +1,166 @@
|
|
1
|
+
shared_examples "MongoLock driver that can release locks" do
|
2
|
+
|
3
|
+
describe Mongo::Lock do
|
4
|
+
|
5
|
+
let(:lock) { Mongo::Lock.acquire('my_lock', owner: 'spence', expire_in: 0.1.seconds, timeout_in: 0.01, frequency: 0.01) }
|
6
|
+
|
7
|
+
describe '.release' do
|
8
|
+
|
9
|
+
it "creates a new Mongo::Lock instance" do
|
10
|
+
lock
|
11
|
+
expect(Mongo::Lock.release 'my_lock', owner: 'spence').to be_a Mongo::Lock
|
12
|
+
end
|
13
|
+
|
14
|
+
it "calls #release to release the lock" do
|
15
|
+
expect_any_instance_of(Mongo::Lock).to receive(:release)
|
16
|
+
Mongo::Lock.release 'my_lock', owner: 'spence'
|
17
|
+
end
|
18
|
+
|
19
|
+
context "when options are provided" do
|
20
|
+
|
21
|
+
it "passes them to the new lock" do
|
22
|
+
l = Mongo::Lock.release 'my_lock', owner: 'spence'
|
23
|
+
# expect(l.configuration.owner).to eql 'spence'
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#release' do
|
31
|
+
|
32
|
+
context "when lock is acquired" do
|
33
|
+
|
34
|
+
before :each do
|
35
|
+
my_collection.insert key: 'my_lock', owner: 'spence'
|
36
|
+
end
|
37
|
+
|
38
|
+
let(:lock) { Mongo::Lock.acquire 'my_lock', owner: 'spence' }
|
39
|
+
|
40
|
+
it "releases the lock" do
|
41
|
+
lock.release
|
42
|
+
expect(my_collection.find(key: 'my_lock', owner: 'spence').count).to be 0
|
43
|
+
end
|
44
|
+
|
45
|
+
it "returns true" do
|
46
|
+
expect(lock.release).to be_true
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
context "when the lock isn't acquired" do
|
52
|
+
|
53
|
+
let(:lock) { Mongo::Lock.new 'my_lock', timeout_in: 0.01, frequency: 0.01 }
|
54
|
+
|
55
|
+
it "acquires the lock first" do
|
56
|
+
expect(lock).to receive(:acquire).and_call_original
|
57
|
+
lock.release
|
58
|
+
end
|
59
|
+
|
60
|
+
it "returns true" do
|
61
|
+
expect(lock.release).to be_true
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
context "when the lock isn't acquired and cant be" do
|
67
|
+
|
68
|
+
let(:lock) { Mongo::Lock.new 'my_lock', timeout_in: 1, frequency: 0.01 }
|
69
|
+
|
70
|
+
it "returns false" do
|
71
|
+
my_collection.insert key: 'my_lock', owner: 'tobie', expires_at: 1.seconds.from_now
|
72
|
+
expect(lock.release timeout_in: 0.01).to be_false
|
73
|
+
end
|
74
|
+
|
75
|
+
it "doesn't release the lock" do
|
76
|
+
my_collection.insert key: 'my_lock', owner: 'tobie', expires_at: 1.seconds.from_now
|
77
|
+
lock.release timeout_in: 0.01
|
78
|
+
expect(my_collection.find(key: 'my_lock', owner: 'tobie').count).to be 1
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
context "when the lock was acquired but has since expired" do
|
84
|
+
|
85
|
+
it "returns true" do
|
86
|
+
lock
|
87
|
+
sleep 0.2
|
88
|
+
expect(lock.release).to be_true
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
context "when the lock was acquired but has already been released" do
|
94
|
+
|
95
|
+
it "returns true" do
|
96
|
+
lock.release
|
97
|
+
expect(lock.release).to be_true
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
|
102
|
+
context "when the lock is already acquired but by the same owner in a different instance" do
|
103
|
+
|
104
|
+
let (:different_instance) { Mongo::Lock.release 'my_lock', owner: 'spence' }
|
105
|
+
|
106
|
+
it "releases the lock" do
|
107
|
+
lock
|
108
|
+
different_instance.release
|
109
|
+
expect(my_collection.find(key: 'my_lock', owner: 'spence').count).to be 0
|
110
|
+
end
|
111
|
+
|
112
|
+
it "returns true" do
|
113
|
+
lock
|
114
|
+
expect(different_instance.release).to be_true
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
context "when the raise option is set to true" do
|
120
|
+
|
121
|
+
let(:lock) { Mongo::Lock.new 'my_lock', should_raise: true, timeout_in: 0.1, frequency: 0.01 }
|
122
|
+
|
123
|
+
context "when the lock isn't acquired and cant be" do
|
124
|
+
|
125
|
+
it "raises Mongo::Lock::NotReleasedError" do
|
126
|
+
my_collection.insert key: 'my_lock', owner: 'tobie', expires_at: 1.seconds.from_now
|
127
|
+
expect{ lock.release }.to raise_error Mongo::Lock::NotReleasedError
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
|
134
|
+
context "when options are provided" do
|
135
|
+
|
136
|
+
it "they override the defaults" do
|
137
|
+
my_collection.insert key: 'my_lock', owner: 'tobie', expires_at: 1.seconds.from_now
|
138
|
+
expect(lock.release owner: 'tobie').to be_true
|
139
|
+
expect(my_collection.find(key: 'my_lock', owner: 'tobie').count).to be 0
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
143
|
+
|
144
|
+
end
|
145
|
+
|
146
|
+
describe '.release!' do
|
147
|
+
|
148
|
+
it "calls .release with raise errors option set to true" do
|
149
|
+
expect(Mongo::Lock).to receive(:init_and_send).with('my_lock', { owner: 'tobie' }, :release!)
|
150
|
+
Mongo::Lock.release! 'my_lock', owner: 'tobie'
|
151
|
+
end
|
152
|
+
|
153
|
+
end
|
154
|
+
|
155
|
+
describe '#release!' do
|
156
|
+
|
157
|
+
it "calls .release with raise errors option set to true" do
|
158
|
+
expect(lock).to receive(:release).with({ limit: 3, should_raise: true })
|
159
|
+
lock.release! limit: 3
|
160
|
+
end
|
161
|
+
|
162
|
+
end
|
163
|
+
|
164
|
+
end
|
165
|
+
|
166
|
+
end
|
data/spec/initialise_spec.rb
CHANGED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'mongo-lock/drivers/mongo'
|
3
|
+
|
4
|
+
describe Mongo::Lock::Drivers::Mongo do
|
5
|
+
|
6
|
+
let(:collection1) { my_collection }
|
7
|
+
let(:collection2) { other_collection }
|
8
|
+
let(:collection3) { another_collection }
|
9
|
+
|
10
|
+
configure_for_mongo
|
11
|
+
|
12
|
+
it_behaves_like "MongoLock driver that can aquire locks"
|
13
|
+
it_behaves_like "MongoLock driver that can find if a lock is acquired"
|
14
|
+
it_behaves_like "MongoLock driver that can find if a lock is available"
|
15
|
+
it_behaves_like "MongoLock driver that can clear expired locks"
|
16
|
+
it_behaves_like "MongoLock driver that can ensure indexes"
|
17
|
+
it_behaves_like "MongoLock driver that can find if a lock have expired"
|
18
|
+
it_behaves_like "MongoLock driver that can extend locks"
|
19
|
+
it_behaves_like "MongoLock driver that can release all locks"
|
20
|
+
it_behaves_like "MongoLock driver that can release locks"
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'mongo-lock/drivers/moped'
|
3
|
+
|
4
|
+
describe Mongo::Lock::Drivers::Moped do
|
5
|
+
|
6
|
+
let(:collection1) { my_moped_collection }
|
7
|
+
let(:collection2) { other_moped_collection }
|
8
|
+
let(:collection3) { another_moped_collection }
|
9
|
+
|
10
|
+
configure_for_moped
|
11
|
+
|
12
|
+
it_behaves_like "MongoLock driver that can aquire locks"
|
13
|
+
it_behaves_like "MongoLock driver that can find if a lock is acquired"
|
14
|
+
it_behaves_like "MongoLock driver that can find if a lock is available"
|
15
|
+
it_behaves_like "MongoLock driver that can clear expired locks"
|
16
|
+
it_behaves_like "MongoLock driver that can ensure indexes"
|
17
|
+
it_behaves_like "MongoLock driver that can find if a lock have expired"
|
18
|
+
it_behaves_like "MongoLock driver that can extend locks"
|
19
|
+
it_behaves_like "MongoLock driver that can release all locks"
|
20
|
+
it_behaves_like "MongoLock driver that can release locks"
|
21
|
+
|
22
|
+
end
|
data/spec/rake_spec.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -3,6 +3,7 @@ Coveralls.wear!
|
|
3
3
|
require 'rails'
|
4
4
|
require 'mongo-lock'
|
5
5
|
require 'mongo'
|
6
|
+
require 'moped'
|
6
7
|
require 'active_support/core_ext/numeric/time'
|
7
8
|
|
8
9
|
RSpec.configure do |config|
|
@@ -18,18 +19,12 @@ RSpec.configure do |config|
|
|
18
19
|
config.filter_run_excluding :wip => true
|
19
20
|
|
20
21
|
Dir[File.expand_path("../support/**/*.rb", __FILE__)].each {|f| require f }
|
22
|
+
Dir[File.expand_path("../examples/**/*.rb", __FILE__)].each {|f| require f }
|
21
23
|
|
22
24
|
require 'pry'
|
23
25
|
|
24
26
|
include MongoHelper
|
25
27
|
|
26
|
-
config.before :each do
|
27
|
-
database.drop_collection("locks")
|
28
|
-
database.drop_collection("other_locks")
|
29
|
-
database.drop_collection("another_locks")
|
30
|
-
Mongo::Lock.configure collection: my_collection
|
31
|
-
end
|
32
|
-
|
33
28
|
config.after :each do
|
34
29
|
end
|
35
30
|
|
@@ -1,5 +1,15 @@
|
|
1
1
|
module MongoHelper
|
2
2
|
|
3
|
+
def configure_for_mongo
|
4
|
+
before :each do
|
5
|
+
Mongo::Lock.configure collections: {}
|
6
|
+
database.drop_collection("locks")
|
7
|
+
database.drop_collection("other_locks")
|
8
|
+
database.drop_collection("another_locks")
|
9
|
+
Mongo::Lock.configure collection: my_collection
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
3
13
|
def connection
|
4
14
|
@connection ||= Mongo::Connection.new("localhost")
|
5
15
|
end
|
@@ -20,4 +30,35 @@ module MongoHelper
|
|
20
30
|
@another_collection ||= database.collection :another_locks
|
21
31
|
end
|
22
32
|
|
33
|
+
def configure_for_moped
|
34
|
+
before :each do
|
35
|
+
Mongo::Lock.configure collections: {}
|
36
|
+
database.drop_collection("locks")
|
37
|
+
database.drop_collection("other_locks")
|
38
|
+
database.drop_collection("another_locks")
|
39
|
+
Mongo::Lock.configure collection: my_moped_collection
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def moped_connection
|
44
|
+
@moped_connection ||= Moped::Session.new([ "127.0.0.1:27017" ])
|
45
|
+
end
|
46
|
+
|
47
|
+
def moped_database
|
48
|
+
moped_connection.use "mongo_lock_tests"
|
49
|
+
moped_connection
|
50
|
+
end
|
51
|
+
|
52
|
+
def my_moped_collection
|
53
|
+
@my_moped_collection ||= moped_database[:locks]
|
54
|
+
end
|
55
|
+
|
56
|
+
def other_moped_collection
|
57
|
+
@other_moped_collection ||= moped_database[:other_locks]
|
58
|
+
end
|
59
|
+
|
60
|
+
def another_moped_collection
|
61
|
+
@another_moped_collection ||= moped_database[:another_locks]
|
62
|
+
end
|
63
|
+
|
23
64
|
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.
|
4
|
+
version: 1.2.0
|
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-04-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongo
|
@@ -17,7 +17,21 @@ dependencies:
|
|
17
17
|
- - ! '>='
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
|
-
type: :
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: moped
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
21
35
|
prerelease: false
|
22
36
|
version_requirements: !ruby/object:Gem::Requirement
|
23
37
|
requirements:
|
@@ -95,7 +109,7 @@ dependencies:
|
|
95
109
|
- !ruby/object:Gem::Version
|
96
110
|
version: '0'
|
97
111
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
112
|
+
name: bson_ext
|
99
113
|
requirement: !ruby/object:Gem::Requirement
|
100
114
|
requirements:
|
101
115
|
- - ! '>='
|
@@ -108,6 +122,20 @@ dependencies:
|
|
108
122
|
- - ! '>='
|
109
123
|
- !ruby/object:Gem::Version
|
110
124
|
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rails
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ~>
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 4.0.0
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ~>
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 4.0.0
|
111
139
|
description: Key based pessimistic locking for Ruby and MongoDB. Is this key avaliable?
|
112
140
|
Yes - Lock it for me for a sec will you. No - OK I'll just wait here until its ready.
|
113
141
|
email: msaspence@gmail.com
|
@@ -127,24 +155,29 @@ files:
|
|
127
155
|
- lib/mongo-lock.rb
|
128
156
|
- lib/mongo-lock/class_convenience_methods.rb
|
129
157
|
- lib/mongo-lock/configuration.rb
|
130
|
-
- lib/mongo-lock/
|
158
|
+
- lib/mongo-lock/drivers/base.rb
|
159
|
+
- lib/mongo-lock/drivers/mongo.rb
|
160
|
+
- lib/mongo-lock/drivers/moped.rb
|
131
161
|
- lib/mongo-lock/railtie.rb
|
132
162
|
- lib/mongo-lock/railties/mongo.rake
|
163
|
+
- lib/mongo-lock/send_with_raise_methods.rb
|
133
164
|
- lib/mongo-lock/version.rb
|
134
165
|
- mongo-lock.gemspec
|
135
|
-
- spec/acquire_spec.rb
|
136
|
-
- spec/acquired_spec.rb
|
137
|
-
- spec/available_spec.rb
|
138
|
-
- spec/clear_expired_spec.rb
|
139
166
|
- spec/configuration_spec.rb
|
140
167
|
- spec/configure_spec.rb
|
141
|
-
- spec/
|
142
|
-
- spec/
|
143
|
-
- spec/
|
168
|
+
- spec/examples/acquire_example.rb
|
169
|
+
- spec/examples/acquired_example.rb
|
170
|
+
- spec/examples/available_example.rb
|
171
|
+
- spec/examples/clear_expired_example.rb
|
172
|
+
- spec/examples/ensure_indexes_example.rb
|
173
|
+
- spec/examples/expired_example.rb
|
174
|
+
- spec/examples/extend_by_example.rb
|
175
|
+
- spec/examples/release_all_example.rb
|
176
|
+
- spec/examples/release_example.rb
|
144
177
|
- spec/initialise_spec.rb
|
178
|
+
- spec/mongo_driver_spec.rb
|
179
|
+
- spec/moped_driver_spec.rb
|
145
180
|
- spec/rake_spec.rb
|
146
|
-
- spec/release_all_spec.rb
|
147
|
-
- spec/release_spec.rb
|
148
181
|
- spec/spec_helper.rb
|
149
182
|
- spec/support/mongo_helper.rb
|
150
183
|
homepage: https://github.com/trakio/mongo-lock
|
@@ -172,18 +205,20 @@ signing_key:
|
|
172
205
|
specification_version: 4
|
173
206
|
summary: Pessimistic locking for Ruby and MongoDB
|
174
207
|
test_files:
|
175
|
-
- spec/acquire_spec.rb
|
176
|
-
- spec/acquired_spec.rb
|
177
|
-
- spec/available_spec.rb
|
178
|
-
- spec/clear_expired_spec.rb
|
179
208
|
- spec/configuration_spec.rb
|
180
209
|
- spec/configure_spec.rb
|
181
|
-
- spec/
|
182
|
-
- spec/
|
183
|
-
- spec/
|
210
|
+
- spec/examples/acquire_example.rb
|
211
|
+
- spec/examples/acquired_example.rb
|
212
|
+
- spec/examples/available_example.rb
|
213
|
+
- spec/examples/clear_expired_example.rb
|
214
|
+
- spec/examples/ensure_indexes_example.rb
|
215
|
+
- spec/examples/expired_example.rb
|
216
|
+
- spec/examples/extend_by_example.rb
|
217
|
+
- spec/examples/release_all_example.rb
|
218
|
+
- spec/examples/release_example.rb
|
184
219
|
- spec/initialise_spec.rb
|
220
|
+
- spec/mongo_driver_spec.rb
|
221
|
+
- spec/moped_driver_spec.rb
|
185
222
|
- spec/rake_spec.rb
|
186
|
-
- spec/release_all_spec.rb
|
187
|
-
- spec/release_spec.rb
|
188
223
|
- spec/spec_helper.rb
|
189
224
|
- spec/support/mongo_helper.rb
|