Permalink
Browse files

Merge pull request #258 from vcr/fix_around_http_request_hook

Fix issue with `around_http_request` when using multiple threads.
  • Loading branch information...
2 parents ffaad77 + 25467d5 commit d6b3058f27529de5f209f1625fe155f306fce6b6 @myronmarston myronmarston committed Jan 20, 2013
Showing with 32 additions and 1 deletion.
  1. +4 −1 lib/vcr/configuration.rb
  2. +28 −0 spec/acceptance/threading_spec.rb
View
@@ -392,13 +392,16 @@ def around_http_request(*filters, &block)
"VCR::Configuration#around_http_request requires fibers, " +
"which are not available on your ruby intepreter."
else
- fiber, hook_allowed, hook_decaration = nil, false, caller.first
+ fibers = {}
+ hook_allowed, hook_decaration = false, caller.first
before_http_request(*filters) do |request|
hook_allowed = true
fiber = start_new_fiber_for(request, block)
+ fibers[Thread.current] = fiber
end
after_http_request(lambda { hook_allowed }) do |request, response|
+ fiber = fibers.delete(Thread.current)
resume_fiber(fiber, response, hook_decaration)
end
end
@@ -0,0 +1,28 @@
+require 'spec_helper'
+
+describe VCR do
+ context 'when used in a multithreaded environment', :with_monkey_patches => :excon do
+ def recorded_content_for(name)
+ VCR.cassette_persisters[:file_system]["#{name}.yml"].to_s
+ end
+
+ it 'can use a cassette in an #around_http_request hook', :if => (RUBY_VERSION.to_f > 1.8) do
+ VCR.configure do |vcr|
+ vcr.around_http_request do |req|
+ VCR.use_cassette(req.parsed_uri.path, &req)
+ end
+ end
+
+ thread = Thread.start do
+ Excon.get "http://localhost:#{VCR::SinatraApp.port}/search?q=thread"
+ end
+
+ Excon.get "http://localhost:#{VCR::SinatraApp.port}/foo",
+ :response_block => Proc.new { thread.join }
+
+ expect(recorded_content_for("search") +
+ recorded_content_for("foo")).to include("query: thread", "FOO!")
+ end
+ end
+end
+

0 comments on commit d6b3058

Please sign in to comment.