зеркало из https://github.com/github/ruby.git
Accept `sleep(nil)` as sleep forever. (#7484)
This commit is contained in:
Родитель
dcc8ecdee8
Коммит
86d38b4520
|
@ -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
|
||||
|
|
Загрузка…
Ссылка в новой задаче