redis-em-mutex 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,34 +7,34 @@ require 'redis-em-mutex'
7
7
  describe Redis::EM::Mutex do
8
8
 
9
9
  it "should lock and prevent locking on the same semaphore" do
10
- described_class.new(@lock_names.first).owned?.should be_false
10
+ described_class.new(@lock_names.first).owned?.should be false
11
11
  mutex = described_class.lock(@lock_names.first)
12
12
  mutex.names.should eq [@lock_names.first]
13
- mutex.locked?.should be_true
14
- mutex.owned?.should be_true
13
+ mutex.locked?.should be true
14
+ mutex.owned?.should be true
15
15
  mutex.should be_an_instance_of described_class
16
- described_class.new(@lock_names.first).try_lock.should be_false
16
+ described_class.new(@lock_names.first).try_lock.should be false
17
17
  expect {
18
18
  mutex.lock
19
19
  }.to raise_error(Redis::EM::Mutex::MutexError, /deadlock; recursive locking/)
20
20
  mutex.unlock.should be_an_instance_of described_class
21
- mutex.locked?.should be_false
22
- mutex.owned?.should be_false
23
- mutex.try_lock.should be_true
21
+ mutex.locked?.should be false
22
+ mutex.owned?.should be false
23
+ mutex.try_lock.should be true
24
24
  mutex.unlock if mutex
25
25
  end
26
26
 
27
27
  it "should lock and prevent locking on the same multiple semaphores" do
28
28
  mutex = described_class.lock(*@lock_names)
29
29
  mutex.names.should eq @lock_names
30
- mutex.locked?.should be_true
31
- mutex.owned?.should be_true
30
+ mutex.locked?.should be true
31
+ mutex.owned?.should be true
32
32
  mutex.should be_an_instance_of described_class
33
- described_class.new(*@lock_names).try_lock.should be_false
33
+ described_class.new(*@lock_names).try_lock.should be false
34
34
  @lock_names.each do |name|
35
- described_class.new(name).try_lock.should be_false
35
+ described_class.new(name).try_lock.should be false
36
36
  end
37
- mutex.try_lock.should be_false
37
+ mutex.try_lock.should be false
38
38
  expect {
39
39
  mutex.lock
40
40
  }.to raise_error(Redis::EM::Mutex::MutexError, /deadlock; recursive locking/)
@@ -44,25 +44,25 @@ describe Redis::EM::Mutex do
44
44
  }.to raise_error(Redis::EM::Mutex::MutexError, /deadlock; recursive locking/)
45
45
  end
46
46
  mutex.unlock.should be_an_instance_of described_class
47
- mutex.locked?.should be_false
48
- mutex.owned?.should be_false
49
- mutex.try_lock.should be_true
47
+ mutex.locked?.should be false
48
+ mutex.owned?.should be false
49
+ mutex.try_lock.should be true
50
50
  mutex.unlock if mutex
51
51
  end
52
52
 
53
53
  it "should lock and prevent other fibers to lock on the same semaphore" do
54
54
  mutex = described_class.lock(@lock_names.first)
55
55
  mutex.should be_an_instance_of described_class
56
- mutex.owned?.should be_true
56
+ mutex.owned?.should be true
57
57
  locked = true
58
58
  ::EM::Synchrony.next_tick do
59
59
  begin
60
- mutex.try_lock.should be_false
61
- mutex.owned?.should be_false
60
+ mutex.try_lock.should be false
61
+ mutex.owned?.should be false
62
62
  start = Time.now
63
63
  mutex.synchronize do
64
64
  (Time.now - start).should be_within(0.015).of(0.26)
65
- locked.should be_false
65
+ locked.should be false
66
66
  locked = nil
67
67
  end
68
68
  rescue Exception => e
@@ -71,9 +71,9 @@ describe Redis::EM::Mutex do
71
71
  end
72
72
  ::EM::Synchrony.sleep 0.25
73
73
  locked = false
74
- mutex.owned?.should be_true
74
+ mutex.owned?.should be true
75
75
  mutex.unlock.should be_an_instance_of described_class
76
- mutex.owned?.should be_false
76
+ mutex.owned?.should be false
77
77
  ::EM::Synchrony.sleep 0.2
