New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve the output matcher to make it thread safe #642
Comments
Thanks @cupakromer for this fix re-implementing `capture` now that Rails 4.2 has made it private.
This seems to work in my initial testing: def self.capture(&block)
reader, writer = IO.pipe
fork do
reader.close
$stdout = writer
block.call
end
writer.close
reader.read
end Downsides include:
|
We can't use |
|
Gotcha. No forking allowed. Is there a thread-safe implementation of the output matcher? Put me down as pessimistic. Does the output matcher need to go away in favor of a thread-safe Rspec? |
I'm not sure. I haven't looked into this issue at all yet, honestly.
I know minitest has a similar feature, and it has a multithreaded parallel test runner, so it doesn't seem like they are fundamentally incompatible. Also, the matcher is brand new in 3.0...so I don't want to consider removing it. If we simply can't find a way to make it threadsafe, we can (and should) document that this particular matcher isn't threadsafe so users are aware of the issue. |
minitest is using the same thread-unsafe (StringIO, global swapping) implementation: https://github.com/seattlerb/minitest/blob/master/lib/minitest/assertions.rb#L399 |
Maybe see rubygems, per my comment rails/rails#13392 (comment) |
I feel like a mutex is best way to guarantee threadsafety (possibly combined with the rubytapas-style capturing mentioned by @bf4 in the other comment), however it's probably important to not initialize the mutex with a non-atomic |
The current output matcher is not considered thread safe.
Rails had a similar feature
capture
andsilence
which were both recently deprecated. This caused issues with the ammeter gem which relied on those methods. The patch applied there was near identical to how theoutput
matcher works. Based on feedback this is not considered thread safe either.The text was updated successfully, but these errors were encountered: