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

ruby-changes:32137

From: nobu <ko1@a...>
Date: Sun, 15 Dec 2013 07:03:44 +0900 (JST)
Subject: [ruby-changes:32137] nobu:r44216 (trunk): test_thread.rb: timing issues

nobu	2013-12-15 07:03:24 +0900 (Sun, 15 Dec 2013)

  New Revision: 44216

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=44216

  Log:
    test_thread.rb: timing issues
    
    * test/ruby/test_thread.rb: get rid of timing issues and reduce
      sleep times.

  Modified files:
    trunk/test/ruby/lbtest.rb
    trunk/test/ruby/test_thread.rb
Index: test/ruby/lbtest.rb
===================================================================
--- test/ruby/lbtest.rb	(revision 44215)
+++ test/ruby/lbtest.rb	(revision 44216)
@@ -35,7 +35,7 @@ lb = LocalBarrier.new(n) https://github.com/ruby/ruby/blob/trunk/test/ruby/lbtest.rb#L35
 
 (n - 1).times do |i|
   Thread.start do
-    sleep((rand(n) + 1) / 10.0)
+    sleep((rand(n) + 1) / 100.0)
     print "#{i}: done\n"
     lb.sync
     print "#{i}: cont\n"
Index: test/ruby/test_thread.rb
===================================================================
--- test/ruby/test_thread.rb	(revision 44215)
+++ test/ruby/test_thread.rb	(revision 44216)
@@ -165,22 +165,27 @@ class TestThread < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L165
 
   def test_join
     t = Thread.new { sleep }
-    assert_nil(t.join(0.5))
+    assert_nil(t.join(0.05))
 
   ensure
     t.kill if t
   end
 
   def test_join2
-    t1 = Thread.new { sleep(1.5) }
+    ok = false
+    t1 = Thread.new { ok = true; sleep }
+    Thread.pass until ok
+    Thread.pass until t1.stop?
     t2 = Thread.new do
-      t1.join(1)
+      Thread.pass while ok
+      t1.join(0.01)
     end
     t3 = Thread.new do
-      sleep 0.5
+      ok = false
       t1.join
     end
     assert_nil(t2.value)
+    t1.wakeup
     assert_equal(t1, t3.value)
 
   ensure
@@ -232,10 +237,10 @@ class TestThread < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L237
       Thread.stop
       s += 1
     end
-    sleep 0.5
+    Thread.pass until t.stop?
     assert_equal(1, s)
     t.wakeup
-    sleep 0.5
+    Thread.pass while t.alive?
     assert_equal(2, s)
     assert_raise(ThreadError) { t.wakeup }
 
@@ -258,7 +263,7 @@ class TestThread < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L263
     assert_in_out_err([], <<-INPUT) do |r, e|
       t1 = Thread.new { sleep }
       Thread.pass
-      t2 = Thread.new { loop { } }
+      t2 = Thread.new { loop { Thread.pass } }
       Thread.new { }.join
       p [Thread.current, t1, t2].map{|t| t.object_id }.sort
       p Thread.list.map{|t| t.object_id }.sort
@@ -279,8 +284,8 @@ class TestThread < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L284
     assert_in_out_err([], <<-INPUT, %w(false 1), [])
       p Thread.abort_on_exception
       begin
-        Thread.new { raise }
-        sleep 0.5
+        t = Thread.new { raise }
+        Thread.pass until t.stop?
         p 1
       rescue
         p 2
@@ -302,8 +307,8 @@ class TestThread < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L307
     assert_in_out_err(%w(--disable-gems -d), <<-INPUT, %w(false 2), %r".+")
       p Thread.abort_on_exception
       begin
-        Thread.new { raise }
-        sleep 0.5
+        t = Thread.new { raise }
+        Thread.pass until t.stop?
         p 1
       rescue
         p 2
@@ -313,9 +318,11 @@ class TestThread < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L318
     assert_in_out_err([], <<-INPUT, %w(false true 2), [])
       p Thread.abort_on_exception
       begin
-        t = Thread.new { sleep 0.5; raise }
+        ok = false
+        t = Thread.new { Thread.pass until ok; raise }
         t.abort_on_exception = true
         p t.abort_on_exception
+        ok = 1
         sleep 1
         p 1
       rescue
