Skip to content

Commit

Permalink
Fix around_http_request to support multiple requests.
Browse files Browse the repository at this point in the history
Fibers can only be used once so we have to create a new one for each request.
  • Loading branch information
myronmarston committed Nov 25, 2011
1 parent 8cefe96 commit 78e7581
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/vcr/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

8 changes: 8 additions & 0 deletions spec/support/shared_example_groups/hook_into_http_library.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 78e7581

Please sign in to comment.