[前][次][番号順一覧][スレッド一覧]

ruby-changes:72512

From: Nobuyoshi <ko1@a...>
Date: Tue, 12 Jul 2022 19:43:25 +0900 (JST)
Subject: [ruby-changes:72512] 0f8a0c5f37 (master): Refactor tests for ThreadInstrumentation counters

https://git.ruby-lang.org/ruby.git/commit/?id=0f8a0c5f37

From 0f8a0c5f371b0886e8e31e35a9095bc9843de27c Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Sun, 10 Jul 2022 13:01:21 +0900
Subject: Refactor tests for ThreadInstrumentation counters

* Extracted some assertions.
* Assert counter values should be positive.
---
 test/-ext-/thread/test_instrumentation_api.rb | 31 ++++++++++++++-------------
 1 file changed, 16 insertions(+), 15 deletions(-)

diff --git a/test/-ext-/thread/test_instrumentation_api.rb b/test/-ext-/thread/test_instrumentation_api.rb
index 4e74a4cc6a..b8f21fa92a 100644
--- a/test/-ext-/thread/test_instrumentation_api.rb
+++ b/test/-ext-/thread/test_instrumentation_api.rb
@@ -21,21 +21,15 @@ class TestThreadInstrumentation < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/-ext-/thread/test_instrumentation_api.rb#L21
     threads = threaded_cpu_work
     assert_equal [false] * THREADS_COUNT, threads.map(&:status)
     counters = Bug::ThreadInstrumentation.counters
-    counters.each do |c|
-      assert_predicate c, :nonzero?, "Call counters: #{counters.inspect}"
-    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.
+    assert_join_counters(counters)
+    assert_global_join_counters(counters)
   end
 
   def test_join_counters # Bug #18900
     thr = Thread.new { fib(30) }
     Bug::ThreadInstrumentation.reset_counters
     thr.join
-    Bug::ThreadInstrumentation.local_counters.each_with_index do |counter, index|
-      assert_operator counter, :>, 0, "counter[#{index}]"
-    end
+    assert_join_counters(Bug::ThreadInstrumentation.local_counters)
   end
 
   def test_thread_instrumentation_fork_safe
@@ -56,12 +50,8 @@ class TestThreadInstrumentation < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/-ext-/thread/test_instrumentation_api.rb#L50
     assert_predicate $?, :success?
 
     assert_equal [false] * THREADS_COUNT, thread_statuses
-    counters.each do |c|
-      assert_predicate c, :nonzero?, "Call counters: #{counters.inspect}"
-    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.
+    assert_join_counters(counters)
+    assert_global_join_counters(counters)
   end
 
   def test_thread_instrumentation_unregister
@@ -79,4 +69,15 @@ class TestThreadInstrumentation < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/-ext-/thread/test_instrumentation_api.rb#L69
   def threaded_cpu_work(size = 20)
     THREADS_COUNT.times.map { Thread.new { fib(size) } }.each(&:join)
   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
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]