2022-01-27 11:12:22 -05:00
|
|
|
# frozen_string_literal: false
|
2022-06-19 10:33:26 -04:00
|
|
|
require 'envutil'
|
|
|
|
|
2022-01-27 11:12:22 -05:00
|
|
|
class TestThreadInstrumentation < Test::Unit::TestCase
|
|
|
|
def setup
|
2022-07-07 09:20:35 -04:00
|
|
|
pend("No windows support") if /mswin|mingw|bccwin/ =~ RUBY_PLATFORM
|
2022-06-17 04:09:02 -04:00
|
|
|
|
2022-01-27 11:12:22 -05:00
|
|
|
require '-test-/thread/instrumentation'
|
2022-07-13 07:13:33 -04:00
|
|
|
|
|
|
|
Thread.list.each do |thread|
|
|
|
|
if thread != Thread.current
|
|
|
|
thread.kill
|
|
|
|
thread.join rescue nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_equal [Thread.current], Thread.list
|
|
|
|
|
2022-01-27 11:12:22 -05:00
|
|
|
Bug::ThreadInstrumentation.reset_counters
|
|
|
|
Bug::ThreadInstrumentation::register_callback
|
2022-07-07 09:20:35 -04:00
|
|
|
end
|
2022-01-27 11:12:22 -05:00
|
|
|
|
2022-07-07 09:20:35 -04:00
|
|
|
def teardown
|
|
|
|
return if /mswin|mingw|bccwin/ =~ RUBY_PLATFORM
|
|
|
|
Bug::ThreadInstrumentation::unregister_callback
|
|
|
|
end
|
|
|
|
|
|
|
|
THREADS_COUNT = 3
|
|
|
|
|
|
|
|
def test_thread_instrumentation
|
|
|
|
threads = threaded_cpu_work
|
|
|
|
assert_equal [false] * THREADS_COUNT, threads.map(&:status)
|
|
|
|
counters = Bug::ThreadInstrumentation.counters
|
2022-07-10 00:01:21 -04:00
|
|
|
assert_join_counters(counters)
|
|
|
|
assert_global_join_counters(counters)
|
2022-07-07 09:20:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_join_counters # Bug #18900
|
|
|
|
thr = Thread.new { fib(30) }
|
|
|
|
Bug::ThreadInstrumentation.reset_counters
|
|
|
|
thr.join
|
2022-07-10 00:01:21 -04:00
|
|
|
assert_join_counters(Bug::ThreadInstrumentation.local_counters)
|
2022-01-27 11:12:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_thread_instrumentation_fork_safe
|
|
|
|
skip "No fork()" unless Process.respond_to?(:fork)
|
|
|
|
|
2022-07-10 00:01:04 -04:00
|
|
|
thread_statuses = counters = nil
|
|
|
|
IO.popen("-") do |read_pipe|
|
|
|
|
if read_pipe
|
|
|
|
thread_statuses = Marshal.load(read_pipe)
|
|
|
|
counters = Marshal.load(read_pipe)
|
|
|
|
else
|
|
|
|
Bug::ThreadInstrumentation.reset_counters
|
|
|
|
threads = threaded_cpu_work
|
|
|
|
Marshal.dump(threads.map(&:status), STDOUT)
|
|
|
|
Marshal.dump(Bug::ThreadInstrumentation.counters, STDOUT)
|
|
|
|
end
|
2022-01-27 11:12:22 -05:00
|
|
|
end
|
2022-07-10 00:01:04 -04:00
|
|
|
assert_predicate $?, :success?
|
2022-07-07 09:20:35 -04:00
|
|
|
|
|
|
|
assert_equal [false] * THREADS_COUNT, thread_statuses
|
2022-07-10 00:01:21 -04:00
|
|
|
assert_join_counters(counters)
|
|
|
|
assert_global_join_counters(counters)
|
2022-01-27 11:12:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_thread_instrumentation_unregister
|
2022-07-07 09:20:35 -04:00
|
|
|
Bug::ThreadInstrumentation::unregister_callback
|
2022-01-27 11:12:22 -05:00
|
|
|
assert Bug::ThreadInstrumentation::register_and_unregister_callbacks
|
|
|
|
end
|
|
|
|
|
2022-06-07 04:51:28 -04:00
|
|
|
private
|
|
|
|
|
2022-07-07 09:20:35 -04:00
|
|
|
def fib(n = 20)
|
|
|
|
return n if n <= 1
|
|
|
|
fib(n-1) + fib(n-2)
|
|
|
|
end
|
|
|
|
|
|
|
|
def threaded_cpu_work(size = 20)
|
|
|
|
THREADS_COUNT.times.map { Thread.new { fib(size) } }.each(&:join)
|
2022-06-07 04:51:28 -04:00
|
|
|
end
|
2022-07-10 00:01:21 -04:00
|
|
|
|
|
|
|
def assert_join_counters(counters)
|
|
|
|
counters.each_with_index do |c, i|
|
|
|
|
assert_operator c, :>, 0, "Call counters[#{i}]: #{counters.inspect}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_global_join_counters(counters)
|
|
|
|
assert_equal THREADS_COUNT, counters.first
|
|
|
|
end
|
2022-06-07 04:51:28 -04:00
|
|
|
end
|