discourse/spec/lib/distributed_memoizer_spec.rb
Jarek Radosz 3c44bed545
DEV: Make DistributedMemoizer use DistributedMutex (#16229)
Its implementation was already distributed-mutex-like, with slight differences that did not seem necessary.
2022-04-05 19:29:58 +02:00

43 lines
884 B
Ruby

# frozen_string_literal: true
describe DistributedMemoizer do
after do
Discourse.redis.del(DistributedMemoizer.redis_key("hello"))
Discourse.redis.del(DistributedMemoizer.redis_lock_key("hello"))
end
def memoize(&block)
DistributedMemoizer.memoize("hello", duration = 120, &block)
end
it "returns the value of a block" do
expect(memoize { "abc" }).to eq("abc")
end
it "return the old value once memoized" do
memoize do
"abc"
end
expect(memoize { "world" }).to eq("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)
expect(results.uniq.length).to eq(1)
expect(results.count).to eq(5)
end
end