493d437e79
* Remove outdated option https://github.com/rspec/rspec-core/commit/04078317ba6577699d06cf4dccf014254dcde7a6 * Use the non-globally exposed RSpec syntax https://github.com/rspec/rspec-core/pull/2803 * Use the non-globally exposed RSpec syntax, cont https://github.com/rspec/rspec-core/pull/2803 * Comply to strict predicate matchers See: - https://github.com/rspec/rspec-expectations/pull/1195 - https://github.com/rspec/rspec-expectations/pull/1196 - https://github.com/rspec/rspec-expectations/pull/1277
43 lines
890 B
Ruby
43 lines
890 B
Ruby
# frozen_string_literal: true
|
|
|
|
RSpec.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
|