78
78
  locked.should be_nil
79
79
  end
@@ -81,25 +81,25 @@ describe Redis::EM::Mutex do
81
81
  it "should lock and prevent other fibers to lock on the same multiple semaphores" do
82
82
  mutex = described_class.lock(*@lock_names)
83
83
  mutex.should be_an_instance_of described_class
84
- mutex.owned?.should be_true
84
+ mutex.owned?.should be true
85
85
  locked = true
86
86
  ::EM::Synchrony.next_tick do
87
87
  begin
88
- locked.should be_true
89
- mutex.try_lock.should be_false
90
- mutex.owned?.should be_false
88
+ locked.should be true
89
+ mutex.try_lock.should be false
90
+ mutex.owned?.should be false
91
91
  start = Time.now
92
92
  mutex.synchronize do
93
- mutex.owned?.should be_true
93
+ mutex.owned?.should be true
94
94
  (Time.now - start).should be_within(0.015).of(0.26)
95
- locked.should be_false
95
+ locked.should be false
96
96
  end
97
- mutex.owned?.should be_false
97
+ mutex.owned?.should be false
98
98
  ::EM::Synchrony.sleep 0.1
99
99
  start = Time.now
100
100
  ::EM::Synchrony::FiberIterator.new(@lock_names, @lock_names.length).each do |name|
101
101
  begin
102
- locked.should be_true
102
+ locked.should be true
103
103
  described_class.new(name).synchronize do
104
104
  (Time.now - start).should be_within(0.015).of(0.26)
105
105
  locked.should be_an_instance_of Fixnum
@@ -115,13 +115,13 @@ describe Redis::EM::Mutex do
115
115
  end
116
116
  ::EM::Synchrony.sleep 0.25
117
117
  locked = false
118
- mutex.owned?.should be_true
118
+ mutex.owned?.should be true
119
119
  mutex.unlock.should be_an_instance_of described_class
120
- mutex.owned?.should be_false
120
+ mutex.owned?.should be false
121
121
  ::EM::Synchrony.sleep 0.1
122
122
 
123
123
  locked = true
124
- mutex.lock.should be_true
124
+ mutex.lock.should be true
125
125
  ::EM::Synchrony.sleep 0.25
126
126
  locked = 10
127
127
  mutex.unlock.should be_an_instance_of described_class
@@ -132,15 +132,15 @@ describe Redis::EM::Mutex do
132
132
  it "should lock and prevent other fibers to lock on the same semaphore with block timeout" do
133
133
  mutex = described_class.lock(*@lock_names)
134
134
  mutex.should be_an_instance_of described_class
135
- mutex.owned?.should be_true
135
+ mutex.owned?.should be true
136
136
  locked = true
137
137
  ::EM::Synchrony.next_tick do
138
138
  begin
139
139
  start = Time.now
140
- mutex.lock(0.25).should be_false
141
- mutex.owned?.should be_false
140
+ mutex.lock(0.25).should be false
141
+ mutex.owned?.should be false
142
142
  (Time.now - start).should be_within(0.01).of(0.26)
143
- locked.should be_true
143
+ locked.should be true
144
144
  locked = nil
145
145
  rescue Exception => e
146
146
  @exception = e
@@ -149,34 +149,37 @@ describe Redis::EM::Mutex do
149
149
  ::EM::Synchrony.sleep 0.26
150
150
  locked.should be_nil
151
151
  locked = false
152
- mutex.locked?.should be_true
153
- mutex.owned?.should be_true
152
+ mutex.locked?.should be true
153
+ mutex.owned?.should be true
154
154
  mutex.unlock.should be_an_instance_of described_class
155
- mutex.locked?.should be_false
156
- mutex.owned?.should be_false
155
+ mutex.locked?.should be false
156
+ mutex.owned?.should be false
157
157
  end
158
158
 
159
159
  it "should lock and expire while other fiber lock on the same semaphore with block timeout" do
160
+ expect {
161
+ described_class.new(*@lock_names, expire: -1)
162
+ }.to raise_error(ArgumentError, /expire_timeout value must be greater than 0/)
160
163
  mutex = described_class.lock(*@lock_names, expire: 0.2499999)
