1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/test/fiber/test_sleep.rb

72 lines
1.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require 'test/unit'
require_relative 'scheduler'
class TestFiberSleep < Test::Unit::TestCase
ITEMS = [0, 1, 2, 3, 4]
def test_sleep
items = []
thread = Thread.new do
scheduler = Scheduler.new
2020-10-15 21:25:58 -04:00
Fiber.set_scheduler scheduler
5.times do |i|
Fiber.schedule do
2020-06-09 08:42:06 -04:00
assert_operator sleep(i/100.0), :>=, 0
items << i
end
end
# Should be 5 fibers waiting:
assert_equal scheduler.waiting.size, 5
end
thread.join
assert_equal ITEMS, items
end
def test_sleep_returns_seconds_slept
seconds = nil
thread = Thread.new do
scheduler = Scheduler.new
2020-10-15 21:25:58 -04:00
Fiber.set_scheduler scheduler
Fiber.schedule do
seconds = sleep(2)
end
end
thread.join
2020-06-09 08:42:06 -04:00
assert_operator seconds, :>=, 2, "actual: %p" % seconds
end
def test_broken_sleep
thread = Thread.new do
Thread.current.report_on_exception = false
scheduler = Scheduler.new
def scheduler.kernel_sleep(duration = nil)
raise "Broken sleep!"
end
Fiber.set_scheduler scheduler
Fiber.schedule do
sleep 0
end
2021-06-14 11:02:15 -04:00
ensure
scheduler.close
end
assert_raise(RuntimeError) do
thread.join
end
end
end