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.
Files changed (39) hide show
  1. checksums.yaml +8 -8
  2. data/Gemfile.lock +41 -38
  3. data/README.md +7 -4
  4. data/lib/mongo-lock.rb +22 -37
  5. data/lib/mongo-lock/configuration.rb +38 -4
  6. data/lib/mongo-lock/drivers/base.rb +41 -0
  7. data/lib/mongo-lock/drivers/mongo.rb +99 -0
  8. data/lib/mongo-lock/drivers/moped.rb +62 -0
  9. data/lib/mongo-lock/send_with_raise_methods.rb +28 -0
  10. data/lib/mongo-lock/version.rb +1 -1
  11. data/mongo-lock.gemspec +4 -3
  12. data/spec/configuration_spec.rb +66 -0
  13. data/spec/configure_spec.rb +8 -2
  14. data/spec/examples/acquire_example.rb +219 -0
  15. data/spec/examples/acquired_example.rb +54 -0
  16. data/spec/examples/available_example.rb +70 -0
  17. data/spec/examples/clear_expired_example.rb +100 -0
  18. data/spec/examples/ensure_indexes_example.rb +38 -0
  19. data/spec/examples/expired_example.rb +41 -0
  20. data/spec/examples/extend_by_example.rb +137 -0
  21. data/spec/examples/release_all_example.rb +117 -0
  22. data/spec/examples/release_example.rb +166 -0
  23. data/spec/initialise_spec.rb +2 -0
  24. data/spec/mongo_driver_spec.rb +22 -0
  25. data/spec/moped_driver_spec.rb +22 -0
  26. data/spec/rake_spec.rb +1 -1
  27. data/spec/spec_helper.rb +2 -7
  28. data/spec/support/mongo_helper.rb +41 -0
  29. metadata +58 -23
  30. data/lib/mongo-lock/mongo_queries.rb +0 -97
  31. data/spec/acquire_spec.rb +0 -217
  32. data/spec/acquired_spec.rb +0 -53
  33. data/spec/available_spec.rb +0 -68
  34. data/spec/clear_expired_spec.rb +0 -98
  35. data/spec/ensure_indexes_spec.rb +0 -34
  36. data/spec/expired_spec.rb +0 -39
  37. data/spec/extend_by_spec.rb +0 -135
  38. data/spec/release_all_spec.rb +0 -115
  39. data/spec/release_spec.rb +0 -164
