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:
Loïc Guitaut
2024-07-31 10:12:54 +02:00
committed by Loïc Guitaut
parent 6f80ebfc41
commit 9e9d88f078
4 changed files with 34 additions and 8 deletions

View File

@ -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