From 4f6be17d5e2c06a5a1c90bb105d23805ca6a97a0 Mon Sep 17 00:00:00 2001 From: Jean Luis Urena Date: Mon, 8 Jul 2024 11:04:55 -0400 Subject: [PATCH] Fix rails 4.2 failing specs --- spec/cached_resource/caching_spec.rb | 80 ++++++++++++++-------------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/spec/cached_resource/caching_spec.rb b/spec/cached_resource/caching_spec.rb index efc927e..7869e3e 100644 --- a/spec/cached_resource/caching_spec.rb +++ b/spec/cached_resource/caching_spec.rb @@ -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