161
164
  mutex.expire_timeout.should eq 0.2499999
162
165
  mutex.should be_an_instance_of described_class
163
- mutex.owned?.should be_true
166
+ mutex.owned?.should be true
164
167
  locked = true
165
168
  ::EM::Synchrony.next_tick do
166
169
  begin
167
- mutex.owned?.should be_false
170
+ mutex.owned?.should be false
168
171
  start = Time.now
169
- mutex.lock(0.25).should be_true
172
+ mutex.lock(0.25).should be true
170
173
  (Time.now - start).should be_within(0.011).of(0.26)
171
- locked.should be_true
174
+ locked.should be true
172
175
  locked = nil
173
- mutex.locked?.should be_true
174
- mutex.owned?.should be_true
176
+ mutex.locked?.should be true
177
+ mutex.owned?.should be true
175
178
  ::EM::Synchrony.sleep 0.2
176
- locked.should be_false
179
+ locked.should be false
177
180
  mutex.unlock.should be_an_instance_of described_class
178
- mutex.owned?.should be_false
179
- mutex.locked?.should be_false
181
+ mutex.owned?.should be false
182
+ mutex.locked?.should be false
180
183
  rescue Exception => e
181
184
  @exception = e
182
185
  end
@@ -184,45 +187,45 @@ describe Redis::EM::Mutex do
184
187
  ::EM::Synchrony.sleep 0.26
185
188
  locked.should be_nil
186
189
  locked = false
187
- mutex.locked?.should be_true
188
- mutex.owned?.should be_false
190
+ mutex.locked?.should be true
191
+ mutex.owned?.should be false
189
192
  mutex.unlock.should be_an_instance_of described_class
190
- mutex.locked?.should be_true
191
- mutex.owned?.should be_false
193
+ mutex.locked?.should be true
194
+ mutex.owned?.should be false
192
195
  ::EM::Synchrony.sleep 0.5
193
196
  end
194
197
 
195
198
  it "should lock and prevent (with refresh) other fibers to lock on the same semaphore with block timeout" do
196
199
  mutex = described_class.lock(*@lock_names, expire: 0.11)
197
200
  mutex.should be_an_instance_of described_class
198
- mutex.owned?.should be_true
201
+ mutex.owned?.should be true
199
202
  locked = true
200
203
  ::EM::Synchrony.next_tick do
201
204
  begin
202
205
  start = Time.now
203
- mutex.lock(0.3).should be_false
204
- mutex.owned?.should be_false
206
+ mutex.lock(0.3).should be false
207
+ mutex.owned?.should be false
205
208
  (Time.now - start).should be_within(0.01).of(0.31)
206
- locked.should be_true
209
+ locked.should be true
207
210
  locked = nil
208
211
  rescue Exception => e
209
212
  @exception = e
210
213
  end
211
214
  end
212
215
  ::EM::Synchrony.sleep 0.08
213
- mutex.owned?.should be_true
214
- mutex.refresh
216
+ mutex.owned?.should be true
217
+ mutex.refresh.should be true
215
218
  ::EM::Synchrony.sleep 0.08
216
- mutex.owned?.should be_true
217
- mutex.refresh(0.5)
219
+ mutex.owned?.should be true
220
+ mutex.refresh(0.5).should be true
218
221
  ::EM::Synchrony.sleep 0.15
219
222
  locked.should be_nil
220
223
  locked = false
221
- mutex.locked?.should be_true
222
- mutex.owned?.should be_true
224
+ mutex.locked?.should be true
225
+ mutex.owned?.should be true
223
226
  mutex.unlock.should be_an_instance_of described_class
224
- mutex.locked?.should be_false
225
- mutex.owned?.should be_false
227
+ mutex.locked?.should be false
228
+ mutex.owned?.should be false
226
229
  end
227
230
 
228
231
  it "should lock some resource and play with it safely" do
@@ -233,23 +236,23 @@ describe Redis::EM::Mutex do
233
236
  begin
234
237
  was_locked = false
235
238
  redis = Redis.new @redis_options
236
- mutex.owned?.should be_false
239
+ mutex.owned?.should be false
237
240
  mutex.synchronize do
238
- mutex.owned?.should be_true
241
+ mutex.owned?.should be true
239
242
  was_locked = true
