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
nicholas a. evans b6bae0ec44 Return seconds slept after Thread.scheduler wait_sleep
Kernel#sleep should still return seconds slept, even when using a
Thread.scheduler. The return value of Scheduler#wait_sleep can be
ignored.
2020-06-09 18:57:17 +09:00

47 lines
849 B
Ruby

# 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
Thread.current.scheduler = scheduler
5.times do |i|
Fiber do
assert(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
Thread.current.scheduler = scheduler
Fiber do
seconds = sleep(2)
end
end
thread.join
assert(seconds >= 2, "actual: %p" % seconds)
end
end