Accept `sleep(nil)` as sleep forever. (#7484)

This commit is contained in:
Samuel Williams 2023-03-10 16:40:05 +13:00 коммит произвёл GitHub
Родитель dcc8ecdee8
Коммит 86d38b4520
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 27 добавлений и 5 удалений

Просмотреть файл

@ -4918,6 +4918,9 @@ rb_f_spawn(int argc, VALUE *argv, VALUE _)
* thread calls Thread#run. Called without an argument, sleep()
* will sleep forever.
*
* If the +duration+ is not supplied, or is +nil+, the thread sleeps forever.
* Threads in this state may still be interrupted by other threads.
*
* Time.new #=> 2008-03-08 19:56:19 +0900
* sleep 1.2 #=> 1
* Time.new #=> 2008-03-08 19:56:20 +0900
@ -4935,7 +4938,7 @@ rb_f_sleep(int argc, VALUE *argv, VALUE _)
rb_fiber_scheduler_kernel_sleepv(scheduler, argc, argv);
}
else {
if (argc == 0) {
if (argc == 0 || (argc == 1 && NIL_P(argv[0]))) {
rb_thread_sleep_forever();
}
else {

Просмотреть файл

@ -33,10 +33,6 @@ describe "Kernel#sleep" do
-> { sleep(-1) }.should raise_error(ArgumentError)
end
it "raises a TypeError when passed nil" do
-> { sleep(nil) }.should raise_error(TypeError)
end
it "raises a TypeError when passed a String" do
-> { sleep('2') }.should raise_error(TypeError)
end
@ -55,6 +51,29 @@ describe "Kernel#sleep" do
t.wakeup
t.value.should == 5
end
ruby_version_is ""..."3.3" do
it "raises a TypeError when passed nil" do
-> { sleep(nil) }.should raise_error(TypeError)
end
end
ruby_version_is "3.3" do
it "accepts a nil duration" do
running = false
t = Thread.new do
running = true
sleep(nil)
5
end
Thread.pass until running
Thread.pass while t.status and t.status != "sleep"
t.wakeup
t.value.should == 5
end
end
end
describe "Kernel.sleep" do