discourse/spec/components/distributed_memoizer_spec.rb
Sam Saffron 4ea21fa2d0 DEV: use #frozen_string_literal: true on all spec
This change both speeds up specs (less strings to allocate) and helps catch
cases where methods in Discourse are mutating inputs.

Overall we will be migrating everything to use #frozen_string_literal: true
it will take a while, but this is the first and safest move in this direction
2019-04-30 10:27:42 +10:00

58 lines
1.1 KiB
Ruby

# frozen_string_literal: true
require 'rails_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
expect(memoize do
"abc"
end).to eq("abc")
end
it "return the old value once memoized" do
memoize do
"abc"
end
expect(memoize do
"world"
end).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