Skip to content

Commit

Permalink
Fix rack middleware so that it is threadsafe.
Browse files Browse the repository at this point in the history
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 vcr#58.
  • Loading branch information
myronmarston committed May 12, 2011
1 parent b466443 commit eb530ae
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 4 additions & 2 deletions lib/vcr/middleware/rack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions spec/vcr/middleware/rack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.