2020-05-14 06:10:55 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
require 'test/unit'
|
|
|
|
require_relative 'scheduler'
|
|
|
|
|
2020-05-23 03:43:58 -04:00
|
|
|
class TestFiberSleep < Test::Unit::TestCase
|
2020-05-14 06:10:55 -04:00
|
|
|
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
|
2020-05-14 06:10:55 -04:00
|
|
|
|
|
|
|
5.times do |i|
|
2020-08-20 08:52:36 -04:00
|
|
|
Fiber.schedule do
|
2020-06-09 08:42:06 -04:00
|
|
|
assert_operator sleep(i/100.0), :>=, 0
|
2020-05-14 06:10:55 -04:00
|
|
|
items << i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Should be 5 fibers waiting:
|
|
|
|
assert_equal scheduler.waiting.size, 5
|
|
|
|
end
|
|
|
|
|
|
|
|
thread.join
|
|
|
|
|
|
|
|
assert_equal ITEMS, items
|
|
|
|
end
|
2020-06-09 00:30:43 -04:00
|
|
|
|
|
|
|
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
|
2020-08-20 08:52:36 -04:00
|
|
|
Fiber.schedule do
|
2020-06-09 00:30:43 -04:00
|
|
|
seconds = sleep(2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
thread.join
|
|
|
|
|
2020-06-09 08:42:06 -04:00
|
|
|
assert_operator seconds, :>=, 2, "actual: %p" % seconds
|
2020-06-09 00:30:43 -04:00
|
|
|
end
|
2020-05-14 06:10:55 -04:00
|
|
|
end
|