mirror of
https://github.com/discourse/discourse.git
synced 2025-05-21 18:12:32 +08:00
distributed memoizer added to ensure absolute duplicate posts don't get through
in case of an absolute dupe just return the memoized post This works around issues with wordpress being crazy
This commit is contained in:
55
spec/components/distributed_memoizer_spec.rb
Normal file
55
spec/components/distributed_memoizer_spec.rb
Normal file
@ -0,0 +1,55 @@
|
||||
require 'spec_helper'
|
||||
require_dependency 'distributed_memoizer'
|
||||
|
||||
describe DistributedMemoizer do
|
||||
|
||||
before do
|
||||
$redis.del(DistributedMemoizer.redis_key("hello"))
|
||||
$redis.del(DistributedMemoizer.redis_lock_key("hello"))
|
||||
$redis.unwatch
|
||||
end
|
||||
|
||||
# NOTE we could use a mock redis here, but I think it makes sense to test the real thing
|
||||
# let(:mock_redis) { MockRedis.new }
|
||||
|
||||
def memoize(&block)
|
||||
DistributedMemoizer.memoize("hello", duration = 120, &block)
|
||||
end
|
||||
|
||||
it "returns the value of a block" do
|
||||
memoize do
|
||||
"abc"
|
||||
end.should == "abc"
|
||||
end
|
||||
|
||||
it "return the old value once memoized" do
|
||||
|
||||
memoize do
|
||||
"abc"
|
||||
end
|
||||
|
||||
memoize do
|
||||
"world"
|
||||
end.should == "abc"
|
||||
end
|
||||
|
||||
it "memoizes correctly when used concurrently" do
|
||||
results = []
|
||||
threads = []
|
||||
|
||||
5.times do
|
||||
threads << Thread.new do
|
||||
results << memoize do
|
||||
sleep 0.001
|
||||
SecureRandom.hex
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
threads.each(&:join)
|
||||
results.uniq.length.should == 1
|
||||
results.count.should == 5
|
||||
|
||||
end
|
||||
|
||||
end
|
Reference in New Issue
Block a user