@@ -329,7 +336,7 @@ class TestThread < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L336
     b = Thread.new { Thread.stop }
     c = Thread.new { Thread.exit }
     e = Thread.current
-    sleep 0.5
+    Thread.pass while a.alive? or !b.stop? or c.alive?
 
     assert_equal(nil, a.status)
     assert_predicate(a, :stop?)
@@ -352,8 +359,9 @@ class TestThread < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L359
   end
 
   def test_safe_level
-    t = Thread.new { $SAFE = 3; sleep }
-    sleep 0.5
+    ok = false
+    t = Thread.new { $SAFE = 3; ok = true; sleep }
+    Thread.pass until ok
     assert_equal(0, Thread.current.safe_level)
     assert_equal(3, t.safe_level)
 
@@ -392,11 +400,12 @@ class TestThread < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L400
   end
 
   def test_select_wait
-    assert_nil(IO.select(nil, nil, nil, 1))
+    assert_nil(IO.select(nil, nil, nil, 0.001))
     t = Thread.new do
       IO.select(nil, nil, nil, nil)
     end
-    sleep 0.5
+    Thread.pass until t.stop?
+    assert_predicate(t, :alive?)
     t.kill
   end
 
@@ -418,7 +427,7 @@ class TestThread < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L427
       m.lock
       :foo
     end
-    sleep 0.5
+    Thread.pass until t.stop?
     t.kill
     assert_nil(t.value)
   end
@@ -499,12 +508,13 @@ class TestThread < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L508
   def make_handle_interrupt_test_thread1 flag
     r = []
     ready_p = false
+    done = false
     th = Thread.new{
       begin
         Thread.handle_interrupt(RuntimeError => flag){
           begin
             ready_p = true
-            sleep 0.5
+            sleep 0.01 until done
           rescue
             r << :c1
           end
@@ -516,6 +526,7 @@ class TestThread < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L526
     Thread.pass until ready_p
     th.raise
     begin
+      done = true
       th.join
     rescue
       r << :c3
@@ -609,7 +620,7 @@ class TestThread < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L620
         }
       }
 
-      sleep 0.1
+      Thread.pass while t.stop?
       t.raise RuntimeError
       th_waiting = false
       t.join rescue nil
@@ -619,10 +630,11 @@ class TestThread < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L630
 
   def test_handle_interrupt_and_p
     assert_in_out_err([], <<-INPUT, %w(:ok :ok), [])
-      th_waiting = true
+      th_waiting = false
 
       t = Thread.new {
         Thread.handle_interrupt(RuntimeError => :on_blocking) {
+          th_waiting = true
           nil while th_waiting
           # p shouldn't provide interruptible point
           p :ok
@@ -630,7 +642,7 @@ class TestThread < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L642
         }
       }
 
-      sleep 0.1
+      Thread.pass until th_waiting
       t.raise RuntimeError
       th_waiting = false
       t.join rescue nil
@@ -640,11 +652,12 @@ class TestThread < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L652
   def test_handle_interrupted?
     q = Queue.new
     Thread.handle_interrupt(RuntimeError => :never){
+      done = false
       th = Thread.new{
         q.push :e
         begin
           begin
-            sleep 0.5
+            Thread.pass until done
           rescue => e
             q.push :ng1
           end
@@ -661,6 +674,7 @@ class TestThread < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L674
       }
       q.pop
       th.raise
+      done = true
       th.join
       assert_equal(:ok, q.pop)
     }
@@ -726,8 +740,9 @@ _eom https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L740
 
   def test_thread_join_in_trap
     assert_separately [], <<-'EOS'
+    t0 = Thread.current
     assert_nothing_raised{
-      t = Thread.new{ sleep 0.2; Process.kill(:INT, $$) }
+      t = Thread.new {Thread.pass until t0.stop?; Process.kill(:INT, $$)}
 
       Signal.trap :INT do
         t.join
@@ -736,18 +751,17 @@ _eom https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L751
       t.join
     }
     EOS
+  end
 
+  def test_thread_value_in_trap
     assert_separately [], <<-'EOS'
