1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/test/-ext-/thread/test_instrumentation_api.rb
Jean Boussier b1e6e58cd1 Refactor TestThreadInstrumentation to investigate CI flakiness
`test_thread_instrumentation_fork_safe` has been failing occasionaly
on Ubuntu and Arch. At this stage we're not sure why, all we know is
that the child exit with status 1.

I suspect that something entirely unrelated cause the forked children
to fail on exit, so by using `exit!(0)` and doing assertions in the
parent I hope to be resilient to that.
2022-06-07 13:19:34 +02:00

64 lines
1.7 KiB
Ruby

# frozen_string_literal: false
class TestThreadInstrumentation < Test::Unit::TestCase
def setup
pend("TODO: No windows support yet") if /mswin|mingw|bccwin/ =~ RUBY_PLATFORM
end
def test_thread_instrumentation
require '-test-/thread/instrumentation'
Bug::ThreadInstrumentation.reset_counters
Bug::ThreadInstrumentation::register_callback
begin
threaded_cpu_work
counters = Bug::ThreadInstrumentation.counters
counters.each do |c|
assert_predicate c,:nonzero?, "Call counters: #{counters.inspect}"
end
ensure
Bug::ThreadInstrumentation::unregister_callback
end
end
def test_thread_instrumentation_fork_safe
skip "No fork()" unless Process.respond_to?(:fork)
require '-test-/thread/instrumentation'
Bug::ThreadInstrumentation::register_callback
read_pipe, write_pipe = IO.pipe
begin
pid = fork do
Bug::ThreadInstrumentation.reset_counters
threaded_cpu_work
write_pipe.write(Marshal.dump(Bug::ThreadInstrumentation.counters))
write_pipe.close
exit!(0)
end
write_pipe.close
_, status = Process.wait2(pid)
assert_predicate status, :success?
counters = Marshal.load(read_pipe)
read_pipe.close
counters.each do |c|
assert_predicate c,:nonzero?, "Call counters: #{counters.inspect}"
end
ensure
Bug::ThreadInstrumentation::unregister_callback
end
end
def test_thread_instrumentation_unregister
require '-test-/thread/instrumentation'
assert Bug::ThreadInstrumentation::register_and_unregister_callbacks
end
private
def threaded_cpu_work
5.times.map { Thread.new { 100.times { |i| i + i } } }.each(&:join)
end
end