Skip to content

Commit

Permalink
Fix rails 4.2 failing specs
Browse files Browse the repository at this point in the history
  • Loading branch information
jlurena committed Jul 8, 2024
1 parent 8f9fe95 commit 4f6be17
Showing 1 changed file with 41 additions and 39 deletions.
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

0 comments on commit 4f6be17

Please sign in to comment.