Permalink
Browse files

Fix rack middleware so that it is threadsafe.

VCR as a whole is not designed to be threadsafe (running your tests in multiple threads is asking for trouble), but the rack middleware needs to be, since some rack servers (like WEBrick) are multithreaded.

Closes #58.
  • Loading branch information...
1 parent b466443 commit eb530ae6a7a3648b1d626d0ab91a6c4de361d356 @myronmarston committed May 12, 2011
Showing with 25 additions and 2 deletions.
  1. +1 −0 CHANGELOG.md
  2. +4 −2 lib/vcr/middleware/rack.rb
  3. +20 −0 spec/vcr/middleware/rack_spec.rb
View
@@ -9,6 +9,7 @@
* Add `:ignore_cassettes` option to `VCR.turn_off!`. This causes
cassette insertions to be ignored rather than to trigger an error.
Patch provided by [Justin Smestad](https://github.com/jsmestad).
+* Fix rack middleware to make it threadsafe.
## 1.9.0 (April 14, 2011)
@@ -4,8 +4,10 @@ class Rack
include Common
def call(env)
- VCR.use_cassette(*cassette_arguments(env)) do
- @app.call(env)
+ Thread.exclusive do
+ VCR.use_cassette(*cassette_arguments(env)) do
+ @app.call(env)
+ end
end
end
end
@@ -51,4 +51,24 @@
instance.call(env_hash)
end
end
+
+ let(:threaded_app) do
+ lambda do |env|
+ sleep 0.15
+ VCR.send(:cassettes).should have(1).cassette
+ [200, {}, ['OK']]
+ end
+ end
+
+ it 'is thread safe' do
+ stack = described_class.new(threaded_app) do |cassette|
+ cassette.name 'c'
+ end
+
+ thread = Thread.new { stack.call({}) }
+ stack.call({})
+ thread.join
+
+ VCR.current_cassette.should be_nil
+ end
end

0 comments on commit eb530ae

Please sign in to comment.