@@ -1,34 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Mongo::Lock do
4
-
5
- describe '.ensure_indexes' do
6
-
7
- context "when provided with a MongoDB collection" do
8
-
9
- before :each do
10
- Mongo::Lock.ensure_indexes
11
- end
12
-
13
- it "should build ttl index for each collection" do
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
- end
16
-
17
- it "should build an index on key and expires_at for each collection" do
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
28
- end
29
-
30
- end
31
-
32
- end
33
-
34
- end
@@ -1,39 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Mongo::Lock do
4
-
5
- describe '#expired?' do
6
-
7
- let(:lock) { Mongo::Lock.new 'my_lock', owner: 'spence', timeout_in: 0.01, frequency: 0.01 }
8
-
9
- context "when the lock has not been acquired" do
10
-
11
- it "returns false" do
12
- sleep 0.02
13
- expect(lock.expired?).to be_false
14
- end
15
-
16
- end
17
-
18
- context "when the lock has expired" do
19
-
20
- it "returns true" do
21
- lock.acquire expire_in: 0.01
22
- sleep 0.02
23
- expect(lock.expired?).to be_true
24
- end
25
-
26
- end
27
-
28
- context "when the lock hasn't expired" do
29
-
30
- it "returns false" do
31
- lock.acquire expire_in: 0.1
32
- expect(lock.expired?).to be_false
33
- end
34
-
35
- end
36
-
37
- end
38
-
39
- end
@@ -1,135 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Mongo::Lock do
4
-
5
- let(:lock) { Mongo::Lock.new 'my_lock', owner: 'spence', timeout_in: 0.01, frequency: 0.01, expire_in: 1 }
6
-
7
- describe '#extend_by' do
8
-
9
- context "when a time is provided" do
10
-
11
- it "extends the lock" do
12
- lock.acquire
13
- lock.extend_by 60
14
- expect(my_collection.find(owner: 'spence', key: 'my_lock').first['expires_at']).to be_within(1.second).of(60.seconds.from_now)
15
- end
16
-
17
- it "returns true" do
18
- lock.acquire
19
- expect(lock.extend_by 50).to be_true
20
- end
21
-
22
- end
23
-
24
- context "when the lock has expired" do
25
-
26
- let(:lock) { Mongo::Lock.new 'my_lock', owner: 'spence', timeout_in: 0.01, frequency: 0.01, expire_in: 0.11 }
27
-
28
- it "returns false" do
29
- lock.acquire
30
- sleep 0.11
31
- expect(lock.extend_by 10).to be_false
32
- end
33
-
34
- end
35
-
36
- context "when the lock has not been aquired yet" do
37
-
38
- it "returns false" do
39
- lock
40
- expect(lock.extend_by 10).to be_false
41
- end
42
-
43
- end
44
-
45
- context "when the lock has been released" do
46
-
47
- it "returns false" do
48
- lock.acquire
49
- lock.release
50
- expect(lock.extend_by 10).to be_false
51
- end
52
-
53
- end
54
-
55
- context "when the raise option is set to true" do
56
-
57
- let(:lock) { Mongo::Lock.new 'my_lock', owner: 'spence', timeout_in: 0.01, frequency: 0.01, expire_in: 0.01, raise: true }
58
-
59
- context "and the lock has expired" do
60
-
61
- it "raises a Mongo::Lock::NotExtendedError" do
62
- lock.acquire
63
- sleep 0.02
64
- expect{lock.extend_by 10}.to raise_error Mongo::Lock::NotExtendedError
65
- end
66
-
67
- end
68
-
69
- context "and the lock has not been aquired yet" do
70
-
71
- it "raises a Mongo::Lock::NotExtendedError" do
72
- lock
73
- expect{lock.extend_by 10}.to raise_error Mongo::Lock::NotExtendedError
74
- end
75
-
76
- end
77
-
78
- context "and the lock has been released" do
79
-
80
- it "raises a Mongo::Lock::NotExtendedError" do
81
- lock.acquire
82
- lock.release
83
- expect{lock.extend_by 10}.to raise_error Mongo::Lock::NotExtendedError
84
- end
85
-
86
- end
87
-
88
- end
89
-
90
- context "when options are provided" do
91
-
92
- let(:lock) { Mongo::Lock.new 'my_lock', owner: 'spence', timeout_in: 0.01, frequency: 0.01, raise: true }
93
-
94
- it "they override the defaults" do
95
- lock
96
- expect(lock.extend_by 10, raise: false).to be_false
97
- end
98
-
99
- end
100
-
101
- end
102
-
103
- describe '#extend' do
104
-
105
- it "calls #extend_by with the default expire_in config setting" do
106
- expect(lock).to receive(:extend_by).with(lock.configuration.expire_in, {})
107
- lock.extend
108
- end
109
-
110
- it "also passes options on" do
111
- expect(lock).to receive(:extend_by).with(lock.configuration.expire_in, { raise: true })
112
- lock.extend raise: true
113
- end
114
-
115
- end
116
-
117
- describe '.extend_by!' do
118
-
119
- it "calls .extend_by with raise errors option set to true" do
120
- expect(lock).to receive(:extend_by).with( 10, { raise: true })
121
- lock.extend_by! 10
122
- end
123
-
124
- end
125
-
126
- describe '#extend!' do
127
-
128
- it "calls .extend with raise errors option set to true" do
129
- expect(lock).to receive(:extend_by).with(lock.configuration.expire_in, { raise: true })
130
- lock.extend!
131
- end
132
-
133
- end
134
-
135
- end
@@ -1,115 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Mongo::Lock do
4
-
5
- describe '.release_all' do
6
-
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'
13
- my_collection.insert key: 'spences_lock', owner: 'spence'
14
- other_collection.insert key: 'spences_lock', owner: 'spence'
15
- another_collection.insert key: 'spences_lock', owner: 'spence'
16
- end
17
-
18
- it "removes all locks from the database" do
19
- Mongo::Lock.release_all
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
22
- expect(other_collection.count({ key: 'spences_lock', owner: 'spence'})).to eql 0
23
- end
24
-
25
- context "when an owner is provided" do
26
-
27
- before do
28
- Mongo::Lock.release_all owner: 'spence'
29
- end
30
-
31
- it "doesn't release locks belonging to other owners" do
32
- expect(my_collection.find({ key: 'tobies_lock', owner: 'tobie'}).count).to eql 1
33
- end
34
-
35
- it "does release locks belonging to that owner" do
36
- Mongo::Lock.release_all owner: 'spence'
37
- expect(my_collection.find({ key: 'spences_lock', owner: 'spence'}).count).to eql 0
38
- expect(other_collection.find({ key: 'spences_lock', owner: 'spence'}).count).to eql 0
39
- end
40
-
41
- end
42
-
43
- context "when a collection symbol is provided" do
44
-
45
- before do
46
- Mongo::Lock.release_all 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 a collection is provided" do
61
-
62
- before do
63
- Mongo::Lock.release_all collection: other_collection
64
- end
65
-
66
- it "does release locks in that collection" do
67
- expect(other_collection.find({ key: 'spences_lock', owner: 'spence'}).count).to eql 0
68
- end
69
-
70
- it "doesn't release locks in other collections" do
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
-
109
- end
110
-
111
- end
112
-
113
- end
114
-
115
- end
@@ -1,164 +0,0 @@
1
- require 'spec_helper'
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', 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, raise: true })
159
- lock.release! limit: 3
160
- end
161
-
162
- end
163
-
164
- end