Skip to content

Instantly share code, notes, and snippets.

@tommeier
Created March 28, 2014 23:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tommeier/9845210 to your computer and use it in GitHub Desktop.
Save tommeier/9845210 to your computer and use it in GitHub Desktop.
require 'active_support/core_ext/kernel/reporting'
require 'monitor'
# Monkey patch silence_stream to be thread safe
# -> Pull request on Rails: https://github.com/rails/rails/pull/13139
# -> But this will allow us to continue to see log output when running specs
# -> It could be we just patch ActiveRecord::SessionStore that triggers the `.quietly` calls
# on logging the `find_session_id` calls.
module Kernel
# Silences any stream for the duration of the block.
#
# silence_stream(STDOUT) do
# puts 'This will never be seen'
# end
#
def silence_stream(stream)
@@monitor ||= Monitor.new
@@monitor.synchronize do
begin
old_stream = stream.dup
stream.reopen(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? 'NUL:' : '/dev/null')
stream.sync = true
yield
ensure
stream.reopen(old_stream)
end
end
end
end
@alexmchale
Copy link

Thank you so much for figuring this problem out. I was struggling with this for a few hours myself, but never found the source of the problem. Your Gist saved me bigtime!

This method of overloading makes me uncomfortable, with the code copying. I've pushed my version of your fix here: https://gist.github.com/alexmchale/b68a2c72ed1f432418d9

@tommeier
Copy link
Author

Thanks @alexmchale, Rails has just deprecated the non-threadsafe messages, so should be long until this isn't required. See rails/rails@1fbb5c1...3121412

@kpolitowicz
Copy link

Thank you. Capybara is behaving again!

@leods92
Copy link

leods92 commented Apr 22, 2015

Thank you. Capybara is behaving again! [2]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment