Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rails 4.2 failing specs #69

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 41 additions & 39 deletions spec/cached_resource/caching_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,53 +86,55 @@ class NotTheThing < ActiveResource::Base
include_examples "caching"

# NOTE: Possibly flaky, but best easy/cheap way to test concurrency.
context 'when concurrency is turned on' do
before do
# it's on by default, but lets call the method
# to make sure it works
Thing.cached_resource.cache.clear
Thing.cached_resource.on!
NotTheThing.cached_resource.cache.clear
NotTheThing.cached_resource.on!
if ActiveResource::VERSION::MAJOR >= 5
context 'when concurrency is turned on' do
before do
# it's on by default, but lets call the method
# to make sure it works
Thing.cached_resource.cache.clear
Thing.cached_resource.on!
NotTheThing.cached_resource.cache.clear
NotTheThing.cached_resource.on!

ActiveResource::HttpMock.reset!
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/things/5.json", {}, {:thing => {:id => 1, :name => ("x" * 1_000_000) }}.to_json
ActiveResource::HttpMock.reset!
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/things/5.json", {}, {:thing => {:id => 1, :name => ("x" * 1_000_000) }}.to_json
end
end
end

after do
Thing.cached_resource.concurrent_write = false
end

it "should cache a response asynchronusly when on" do
Thing.cached_resource.concurrent_write = true
result = Thing.find(5)
read_from_cache("thing/5").should == nil
loops = 0
begin
Timeout.timeout(5) do
loop do
loops += 1
if read_from_cache("thing/5") == result
break
else
sleep 0.5
after do
Thing.cached_resource.concurrent_write = false
end

it "should cache a response asynchronusly when on" do
Thing.cached_resource.concurrent_write = true
result = Thing.find(5)
read_from_cache("thing/5").should == nil
loops = 0
begin
Timeout.timeout(5) do
loop do
loops += 1
if read_from_cache("thing/5") == result
break
else
sleep 0.5
end
end
end
rescue Timeout::Error
RSpec::Expectations.fail_with("Concurrency failed: Cache was not populated within the expected time")
end
rescue Timeout::Error
RSpec::Expectations.fail_with("Concurrency failed: Cache was not populated within the expected time")
end

loops.should > 0
read_from_cache("thing/5").should == result
end
loops.should > 0
read_from_cache("thing/5").should == result
end

it "will cache a response synchronously when off" do
Thing.cached_resource.concurrent_write = false
result = Thing.find(5)
read_from_cache("thing/5").should == result
it "will cache a response synchronously when off" do
Thing.cached_resource.concurrent_write = false
result = Thing.find(5)
read_from_cache("thing/5").should == result
end
end
end

Expand Down