discourse/spec/components/distributed_memoizer_spec.rb
Andy Waite 3e50313fdc Prepare for separation of RSpec helper files
Since rspec-rails 3, the default installation creates two helper files:
* `spec_helper.rb`
* `rails_helper.rb`

`spec_helper.rb` is intended as a way of running specs that do not
require Rails, whereas `rails_helper.rb` loads Rails (as Discourse's
current `spec_helper.rb` does).

For more information:

https://www.relishapp.com/rspec/rspec-rails/docs/upgrade#default-helper-files

In this commit, I've simply replaced all instances of `spec_helper` with
`rails_helper`, and renamed the original `spec_helper.rb`.

This brings the Discourse project closer to the standard usage of RSpec
in a Rails app.

At present, every spec relies on loading Rails, but there are likely
many that don't need to. In a future pull request, I hope to introduce a
separate, minimal `spec_helper.rb` which can be used in tests which
don't rely on Rails.
2015-12-01 20:39:42 +00:00

56 lines
1.1 KiB
Ruby

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