240
- redis.setnx(play_name, i).should be_true
243
+ redis.setnx(play_name, i).should be true
241
244
  ::EM::Synchrony.sleep 0.1
242
245
  redis.get(play_name).should eq i.to_s
243
246
  redis.del(play_name).should eq 1
244
247
  end
245
- was_locked.should be_true
246
- mutex.owned?.should be_false
248
+ was_locked.should be true
249
+ mutex.owned?.should be false
247
250
  result << i
248
251
  rescue Exception => e
249
252
  @exception = e
250
253
  end
251
254
  end
252
- mutex.locked?.should be_false
255
+ mutex.locked?.should be false
253
256
  result.sort.should eq (0..9).to_a
254
257
  end
255
258
 
@@ -266,16 +269,16 @@ describe Redis::EM::Mutex do
266
269
  (Time.now - time).should be < 0.0009
267
270
  was_locked = true
268
271
  end
269
- was_locked.should be_true
272
+ was_locked.should be true
270
273
  rescue Exception => e
271
274
  @exception = e
272
275
  end
273
276
  end
274
277
  EM::Synchrony.sleep 0.1
275
- mutex.owned?.should be_true
278
+ mutex.owned?.should be true
276
279
  mutex.unlock.should be_an_instance_of described_class
277
280
  time = Time.now
278
- mutex.owned?.should be_false
281
+ mutex.owned?.should be false
279
282
  EM::Synchrony.sleep 0.1
280
283
  end
281
284
 
@@ -301,10 +304,10 @@ describe Redis::EM::Mutex do
301
304
  end.resume
302
305
  end
303
306
  EM::Synchrony.sleep 0.25
304
- mutex.owned?.should be_true
307
+ mutex.owned?.should be true
305
308
  mutex.unlock.should be_an_instance_of described_class
306
309
  time = Time.now.to_f
307
- mutex.owned?.should be_false
310
+ mutex.owned?.should be false
308
311
  EM::Synchrony.sleep 0.25
309
312
  redis = Redis.new @redis_options
310
313
  t1, t2 = redis.mget(time_key1, time_key2)
@@ -316,6 +319,73 @@ describe Redis::EM::Mutex do
316
319
  redis.del(time_key1, time_key2)
317
320
  end
318
321
 
322
+ it "should lock and provide current expiration timeout correctly" do
323
+ begin
324
+ mutex = described_class.new(@lock_names.first)
325
+ mutex.unlock!.should be false
326
+ mutex.expired?.should be_nil
327
+ mutex.locked?.should be false
328
+ mutex.owned?.should be false
329
+ mutex.expires_in.should be_nil
330
+ mutex.expires_at.should be_nil
331
+ mutex.expiration_timestamp.should be_nil
332
+ mutex.expire_timeout.should eq described_class.default_expire
333
+ mutex.expire_timeout = 0.5
334
+ mutex.expire_timeout.should eq 0.5
335
+ start = Time.now
336
+ mutex.lock
337
+ mutex.expired?.should be false
338
+ now = Time.now
339
+ mutex.expires_in.should be_within(0.01).of(mutex.expire_timeout - (now - start))
340
+ mutex.expiration_timestamp.should be_within(0.01).of(start.to_f + mutex.expire_timeout)
341
+ mutex.expires_at.should eq Time.at(mutex.expiration_timestamp)
342
+ mutex.locked?.should be true
343
+ mutex.owned?.should be true
344
+ mutex.unlock!.should be_an_instance_of described_class
345
+ mutex.locked?.should be false
346
+ mutex.owned?.should be false
347
+ mutex.unlock.should be_an_instance_of described_class
348
+ mutex.unlock!.should be false
349
+ mutex.expired?.should be_nil
350
+ mutex.expires_in.should be_nil
351
+ mutex.expires_at.should be_nil
352
+ mutex.expiration_timestamp.should be_nil
353
+ start = Time.now
354
+ mutex.lock
355
+ now = Time.now
356
+ mutex.expires_in.should be_within(0.01).of(mutex.expire_timeout - (now - start))
357
+ EM::Synchrony.sleep mutex.expires_in + 0.001
358
+ mutex.expired?.should be true
359
+ mutex.expires_in.should be < 0
360
+ mutex.expiration_timestamp.should be < Time.now.to_f
361
+ mutex.expires_at.should eq Time.at(mutex.expiration_timestamp)
362
+ mutex.expires_at.should be < Time.now
363
+ start = Time.now
364
+ mutex.refresh.should be true
365
+ now = Time.now
366
+ mutex.expires_in.should be_within(0.01).of(mutex.expire_timeout - (now - start))
367
+ EM::Synchrony.sleep mutex.expires_in + 0.001
368
+ mutex.expired?.should be true
369
+ mutex.expires_in.should be < 0
370
+ mutex.expiration_timestamp.should be < Time.now.to_f
371
+ mutex.expires_at.should eq Time.at(mutex.expiration_timestamp)
372
+ mutex.expires_at.should be < Time.now
373
+ fiber = Fiber.current
374
+ EM::Synchrony.next_tick {
375
+ described_class.lock(@lock_names.first).unlock
376
+ fiber.resume
377
+ }
378
+ Fiber.yield
379
+ mutex.refresh.should be false
380
+ mutex.unlock!.should be false
381
+ mutex.refresh.should be false
382
+ mutex.unlock!.should be false
383
+ mutex.unlock.should be_an_instance_of described_class
384
+ ensure
385
+ mutex.unlock if mutex
386
+ end
387
+ end
388
+
319
389
  around(:each) do |testcase|
