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

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.
This commit is contained in:
nicholas a. evans 2020-06-09 00:30:43 -04:00 committed by Nobuyoshi Nakada
parent 964829495c
commit b6bae0ec44
Notes: git 2020-06-09 18:57:40 +09:00
2 changed files with 27 additions and 10 deletions

View file

@ -4920,20 +4920,20 @@ rb_f_spawn(int argc, VALUE *argv, VALUE _)
static VALUE
rb_f_sleep(int argc, VALUE *argv, VALUE _)
{
time_t beg = time(0);
VALUE scheduler = rb_current_thread_scheduler();
if (scheduler != Qnil) {
VALUE result = rb_funcallv(scheduler, rb_intern("wait_sleep"), argc, argv);
return RTEST(result);
}
time_t beg = time(0);
if (argc == 0) {
rb_thread_sleep_forever();
rb_funcallv(scheduler, rb_intern("wait_sleep"), argc, argv);
}
else {
rb_check_arity(argc, 0, 1);
rb_thread_wait_for(rb_time_interval(argv[0]));
if (argc == 0) {
rb_thread_sleep_forever();
}
else {
rb_check_arity(argc, 0, 1);
rb_thread_wait_for(rb_time_interval(argv[0]));
}
}
time_t end = time(0) - beg;

View file

@ -14,7 +14,7 @@ class TestFiberSleep < Test::Unit::TestCase
5.times do |i|
Fiber do
sleep(i/100.0)
assert(sleep(i/100.0) >= 0)
items << i
end
end
@ -27,4 +27,21 @@ class TestFiberSleep < Test::Unit::TestCase
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