Skip to content

Instantly share code, notes, and snippets.

# Add to spec/spec_helper.rb, within Rspec.configure block:
#config.before(:each) do
# DeferredGarbageCollection.start
#end
#config.after(:each) do
# DeferredGarbageCollection.reconsider
#end
class DeferredGarbageCollection
@mgarrick
mgarrick / timeout.rb
Created July 13, 2012 23:22 — forked from lpar/timeout.rb
Run a shell command in a separate thread, terminate it after a time limit, return its output
# Runs a specified shell command in a separate thread.
# If it exceeds the given timeout in seconds, kills it.
# Passes stdout, stderr, thread, and a boolean indicating a timeout occurred to the passed in block.
# Uses Kernel.select to wait up to the tick length (in seconds) between
# checks on the command's status
#
# If you've got a cleaner way of doing this, I'd be interested to see it.
# If you think you can do it with Ruby's Timeout module, think again.
def run_with_timeout(*command)
options = command.extract_options!.reverse_merge(timeout: 60, tick: 1, cleanup_sleep: 0.1, buffer_size: 10240)