320
390
  @after_em_stop = nil
321
391
  @exception = nil
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redis-em-mutex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-13 00:00:00.000000000 Z
12
+ date: 2012-09-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis
16
- requirement: &90657860 !ruby/object:Gem::Requirement
16
+ requirement: &133617360 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.0.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *90657860
24
+ version_requirements: *133617360
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: hiredis
27
- requirement: &90657060 !ruby/object:Gem::Requirement
27
+ requirement: &133616520 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.4.5
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *90657060
35
+ version_requirements: *133616520
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: eventmachine
38
- requirement: &90656280 !ruby/object:Gem::Requirement
38
+ requirement: &133615240 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.0.0.beta.1
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *90656280
46
+ version_requirements: *133615240
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &90655560 !ruby/object:Gem::Requirement
49
+ requirement: &133614140 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 2.8.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *90655560
57
+ version_requirements: *133614140
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: eventmachine
60
- requirement: &90654360 !ruby/object:Gem::Requirement
60
+ requirement: &133629580 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 1.0.0
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *90654360
68
+ version_requirements: *133629580
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: em-synchrony
71
- requirement: &90652800 !ruby/object:Gem::Requirement
71
+ requirement: &133629000 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: 1.0.0
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *90652800
79
+ version_requirements: *133629000
80
80
  description: Cross server-process-fiber EventMachine + Redis based semaphore with
81
81
  many features
82
82
  email: rafal@yeondir.com
@@ -91,11 +91,14 @@ files:
91
91
  - lib/redis-em-mutex.rb
92
92
  - lib/redis/em-mutex.rb
93
93
  - lib/redis/em-mutex/macro.rb
94
+ - lib/redis/em-mutex/ns.rb
94
95
  - lib/redis/em-mutex/version.rb
95
96
  - redis-em-mutex.gemspec
97
+ - spec/redis-em-mutex-condition.rb
96
98
  - spec/redis-em-mutex-features.rb
97
99
  - spec/redis-em-mutex-macros.rb
98
100
  - spec/redis-em-mutex-namespaces.rb
101
+ - spec/redis-em-mutex-owners.rb
99
102
  - spec/redis-em-mutex-semaphores.rb
100
103
  homepage: http://github.com/royaltm/redis-em-mutex
101
104
  licenses: []
@@ -127,6 +130,8 @@ signing_key:
127
130
  specification_version: 3
128
131
  summary: Cross server-process-fiber EventMachine + Redis based semaphore
129
132
  test_files:
133
+ - spec/redis-em-mutex-owners.rb
134
+ - spec/redis-em-mutex-condition.rb
130
135
  - spec/redis-em-mutex-namespaces.rb
131
136
  - spec/redis-em-mutex-semaphores.rb
132
137
  - spec/redis-em-mutex-features.rb