2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2011-02-23 09:08:25 -05:00
|
|
|
require 'test/unit'
|
|
|
|
require 'timeout'
|
|
|
|
|
|
|
|
module TestParallel
|
2014-05-17 02:26:51 -04:00
|
|
|
PARALLEL_RB = "#{File.dirname(__FILE__)}/../lib/test/unit/parallel.rb"
|
2011-02-23 09:08:25 -05:00
|
|
|
TESTS = "#{File.dirname(__FILE__)}/tests_for_parallel"
|
2018-04-28 04:41:19 -04:00
|
|
|
TIMEOUT = RubyVM::MJIT.enabled? ? 100 : 10 # use large timeout for --jit-wait
|
2011-02-23 09:08:25 -05:00
|
|
|
|
|
|
|
class TestParallelWorker < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
i, @worker_in = IO.pipe
|
|
|
|
@worker_out, o = IO.pipe
|
2011-02-23 23:47:48 -05:00
|
|
|
@worker_pid = spawn(*@options[:ruby], PARALLEL_RB,
|
2011-08-02 07:18:58 -04:00
|
|
|
"--ruby", @options[:ruby].join(" "),
|
2011-02-23 09:08:25 -05:00
|
|
|
"-j", "t1", "-v", out: o, in: i)
|
|
|
|
[i,o].each(&:close)
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2011-02-27 21:58:35 -05:00
|
|
|
if @worker_pid && @worker_in
|
|
|
|
begin
|
2011-07-07 04:56:12 -04:00
|
|
|
begin
|
|
|
|
@worker_in.puts "quit"
|
|
|
|
rescue IOError, Errno::EPIPE
|
|
|
|
end
|
2015-07-13 06:07:01 -04:00
|
|
|
Timeout.timeout(2) do
|
2011-02-27 21:58:35 -05:00
|
|
|
Process.waitpid(@worker_pid)
|
|
|
|
end
|
2011-07-07 04:56:12 -04:00
|
|
|
rescue Timeout::Error
|
2011-06-12 14:20:25 -04:00
|
|
|
begin
|
|
|
|
Process.kill(:KILL, @worker_pid)
|
|
|
|
rescue Errno::ESRCH
|
|
|
|
end
|
2011-02-23 09:08:25 -05:00
|
|
|
end
|
|
|
|
end
|
2014-05-30 10:17:46 -04:00
|
|
|
ensure
|
2017-10-25 09:29:27 -04:00
|
|
|
begin
|
|
|
|
@worker_in.close
|
|
|
|
@worker_out.close
|
|
|
|
rescue Errno::EPIPE
|
|
|
|
# may already broken and rescue'ed in above code
|
|
|
|
end
|
2011-02-23 09:08:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_run
|
2018-04-28 04:41:19 -04:00
|
|
|
Timeout.timeout(TIMEOUT) do
|
2011-02-23 09:08:25 -05:00
|
|
|
assert_match(/^ready/,@worker_out.gets)
|
2011-07-10 08:19:16 -04:00
|
|
|
@worker_in.puts "run #{TESTS}/ptest_first.rb test"
|
2011-02-23 09:08:25 -05:00
|
|
|
assert_match(/^okay/,@worker_out.gets)
|
2017-02-06 22:55:47 -05:00
|
|
|
assert_match(/^record/,@worker_out.gets)
|
2011-02-23 09:08:25 -05:00
|
|
|
assert_match(/^p/,@worker_out.gets)
|
|
|
|
assert_match(/^done/,@worker_out.gets)
|
|
|
|
assert_match(/^ready/,@worker_out.gets)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_run_multiple_testcase_in_one_file
|
2018-04-28 04:41:19 -04:00
|
|
|
Timeout.timeout(TIMEOUT) do
|
2011-02-23 09:08:25 -05:00
|
|
|
assert_match(/^ready/,@worker_out.gets)
|
2011-07-10 08:19:16 -04:00
|
|
|
@worker_in.puts "run #{TESTS}/ptest_second.rb test"
|
2011-02-23 09:08:25 -05:00
|
|
|
assert_match(/^okay/,@worker_out.gets)
|
2017-02-06 22:55:47 -05:00
|
|
|
assert_match(/^record/,@worker_out.gets)
|
2011-02-23 09:08:25 -05:00
|
|
|
assert_match(/^p/,@worker_out.gets)
|
|
|
|
assert_match(/^done/,@worker_out.gets)
|
2017-02-06 22:55:47 -05:00
|
|
|
assert_match(/^record/,@worker_out.gets)
|
2011-02-23 09:08:25 -05:00
|
|
|
assert_match(/^p/,@worker_out.gets)
|
|
|
|
assert_match(/^done/,@worker_out.gets)
|
|
|
|
assert_match(/^ready/,@worker_out.gets)
|
|
|
|
end
|
|
|
|
end
|
2011-02-26 09:51:35 -05:00
|
|
|
|
2011-02-23 09:08:25 -05:00
|
|
|
def test_accept_run_command_multiple_times
|
2018-04-28 04:41:19 -04:00
|
|
|
Timeout.timeout(TIMEOUT) do
|
2011-02-23 09:08:25 -05:00
|
|
|
assert_match(/^ready/,@worker_out.gets)
|
2011-07-10 08:19:16 -04:00
|
|
|
@worker_in.puts "run #{TESTS}/ptest_first.rb test"
|
2011-02-23 09:08:25 -05:00
|
|
|
assert_match(/^okay/,@worker_out.gets)
|
2017-02-06 22:55:47 -05:00
|
|
|
assert_match(/^record/,@worker_out.gets)
|
2011-02-23 09:08:25 -05:00
|
|
|
assert_match(/^p/,@worker_out.gets)
|
|
|
|
assert_match(/^done/,@worker_out.gets)
|
|
|
|
assert_match(/^ready/,@worker_out.gets)
|
2011-07-10 08:19:16 -04:00
|
|
|
@worker_in.puts "run #{TESTS}/ptest_second.rb test"
|
2011-02-23 09:08:25 -05:00
|
|
|
assert_match(/^okay/,@worker_out.gets)
|
2017-02-06 22:55:47 -05:00
|
|
|
assert_match(/^record/,@worker_out.gets)
|
2011-02-23 09:08:25 -05:00
|
|
|
assert_match(/^p/,@worker_out.gets)
|
|
|
|
assert_match(/^done/,@worker_out.gets)
|
2017-02-06 22:55:47 -05:00
|
|
|
assert_match(/^record/,@worker_out.gets)
|
2011-02-23 09:08:25 -05:00
|
|
|
assert_match(/^p/,@worker_out.gets)
|
|
|
|
assert_match(/^done/,@worker_out.gets)
|
|
|
|
assert_match(/^ready/,@worker_out.gets)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_p
|
2018-04-28 04:41:19 -04:00
|
|
|
Timeout.timeout(TIMEOUT) do
|
2011-07-10 08:19:16 -04:00
|
|
|
@worker_in.puts "run #{TESTS}/ptest_first.rb test"
|
2011-02-23 09:08:25 -05:00
|
|
|
while buf = @worker_out.gets
|
|
|
|
break if /^p (.+?)$/ =~ buf
|
|
|
|
end
|
2011-07-10 08:19:16 -04:00
|
|
|
assert_match(/TestA#test_nothing_test = \d+\.\d+ s = \.\n/, $1.chomp.unpack("m")[0])
|
2011-02-23 09:08:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_done
|
2018-04-28 04:41:19 -04:00
|
|
|
Timeout.timeout(TIMEOUT) do
|
2011-07-10 08:19:16 -04:00
|
|
|
@worker_in.puts "run #{TESTS}/ptest_forth.rb test"
|
2017-02-06 22:55:47 -05:00
|
|
|
while buf = @worker_out.gets
|
|
|
|
break if /^done (.+?)$/ =~ buf
|
|
|
|
end
|
2011-02-23 09:08:25 -05:00
|
|
|
|
|
|
|
result = Marshal.load($1.chomp.unpack("m")[0])
|
|
|
|
|
2014-05-15 08:31:26 -04:00
|
|
|
assert_equal(5, result[0])
|
2012-08-20 20:07:32 -04:00
|
|
|
assert_equal(2, result[1])
|
2011-02-23 09:08:25 -05:00
|
|
|
assert_kind_of(Array,result[2])
|
|
|
|
assert_kind_of(Array,result[3])
|
|
|
|
assert_kind_of(Array,result[4])
|
2012-06-15 18:46:08 -04:00
|
|
|
assert_kind_of(Array,result[2][1])
|
|
|
|
assert_kind_of(MiniTest::Assertion,result[2][0][2])
|
|
|
|
assert_kind_of(MiniTest::Skip,result[2][1][2])
|
2014-05-15 08:31:26 -04:00
|
|
|
assert_kind_of(MiniTest::Skip,result[2][2][2])
|
|
|
|
assert_kind_of(Exception, result[2][3][2])
|
2011-02-23 09:08:25 -05:00
|
|
|
assert_equal(result[5], "TestE")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_quit
|
2018-04-28 04:41:19 -04:00
|
|
|
Timeout.timeout(TIMEOUT) do
|
2011-02-23 09:08:25 -05:00
|
|
|
@worker_in.puts "quit"
|
|
|
|
assert_match(/^bye$/m,@worker_out.read)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestParallel < Test::Unit::TestCase
|
|
|
|
def spawn_runner(*opt_args)
|
|
|
|
@test_out, o = IO.pipe
|
2011-02-23 23:47:48 -05:00
|
|
|
@test_pid = spawn(*@options[:ruby], TESTS+"/runner.rb",
|
2011-08-02 07:18:58 -04:00
|
|
|
"--ruby", @options[:ruby].join(" "),
|
2011-06-16 07:24:23 -04:00
|
|
|
"-j","t1",*opt_args, out: o, err: o)
|
2011-02-23 09:08:25 -05:00
|
|
|
o.close
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
begin
|
|
|
|
if @test_pid
|
2015-07-13 06:07:01 -04:00
|
|
|
Timeout.timeout(2) do
|
2011-02-23 09:08:25 -05:00
|
|
|
Process.waitpid(@test_pid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue Timeout::Error
|
|
|
|
Process.kill(:KILL, @test_pid) if @test_pid
|
|
|
|
ensure
|
2018-06-09 23:10:13 -04:00
|
|
|
@test_out&.close
|
2011-02-23 09:08:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-15 19:06:00 -04:00
|
|
|
def test_ignore_jzero
|
|
|
|
@test_out, o = IO.pipe
|
|
|
|
@test_pid = spawn(*@options[:ruby], TESTS+"/runner.rb",
|
2011-08-02 07:18:58 -04:00
|
|
|
"--ruby", @options[:ruby].join(" "),
|
2011-06-15 19:06:00 -04:00
|
|
|
"-j","0", out: File::NULL, err: o)
|
|
|
|
o.close
|
2018-04-28 04:41:19 -04:00
|
|
|
Timeout.timeout(TIMEOUT) {
|
2011-06-15 19:06:00 -04:00
|
|
|
assert_match(/Error: parameter of -j option should be greater than 0/,@test_out.read)
|
|
|
|
}
|
|
|
|
end
|
2011-02-26 09:51:35 -05:00
|
|
|
|
2011-02-23 09:08:25 -05:00
|
|
|
def test_should_run_all_without_any_leaks
|
|
|
|
spawn_runner
|
2018-04-28 04:41:19 -04:00
|
|
|
buf = Timeout.timeout(TIMEOUT) {@test_out.read}
|
2017-02-07 03:09:23 -05:00
|
|
|
assert_match(/^9 tests/,buf)
|
2011-02-23 09:08:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_retry_failed_on_workers
|
|
|
|
spawn_runner
|
2018-04-28 04:41:19 -04:00
|
|
|
buf = Timeout.timeout(TIMEOUT) {@test_out.read}
|
2011-02-23 09:08:25 -05:00
|
|
|
assert_match(/^Retrying\.+$/,buf)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_no_retry_option
|
|
|
|
spawn_runner "--no-retry"
|
2018-04-28 04:41:19 -04:00
|
|
|
buf = Timeout.timeout(TIMEOUT) {@test_out.read}
|
2011-02-23 09:08:25 -05:00
|
|
|
refute_match(/^Retrying\.+$/,buf)
|
2013-05-02 02:07:17 -04:00
|
|
|
assert_match(/^ +\d+\) Failure:\nTestD#test_fail_at_worker/,buf)
|
2011-02-23 09:08:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_jobs_status
|
|
|
|
spawn_runner "--jobs-status"
|
2018-04-28 04:41:19 -04:00
|
|
|
buf = Timeout.timeout(TIMEOUT) {@test_out.read}
|
2011-07-10 08:19:16 -04:00
|
|
|
assert_match(/\d+=ptest_(first|second|third|forth) */,buf)
|
2011-02-23 09:08:25 -05:00
|
|
|
end
|
|
|
|
|
2011-12-25 09:17:28 -05:00
|
|
|
def test_separate
|
|
|
|
# this test depends to --jobs-status
|
|
|
|
spawn_runner "--jobs-status", "--separate"
|
2018-04-28 04:41:19 -04:00
|
|
|
buf = Timeout.timeout(TIMEOUT) {@test_out.read}
|
2018-10-23 10:17:16 -04:00
|
|
|
assert(buf.scan(/^\[\s*\d+\/\d+\]\s*(\d+?)=/).flatten.uniq.size > 1,
|
|
|
|
message("retried tests should run in different processes") {buf})
|
2011-12-25 09:17:28 -05:00
|
|
|
end
|
2011-02-23 09:08:25 -05:00
|
|
|
end
|
|
|
|
end
|