mirror of
https://github.com/discourse/discourse.git
synced 2025-05-21 18:12:32 +08:00
DEV: Implement distributed mutex in lua (#16228)
The rationale behind this was mostly to stop using `redis.synchronize` (now removed in redis gem 4.6)
This commit is contained in:
@ -31,38 +31,28 @@ describe DistributedMutex do
|
||||
|
||||
Discourse.redis.setnx key, Time.now.to_i - 1
|
||||
|
||||
start = Time.now.to_i
|
||||
start = Time.now
|
||||
m.synchronize do
|
||||
"nop"
|
||||
end
|
||||
|
||||
# no longer than a second
|
||||
expect(Time.now.to_i).to be <= start + 1
|
||||
expect(Time.now).to be <= start + 1
|
||||
end
|
||||
|
||||
# expected: 1574200319
|
||||
# got: 1574200320
|
||||
#
|
||||
# (compared using ==)
|
||||
# ./spec/components/distributed_mutex_spec.rb:60:in `block (3 levels) in <main>'
|
||||
# ./lib/distributed_mutex.rb:33:in `block in synchronize'
|
||||
xit 'allows the validity of the lock to be configured' do
|
||||
freeze_time
|
||||
|
||||
it "allows the validity of the lock to be configured" do
|
||||
mutex = DistributedMutex.new(key, validity: 2)
|
||||
|
||||
mutex.synchronize do
|
||||
expect(Discourse.redis.ttl(key)).to eq(2)
|
||||
expect(Discourse.redis.get(key).to_i).to eq(Time.now.to_i + 2)
|
||||
expect(Discourse.redis.ttl(key)).to be <= 3
|
||||
expect(Discourse.redis.get(key).to_i).to be_within(1.second).of(Time.now.to_i + 2)
|
||||
end
|
||||
|
||||
mutex = DistributedMutex.new(key)
|
||||
|
||||
mutex.synchronize do
|
||||
expect(Discourse.redis.ttl(key)).to eq(DistributedMutex::DEFAULT_VALIDITY)
|
||||
|
||||
expect(Discourse.redis.get(key).to_i)
|
||||
.to eq(Time.now.to_i + DistributedMutex::DEFAULT_VALIDITY)
|
||||
expect(Discourse.redis.ttl(key)).to be <= DistributedMutex::DEFAULT_VALIDITY + 1
|
||||
expect(Discourse.redis.get(key).to_i).to be_within(1.second).of(Time.now.to_i + DistributedMutex::DEFAULT_VALIDITY)
|
||||
end
|
||||
end
|
||||
|
||||
@ -78,7 +68,7 @@ describe DistributedMutex do
|
||||
|
||||
context "readonly redis" do
|
||||
before do
|
||||
Discourse.redis.slaveof "127.0.0.1", "99991"
|
||||
Discourse.redis.slaveof "127.0.0.1", "65534"
|
||||
end
|
||||
|
||||
after do
|
||||
@ -97,7 +87,7 @@ describe DistributedMutex do
|
||||
}.to raise_error(Discourse::ReadOnly)
|
||||
|
||||
expect(done).to eq(false)
|
||||
expect(Time.now - start).to be <= 1.second
|
||||
expect(Time.now).to be <= start + 1
|
||||
end
|
||||
end
|
||||
|
||||
|
Reference in New Issue
Block a user