From 78e7581d8b34337dbcf62461499f41c0d5c64fce Mon Sep 17 00:00:00 2001 From: Myron Marston Date: Thu, 24 Nov 2011 22:38:39 -0800 Subject: [PATCH] Fix around_http_request to support multiple requests. Fibers can only be used once so we have to create a new one for each request. --- lib/vcr/configuration.rb | 11 ++++++++--- .../shared_example_groups/hook_into_http_library.rb | 8 ++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/vcr/configuration.rb b/lib/vcr/configuration.rb index 8f7be6cb..03d3c361 100644 --- a/lib/vcr/configuration.rb +++ b/lib/vcr/configuration.rb @@ -80,9 +80,8 @@ def around_http_request(&block) "VCR::Configuration#around_http_request requires fibers, " + "which are not available on your ruby intepreter." else - hook_decaration = caller.first - fiber = Fiber.new(&block) - before_http_request { |request| fiber.resume(request.fiber_aware) } + fiber, hook_decaration = nil, caller.first + before_http_request { |request| fiber = start_new_fiber_for(request, block) } after_http_request { |request| resume_fiber(fiber, hook_decaration) } end @@ -103,6 +102,12 @@ def resume_fiber(fiber, hook_declaration) "Your around_http_request hook declared at #{hook_declaration}" + " must call #proceed on the yielded request but did not." end + + def start_new_fiber_for(request, block) + Fiber.new(&block).tap do |fiber| + fiber.resume(request.fiber_aware) + end + end end end diff --git a/spec/support/shared_example_groups/hook_into_http_library.rb b/spec/support/shared_example_groups/hook_into_http_library.rb index 365ff60a..a6d83910 100644 --- a/spec/support/shared_example_groups/hook_into_http_library.rb +++ b/spec/support/shared_example_groups/hook_into_http_library.rb @@ -192,6 +192,14 @@ def self.test_playback(description, url) error.message.should include(hook_declaration) } end + + it 'does not get a dead fiber error when multiple requests are made' do + VCR.configuration.around_http_request do |request| + VCR.use_cassette('new_cassette', &request) + end + + 3.times { make_http_request(:get, request_url) } + end end if RUBY_VERSION >= '1.9' context "when the request is ignored" do