2012-12-21 02:35:47 -05:00
|
|
|
# -*- coding: us-ascii -*-
|
2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2007-08-27 12:48:14 -04:00
|
|
|
require 'test/unit'
|
2018-02-03 17:24:17 -05:00
|
|
|
require "rbconfig/sizeof"
|
2018-05-15 10:31:47 -04:00
|
|
|
require "timeout"
|
2007-08-27 12:48:14 -04:00
|
|
|
|
|
|
|
class TestThread < Test::Unit::TestCase
|
2008-04-08 23:12:03 -04:00
|
|
|
class Thread < ::Thread
|
2008-05-11 06:06:58 -04:00
|
|
|
Threads = []
|
2008-04-08 23:12:03 -04:00
|
|
|
def self.new(*)
|
|
|
|
th = super
|
2008-05-11 06:06:58 -04:00
|
|
|
Threads << th
|
2008-04-08 23:12:03 -04:00
|
|
|
th
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-11 06:06:58 -04:00
|
|
|
def setup
|
|
|
|
Thread::Threads.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
Thread::Threads.each do |t|
|
|
|
|
t.kill if t.alive?
|
|
|
|
begin
|
|
|
|
t.join
|
|
|
|
rescue Exception
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-26 03:00:02 -04:00
|
|
|
def test_inspect
|
|
|
|
th = Module.new {break module_eval("class C\u{30b9 30ec 30c3 30c9} < Thread; self; end")}.start{}
|
|
|
|
assert_match(/::C\u{30b9 30ec 30c3 30c9}:/, th.inspect)
|
|
|
|
ensure
|
|
|
|
th.join
|
|
|
|
end
|
|
|
|
|
2017-06-28 11:25:30 -04:00
|
|
|
def test_inspect_with_fiber
|
|
|
|
inspect1 = inspect2 = nil
|
|
|
|
|
|
|
|
Thread.new{
|
|
|
|
inspect1 = Thread.current.inspect
|
|
|
|
Fiber.new{
|
|
|
|
inspect2 = Thread.current.inspect
|
|
|
|
}.resume
|
|
|
|
}.join
|
|
|
|
|
|
|
|
assert_equal inspect1, inspect2, '[Bug #13689]'
|
|
|
|
end
|
|
|
|
|
2012-10-29 13:22:36 -04:00
|
|
|
def test_main_thread_variable_in_enumerator
|
|
|
|
assert_equal Thread.main, Thread.current
|
|
|
|
|
|
|
|
Thread.current.thread_variable_set :foo, "bar"
|
|
|
|
|
|
|
|
thread, value = Fiber.new {
|
|
|
|
Fiber.yield [Thread.current, Thread.current.thread_variable_get(:foo)]
|
|
|
|
}.resume
|
|
|
|
|
|
|
|
assert_equal Thread.current, thread
|
|
|
|
assert_equal Thread.current.thread_variable_get(:foo), value
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_thread_variable_in_enumerator
|
|
|
|
Thread.new {
|
|
|
|
Thread.current.thread_variable_set :foo, "bar"
|
|
|
|
|
|
|
|
thread, value = Fiber.new {
|
|
|
|
Fiber.yield [Thread.current, Thread.current.thread_variable_get(:foo)]
|
|
|
|
}.resume
|
|
|
|
|
|
|
|
assert_equal Thread.current, thread
|
|
|
|
assert_equal Thread.current.thread_variable_get(:foo), value
|
|
|
|
}.join
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_thread_variables
|
|
|
|
assert_equal [], Thread.new { Thread.current.thread_variables }.join.value
|
|
|
|
|
|
|
|
t = Thread.new {
|
|
|
|
Thread.current.thread_variable_set(:foo, "bar")
|
|
|
|
Thread.current.thread_variables
|
|
|
|
}
|
|
|
|
assert_equal [:foo], t.join.value
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_thread_variable?
|
2013-12-13 04:18:05 -05:00
|
|
|
Thread.new { assert_not_send([Thread.current, :thread_variable?, "foo"]) }.value
|
2012-10-29 13:22:36 -04:00
|
|
|
t = Thread.new {
|
|
|
|
Thread.current.thread_variable_set("foo", "bar")
|
|
|
|
}.join
|
|
|
|
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_send([t, :thread_variable?, "foo"])
|
|
|
|
assert_send([t, :thread_variable?, :foo])
|
|
|
|
assert_not_send([t, :thread_variable?, :bar])
|
2012-10-29 13:22:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_thread_variable_strings_and_symbols_are_the_same_key
|
|
|
|
t = Thread.new {}.join
|
|
|
|
t.thread_variable_set("foo", "bar")
|
|
|
|
assert_equal "bar", t.thread_variable_get(:foo)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_thread_variable_frozen
|
|
|
|
t = Thread.new { }.join
|
|
|
|
t.freeze
|
2017-12-11 19:46:34 -05:00
|
|
|
assert_raise(FrozenError) do
|
2012-10-29 13:22:36 -04:00
|
|
|
t.thread_variable_set(:foo, "bar")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-08-27 12:48:14 -04:00
|
|
|
def test_mutex_synchronize
|
2016-08-30 02:22:30 -04:00
|
|
|
m = Thread::Mutex.new
|
2007-08-27 12:48:14 -04:00
|
|
|
r = 0
|
2015-09-15 15:23:43 -04:00
|
|
|
num_threads = 10
|
|
|
|
loop=100
|
|
|
|
(1..num_threads).map{
|
2007-08-27 12:48:14 -04:00
|
|
|
Thread.new{
|
2015-09-15 15:23:43 -04:00
|
|
|
loop.times{
|
2007-08-27 12:48:14 -04:00
|
|
|
m.synchronize{
|
2015-09-15 15:23:43 -04:00
|
|
|
tmp = r
|
|
|
|
# empty and waste loop for making thread preemption
|
|
|
|
100.times {
|
|
|
|
}
|
|
|
|
r = tmp + 1
|
2007-08-27 12:48:14 -04:00
|
|
|
}
|
2015-09-15 15:23:43 -04:00
|
|
|
}
|
2007-08-27 12:48:14 -04:00
|
|
|
}
|
|
|
|
}.each{|e|
|
|
|
|
e.join
|
|
|
|
}
|
2015-09-15 15:23:43 -04:00
|
|
|
assert_equal(num_threads*loop, r)
|
2007-08-27 12:48:14 -04:00
|
|
|
end
|
2008-04-08 23:12:03 -04:00
|
|
|
|
2013-03-29 09:09:52 -04:00
|
|
|
def test_mutex_synchronize_yields_no_block_params
|
|
|
|
bug8097 = '[ruby-core:53424] [Bug #8097]'
|
2016-08-30 02:22:30 -04:00
|
|
|
assert_empty(Thread::Mutex.new.synchronize {|*params| break params}, bug8097)
|
2013-03-29 09:09:52 -04:00
|
|
|
end
|
|
|
|
|
2008-04-08 23:12:03 -04:00
|
|
|
def test_local_barrier
|
|
|
|
dir = File.dirname(__FILE__)
|
|
|
|
lbtest = File.join(dir, "lbtest.rb")
|
|
|
|
$:.unshift File.join(File.dirname(dir), 'ruby')
|
|
|
|
$:.shift
|
2009-11-04 14:01:41 -05:00
|
|
|
3.times {
|
2012-07-22 19:11:05 -04:00
|
|
|
`#{EnvUtil.rubybin} #{lbtest}`
|
2012-12-21 02:35:49 -05:00
|
|
|
assert_not_predicate($?, :coredump?, '[ruby-dev:30653]')
|
2008-04-08 23:12:03 -04:00
|
|
|
}
|
|
|
|
end
|
2008-04-23 11:22:13 -04:00
|
|
|
|
|
|
|
def test_priority
|
|
|
|
c1 = c2 = 0
|
2014-01-16 01:34:47 -05:00
|
|
|
run = true
|
|
|
|
t1 = Thread.new { c1 += 1 while run }
|
2011-11-23 19:46:34 -05:00
|
|
|
t1.priority = 3
|
2014-01-16 01:34:47 -05:00
|
|
|
t2 = Thread.new { c2 += 1 while run }
|
2008-04-23 11:22:13 -04:00
|
|
|
t2.priority = -3
|
2011-11-23 19:46:34 -05:00
|
|
|
assert_equal(3, t1.priority)
|
2008-04-24 10:02:15 -04:00
|
|
|
assert_equal(-3, t2.priority)
|
2008-04-23 11:22:13 -04:00
|
|
|
sleep 0.5
|
2009-11-04 14:01:41 -05:00
|
|
|
5.times do
|
2014-01-16 01:34:47 -05:00
|
|
|
assert_not_predicate(t1, :stop?)
|
|
|
|
assert_not_predicate(t2, :stop?)
|
2011-09-10 06:34:15 -04:00
|
|
|
break if c1 > c2
|
2009-11-04 14:01:41 -05:00
|
|
|
sleep 0.1
|
|
|
|
end
|
2014-01-16 01:34:47 -05:00
|
|
|
run = false
|
2008-04-23 11:22:13 -04:00
|
|
|
t1.kill
|
|
|
|
t2.kill
|
2013-12-14 17:03:19 -05:00
|
|
|
assert_operator(c1, :>, c2, "[ruby-dev:33124]") # not guaranteed
|
2008-04-23 11:22:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_new
|
|
|
|
assert_raise(ThreadError) do
|
|
|
|
Thread.new
|
|
|
|
end
|
|
|
|
|
|
|
|
t1 = Thread.new { sleep }
|
|
|
|
assert_raise(ThreadError) do
|
|
|
|
t1.instance_eval { initialize { } }
|
|
|
|
end
|
|
|
|
|
|
|
|
t2 = Thread.new(&method(:sleep).to_proc)
|
|
|
|
assert_raise(ThreadError) do
|
|
|
|
t2.instance_eval { initialize { } }
|
|
|
|
end
|
|
|
|
|
|
|
|
ensure
|
|
|
|
t1.kill if t1
|
|
|
|
t2.kill if t2
|
|
|
|
end
|
|
|
|
|
2017-03-14 01:54:35 -04:00
|
|
|
def test_new_symbol_proc
|
|
|
|
bug = '[ruby-core:80147] [Bug #13313]'
|
|
|
|
assert_ruby_status([], "#{<<-"begin;"}\n#{<<-'end;'}", bug)
|
|
|
|
begin;
|
|
|
|
exit("1" == Thread.start(1, &:to_s).value)
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
2008-04-23 11:22:13 -04:00
|
|
|
def test_join
|
|
|
|
t = Thread.new { sleep }
|
2013-12-14 17:03:24 -05:00
|
|
|
assert_nil(t.join(0.05))
|
2008-04-23 11:22:13 -04:00
|
|
|
|
|
|
|
ensure
|
|
|
|
t.kill if t
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_join2
|
2013-12-14 17:03:24 -05:00
|
|
|
ok = false
|
|
|
|
t1 = Thread.new { ok = true; sleep }
|
|
|
|
Thread.pass until ok
|
|
|
|
Thread.pass until t1.stop?
|
2008-04-23 11:22:13 -04:00
|
|
|
t2 = Thread.new do
|
2013-12-14 17:03:24 -05:00
|
|
|
Thread.pass while ok
|
|
|
|
t1.join(0.01)
|
2008-04-23 11:22:13 -04:00
|
|
|
end
|
|
|
|
t3 = Thread.new do
|
2013-12-14 17:03:24 -05:00
|
|
|
ok = false
|
2008-04-23 11:22:13 -04:00
|
|
|
t1.join
|
|
|
|
end
|
|
|
|
assert_nil(t2.value)
|
2013-12-14 17:03:24 -05:00
|
|
|
t1.wakeup
|
2008-04-23 11:22:13 -04:00
|
|
|
assert_equal(t1, t3.value)
|
|
|
|
|
|
|
|
ensure
|
|
|
|
t1.kill if t1
|
|
|
|
t2.kill if t2
|
|
|
|
t3.kill if t3
|
|
|
|
end
|
|
|
|
|
2018-05-15 10:31:47 -04:00
|
|
|
{ 'FIXNUM_MAX' => RbConfig::LIMITS['FIXNUM_MAX'],
|
|
|
|
'UINT64_MAX' => RbConfig::LIMITS['UINT64_MAX'],
|
|
|
|
'INFINITY' => Float::INFINITY
|
|
|
|
}.each do |name, limit|
|
|
|
|
define_method("test_join_limit_#{name}") do
|
2018-02-03 17:24:17 -05:00
|
|
|
t = Thread.new {}
|
|
|
|
assert_same t, t.join(limit), "limit=#{limit.inspect}"
|
|
|
|
end
|
2018-05-15 10:31:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
{ 'minus_1' => -1,
|
|
|
|
'minus_0_1' => -0.1,
|
|
|
|
'FIXNUM_MIN' => RbConfig::LIMITS['FIXNUM_MIN'],
|
|
|
|
'INT64_MIN' => RbConfig::LIMITS['INT64_MIN'],
|
|
|
|
'minus_INFINITY' => -Float::INFINITY
|
|
|
|
}.each do |name, limit|
|
|
|
|
define_method("test_join_limit_negative_#{name}") do
|
|
|
|
t = Thread.new { sleep }
|
|
|
|
begin
|
|
|
|
assert_nothing_raised(Timeout::Error) do
|
|
|
|
Timeout.timeout(30) do
|
|
|
|
assert_nil t.join(limit), "limit=#{limit.inspect}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
t.kill
|
|
|
|
end
|
2018-02-17 22:00:33 -05:00
|
|
|
end
|
2018-02-03 17:24:17 -05:00
|
|
|
end
|
|
|
|
|
2008-04-23 11:22:13 -04:00
|
|
|
def test_kill_main_thread
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err([], <<-INPUT, %w(1), [])
|
|
|
|
p 1
|
|
|
|
Thread.kill Thread.current
|
|
|
|
p 2
|
|
|
|
INPUT
|
2008-04-23 11:22:13 -04:00
|
|
|
end
|
|
|
|
|
2011-06-09 09:38:27 -04:00
|
|
|
def test_kill_wrong_argument
|
|
|
|
bug4367 = '[ruby-core:35086]'
|
|
|
|
assert_raise(TypeError, bug4367) {
|
|
|
|
Thread.kill(nil)
|
|
|
|
}
|
2011-06-09 09:58:09 -04:00
|
|
|
o = Object.new
|
|
|
|
assert_raise(TypeError, bug4367) {
|
|
|
|
Thread.kill(o)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_kill_thread_subclass
|
|
|
|
c = Class.new(Thread)
|
|
|
|
t = c.new { sleep 10 }
|
|
|
|
assert_nothing_raised { Thread.kill(t) }
|
|
|
|
assert_equal(nil, t.value)
|
2011-06-09 09:38:27 -04:00
|
|
|
end
|
|
|
|
|
2008-04-23 11:22:13 -04:00
|
|
|
def test_exit
|
|
|
|
s = 0
|
|
|
|
Thread.new do
|
|
|
|
s += 1
|
|
|
|
Thread.exit
|
|
|
|
s += 2
|
|
|
|
end.join
|
|
|
|
assert_equal(1, s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_wakeup
|
|
|
|
s = 0
|
|
|
|
t = Thread.new do
|
|
|
|
s += 1
|
|
|
|
Thread.stop
|
|
|
|
s += 1
|
|
|
|
end
|
2013-12-14 17:03:24 -05:00
|
|
|
Thread.pass until t.stop?
|
mjit_compile.c: merge initial JIT compiler
which has been developed by Takashi Kokubun <takashikkbn@gmail> as
YARV-MJIT. Many of its bugs are fixed by wanabe <s.wanabe@gmail.com>.
This JIT compiler is designed to be a safe migration path to introduce
JIT compiler to MRI. So this commit does not include any bytecode
changes or dynamic instruction modifications, which are done in original
MJIT.
This commit even strips off some aggressive optimizations from
YARV-MJIT, and thus it's slower than YARV-MJIT too. But it's still
fairly faster than Ruby 2.5 in some benchmarks (attached below).
Note that this JIT compiler passes `make test`, `make test-all`, `make
test-spec` without JIT, and even with JIT. Not only it's perfectly safe
with JIT disabled because it does not replace VM instructions unlike
MJIT, but also with JIT enabled it stably runs Ruby applications
including Rails applications.
I'm expecting this version as just "initial" JIT compiler. I have many
optimization ideas which are skipped for initial merging, and you may
easily replace this JIT compiler with a faster one by just replacing
mjit_compile.c. `mjit_compile` interface is designed for the purpose.
common.mk: update dependencies for mjit_compile.c.
internal.h: declare `rb_vm_insn_addr2insn` for MJIT.
vm.c: exclude some definitions if `-DMJIT_HEADER` is provided to
compiler. This avoids to include some functions which take a long time
to compile, e.g. vm_exec_core. Some of the purpose is achieved in
transform_mjit_header.rb (see `IGNORED_FUNCTIONS`) but others are
manually resolved for now. Load mjit_helper.h for MJIT header.
mjit_helper.h: New. This is a file used only by JIT-ed code. I'll
refactor `mjit_call_cfunc` later.
vm_eval.c: add some #ifdef switches to skip compiling some functions
like Init_vm_eval.
win32/mkexports.rb: export thread/ec functions, which are used by MJIT.
include/ruby/defines.h: add MJIT_FUNC_EXPORTED macro alis to clarify
that a function is exported only for MJIT.
array.c: export a function used by MJIT.
bignum.c: ditto.
class.c: ditto.
compile.c: ditto.
error.c: ditto.
gc.c: ditto.
hash.c: ditto.
iseq.c: ditto.
numeric.c: ditto.
object.c: ditto.
proc.c: ditto.
re.c: ditto.
st.c: ditto.
string.c: ditto.
thread.c: ditto.
variable.c: ditto.
vm_backtrace.c: ditto.
vm_insnhelper.c: ditto.
vm_method.c: ditto.
I would like to improve maintainability of function exports, but I
believe this way is acceptable as initial merging if we clarify the
new exports are for MJIT (so that we can use them as TODO list to fix)
and add unit tests to detect unresolved symbols.
I'll add unit tests of JIT compilations in succeeding commits.
Author: Takashi Kokubun <takashikkbn@gmail.com>
Contributor: wanabe <s.wanabe@gmail.com>
Part of [Feature #14235]
---
* Known issues
* Code generated by gcc is faster than clang. The benchmark may be worse
in macOS. Following benchmark result is provided by gcc w/ Linux.
* Performance is decreased when Google Chrome is running
* JIT can work on MinGW, but it doesn't improve performance at least
in short running benchmark.
* Currently it doesn't perform well with Rails. We'll try to fix this
before release.
---
* Benchmark reslts
Benchmarked with:
Intel 4.0GHz i7-4790K with 16GB memory under x86-64 Ubuntu 8 Cores
- 2.0.0-p0: Ruby 2.0.0-p0
- r62186: Ruby trunk (early 2.6.0), before MJIT changes
- JIT off: On this commit, but without `--jit` option
- JIT on: On this commit, and with `--jit` option
** Optcarrot fps
Benchmark: https://github.com/mame/optcarrot
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:--------|:--------|:--------|:--------|:--------|
|fps |37.32 |51.46 |51.31 |58.88 |
|vs 2.0.0 |1.00x |1.38x |1.37x |1.58x |
** MJIT benchmarks
Benchmark: https://github.com/benchmark-driver/mjit-benchmarks
(Original: https://github.com/vnmakarov/ruby/tree/rtl_mjit_branch/MJIT-benchmarks)
| |2.0.0-p0 |r62186 |JIT off |JIT on |
|:----------|:--------|:--------|:--------|:--------|
|aread |1.00 |1.09 |1.07 |2.19 |
|aref |1.00 |1.13 |1.11 |2.22 |
|aset |1.00 |1.50 |1.45 |2.64 |
|awrite |1.00 |1.17 |1.13 |2.20 |
|call |1.00 |1.29 |1.26 |2.02 |
|const2 |1.00 |1.10 |1.10 |2.19 |
|const |1.00 |1.11 |1.10 |2.19 |
|fannk |1.00 |1.04 |1.02 |1.00 |
|fib |1.00 |1.32 |1.31 |1.84 |
|ivread |1.00 |1.13 |1.12 |2.43 |
|ivwrite |1.00 |1.23 |1.21 |2.40 |
|mandelbrot |1.00 |1.13 |1.16 |1.28 |
|meteor |1.00 |2.97 |2.92 |3.17 |
|nbody |1.00 |1.17 |1.15 |1.49 |
|nest-ntimes|1.00 |1.22 |1.20 |1.39 |
|nest-while |1.00 |1.10 |1.10 |1.37 |
|norm |1.00 |1.18 |1.16 |1.24 |
|nsvb |1.00 |1.16 |1.16 |1.17 |
|red-black |1.00 |1.02 |0.99 |1.12 |
|sieve |1.00 |1.30 |1.28 |1.62 |
|trees |1.00 |1.14 |1.13 |1.19 |
|while |1.00 |1.12 |1.11 |2.41 |
** Discourse's script/bench.rb
Benchmark: https://github.com/discourse/discourse/blob/v1.8.7/script/bench.rb
NOTE: Rails performance was somehow a little degraded with JIT for now.
We should fix this.
(At least I know opt_aref is performing badly in JIT and I have an idea
to fix it. Please wait for the fix.)
*** JIT off
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 17
75: 18
90: 22
99: 29
home_admin:
50: 21
75: 21
90: 27
99: 40
topic_admin:
50: 17
75: 18
90: 22
99: 32
categories:
50: 35
75: 41
90: 43
99: 77
home:
50: 39
75: 46
90: 49
99: 95
topic:
50: 46
75: 52
90: 56
99: 101
*** JIT on
Your Results: (note for timings- percentile is first, duration is second in millisecs)
categories_admin:
50: 19
75: 21
90: 25
99: 33
home_admin:
50: 24
75: 26
90: 30
99: 35
topic_admin:
50: 19
75: 20
90: 25
99: 30
categories:
50: 40
75: 44
90: 48
99: 76
home:
50: 42
75: 48
90: 51
99: 89
topic:
50: 49
75: 55
90: 58
99: 99
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62197 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-04 06:22:28 -05:00
|
|
|
sleep 1 if RubyVM::MJIT.enabled? # t.stop? behaves unexpectedly with --jit-wait
|
2008-04-23 11:22:13 -04:00
|
|
|
assert_equal(1, s)
|
|
|
|
t.wakeup
|
2013-12-14 17:03:24 -05:00
|
|
|
Thread.pass while t.alive?
|
2008-04-23 11:22:13 -04:00
|
|
|
assert_equal(2, s)
|
|
|
|
assert_raise(ThreadError) { t.wakeup }
|
|
|
|
|
|
|
|
ensure
|
|
|
|
t.kill if t
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stop
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err([], <<-INPUT, %w(2), [])
|
|
|
|
begin
|
|
|
|
Thread.stop
|
|
|
|
p 1
|
|
|
|
rescue ThreadError
|
|
|
|
p 2
|
|
|
|
end
|
|
|
|
INPUT
|
2008-04-23 11:22:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_list
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err([], <<-INPUT) do |r, e|
|
|
|
|
t1 = Thread.new { sleep }
|
2008-08-13 10:22:48 -04:00
|
|
|
Thread.pass
|
2013-12-14 17:03:24 -05:00
|
|
|
t2 = Thread.new { loop { Thread.pass } }
|
2010-06-23 01:32:46 -04:00
|
|
|
Thread.new { }.join
|
2010-02-19 02:01:11 -05:00
|
|
|
p [Thread.current, t1, t2].map{|t| t.object_id }.sort
|
|
|
|
p Thread.list.map{|t| t.object_id }.sort
|
2008-07-15 11:26:04 -04:00
|
|
|
INPUT
|
|
|
|
assert_equal(r.first, r.last)
|
|
|
|
assert_equal([], e)
|
2008-04-23 11:22:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_main
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err([], <<-INPUT, %w(true false), [])
|
|
|
|
p Thread.main == Thread.current
|
|
|
|
Thread.new { p Thread.main == Thread.current }.join
|
|
|
|
INPUT
|
2008-04-23 11:22:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_abort_on_exception
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err([], <<-INPUT, %w(false 1), [])
|
|
|
|
p Thread.abort_on_exception
|
|
|
|
begin
|
2017-12-12 13:43:42 -05:00
|
|
|
t = Thread.new {
|
|
|
|
Thread.current.report_on_exception = false
|
|
|
|
raise
|
|
|
|
}
|
2013-12-14 17:03:24 -05:00
|
|
|
Thread.pass until t.stop?
|
2008-07-15 11:26:04 -04:00
|
|
|
p 1
|
|
|
|
rescue
|
|
|
|
p 2
|
|
|
|
end
|
|
|
|
INPUT
|
2008-04-23 11:22:13 -04:00
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err([], <<-INPUT, %w(true 2), [])
|
|
|
|
Thread.abort_on_exception = true
|
|
|
|
p Thread.abort_on_exception
|
|
|
|
begin
|
2017-12-12 13:43:42 -05:00
|
|
|
Thread.new {
|
|
|
|
Thread.current.report_on_exception = false
|
|
|
|
raise
|
|
|
|
}
|
2008-07-15 11:26:04 -04:00
|
|
|
sleep 0.5
|
|
|
|
p 1
|
|
|
|
rescue
|
|
|
|
p 2
|
|
|
|
end
|
|
|
|
INPUT
|
2008-04-23 11:22:13 -04:00
|
|
|
|
2011-02-11 05:45:34 -05:00
|
|
|
assert_in_out_err(%w(--disable-gems -d), <<-INPUT, %w(false 2), %r".+")
|
2008-07-15 11:26:04 -04:00
|
|
|
p Thread.abort_on_exception
|
|
|
|
begin
|
2013-12-14 17:03:24 -05:00
|
|
|
t = Thread.new { raise }
|
|
|
|
Thread.pass until t.stop?
|
2008-07-15 11:26:04 -04:00
|
|
|
p 1
|
|
|
|
rescue
|
|
|
|
p 2
|
|
|
|
end
|
|
|
|
INPUT
|
2008-04-23 11:22:13 -04:00
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err([], <<-INPUT, %w(false true 2), [])
|
|
|
|
p Thread.abort_on_exception
|
|
|
|
begin
|
2013-12-14 17:03:24 -05:00
|
|
|
ok = false
|
2017-12-12 13:43:42 -05:00
|
|
|
t = Thread.new {
|
|
|
|
Thread.current.report_on_exception = false
|
|
|
|
Thread.pass until ok
|
|
|
|
raise
|
|
|
|
}
|
2008-07-15 11:26:04 -04:00
|
|
|
t.abort_on_exception = true
|
|
|
|
p t.abort_on_exception
|
2013-12-14 17:03:24 -05:00
|
|
|
ok = 1
|
2008-07-15 11:26:04 -04:00
|
|
|
sleep 1
|
|
|
|
p 1
|
|
|
|
rescue
|
|
|
|
p 2
|
|
|
|
end
|
|
|
|
INPUT
|
2008-04-23 11:22:13 -04:00
|
|
|
end
|
|
|
|
|
2016-06-05 20:25:38 -04:00
|
|
|
def test_report_on_exception
|
2017-09-18 22:42:08 -04:00
|
|
|
assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")
|
|
|
|
begin;
|
2016-08-30 02:22:30 -04:00
|
|
|
q1 = Thread::Queue.new
|
|
|
|
q2 = Thread::Queue.new
|
2016-06-05 20:25:38 -04:00
|
|
|
|
2017-12-12 13:43:42 -05:00
|
|
|
assert_equal(true, Thread.report_on_exception,
|
|
|
|
"global flag is true by default")
|
2017-12-14 08:08:02 -05:00
|
|
|
assert_equal(true, Thread.current.report_on_exception,
|
|
|
|
"the main thread has report_on_exception=true")
|
2016-06-05 20:25:38 -04:00
|
|
|
|
2017-12-12 13:43:42 -05:00
|
|
|
Thread.report_on_exception = true
|
|
|
|
Thread.current.report_on_exception = false
|
|
|
|
assert_equal(true,
|
2016-06-05 20:25:38 -04:00
|
|
|
Thread.start {Thread.current.report_on_exception}.value,
|
2017-12-12 13:43:42 -05:00
|
|
|
"should not inherit from the parent thread but from the global flag")
|
2016-06-05 20:25:38 -04:00
|
|
|
|
2017-12-12 13:43:42 -05:00
|
|
|
assert_warn("", "exception should be ignored silently when false") {
|
2016-06-05 20:25:38 -04:00
|
|
|
th = Thread.start {
|
2017-12-12 13:43:42 -05:00
|
|
|
Thread.current.report_on_exception = false
|
2016-06-05 20:25:38 -04:00
|
|
|
q1.push(Thread.current.report_on_exception)
|
|
|
|
raise "report 1"
|
|
|
|
}
|
|
|
|
assert_equal(false, q1.pop)
|
|
|
|
Thread.pass while th.alive?
|
|
|
|
}
|
|
|
|
|
2017-12-12 13:43:42 -05:00
|
|
|
assert_warn(/report 2/, "exception should be reported when true") {
|
2016-06-05 20:25:38 -04:00
|
|
|
th = Thread.start {
|
|
|
|
q1.push(Thread.current.report_on_exception = true)
|
|
|
|
raise "report 2"
|
|
|
|
}
|
|
|
|
assert_equal(true, q1.pop)
|
|
|
|
Thread.pass while th.alive?
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_warn("", "the global flag should not affect already started threads") {
|
2017-12-12 13:43:42 -05:00
|
|
|
Thread.report_on_exception = false
|
2016-06-05 20:25:38 -04:00
|
|
|
th = Thread.start {
|
|
|
|
q2.pop
|
|
|
|
q1.push(Thread.current.report_on_exception)
|
|
|
|
raise "report 3"
|
|
|
|
}
|
|
|
|
q2.push(Thread.report_on_exception = true)
|
|
|
|
assert_equal(false, q1.pop)
|
|
|
|
Thread.pass while th.alive?
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_warn(/report 4/, "should defaults to the global flag at the start") {
|
2017-12-12 13:43:42 -05:00
|
|
|
Thread.report_on_exception = true
|
2016-06-05 20:25:38 -04:00
|
|
|
th = Thread.start {
|
|
|
|
q1.push(Thread.current.report_on_exception)
|
|
|
|
raise "report 4"
|
|
|
|
}
|
|
|
|
assert_equal(true, q1.pop)
|
|
|
|
Thread.pass while th.alive?
|
|
|
|
}
|
2017-09-18 22:42:08 -04:00
|
|
|
|
2017-12-12 13:43:42 -05:00
|
|
|
assert_warn(/report 5/, "should first report and then raise with report_on_exception + abort_on_exception") {
|
2017-09-18 22:42:08 -04:00
|
|
|
th = Thread.start {
|
|
|
|
Thread.current.report_on_exception = true
|
|
|
|
Thread.current.abort_on_exception = true
|
|
|
|
q2.pop
|
|
|
|
raise "report 5"
|
|
|
|
}
|
|
|
|
assert_raise_with_message(RuntimeError, "report 5") {
|
|
|
|
q2.push(true)
|
|
|
|
Thread.pass while th.alive?
|
|
|
|
}
|
|
|
|
}
|
2016-06-05 20:25:38 -04:00
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
2008-04-23 11:22:13 -04:00
|
|
|
def test_status_and_stop_p
|
2017-12-12 13:44:49 -05:00
|
|
|
a = ::Thread.new {
|
|
|
|
Thread.current.report_on_exception = false
|
|
|
|
raise("die now")
|
|
|
|
}
|
2008-04-23 11:22:13 -04:00
|
|
|
b = Thread.new { Thread.stop }
|
|
|
|
c = Thread.new { Thread.exit }
|
|
|
|
e = Thread.current
|
2013-12-14 17:03:24 -05:00
|
|
|
Thread.pass while a.alive? or !b.stop? or c.alive?
|
2008-04-23 11:22:13 -04:00
|
|
|
|
|
|
|
assert_equal(nil, a.status)
|
2012-12-21 02:35:49 -05:00
|
|
|
assert_predicate(a, :stop?)
|
2008-08-25 09:46:34 -04:00
|
|
|
|
2008-04-23 11:22:13 -04:00
|
|
|
assert_equal("sleep", b.status)
|
2012-12-21 02:35:49 -05:00
|
|
|
assert_predicate(b, :stop?)
|
2008-08-25 09:46:34 -04:00
|
|
|
|
2008-04-23 11:22:13 -04:00
|
|
|
assert_equal(false, c.status)
|
|
|
|
assert_match(/^#<TestThread::Thread:.* dead>$/, c.inspect)
|
2012-12-21 02:35:49 -05:00
|
|
|
assert_predicate(c, :stop?)
|
2008-08-25 09:46:34 -04:00
|
|
|
|
2012-04-21 21:46:45 -04:00
|
|
|
es1 = e.status
|
|
|
|
es2 = e.stop?
|
|
|
|
assert_equal(["run", false], [es1, es2])
|
2008-04-23 11:22:13 -04:00
|
|
|
|
|
|
|
ensure
|
|
|
|
a.kill if a
|
|
|
|
b.kill if b
|
|
|
|
c.kill if c
|
|
|
|
end
|
|
|
|
|
2014-05-27 23:05:48 -04:00
|
|
|
def test_switch_while_busy_loop
|
|
|
|
bug1402 = "[ruby-dev:38319] [Bug #1402]"
|
|
|
|
flag = true
|
|
|
|
th = Thread.current
|
|
|
|
waiter = Thread.start {
|
|
|
|
sleep 0.1
|
|
|
|
flag = false
|
|
|
|
sleep 1
|
|
|
|
th.raise(bug1402)
|
|
|
|
}
|
|
|
|
assert_nothing_raised(RuntimeError, bug1402) do
|
|
|
|
nil while flag
|
|
|
|
end
|
|
|
|
assert(!flag, bug1402)
|
|
|
|
ensure
|
|
|
|
waiter.kill.join
|
|
|
|
end
|
|
|
|
|
2008-04-23 11:22:13 -04:00
|
|
|
def test_safe_level
|
2013-12-14 17:03:24 -05:00
|
|
|
ok = false
|
2013-12-16 00:03:56 -05:00
|
|
|
t = Thread.new do
|
|
|
|
EnvUtil.suppress_warning do
|
* include/ruby/ruby.h: $SAFE=3 is now obsolete.
* ext/socket/init.c, ext/socket/socket.c, ext/socket/tcpsocket.c
ext/socket/udpsocket.c, gc.c, object.c, re.c, safe.c: removed code
for $SAFE=3
* bootstraptest/test_method.rb, test/erb/test_erb.rb, test/ruby/test_dir.rb
test/ruby/test_file.rb, test/ruby/test_method.rb, test/ruby/test_regexp.rb
test/ruby/test_thread.rb: remove tests for $SAFE=3
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50932 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-06-17 01:29:51 -04:00
|
|
|
$SAFE = 1
|
2013-12-16 00:03:56 -05:00
|
|
|
end
|
|
|
|
ok = true
|
|
|
|
sleep
|
|
|
|
end
|
2013-12-14 17:03:24 -05:00
|
|
|
Thread.pass until ok
|
2017-12-28 15:09:24 -05:00
|
|
|
assert_equal($SAFE, Thread.current.safe_level)
|
|
|
|
assert_equal($SAFE, t.safe_level)
|
2008-04-23 11:22:13 -04:00
|
|
|
ensure
|
2017-12-28 15:09:24 -05:00
|
|
|
$SAFE = 0
|
2008-04-23 11:22:13 -04:00
|
|
|
t.kill if t
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_thread_local
|
|
|
|
t = Thread.new { sleep }
|
|
|
|
|
|
|
|
assert_equal(false, t.key?(:foo))
|
|
|
|
|
|
|
|
t["foo"] = "foo"
|
|
|
|
t["bar"] = "bar"
|
|
|
|
t["baz"] = "baz"
|
|
|
|
|
|
|
|
assert_equal(true, t.key?(:foo))
|
|
|
|
assert_equal(true, t.key?("foo"))
|
|
|
|
assert_equal(false, t.key?(:qux))
|
|
|
|
assert_equal(false, t.key?("qux"))
|
|
|
|
|
2016-11-14 14:27:24 -05:00
|
|
|
assert_equal([:foo, :bar, :baz].sort, t.keys.sort)
|
2008-04-23 11:22:13 -04:00
|
|
|
|
|
|
|
ensure
|
|
|
|
t.kill if t
|
|
|
|
end
|
|
|
|
|
2017-02-22 02:16:13 -05:00
|
|
|
def test_thread_local_fetch
|
|
|
|
t = Thread.new { sleep }
|
|
|
|
|
|
|
|
assert_equal(false, t.key?(:foo))
|
|
|
|
|
|
|
|
t["foo"] = "foo"
|
|
|
|
t["bar"] = "bar"
|
|
|
|
t["baz"] = "baz"
|
|
|
|
|
|
|
|
x = nil
|
|
|
|
assert_equal("foo", t.fetch(:foo, 0))
|
|
|
|
assert_equal("foo", t.fetch(:foo) {x = true})
|
|
|
|
assert_nil(x)
|
|
|
|
assert_equal("foo", t.fetch("foo", 0))
|
|
|
|
assert_equal("foo", t.fetch("foo") {x = true})
|
|
|
|
assert_nil(x)
|
|
|
|
|
|
|
|
x = nil
|
|
|
|
assert_equal(0, t.fetch(:qux, 0))
|
|
|
|
assert_equal(1, t.fetch(:qux) {x = 1})
|
|
|
|
assert_equal(1, x)
|
|
|
|
assert_equal(2, t.fetch("qux", 2))
|
|
|
|
assert_equal(3, t.fetch("qux") {x = 3})
|
|
|
|
assert_equal(3, x)
|
|
|
|
|
2017-12-27 19:00:05 -05:00
|
|
|
e = assert_raise(KeyError) {t.fetch(:qux)}
|
|
|
|
assert_equal(:qux, e.key)
|
|
|
|
assert_equal(t, e.receiver)
|
2017-02-22 02:16:13 -05:00
|
|
|
ensure
|
|
|
|
t.kill if t
|
|
|
|
end
|
|
|
|
|
2008-04-23 11:22:13 -04:00
|
|
|
def test_thread_local_security
|
2017-12-12 13:44:49 -05:00
|
|
|
Thread.new do
|
|
|
|
Thread.current[:foo] = :bar
|
|
|
|
Thread.current.freeze
|
|
|
|
assert_raise(FrozenError) do
|
2008-04-23 11:22:13 -04:00
|
|
|
Thread.current[:foo] = :baz
|
2017-12-12 13:44:49 -05:00
|
|
|
end
|
|
|
|
end.join
|
2008-04-23 11:22:13 -04:00
|
|
|
end
|
|
|
|
|
2014-12-28 21:18:20 -05:00
|
|
|
def test_thread_local_dynamic_symbol
|
|
|
|
bug10667 = '[ruby-core:67185] [Bug #10667]'
|
|
|
|
t = Thread.new {}.join
|
|
|
|
key_str = "foo#{rand}"
|
|
|
|
key_sym = key_str.to_sym
|
|
|
|
t.thread_variable_set(key_str, "bar")
|
|
|
|
assert_equal("bar", t.thread_variable_get(key_str), "#{bug10667}: string key")
|
|
|
|
assert_equal("bar", t.thread_variable_get(key_sym), "#{bug10667}: symbol key")
|
|
|
|
end
|
|
|
|
|
2008-04-23 11:22:13 -04:00
|
|
|
def test_select_wait
|
2013-12-14 17:03:24 -05:00
|
|
|
assert_nil(IO.select(nil, nil, nil, 0.001))
|
2008-04-23 11:22:13 -04:00
|
|
|
t = Thread.new do
|
2008-04-24 10:02:15 -04:00
|
|
|
IO.select(nil, nil, nil, nil)
|
2008-04-23 11:22:13 -04:00
|
|
|
end
|
2013-12-14 17:03:24 -05:00
|
|
|
Thread.pass until t.stop?
|
|
|
|
assert_predicate(t, :alive?)
|
2008-04-23 11:22:13 -04:00
|
|
|
t.kill
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mutex_deadlock
|
2016-08-30 02:22:30 -04:00
|
|
|
m = Thread::Mutex.new
|
2008-04-23 11:22:13 -04:00
|
|
|
m.synchronize do
|
|
|
|
assert_raise(ThreadError) do
|
|
|
|
m.synchronize do
|
|
|
|
assert(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mutex_interrupt
|
2016-08-30 02:22:30 -04:00
|
|
|
m = Thread::Mutex.new
|
2008-04-23 11:22:13 -04:00
|
|
|
m.lock
|
|
|
|
t = Thread.new do
|
|
|
|
m.lock
|
|
|
|
:foo
|
|
|
|
end
|
2013-12-14 17:03:24 -05:00
|
|
|
Thread.pass until t.stop?
|
2008-04-23 11:22:13 -04:00
|
|
|
t.kill
|
|
|
|
assert_nil(t.value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mutex_illegal_unlock
|
2016-08-30 02:22:30 -04:00
|
|
|
m = Thread::Mutex.new
|
2008-04-23 11:22:13 -04:00
|
|
|
m.lock
|
2017-12-12 13:44:49 -05:00
|
|
|
Thread.new do
|
|
|
|
assert_raise(ThreadError) do
|
2008-04-23 11:22:13 -04:00
|
|
|
m.unlock
|
2017-12-12 13:44:49 -05:00
|
|
|
end
|
|
|
|
end.join
|
2008-04-23 11:22:13 -04:00
|
|
|
end
|
|
|
|
|
2008-06-12 11:42:14 -04:00
|
|
|
def test_mutex_fifo_like_lock
|
2016-08-30 02:22:30 -04:00
|
|
|
m1 = Thread::Mutex.new
|
|
|
|
m2 = Thread::Mutex.new
|
2008-06-12 11:42:14 -04:00
|
|
|
m1.lock
|
|
|
|
m2.lock
|
|
|
|
m1.unlock
|
|
|
|
m2.unlock
|
|
|
|
assert_equal(false, m1.locked?)
|
|
|
|
assert_equal(false, m2.locked?)
|
|
|
|
|
2016-08-30 02:22:30 -04:00
|
|
|
m3 = Thread::Mutex.new
|
2008-06-12 11:42:14 -04:00
|
|
|
m1.lock
|
|
|
|
m2.lock
|
|
|
|
m3.lock
|
|
|
|
m1.unlock
|
|
|
|
m2.unlock
|
|
|
|
m3.unlock
|
|
|
|
assert_equal(false, m1.locked?)
|
|
|
|
assert_equal(false, m2.locked?)
|
|
|
|
assert_equal(false, m3.locked?)
|
|
|
|
end
|
|
|
|
|
2008-12-28 22:03:09 -05:00
|
|
|
def test_mutex_trylock
|
2016-08-30 02:22:30 -04:00
|
|
|
m = Thread::Mutex.new
|
2008-12-28 22:03:09 -05:00
|
|
|
assert_equal(true, m.try_lock)
|
|
|
|
assert_equal(false, m.try_lock, '[ruby-core:20943]')
|
|
|
|
|
|
|
|
Thread.new{
|
|
|
|
assert_equal(false, m.try_lock)
|
|
|
|
}.join
|
|
|
|
|
|
|
|
m.unlock
|
|
|
|
end
|
|
|
|
|
2013-12-03 08:32:24 -05:00
|
|
|
def test_recursive_outer
|
|
|
|
arr = []
|
|
|
|
obj = Struct.new(:foo, :visited).new(arr, false)
|
|
|
|
arr << obj
|
|
|
|
def obj.hash
|
|
|
|
self[:visited] = true
|
|
|
|
super
|
|
|
|
raise "recursive_outer should short circuit intermediate calls"
|
|
|
|
end
|
|
|
|
assert_nothing_raised {arr.hash}
|
|
|
|
assert(obj[:visited], "obj.hash was not called")
|
|
|
|
end
|
|
|
|
|
2011-02-11 05:45:34 -05:00
|
|
|
def test_thread_instance_variable
|
|
|
|
bug4389 = '[ruby-core:35192]'
|
|
|
|
assert_in_out_err([], <<-INPUT, %w(), [], bug4389)
|
|
|
|
class << Thread.current
|
|
|
|
@data = :data
|
|
|
|
end
|
|
|
|
INPUT
|
|
|
|
end
|
2011-07-30 22:32:48 -04:00
|
|
|
|
|
|
|
def test_no_valid_cfp
|
2016-10-31 11:17:26 -04:00
|
|
|
skip 'with win32ole, cannot run this testcase because win32ole redefines Thread#initialize' if defined?(WIN32OLE)
|
2011-07-30 22:32:48 -04:00
|
|
|
bug5083 = '[ruby-dev:44208]'
|
2014-03-05 08:27:22 -05:00
|
|
|
assert_equal([], Thread.new(&Module.method(:nesting)).value, bug5083)
|
|
|
|
assert_instance_of(Thread, Thread.new(:to_s, &Class.new.method(:undef_method)).join, bug5083)
|
2011-07-30 22:32:48 -04:00
|
|
|
end
|
2012-07-19 10:19:40 -04:00
|
|
|
|
2012-12-23 05:18:58 -05:00
|
|
|
def make_handle_interrupt_test_thread1 flag
|
2012-07-19 10:19:40 -04:00
|
|
|
r = []
|
2017-04-20 18:52:57 -04:00
|
|
|
ready_q = Queue.new
|
|
|
|
done_q = Queue.new
|
2012-07-19 10:19:40 -04:00
|
|
|
th = Thread.new{
|
|
|
|
begin
|
2012-12-23 05:18:58 -05:00
|
|
|
Thread.handle_interrupt(RuntimeError => flag){
|
2012-07-19 10:19:40 -04:00
|
|
|
begin
|
2017-04-20 18:52:57 -04:00
|
|
|
ready_q << true
|
|
|
|
done_q.pop
|
2012-07-19 10:19:40 -04:00
|
|
|
rescue
|
|
|
|
r << :c1
|
|
|
|
end
|
|
|
|
}
|
|
|
|
rescue
|
|
|
|
r << :c2
|
|
|
|
end
|
|
|
|
}
|
2017-04-20 18:52:57 -04:00
|
|
|
ready_q.pop
|
2012-07-19 10:19:40 -04:00
|
|
|
th.raise
|
|
|
|
begin
|
2017-04-20 18:52:57 -04:00
|
|
|
done_q << true
|
2012-07-19 10:19:40 -04:00
|
|
|
th.join
|
|
|
|
rescue
|
|
|
|
r << :c3
|
|
|
|
end
|
|
|
|
r
|
|
|
|
end
|
|
|
|
|
2012-12-23 05:18:58 -05:00
|
|
|
def test_handle_interrupt
|
|
|
|
[[:never, :c2],
|
2012-07-19 10:19:40 -04:00
|
|
|
[:immediate, :c1],
|
|
|
|
[:on_blocking, :c1]].each{|(flag, c)|
|
2012-12-23 05:18:58 -05:00
|
|
|
assert_equal([flag, c], [flag] + make_handle_interrupt_test_thread1(flag))
|
2012-07-19 10:19:40 -04:00
|
|
|
}
|
|
|
|
# TODO: complex cases are needed.
|
|
|
|
end
|
|
|
|
|
2012-12-23 05:18:58 -05:00
|
|
|
def test_handle_interrupt_invalid_argument
|
2012-11-30 12:39:48 -05:00
|
|
|
assert_raise(ArgumentError) {
|
2012-12-23 05:18:58 -05:00
|
|
|
Thread.handle_interrupt(RuntimeError => :immediate) # no block
|
2012-11-30 12:39:48 -05:00
|
|
|
}
|
|
|
|
assert_raise(ArgumentError) {
|
2012-12-23 05:18:58 -05:00
|
|
|
Thread.handle_interrupt(RuntimeError => :xyzzy) {}
|
2012-11-30 12:39:48 -05:00
|
|
|
}
|
|
|
|
assert_raise(TypeError) {
|
2012-12-23 05:18:58 -05:00
|
|
|
Thread.handle_interrupt([]) {} # array
|
2012-11-30 12:39:48 -05:00
|
|
|
}
|
|
|
|
end
|
2012-12-05 04:54:58 -05:00
|
|
|
|
2012-12-23 05:18:58 -05:00
|
|
|
def for_test_handle_interrupt_with_return
|
|
|
|
Thread.handle_interrupt(Object => :never){
|
2012-12-05 04:54:58 -05:00
|
|
|
Thread.current.raise RuntimeError.new("have to be rescured")
|
2012-12-07 00:02:18 -05:00
|
|
|
return
|
2012-12-05 04:54:58 -05:00
|
|
|
}
|
|
|
|
rescue
|
|
|
|
end
|
|
|
|
|
2012-12-23 05:18:58 -05:00
|
|
|
def test_handle_interrupt_with_return
|
2012-12-05 04:54:58 -05:00
|
|
|
assert_nothing_raised do
|
2012-12-23 05:18:58 -05:00
|
|
|
for_test_handle_interrupt_with_return
|
2012-12-05 10:12:58 -05:00
|
|
|
_dummy_for_check_ints=nil
|
2012-12-05 04:54:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-23 05:18:58 -05:00
|
|
|
def test_handle_interrupt_with_break
|
2012-12-05 04:54:58 -05:00
|
|
|
assert_nothing_raised do
|
|
|
|
begin
|
2012-12-23 05:18:58 -05:00
|
|
|
Thread.handle_interrupt(Object => :never){
|
2012-12-05 04:54:58 -05:00
|
|
|
Thread.current.raise RuntimeError.new("have to be rescured")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
rescue
|
|
|
|
end
|
2012-12-05 10:12:58 -05:00
|
|
|
_dummy_for_check_ints=nil
|
2012-12-05 04:54:58 -05:00
|
|
|
end
|
|
|
|
end
|
2012-12-05 08:35:05 -05:00
|
|
|
|
2012-12-23 05:18:58 -05:00
|
|
|
def test_handle_interrupt_blocking
|
2012-12-04 14:06:46 -05:00
|
|
|
r=:ng
|
2012-12-04 13:38:21 -05:00
|
|
|
e=Class.new(Exception)
|
|
|
|
th_s = Thread.current
|
2017-12-12 13:44:49 -05:00
|
|
|
th = Thread.start{
|
|
|
|
assert_raise(RuntimeError) {
|
2012-12-23 05:18:58 -05:00
|
|
|
Thread.handle_interrupt(Object => :on_blocking){
|
2012-12-04 13:38:21 -05:00
|
|
|
begin
|
2015-10-28 14:58:34 -04:00
|
|
|
Thread.pass until r == :wait
|
2017-12-12 13:44:49 -05:00
|
|
|
Thread.current.raise RuntimeError, "will raise in sleep"
|
2015-10-05 10:23:54 -04:00
|
|
|
r = :ok
|
2012-12-04 13:38:21 -05:00
|
|
|
sleep
|
|
|
|
ensure
|
2015-10-01 21:23:48 -04:00
|
|
|
th_s.raise e, "raise from ensure", $@
|
2012-12-04 13:38:21 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
2017-12-12 13:44:49 -05:00
|
|
|
}
|
|
|
|
assert_raise(e) {r = :wait; sleep 0.2}
|
2017-12-12 15:09:36 -05:00
|
|
|
th.join
|
2012-12-04 13:38:21 -05:00
|
|
|
assert_equal(:ok,r)
|
|
|
|
end
|
2012-11-30 12:39:48 -05:00
|
|
|
|
2012-12-23 05:18:58 -05:00
|
|
|
def test_handle_interrupt_and_io
|
2012-12-05 14:37:37 -05:00
|
|
|
assert_in_out_err([], <<-INPUT, %w(ok), [])
|
|
|
|
th_waiting = true
|
|
|
|
|
|
|
|
t = Thread.new {
|
2017-12-12 13:43:42 -05:00
|
|
|
Thread.current.report_on_exception = false
|
2012-12-23 05:18:58 -05:00
|
|
|
Thread.handle_interrupt(RuntimeError => :on_blocking) {
|
2012-12-05 14:37:37 -05:00
|
|
|
nil while th_waiting
|
|
|
|
# async interrupt should be raised _before_ writing puts arguments
|
|
|
|
puts "ng"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-14 17:03:24 -05:00
|
|
|
Thread.pass while t.stop?
|
2012-12-05 14:37:37 -05:00
|
|
|
t.raise RuntimeError
|
|
|
|
th_waiting = false
|
|
|
|
t.join rescue nil
|
|
|
|
puts "ok"
|
|
|
|
INPUT
|
|
|
|
end
|
|
|
|
|
2012-12-23 05:18:58 -05:00
|
|
|
def test_handle_interrupt_and_p
|
2012-12-05 14:37:49 -05:00
|
|
|
assert_in_out_err([], <<-INPUT, %w(:ok :ok), [])
|
2013-12-14 17:03:24 -05:00
|
|
|
th_waiting = false
|
2012-12-05 14:37:49 -05:00
|
|
|
|
|
|
|
t = Thread.new {
|
2017-12-12 13:43:42 -05:00
|
|
|
Thread.current.report_on_exception = false
|
2012-12-23 05:18:58 -05:00
|
|
|
Thread.handle_interrupt(RuntimeError => :on_blocking) {
|
2013-12-14 17:03:24 -05:00
|
|
|
th_waiting = true
|
2012-12-05 14:37:49 -05:00
|
|
|
nil while th_waiting
|
|
|
|
# p shouldn't provide interruptible point
|
|
|
|
p :ok
|
|
|
|
p :ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-14 17:03:24 -05:00
|
|
|
Thread.pass until th_waiting
|
2012-12-05 14:37:49 -05:00
|
|
|
t.raise RuntimeError
|
|
|
|
th_waiting = false
|
|
|
|
t.join rescue nil
|
|
|
|
INPUT
|
|
|
|
end
|
|
|
|
|
2012-12-23 05:18:58 -05:00
|
|
|
def test_handle_interrupted?
|
2016-08-30 02:22:30 -04:00
|
|
|
q = Thread::Queue.new
|
2012-12-23 05:18:58 -05:00
|
|
|
Thread.handle_interrupt(RuntimeError => :never){
|
2013-12-14 17:03:24 -05:00
|
|
|
done = false
|
2012-07-19 10:19:40 -04:00
|
|
|
th = Thread.new{
|
|
|
|
q.push :e
|
|
|
|
begin
|
|
|
|
begin
|
2013-12-14 17:03:24 -05:00
|
|
|
Thread.pass until done
|
2012-07-19 10:19:40 -04:00
|
|
|
rescue => e
|
|
|
|
q.push :ng1
|
|
|
|
end
|
|
|
|
begin
|
2013-02-16 06:14:23 -05:00
|
|
|
Thread.handle_interrupt(Object => :immediate){} if Thread.pending_interrupt?
|
|
|
|
rescue RuntimeError => e
|
2012-07-19 10:19:40 -04:00
|
|
|
q.push :ok
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
q.push :ng2
|
|
|
|
ensure
|
|
|
|
q.push :ng3
|
|
|
|
end
|
|
|
|
}
|
|
|
|
q.pop
|
|
|
|
th.raise
|
2013-12-14 17:03:24 -05:00
|
|
|
done = true
|
2012-07-19 10:19:40 -04:00
|
|
|
th.join
|
|
|
|
assert_equal(:ok, q.pop)
|
|
|
|
}
|
|
|
|
end
|
2012-07-22 12:48:49 -04:00
|
|
|
|
|
|
|
def test_thread_timer_and_ensure
|
2016-02-22 21:03:37 -05:00
|
|
|
assert_normal_exit(<<_eom, 'r36492', timeout: 10)
|
2012-07-22 12:48:49 -04:00
|
|
|
flag = false
|
|
|
|
t = Thread.new do
|
|
|
|
begin
|
|
|
|
sleep
|
|
|
|
ensure
|
|
|
|
1 until flag
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Thread.pass until t.status == "sleep"
|
|
|
|
|
|
|
|
t.kill
|
|
|
|
t.alive? == true
|
|
|
|
flag = true
|
|
|
|
t.join
|
|
|
|
_eom
|
|
|
|
end
|
2008-06-02 08:57:18 -04:00
|
|
|
|
|
|
|
def test_uninitialized
|
2016-01-07 00:49:31 -05:00
|
|
|
c = Class.new(Thread) {def initialize; end}
|
2008-06-02 08:57:18 -04:00
|
|
|
assert_raise(ThreadError) { c.new.start }
|
2016-01-07 00:49:31 -05:00
|
|
|
|
|
|
|
bug11959 = '[ruby-core:72732] [Bug #11959]'
|
|
|
|
|
|
|
|
c = Class.new(Thread) {def initialize; exit; end}
|
|
|
|
assert_raise(ThreadError, bug11959) { c.new }
|
|
|
|
|
|
|
|
c = Class.new(Thread) {def initialize; raise; end}
|
|
|
|
assert_raise(ThreadError, bug11959) { c.new }
|
|
|
|
|
|
|
|
c = Class.new(Thread) {
|
|
|
|
def initialize
|
|
|
|
pending = pending_interrupt?
|
|
|
|
super {pending}
|
|
|
|
end
|
|
|
|
}
|
|
|
|
assert_equal(false, c.new.value, bug11959)
|
2008-06-02 08:57:18 -04:00
|
|
|
end
|
2009-06-14 01:59:23 -04:00
|
|
|
|
|
|
|
def test_backtrace
|
|
|
|
Thread.new{
|
|
|
|
assert_equal(Array, Thread.main.backtrace.class)
|
|
|
|
}.join
|
|
|
|
|
|
|
|
t = Thread.new{}
|
|
|
|
t.join
|
|
|
|
assert_equal(nil, t.backtrace)
|
|
|
|
end
|
2011-12-13 21:26:20 -05:00
|
|
|
|
|
|
|
def test_thread_timer_and_interrupt
|
|
|
|
bug5757 = '[ruby-dev:44985]'
|
2012-01-31 00:29:52 -05:00
|
|
|
pid = nil
|
2014-05-09 20:32:58 -04:00
|
|
|
cmd = 'Signal.trap(:INT, "DEFAULT"); r,=IO.pipe; Thread.start {Thread.pass until Thread.main.stop?; puts; STDOUT.flush}; r.read'
|
2012-04-07 10:10:30 -04:00
|
|
|
opt = {}
|
|
|
|
opt[:new_pgroup] = true if /mswin|mingw/ =~ RUBY_PLATFORM
|
2016-10-23 23:46:51 -04:00
|
|
|
s, t, _err = EnvUtil.invoke_ruby(['-e', cmd], "", true, true, opt) do |in_p, out_p, err_p, cpid|
|
2012-01-31 00:29:52 -05:00
|
|
|
out_p.gets
|
|
|
|
pid = cpid
|
2016-10-23 23:46:51 -04:00
|
|
|
t0 = Time.now.to_f
|
2012-01-31 00:29:52 -05:00
|
|
|
Process.kill(:SIGINT, pid)
|
|
|
|
Process.wait(pid)
|
2016-10-23 23:46:51 -04:00
|
|
|
t1 = Time.now.to_f
|
|
|
|
[$?, t1 - t0, err_p.read]
|
2012-01-27 19:51:40 -05:00
|
|
|
end
|
2012-07-22 19:11:05 -04:00
|
|
|
assert_equal(pid, s.pid, bug5757)
|
2013-05-01 23:34:15 -04:00
|
|
|
assert_equal([false, true, false, Signal.list["INT"]],
|
|
|
|
[s.exited?, s.signaled?, s.stopped?, s.termsig],
|
|
|
|
"[s.exited?, s.signaled?, s.stopped?, s.termsig]")
|
2016-10-23 23:46:51 -04:00
|
|
|
assert_include(0..2, t, bug5757)
|
2011-12-13 21:26:20 -05:00
|
|
|
end
|
2012-11-26 03:05:49 -05:00
|
|
|
|
|
|
|
def test_thread_join_in_trap
|
2013-11-05 07:25:15 -05:00
|
|
|
assert_separately [], <<-'EOS'
|
2014-05-09 20:32:58 -04:00
|
|
|
Signal.trap(:INT, "DEFAULT")
|
2013-12-14 17:03:24 -05:00
|
|
|
t0 = Thread.current
|
2012-11-28 08:24:36 -05:00
|
|
|
assert_nothing_raised{
|
2013-12-14 17:03:24 -05:00
|
|
|
t = Thread.new {Thread.pass until t0.stop?; Process.kill(:INT, $$)}
|
2012-11-26 03:05:49 -05:00
|
|
|
|
|
|
|
Signal.trap :INT do
|
|
|
|
t.join
|
|
|
|
end
|
|
|
|
|
|
|
|
t.join
|
|
|
|
}
|
2013-11-05 07:25:15 -05:00
|
|
|
EOS
|
2013-12-14 17:03:24 -05:00
|
|
|
end
|
2012-11-27 23:09:38 -05:00
|
|
|
|
2013-12-14 17:03:24 -05:00
|
|
|
def test_thread_value_in_trap
|
2013-11-05 07:25:15 -05:00
|
|
|
assert_separately [], <<-'EOS'
|
2014-05-09 20:32:58 -04:00
|
|
|
Signal.trap(:INT, "DEFAULT")
|
2013-12-14 17:03:24 -05:00
|
|
|
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)
|
2013-11-05 07:25:15 -05:00
|
|
|
EOS
|
2012-11-26 03:05:49 -05:00
|
|
|
end
|
|
|
|
|
2012-11-26 21:00:09 -05:00
|
|
|
def test_thread_join_current
|
2013-10-09 04:41:13 -04:00
|
|
|
assert_raise(ThreadError) do
|
2012-11-26 21:00:09 -05:00
|
|
|
Thread.current.join
|
|
|
|
end
|
|
|
|
end
|
2012-11-26 21:00:19 -05:00
|
|
|
|
|
|
|
def test_thread_join_main_thread
|
2017-12-12 13:44:49 -05:00
|
|
|
Thread.new(Thread.current) {|t|
|
|
|
|
assert_raise(ThreadError) do
|
2012-11-26 21:00:19 -05:00
|
|
|
t.join
|
2017-12-12 13:44:49 -05:00
|
|
|
end
|
|
|
|
}.join
|
2012-11-26 21:00:19 -05:00
|
|
|
end
|
2012-11-26 21:00:40 -05:00
|
|
|
|
|
|
|
def test_main_thread_status_at_exit
|
2014-05-17 23:04:52 -04:00
|
|
|
assert_in_out_err([], <<-'INPUT', ["false false aborting"], [])
|
2016-08-30 02:22:30 -04:00
|
|
|
q = Thread::Queue.new
|
2012-11-26 21:00:40 -05:00
|
|
|
Thread.new(Thread.current) {|mth|
|
|
|
|
begin
|
2014-09-21 00:55:49 -04:00
|
|
|
q.push nil
|
2014-08-19 21:33:40 -04:00
|
|
|
mth.run
|
2014-09-22 19:29:15 -04:00
|
|
|
Thread.pass until mth.stop?
|
2014-05-17 23:04:52 -04:00
|
|
|
p :mth_stopped # don't run if killed by rb_thread_terminate_all
|
2012-11-26 21:00:40 -05:00
|
|
|
ensure
|
2014-05-17 23:04:52 -04:00
|
|
|
puts "#{mth.alive?} #{mth.status} #{Thread.current.status}"
|
2012-11-26 21:00:40 -05:00
|
|
|
end
|
|
|
|
}
|
2014-09-21 00:55:49 -04:00
|
|
|
q.pop
|
2012-11-26 21:00:40 -05:00
|
|
|
INPUT
|
|
|
|
end
|
2012-11-28 03:31:03 -05:00
|
|
|
|
|
|
|
def test_thread_status_in_trap
|
|
|
|
# when running trap handler, Thread#status must show "run"
|
|
|
|
# Even though interrupted from sleeping function
|
|
|
|
assert_in_out_err([], <<-INPUT, %w(sleep run), [])
|
|
|
|
Signal.trap(:INT) {
|
|
|
|
puts Thread.current.status
|
2013-12-14 17:03:24 -05:00
|
|
|
exit
|
2012-11-28 03:31:03 -05:00
|
|
|
}
|
2013-12-14 17:03:24 -05:00
|
|
|
t = Thread.current
|
2012-11-28 03:31:03 -05:00
|
|
|
|
|
|
|
Thread.new(Thread.current) {|mth|
|
2013-12-14 17:03:24 -05:00
|
|
|
Thread.pass until t.stop?
|
2012-11-28 03:31:03 -05:00
|
|
|
puts mth.status
|
|
|
|
Process.kill(:INT, $$)
|
|
|
|
}
|
|
|
|
sleep 0.1
|
|
|
|
INPUT
|
|
|
|
end
|
|
|
|
|
|
|
|
# Bug #7450
|
|
|
|
def test_thread_status_raise_after_kill
|
|
|
|
ary = []
|
|
|
|
|
|
|
|
t = Thread.new {
|
2017-12-12 13:44:49 -05:00
|
|
|
assert_raise(RuntimeError) do
|
2012-11-28 03:31:03 -05:00
|
|
|
begin
|
|
|
|
ary << Thread.current.status
|
2017-12-12 13:44:49 -05:00
|
|
|
sleep #1
|
2012-11-28 03:31:03 -05:00
|
|
|
ensure
|
2017-12-12 13:44:49 -05:00
|
|
|
begin
|
|
|
|
ary << Thread.current.status
|
|
|
|
sleep #2
|
|
|
|
ensure
|
|
|
|
ary << Thread.current.status
|
|
|
|
end
|
2012-11-28 03:31:03 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2017-12-12 13:44:49 -05:00
|
|
|
Thread.pass until ary.size >= 1
|
|
|
|
Thread.pass until t.stop?
|
|
|
|
t.kill # wake up sleep #1
|
|
|
|
Thread.pass until ary.size >= 2
|
|
|
|
Thread.pass until t.stop?
|
|
|
|
t.raise "wakeup" # wake up sleep #2
|
|
|
|
Thread.pass while t.alive?
|
|
|
|
assert_equal(ary, ["run", "aborting", "aborting"])
|
|
|
|
t.join
|
2012-11-28 03:31:03 -05:00
|
|
|
end
|
2012-12-04 11:02:07 -05:00
|
|
|
|
|
|
|
def test_mutex_owned
|
2016-08-30 02:22:30 -04:00
|
|
|
mutex = Thread::Mutex.new
|
2012-12-04 11:02:07 -05:00
|
|
|
|
|
|
|
assert_equal(mutex.owned?, false)
|
|
|
|
mutex.synchronize {
|
|
|
|
# Now, I have the mutex
|
|
|
|
assert_equal(mutex.owned?, true)
|
|
|
|
}
|
|
|
|
assert_equal(mutex.owned?, false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mutex_owned2
|
|
|
|
begin
|
2016-08-30 02:22:30 -04:00
|
|
|
mutex = Thread::Mutex.new
|
2012-12-04 11:02:07 -05:00
|
|
|
th = Thread.new {
|
|
|
|
# lock forever
|
|
|
|
mutex.lock
|
|
|
|
sleep
|
|
|
|
}
|
|
|
|
|
2015-10-05 07:44:30 -04:00
|
|
|
# acquired by another thread.
|
|
|
|
Thread.pass until mutex.locked?
|
2012-12-04 11:02:07 -05:00
|
|
|
assert_equal(mutex.owned?, false)
|
|
|
|
ensure
|
|
|
|
th.kill if th
|
|
|
|
end
|
|
|
|
end
|
2012-12-19 17:29:18 -05:00
|
|
|
|
2013-09-10 19:21:17 -04:00
|
|
|
def test_mutex_unlock_on_trap
|
|
|
|
assert_in_out_err([], <<-INPUT, %w(locked unlocked false), [])
|
2016-08-30 02:22:30 -04:00
|
|
|
m = Thread::Mutex.new
|
2013-09-10 19:21:17 -04:00
|
|
|
|
2013-12-14 17:03:24 -05:00
|
|
|
trapped = false
|
2013-09-10 19:21:17 -04:00
|
|
|
Signal.trap("INT") { |signo|
|
|
|
|
m.unlock
|
2013-12-14 17:03:24 -05:00
|
|
|
trapped = true
|
2013-09-10 19:21:17 -04:00
|
|
|
puts "unlocked"
|
|
|
|
}
|
|
|
|
|
|
|
|
m.lock
|
|
|
|
puts "locked"
|
|
|
|
Process.kill("INT", $$)
|
2013-12-14 17:03:24 -05:00
|
|
|
Thread.pass until trapped
|
2013-09-10 19:21:17 -04:00
|
|
|
puts m.locked?
|
|
|
|
INPUT
|
|
|
|
end
|
|
|
|
|
2012-12-19 17:29:18 -05:00
|
|
|
def invoke_rec script, vm_stack_size, machine_stack_size, use_length = true
|
|
|
|
env = {}
|
|
|
|
env['RUBY_THREAD_VM_STACK_SIZE'] = vm_stack_size.to_s if vm_stack_size
|
|
|
|
env['RUBY_THREAD_MACHINE_STACK_SIZE'] = machine_stack_size.to_s if machine_stack_size
|
|
|
|
out, = EnvUtil.invoke_ruby([env, '-e', script], '', true, true)
|
|
|
|
use_length ? out.length : out
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_stack_size
|
|
|
|
h_default = eval(invoke_rec('p RubyVM::DEFAULT_PARAMS', nil, nil, false))
|
|
|
|
h_0 = eval(invoke_rec('p RubyVM::DEFAULT_PARAMS', 0, 0, false))
|
|
|
|
h_large = eval(invoke_rec('p RubyVM::DEFAULT_PARAMS', 1024 * 1024 * 10, 1024 * 1024 * 10, false))
|
|
|
|
|
2012-12-21 02:35:49 -05:00
|
|
|
assert_operator(h_default[:thread_vm_stack_size], :>, h_0[:thread_vm_stack_size],
|
|
|
|
"0 thread_vm_stack_size")
|
|
|
|
assert_operator(h_default[:thread_vm_stack_size], :<, h_large[:thread_vm_stack_size],
|
|
|
|
"large thread_vm_stack_size")
|
|
|
|
assert_operator(h_default[:thread_machine_stack_size], :>=, h_0[:thread_machine_stack_size],
|
|
|
|
"0 thread_machine_stack_size")
|
|
|
|
assert_operator(h_default[:thread_machine_stack_size], :<=, h_large[:thread_machine_stack_size],
|
|
|
|
"large thread_machine_stack_size")
|
2013-12-14 17:03:24 -05:00
|
|
|
end
|
2012-12-19 17:29:18 -05:00
|
|
|
|
2013-12-14 17:03:24 -05:00
|
|
|
def test_vm_machine_stack_size
|
2012-12-21 02:35:47 -05:00
|
|
|
script = 'def rec; print "."; STDOUT.flush; rec; end; rec'
|
2012-12-19 17:29:18 -05:00
|
|
|
size_default = invoke_rec script, nil, nil
|
2012-12-21 02:35:49 -05:00
|
|
|
assert_operator(size_default, :>, 0, "default size")
|
2012-12-19 17:29:18 -05:00
|
|
|
size_0 = invoke_rec script, 0, nil
|
2012-12-21 02:35:49 -05:00
|
|
|
assert_operator(size_default, :>, size_0, "0 size")
|
2012-12-19 17:29:18 -05:00
|
|
|
size_large = invoke_rec script, 1024 * 1024 * 10, nil
|
2012-12-21 02:35:49 -05:00
|
|
|
assert_operator(size_default, :<, size_large, "large size")
|
2013-12-14 17:03:24 -05:00
|
|
|
end
|
2012-12-19 17:29:18 -05:00
|
|
|
|
2013-12-14 17:03:24 -05:00
|
|
|
def test_machine_stack_size
|
2012-12-19 17:29:18 -05:00
|
|
|
# check machine stack size
|
|
|
|
# Note that machine stack size may not change size (depend on OSs)
|
2012-12-21 02:35:47 -05:00
|
|
|
script = 'def rec; print "."; STDOUT.flush; 1.times{1.times{1.times{rec}}}; end; Thread.new{rec}.join'
|
2012-12-19 17:29:18 -05:00
|
|
|
vm_stack_size = 1024 * 1024
|
|
|
|
size_default = invoke_rec script, vm_stack_size, nil
|
|
|
|
size_0 = invoke_rec script, vm_stack_size, 0
|
2012-12-21 02:35:49 -05:00
|
|
|
assert_operator(size_default, :>=, size_0, "0 size")
|
2012-12-19 17:29:18 -05:00
|
|
|
size_large = invoke_rec script, vm_stack_size, 1024 * 1024 * 10
|
2012-12-21 02:35:49 -05:00
|
|
|
assert_operator(size_default, :<=, size_large, "large size")
|
2013-12-14 17:03:24 -05:00
|
|
|
end unless /mswin|mingw/ =~ RUBY_PLATFORM
|
2013-10-04 22:21:12 -04:00
|
|
|
|
|
|
|
def test_blocking_mutex_unlocked_on_fork
|
|
|
|
bug8433 = '[ruby-core:55102] [Bug #8433]'
|
|
|
|
|
2016-08-30 02:22:30 -04:00
|
|
|
mutex = Thread::Mutex.new
|
2013-10-04 22:21:12 -04:00
|
|
|
flag = false
|
|
|
|
mutex.lock
|
|
|
|
|
|
|
|
th = Thread.new do
|
|
|
|
mutex.synchronize do
|
|
|
|
flag = true
|
|
|
|
sleep
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Thread.pass until th.stop?
|
|
|
|
mutex.unlock
|
|
|
|
|
|
|
|
pid = Process.fork do
|
|
|
|
exit(mutex.locked?)
|
|
|
|
end
|
|
|
|
|
|
|
|
th.kill
|
|
|
|
|
|
|
|
pid, status = Process.waitpid2(pid)
|
|
|
|
assert_equal(false, status.success?, bug8433)
|
2013-10-05 20:16:52 -04:00
|
|
|
end if Process.respond_to?(:fork)
|
2014-05-10 00:32:22 -04:00
|
|
|
|
|
|
|
def test_fork_in_thread
|
|
|
|
bug9751 = '[ruby-core:62070] [Bug #9751]'
|
|
|
|
f = nil
|
|
|
|
th = Thread.start do
|
|
|
|
unless f = IO.popen("-")
|
|
|
|
STDERR.reopen(STDOUT)
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
Process.wait2(f.pid)
|
|
|
|
end
|
2017-09-14 02:07:05 -04:00
|
|
|
unless th.join(EnvUtil.apply_timeout_scale(30))
|
2014-05-10 00:32:22 -04:00
|
|
|
Process.kill(:QUIT, f.pid)
|
2017-05-25 19:43:33 -04:00
|
|
|
Process.kill(:KILL, f.pid) unless th.join(EnvUtil.apply_timeout_scale(1))
|
2014-05-10 00:32:22 -04:00
|
|
|
end
|
|
|
|
_, status = th.value
|
|
|
|
output = f.read
|
|
|
|
f.close
|
|
|
|
assert_not_predicate(status, :signaled?, FailDesc[status, bug9751, output])
|
|
|
|
assert_predicate(status, :success?, bug9751)
|
|
|
|
end if Process.respond_to?(:fork)
|
2015-04-16 04:29:21 -04:00
|
|
|
|
2018-03-05 17:58:13 -05:00
|
|
|
def test_fork_while_locked
|
|
|
|
m = Mutex.new
|
|
|
|
thrs = []
|
|
|
|
3.times do |i|
|
|
|
|
thrs << Thread.new { m.synchronize { Process.waitpid2(fork{})[1] } }
|
|
|
|
end
|
|
|
|
thrs.each do |t|
|
|
|
|
assert_predicate t.value, :success?, '[ruby-core:85940] [Bug #14578]'
|
|
|
|
end
|
|
|
|
end if Process.respond_to?(:fork)
|
|
|
|
|
2015-04-16 04:29:21 -04:00
|
|
|
def test_subclass_no_initialize
|
|
|
|
t = Module.new do
|
|
|
|
break eval("class C\u{30b9 30ec 30c3 30c9} < Thread; self; end")
|
|
|
|
end
|
|
|
|
t.class_eval do
|
|
|
|
def initialize
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_raise_with_message(ThreadError, /C\u{30b9 30ec 30c3 30c9}/) do
|
|
|
|
t.new {}
|
|
|
|
end
|
|
|
|
end
|
2015-06-13 04:39:30 -04:00
|
|
|
|
|
|
|
def test_thread_name
|
2015-12-12 04:03:33 -05:00
|
|
|
t = Thread.start {sleep}
|
2016-01-12 21:24:07 -05:00
|
|
|
sleep 0.001 until t.stop?
|
2015-12-01 09:36:42 -05:00
|
|
|
assert_nil t.name
|
|
|
|
s = t.inspect
|
2015-06-13 04:39:30 -04:00
|
|
|
t.name = 'foo'
|
|
|
|
assert_equal 'foo', t.name
|
2015-12-01 09:36:42 -05:00
|
|
|
t.name = nil
|
|
|
|
assert_nil t.name
|
|
|
|
assert_equal s, t.inspect
|
2015-06-13 04:56:11 -04:00
|
|
|
ensure
|
|
|
|
t.kill
|
2015-06-13 04:39:30 -04:00
|
|
|
t.join
|
|
|
|
end
|
2015-12-01 09:14:07 -05:00
|
|
|
|
|
|
|
def test_thread_invalid_name
|
|
|
|
bug11756 = '[ruby-core:71774] [Bug #11756]'
|
|
|
|
t = Thread.start {}
|
|
|
|
assert_raise(ArgumentError, bug11756) {t.name = "foo\0bar"}
|
2015-12-03 21:22:44 -05:00
|
|
|
assert_raise(ArgumentError, bug11756) {t.name = "foo".encode(Encoding::UTF_32BE)}
|
2015-12-01 09:14:07 -05:00
|
|
|
ensure
|
|
|
|
t.kill
|
|
|
|
t.join
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_thread_invalid_object
|
|
|
|
bug11756 = '[ruby-core:71774] [Bug #11756]'
|
|
|
|
t = Thread.start {}
|
2015-12-01 09:36:42 -05:00
|
|
|
assert_raise(TypeError, bug11756) {t.name = []}
|
2015-12-01 09:14:07 -05:00
|
|
|
ensure
|
|
|
|
t.kill
|
|
|
|
t.join
|
|
|
|
end
|
2016-04-15 08:12:25 -04:00
|
|
|
|
2016-04-15 09:30:03 -04:00
|
|
|
def test_thread_setname_in_initialize
|
2016-04-15 08:12:25 -04:00
|
|
|
bug12290 = '[ruby-core:74963] [Bug #12290]'
|
2016-04-15 09:30:03 -04:00
|
|
|
c = Class.new(Thread) {def initialize() self.name = "foo"; super; end}
|
2016-07-07 14:59:38 -04:00
|
|
|
assert_equal("foo", c.new {Thread.current.name}.value, bug12290)
|
2016-04-15 08:12:25 -04:00
|
|
|
end
|
2017-02-10 03:15:39 -05:00
|
|
|
|
|
|
|
def test_thread_interrupt_for_killed_thread
|
2018-06-29 18:20:12 -04:00
|
|
|
opts = { timeout: 5, timeout_error: nil }
|
|
|
|
|
|
|
|
# prevent SIGABRT from slow shutdown with MJIT
|
|
|
|
opts[:reprieve] = 3 if RubyVM::MJIT.enabled?
|
|
|
|
|
|
|
|
assert_normal_exit(<<-_end, '[Bug #8996]', opts)
|
2017-12-13 04:45:48 -05:00
|
|
|
Thread.report_on_exception = false
|
2017-02-10 03:15:39 -05:00
|
|
|
trap(:TERM){exit}
|
|
|
|
while true
|
|
|
|
t = Thread.new{sleep 0}
|
|
|
|
t.raise Interrupt
|
|
|
|
end
|
|
|
|
_end
|
|
|
|
end
|
2017-12-15 02:27:04 -05:00
|
|
|
|
|
|
|
def test_signal_at_join
|
|
|
|
if /mswin|mingw/ =~ RUBY_PLATFORM
|
|
|
|
skip "can't trap a signal from another process on Windows"
|
|
|
|
# opt = {new_pgroup: true}
|
|
|
|
end
|
2017-12-30 18:06:39 -05:00
|
|
|
assert_separately([], "#{<<~"{#"}\n#{<<~'};'}", timeout: 120)
|
2017-12-15 02:27:04 -05:00
|
|
|
{#
|
|
|
|
n = 1000
|
|
|
|
sig = :INT
|
|
|
|
trap(sig) {}
|
|
|
|
IO.popen([EnvUtil.rubybin, "-e", "#{<<~"{#1"}\n#{<<~'};#1'}"], "r+") do |f|
|
|
|
|
tpid = #{$$}
|
|
|
|
sig = :#{sig}
|
|
|
|
{#1
|
|
|
|
STDOUT.sync = true
|
|
|
|
while gets
|
|
|
|
puts
|
|
|
|
Process.kill(sig, tpid)
|
|
|
|
end
|
|
|
|
};#1
|
|
|
|
assert_nothing_raised do
|
|
|
|
n.times do
|
|
|
|
w = Thread.start do
|
|
|
|
sleep 30
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
f.puts
|
|
|
|
f.gets
|
|
|
|
ensure
|
|
|
|
w.kill
|
|
|
|
w.join
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-12-17 03:26:17 -05:00
|
|
|
n.times do
|
|
|
|
w = Thread.start { sleep 30 }
|
|
|
|
begin
|
|
|
|
f.puts
|
|
|
|
f.gets
|
|
|
|
ensure
|
|
|
|
w.kill
|
|
|
|
t0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
|
|
w.join(30)
|
|
|
|
t1 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
|
|
diff = t1 - t0
|
|
|
|
assert_operator diff, :<=, 2
|
|
|
|
end
|
|
|
|
end
|
2017-12-15 02:27:04 -05:00
|
|
|
end
|
|
|
|
};
|
|
|
|
end
|
2008-01-18 13:48:12 -05:00
|
|
|
end
|