1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Refactor tests for ThreadInstrumentation counters

* Extracted some assertions.
* Assert counter values should be positive.
This commit is contained in:
Nobuyoshi Nakada 2022-07-10 13:01:21 +09:00
parent a6e2f3fd8d
commit 0f8a0c5f37
Notes: git 2022-07-12 19:43:35 +09:00

View file

@ -21,21 +21,15 @@ class TestThreadInstrumentation < Test::Unit::TestCase
threads = threaded_cpu_work threads = threaded_cpu_work
assert_equal [false] * THREADS_COUNT, threads.map(&:status) assert_equal [false] * THREADS_COUNT, threads.map(&:status)
counters = Bug::ThreadInstrumentation.counters counters = Bug::ThreadInstrumentation.counters
counters.each do |c| assert_join_counters(counters)
assert_predicate c, :nonzero?, "Call counters: #{counters.inspect}" assert_global_join_counters(counters)
end
assert_equal THREADS_COUNT, counters.first
assert_in_delta THREADS_COUNT, counters.last, 1 # It's possible that a thread didn't execute its EXIT hook yet.
end end
def test_join_counters # Bug #18900 def test_join_counters # Bug #18900
thr = Thread.new { fib(30) } thr = Thread.new { fib(30) }
Bug::ThreadInstrumentation.reset_counters Bug::ThreadInstrumentation.reset_counters
thr.join thr.join
Bug::ThreadInstrumentation.local_counters.each_with_index do |counter, index| assert_join_counters(Bug::ThreadInstrumentation.local_counters)
assert_operator counter, :>, 0, "counter[#{index}]"
end
end end
def test_thread_instrumentation_fork_safe def test_thread_instrumentation_fork_safe
@ -56,12 +50,8 @@ class TestThreadInstrumentation < Test::Unit::TestCase
assert_predicate $?, :success? assert_predicate $?, :success?
assert_equal [false] * THREADS_COUNT, thread_statuses assert_equal [false] * THREADS_COUNT, thread_statuses
counters.each do |c| assert_join_counters(counters)
assert_predicate c, :nonzero?, "Call counters: #{counters.inspect}" assert_global_join_counters(counters)
end
assert_equal THREADS_COUNT, counters.first
assert_in_delta THREADS_COUNT, counters.last, 1 # It's possible that a thread didn't execute its EXIT hook yet.
end end
def test_thread_instrumentation_unregister def test_thread_instrumentation_unregister
@ -79,4 +69,15 @@ class TestThreadInstrumentation < Test::Unit::TestCase
def threaded_cpu_work(size = 20) def threaded_cpu_work(size = 20)
THREADS_COUNT.times.map { Thread.new { fib(size) } }.each(&:join) THREADS_COUNT.times.map { Thread.new { fib(size) } }.each(&:join)
end end
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
assert_include 0..THREADS_COUNT, counters.last # It's possible that a thread didn't execute its EXIT hook yet.
end
end end