-    assert_equal(:normal_end,
-                 begin
-                   t = Thread.new{ sleep 0.2; Process.kill(:INT, $$); :normal_end }
-
-                   Signal.trap :INT do
-                     t.value
-                   end
-                   t.value
-                 end
-                 )
+    t0 = Thread.current
+    t = Thread.new {Thread.pass until t0.stop?; Process.kill(:INT, $$); :normal_end}
+
+    Signal.trap :INT do
+      t.value
+    end
+    assert_equal(:normal_end, t.value)
     EOS
   end
 
@@ -769,7 +783,7 @@ _eom https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L783
     assert_in_out_err([], <<-INPUT, %w(false), [])
 Thread.new(Thread.current) {|mth|
   begin
-    sleep 0.1
+    Thead.pass until mth.stop?
   ensure
     p mth.alive?
   end
@@ -783,10 +797,12 @@ Thread.new(Thread.current) {|mth| https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L797
     assert_in_out_err([], <<-INPUT, %w(sleep run), [])
       Signal.trap(:INT) {
         puts Thread.current.status
+        exit
       }
+      t = Thread.current
 
       Thread.new(Thread.current) {|mth|
-        sleep 0.01
+        Thread.pass until t.stop?
         puts mth.status
         Process.kill(:INT, $$)
       }
@@ -813,11 +829,13 @@ Thread.new(Thread.current) {|mth| https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L829
     }
 
     begin
-      sleep 0.01
+      Thread.pass until ary.size >= 1
+      Thread.pass until t.stop?
       t.kill  # wake up sleep #1
-      sleep 0.01
+      Thread.pass until ary.size >= 2
+      Thread.pass until t.stop?
       t.raise "wakeup" # wake up sleep #2
-      sleep 0.01
+      Thread.pass while t.alive?
       assert_equal(ary, ["run", "aborting", "aborting"])
     ensure
       t.join rescue nil
@@ -844,7 +862,7 @@ Thread.new(Thread.current) {|mth| https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L862
         sleep
       }
 
-      sleep 0.01 until th.status == "sleep"
+      Thread.pass until th.status == "sleep"
       # acquired another thread.
       assert_equal(mutex.locked?, true)
       assert_equal(mutex.owned?, false)
@@ -857,15 +875,17 @@ Thread.new(Thread.current) {|mth| https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L875
     assert_in_out_err([], <<-INPUT, %w(locked unlocked false), [])
       m = Mutex.new
 
+      trapped = false
       Signal.trap("INT") { |signo|
         m.unlock
+        trapped = true
         puts "unlocked"
       }
 
       m.lock
       puts "locked"
       Process.kill("INT", $$)
-      sleep 0.01
+      Thread.pass until trapped
       puts m.locked?
     INPUT
   end
@@ -891,8 +911,9 @@ Thread.new(Thread.current) {|mth| https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L911
                     "0 thread_machine_stack_size")
     assert_operator(h_default[:thread_machine_stack_size], :<=, h_large[:thread_machine_stack_size],
                     "large thread_machine_stack_size")
+  end
 
-    # check VM machine stack size
+  def test_vm_machine_stack_size
     script = 'def rec; print "."; STDOUT.flush; rec; end; rec'
     size_default = invoke_rec script, nil, nil
     assert_operator(size_default, :>, 0, "default size")
@@ -900,9 +921,9 @@ Thread.new(Thread.current) {|mth| https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L921
     assert_operator(size_default, :>, size_0, "0 size")
     size_large = invoke_rec script, 1024 * 1024 * 10, nil
     assert_operator(size_default, :<, size_large, "large size")
+  end
 
-    return if /mswin|mingw/ =~ RUBY_PLATFORM
-
+  def test_machine_stack_size
     # check machine stack size
     # Note that machine stack size may not change size (depend on OSs)
     script = 'def rec; print "."; STDOUT.flush; 1.times{1.times{1.times{rec}}}; end; Thread.new{rec}.join'
@@ -912,7 +933,7 @@ Thread.new(Thread.current) {|mth| https://github.com/ruby/ruby/blob/trunk/test/ruby/test_thread.rb#L933
     assert_operator(size_default, :>=, size_0, "0 size")
     size_large = invoke_rec script, vm_stack_size, 1024 * 1024 * 10
     assert_operator(size_default, :<=, size_large, "large size")
-  end
+  end unless /mswin|mingw/ =~ RUBY_PLATFORM
 
   def test_blocking_mutex_unlocked_on_fork
     bug8433 = '[ruby-core:55102] [Bug #8433]'

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

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