2021-02-09 01:39:56 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
require "test/unit"
|
|
|
|
require_relative 'scheduler'
|
|
|
|
|
|
|
|
class TestFiberThread < Test::Unit::TestCase
|
|
|
|
def test_thread_join
|
|
|
|
thread = Thread.new do
|
|
|
|
scheduler = Scheduler.new
|
|
|
|
Fiber.set_scheduler scheduler
|
|
|
|
|
|
|
|
result = nil
|
|
|
|
Fiber.schedule do
|
|
|
|
result = Thread.new{:done}.value
|
|
|
|
end
|
|
|
|
|
|
|
|
scheduler.run
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal :done, thread.value
|
|
|
|
end
|
|
|
|
|
2021-07-28 03:55:55 -04:00
|
|
|
def test_thread_join_implicit
|
|
|
|
sleeping = false
|
|
|
|
finished = false
|
|
|
|
|
|
|
|
thread = Thread.new do
|
|
|
|
scheduler = Scheduler.new
|
|
|
|
Fiber.set_scheduler scheduler
|
|
|
|
|
|
|
|
Fiber.schedule do
|
|
|
|
sleeping = true
|
|
|
|
sleep(0.1)
|
|
|
|
finished = true
|
|
|
|
end
|
|
|
|
|
|
|
|
:done
|
|
|
|
end
|
|
|
|
|
|
|
|
Thread.pass until sleeping
|
|
|
|
|
|
|
|
thread.join
|
|
|
|
|
|
|
|
assert_equal :done, thread.value
|
|
|
|
assert finished, "Scheduler thread's task should be finished!"
|
|
|
|
end
|
|
|
|
|
2021-02-09 01:39:56 -05:00
|
|
|
def test_thread_join_blocking
|
|
|
|
thread = Thread.new do
|
|
|
|
scheduler = Scheduler.new
|
|
|
|
Fiber.set_scheduler scheduler
|
|
|
|
|
|
|
|
result = nil
|
|
|
|
Fiber.schedule do
|
|
|
|
Fiber.new(blocking: true) do
|
|
|
|
# This can deadlock if the blocking state is not taken into account:
|
|
|
|
Thread.new do
|
|
|
|
sleep(0)
|
|
|
|
result = :done
|
|
|
|
end.join
|
|
|
|
end.resume
|
|
|
|
end
|
|
|
|
|
|
|
|
scheduler.run
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal :done, thread.value
|
|
|
|
end
|
2021-06-14 01:56:53 -04:00
|
|
|
|
|
|
|
def test_broken_unblock
|
|
|
|
thread = Thread.new do
|
|
|
|
Thread.current.report_on_exception = false
|
|
|
|
|
|
|
|
scheduler = BrokenUnblockScheduler.new
|
|
|
|
|
|
|
|
Fiber.set_scheduler scheduler
|
|
|
|
|
|
|
|
Fiber.schedule do
|
2021-06-14 11:03:05 -04:00
|
|
|
Thread.new{
|
|
|
|
Thread.current.report_on_exception = false
|
|
|
|
}.join
|
2021-06-14 01:56:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
scheduler.run
|
2021-06-14 11:02:15 -04:00
|
|
|
ensure
|
|
|
|
scheduler.close
|
2021-06-14 01:56:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise(RuntimeError) do
|
|
|
|
thread.join
|
|
|
|
end
|
|
|
|
end
|
2021-07-28 03:55:55 -04:00
|
|
|
|
|
|
|
def test_thread_join_hang
|
|
|
|
thread = Thread.new do
|
|
|
|
scheduler = SleepingUnblockScheduler.new
|
|
|
|
|
|
|
|
Fiber.set_scheduler scheduler
|
|
|
|
|
|
|
|
Fiber.schedule do
|
|
|
|
Thread.new{sleep(0.01)}.value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
thread.join
|
|
|
|
end
|
2021-02-09 01:39:56 -05:00
|
|
|
end
|