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

ruby-changes:72215

From: Jean <ko1@a...>
Date: Fri, 17 Jun 2022 22:11:38 +0900 (JST)
Subject: [ruby-changes:72215] c34a5469c8 (master): Debug TestThreadInstrumentation

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

From c34a5469c8d53029a5f3cdde0ca855e47a92c7d7 Mon Sep 17 00:00:00 2001
From: Jean Boussier <jean.boussier@g...>
Date: Fri, 17 Jun 2022 10:09:02 +0200
Subject: Debug TestThreadInstrumentation

It previously failed with:

```
    1) Failure:
  TestThreadInstrumentation#test_thread_instrumentation_fork_safe [/home/runner/work/ruby/ruby/src/test/-ext-/thread/test_instrumentation_api.rb:50]:
  <5> expected but was
  <4>.
```

Suggesting one `EXIT` event wasn't fired or processed.

Adding an assetion on `Thead#status` may help figure out what is wrong.
---
 test/-ext-/thread/test_instrumentation_api.rb | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/test/-ext-/thread/test_instrumentation_api.rb b/test/-ext-/thread/test_instrumentation_api.rb
index ff76e95bcd..adfb6443bc 100644
--- a/test/-ext-/thread/test_instrumentation_api.rb
+++ b/test/-ext-/thread/test_instrumentation_api.rb
@@ -4,17 +4,23 @@ class TestThreadInstrumentation < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/-ext-/thread/test_instrumentation_api.rb#L4
     pend("TODO: No windows support yet") if /mswin|mingw|bccwin/ =~ RUBY_PLATFORM
   end
 
+  THREADS_COUNT = 3
+
   def test_thread_instrumentation
     require '-test-/thread/instrumentation'
     Bug::ThreadInstrumentation.reset_counters
     Bug::ThreadInstrumentation::register_callback
 
     begin
-      threaded_cpu_work
+      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
+
+      sleep 0.01 # Give more time to the native threads to execute their EXIT hook
+      assert_equal counters.first, counters.last # exited as many times as we entered
     ensure
       Bug::ThreadInstrumentation::unregister_callback
     end
@@ -31,8 +37,9 @@ class TestThreadInstrumentation < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/-ext-/thread/test_instrumentation_api.rb#L37
     begin
       pid = fork do
         Bug::ThreadInstrumentation.reset_counters
-        threaded_cpu_work
-
+        threads = threaded_cpu_work
+        write_pipe.write(Marshal.dump(threads.map(&:status)))
+        sleep 0.01 # Give more time to the native threads to execute their EXIT hook
         write_pipe.write(Marshal.dump(Bug::ThreadInstrumentation.counters))
         write_pipe.close
         exit!(0)
@@ -41,6 +48,9 @@ class TestThreadInstrumentation < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/-ext-/thread/test_instrumentation_api.rb#L48
       _, status = Process.wait2(pid)
       assert_predicate status, :success?
 
+      thread_statuses = Marshal.load(read_pipe)
+      assert_equal [false] * THREADS_COUNT, thread_statuses
+
       counters = Marshal.load(read_pipe)
       read_pipe.close
       counters.each do |c|
@@ -61,6 +71,6 @@ class TestThreadInstrumentation < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/-ext-/thread/test_instrumentation_api.rb#L71
   private
 
   def threaded_cpu_work
-    5.times.map { Thread.new { 100.times { |i| i + i } } }.each(&:join)
+    THREADS_COUNT.times.map { Thread.new { 100.times { |i| i + i } } }.each(&:join)
   end
 end
-- 
cgit v1.2.1


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

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