mirror of
https://github.com/discourse/discourse.git
synced 2025-05-23 20:32:44 +08:00
DEV: Use rspec mocks to properly verify a race condition
This is a small followup of https://github.com/discourse/discourse/pull/28124.
This commit is contained in:

committed by
Loïc Guitaut

parent
6f80ebfc41
commit
9e9d88f078
@ -122,14 +122,16 @@ RSpec.describe Cache do
|
||||
end
|
||||
end
|
||||
|
||||
it "isn't prone to a race condition due to key expiring between GET calls" do
|
||||
key = cache.normalize_key("my_key")
|
||||
context "when there is a race condition due to key expiring between GET calls" do
|
||||
before do
|
||||
allow(Discourse.redis).to receive(:get).and_wrap_original do |original_method, *args|
|
||||
original_method.call(*args).tap { Discourse.redis.del(*args) }
|
||||
end
|
||||
end
|
||||
|
||||
# while this is not technically testing the race condition, it's
|
||||
# ensuring we're only calling redis.get once, which is a good enough proxy
|
||||
Discourse.redis.stubs(:get).with(key).returns(Marshal.dump("bob")).once
|
||||
|
||||
expect(fetch_value).to eq("bob")
|
||||
it "isn't prone to that race condition" do
|
||||
expect(fetch_value).to eq("bob")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user