2021-02-11 01:17:54 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
require 'test/unit'
|
|
|
|
require_relative 'scheduler'
|
|
|
|
|
|
|
|
require 'timeout'
|
|
|
|
|
|
|
|
class TestFiberTimeout < Test::Unit::TestCase
|
2020-12-26 04:09:49 -05:00
|
|
|
def test_timeout_after
|
2021-02-11 01:17:54 -05:00
|
|
|
error = nil
|
|
|
|
|
|
|
|
thread = Thread.new do
|
|
|
|
scheduler = Scheduler.new
|
|
|
|
Fiber.set_scheduler scheduler
|
|
|
|
|
|
|
|
Fiber.schedule do
|
|
|
|
begin
|
2021-02-11 21:06:07 -05:00
|
|
|
Timeout.timeout(0.001) do
|
2021-02-11 01:17:54 -05:00
|
|
|
sleep(1)
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
error = $!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
thread.join
|
|
|
|
|
|
|
|
assert_kind_of(Timeout::Error, error)
|
|
|
|
end
|
2021-02-11 21:06:07 -05:00
|
|
|
|
|
|
|
MESSAGE = "Hello World"
|
|
|
|
|
|
|
|
def test_timeout_on_main_fiber
|
|
|
|
message = nil
|
|
|
|
|
|
|
|
thread = Thread.new do
|
|
|
|
scheduler = Scheduler.new
|
|
|
|
Fiber.set_scheduler scheduler
|
|
|
|
|
2021-02-11 22:54:52 -05:00
|
|
|
assert_nil Fiber.current_scheduler
|
|
|
|
|
2021-02-11 21:06:07 -05:00
|
|
|
Timeout.timeout(1) do
|
|
|
|
message = MESSAGE
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
thread.join
|
|
|
|
|
|
|
|
assert_equal MESSAGE, message
|
|
|
|
end
|
2021-02-11 01:17:54 -05:00
|
|
|
end
|