mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 22:43:33 +08:00
FIX: DistributedMutex (#7953)
This commit is contained in:

committed by
GitHub

parent
3b575b82f3
commit
20bc4a38a5
@ -98,4 +98,47 @@ describe DistributedMutex do
|
||||
end
|
||||
end
|
||||
|
||||
context "executions" do
|
||||
it "should not allow critical sections to overlap" do
|
||||
connections = (0...3).map { DiscourseRedis.new }
|
||||
|
||||
scenario =
|
||||
Concurrency::Scenario.new do |execution|
|
||||
locked = false
|
||||
|
||||
$redis.del('mutex_key')
|
||||
|
||||
connections.each do |connection|
|
||||
connection.unwatch
|
||||
end
|
||||
|
||||
3.times do |i|
|
||||
execution.spawn do
|
||||
begin
|
||||
redis =
|
||||
Concurrency::RedisWrapper.new(
|
||||
connections[i],
|
||||
execution
|
||||
)
|
||||
|
||||
2.times do
|
||||
DistributedMutex.synchronize('mutex_key', redis: redis) do
|
||||
raise "already locked #{execution.path}" if locked
|
||||
locked = true
|
||||
|
||||
execution.yield
|
||||
|
||||
raise "already unlocked #{execution.path}" unless locked
|
||||
locked = false
|
||||
end
|
||||
end
|
||||
rescue Redis::ConnectionError
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
scenario.run(runs: 10)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user