Fix issue with `around_http_request` when using multiple threads. #258

Merged
merged 1 commit into from Jan 20, 2013
Jump to file or symbol
Failed to load files and symbols.
+32 −1
Diff settings

Always

Just for now

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
+