module Maxitest::Threads

def maxitest_extra_threads

if setup crashes we do not return anything to make the initial error visible
def maxitest_extra_threads
  @maxitest_threads_before ? Thread.list - @maxitest_threads_before : []
end

def maxitest_kill_extra_threads

also a helper methods for users
def maxitest_kill_extra_threads
  maxitest_extra_threads.map(&:kill).map(&:join)
end

def maxitest_wait_for_extra_threads

also a helper methods for users
def maxitest_wait_for_extra_threads
  sleep 0.01 while maxitest_extra_threads.any?
end

def setup

def setup
  @maxitest_threads_before = Thread.list
  super
end

def teardown

def teardown
  super
  found = maxitest_extra_threads
  raise "Test left #{found.size} extra threads (#{found})" if found.any?
ensure
  maxitest_kill_extra_threads
end