diff options
author | Samuel Williams <[email protected]> | 2023-03-10 16:40:05 +1300 |
---|---|---|
committer | GitHub <[email protected]> | 2023-03-10 16:40:05 +1300 |
commit | 86d38b452005a9168eb2b5eaffd5fb3465313436 (patch) | |
tree | 18a246c3f25a0a35ad4118738345a698252b5d52 | |
parent | dcc8ecdee8d81963d28d974143cb6f2d7ea489c4 (diff) |
Accept `sleep(nil)` as sleep forever. (#7484)
Notes
Notes:
Merged-By: ioquatix <[email protected]>
-rw-r--r-- | process.c | 5 | ||||
-rw-r--r-- | spec/ruby/core/kernel/sleep_spec.rb | 27 |
2 files changed, 27 insertions, 5 deletions
@@ -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 { diff --git a/spec/ruby/core/kernel/sleep_spec.rb b/spec/ruby/core/kernel/sleep_spec.rb index 32da6344c1..44b417a92e 100644 --- a/spec/ruby/core/kernel/sleep_spec.rb +++ b/spec/ruby/core/kernel/sleep_spec.rb @@ -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 |