Skip to content

Commit

Permalink
Deprecate ::sleep(Number) (#14962)
Browse files Browse the repository at this point in the history
This is in line with other places in the standard library that favor `Time::Span` over number types, such as `IO` timeouts (#14368) and `Benchmark.ips` (#14805).
  • Loading branch information
HertzDevil authored Sep 5, 2024
1 parent 95af602 commit f6e2ab3
Show file tree
Hide file tree
Showing 17 changed files with 39 additions and 38 deletions.
2 changes: 1 addition & 1 deletion samples/channel_select.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ def generator(n : T) forall T
channel = Channel(T).new
spawn do
loop do
sleep n
sleep n.seconds
channel.send n
end
end
Expand Down
4 changes: 2 additions & 2 deletions samples/conway.cr
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct ConwayMap
end
end

PAUSE_MILLIS = 20
PAUSE = 20.milliseconds
DEFAULT_COUNT = 300
INITIAL_MAP = [
" 1 ",
Expand All @@ -99,6 +99,6 @@ spawn { gets; exit }
1.upto(DEFAULT_COUNT) do |i|
puts map
puts "n = #{i}\tPress ENTER to exit"
sleep PAUSE_MILLIS * 0.001
sleep PAUSE
map.next
end
2 changes: 1 addition & 1 deletion samples/tcp_client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ socket = TCPSocket.new "127.0.0.1", 9000
10.times do |i|
socket.puts i
puts "Server response: #{socket.gets}"
sleep 0.5
sleep 0.5.seconds
end
4 changes: 2 additions & 2 deletions spec/std/benchmark_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ describe Benchmark::IPS::Job do
# test several things to avoid running a benchmark over and over again in
# the specs
j = Benchmark::IPS::Job.new(0.001, 0.001, interactive: false)
a = j.report("a") { sleep 0.001 }
b = j.report("b") { sleep 0.002 }
a = j.report("a") { sleep 1.milliseconds }
b = j.report("b") { sleep 2.milliseconds }

j.execute

Expand Down
12 changes: 6 additions & 6 deletions spec/std/channel_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe Channel do

it "raises if channel is closed while waiting" do
ch = Channel(String).new
spawn_and_wait(->{ sleep 0.2; ch.close }) do
spawn_and_wait(->{ sleep 0.2.seconds; ch.close }) do
expect_raises Channel::ClosedError do
Channel.select(ch.receive_select_action)
end
Expand All @@ -129,7 +129,7 @@ describe Channel do
end
}

spawn_and_wait(->{ sleep 0.2; ch.close }) do
spawn_and_wait(->{ sleep 0.2.seconds; ch.close }) do
r = parallel p.call, p.call, p.call, p.call
r.should eq({1, 1, 1, 1})
end
Expand Down Expand Up @@ -178,7 +178,7 @@ describe Channel do

it "returns nil channel is closed while waiting" do
ch = Channel(String).new
spawn_and_wait(->{ sleep 0.2; ch.close }) do
spawn_and_wait(->{ sleep 0.2.seconds; ch.close }) do
i, m = Channel.select(ch.receive_select_action?)
m.should be_nil
end
Expand All @@ -191,7 +191,7 @@ describe Channel do
Channel.select(ch.receive_select_action?)
}

spawn_and_wait(->{ sleep 0.2; ch.close }) do
spawn_and_wait(->{ sleep 0.2.seconds; ch.close }) do
r = parallel p.call, p.call, p.call, p.call
r.should eq({ {0, nil}, {0, nil}, {0, nil}, {0, nil} })
end
Expand Down Expand Up @@ -273,7 +273,7 @@ describe Channel do

it "raises if channel is closed while waiting" do
ch = Channel(String).new
spawn_and_wait(->{ sleep 0.2; ch.close }) do
spawn_and_wait(->{ sleep 0.2.seconds; ch.close }) do
expect_raises Channel::ClosedError do
Channel.select(ch.send_select_action("foo"))
end
Expand All @@ -292,7 +292,7 @@ describe Channel do
end
}

spawn_and_wait(->{ sleep 0.2; ch.close }) do
spawn_and_wait(->{ sleep 0.2.seconds; ch.close }) do
r = parallel p.call, p.call, p.call, p.call
r.should eq({1, 1, 1, 1})
end
Expand Down
14 changes: 7 additions & 7 deletions spec/std/http/client/client_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require "http/server"
require "http/log"
require "log/spec"

private def test_server(host, port, read_time = 0, content_type = "text/plain", write_response = true, &)
private def test_server(host, port, read_time = 0.seconds, content_type = "text/plain", write_response = true, &)
server = TCPServer.new(host, port)
begin
spawn do
Expand Down Expand Up @@ -312,12 +312,12 @@ module HTTP
end

it "doesn't read the body if request was HEAD" do
resp_get = test_server("localhost", 0, 0) do |server|
resp_get = test_server("localhost", 0, 0.seconds) do |server|
client = Client.new("localhost", server.local_address.port)
break client.get("/")
end

test_server("localhost", 0, 0) do |server|
test_server("localhost", 0, 0.seconds) do |server|
client = Client.new("localhost", server.local_address.port)
resp_head = client.head("/")
resp_head.headers.should eq(resp_get.headers)
Expand All @@ -338,7 +338,7 @@ module HTTP
end

it "tests read_timeout" do
test_server("localhost", 0, 0) do |server|
test_server("localhost", 0, 0.seconds) do |server|
client = Client.new("localhost", server.local_address.port)
client.read_timeout = 1.second
client.get("/")
Expand All @@ -348,7 +348,7 @@ module HTTP
# it doesn't make sense to try to write because the client will already
# timeout on read. Writing a response could lead on an exception in
# the server if the socket is closed.
test_server("localhost", 0, 0.5, write_response: false) do |server|
test_server("localhost", 0, 0.5.seconds, write_response: false) do |server|
client = Client.new("localhost", server.local_address.port)
expect_raises(IO::TimeoutError, {% if flag?(:win32) %} "WSARecv timed out" {% else %} "Read timed out" {% end %}) do
client.read_timeout = 0.001
Expand All @@ -362,7 +362,7 @@ module HTTP
# it doesn't make sense to try to write because the client will already
# timeout on read. Writing a response could lead on an exception in
# the server if the socket is closed.
test_server("localhost", 0, 0, write_response: false) do |server|
test_server("localhost", 0, 0.seconds, write_response: false) do |server|
client = Client.new("localhost", server.local_address.port)
expect_raises(IO::TimeoutError, {% if flag?(:win32) %} "WSASend timed out" {% else %} "Write timed out" {% end %}) do
client.write_timeout = 0.001
Expand All @@ -372,7 +372,7 @@ module HTTP
end

it "tests connect_timeout" do
test_server("localhost", 0, 0) do |server|
test_server("localhost", 0, 0.seconds) do |server|
client = Client.new("localhost", server.local_address.port)
client.connect_timeout = 0.5
client.get("/")
Expand Down
4 changes: 2 additions & 2 deletions spec/std/http/server/server_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ describe HTTP::Server do
while !server.listening?
Fiber.yield
end
sleep 0.1
sleep 0.1.seconds

schedule_timeout ch

TCPSocket.open(address.address, address.port) { }

# wait before closing the server
sleep 0.1
sleep 0.1.seconds
server.close

ch.receive.should eq SpecChannelStatus::End
Expand Down
2 changes: 1 addition & 1 deletion spec/std/http/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def run_server(server, &)
{% if flag?(:preview_mt) %}
# avoids fiber synchronization issues in specs, like closing the server
# before we properly listen, ...
sleep 0.001
sleep 1.millisecond
{% end %}
yield server_done
ensure
Expand Down
2 changes: 1 addition & 1 deletion spec/std/openssl/ssl/server_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe OpenSSL::SSL::Server do

OpenSSL::SSL::Server.open tcp_server, server_context do |server|
spawn do
sleep 1
sleep 1.second
OpenSSL::SSL::Socket::Client.open(TCPSocket.new(tcp_server.local_address.address, tcp_server.local_address.port), client_context, hostname: "example.com") do |socket|
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/std/signal_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pending_interpreted describe: "Signal" do
Process.signal Signal::USR1, Process.pid
10.times do |i|
break if ran
sleep 0.1
sleep 0.1.seconds
end
ran.should be_true
ensure
Expand All @@ -52,7 +52,7 @@ pending_interpreted describe: "Signal" do
end

Process.signal Signal::USR1, Process.pid
sleep 0.1
sleep 0.1.seconds
ran_first.should be_true
ran_second.should be_true
ensure
Expand Down
4 changes: 2 additions & 2 deletions spec/support/channel.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ def schedule_timeout(c : Channel(SpecChannelStatus))
# TODO: it's not clear why some interpreter specs
# take more than 1 second in some cases.
# See #12429.
sleep 5
sleep 5.seconds
{% else %}
sleep 1
sleep 1.second
{% end %}
c.send(SpecChannelStatus::Timeout)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/support/retry.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def retry(n = 5, &)
if i == 0
Fiber.yield
else
sleep 0.01 * (2**i)
sleep 10.milliseconds * (2**i)
end
else
return
Expand Down
6 changes: 3 additions & 3 deletions src/benchmark.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ require "./benchmark/**"
# require "benchmark"
#
# Benchmark.ips do |x|
# x.report("short sleep") { sleep 0.01 }
# x.report("shorter sleep") { sleep 0.001 }
# x.report("short sleep") { sleep 10.milliseconds }
# x.report("shorter sleep") { sleep 1.millisecond }
# end
# ```
#
Expand All @@ -31,7 +31,7 @@ require "./benchmark/**"
# require "benchmark"
#
# Benchmark.ips(warmup: 4, calculation: 10) do |x|
# x.report("sleep") { sleep 0.01 }
# x.report("sleep") { sleep 10.milliseconds }
# end
# ```
#
Expand Down
5 changes: 3 additions & 2 deletions src/concurrent.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require "crystal/tracing"
#
# While this fiber is waiting this time, other ready-to-execute
# fibers might start their execution.
@[Deprecated("Use `::sleep(Time::Span)` instead")]
def sleep(seconds : Number) : Nil
if seconds < 0
raise ArgumentError.new "Sleep seconds must be positive"
Expand Down Expand Up @@ -42,15 +43,15 @@ end
#
# spawn do
# 6.times do
# sleep 1
# sleep 1.second
# puts 1
# end
# ch.send(nil)
# end
#
# spawn do
# 3.times do
# sleep 2
# sleep 2.seconds
# puts 2
# end
# ch.send(nil)
Expand Down
2 changes: 1 addition & 1 deletion src/crystal/system/unix/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ module Crystal::System::FileDescriptor

if retry
until flock(op)
sleep 0.1
sleep 0.1.seconds
end
else
flock(op) || raise IO::Error.from_errno("Error applying file lock: file is already locked", target: self)
Expand Down
2 changes: 1 addition & 1 deletion src/crystal/system/win32/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ module Crystal::System::FileDescriptor
handle = windows_handle
if retry && system_blocking?
until lock_file(handle, flags)
sleep 0.1
sleep 0.1.seconds
end
else
lock_file(handle, flags) || raise IO::Error.from_winerror("Error applying file lock: file is already locked", target: self)
Expand Down
6 changes: 3 additions & 3 deletions src/signal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ require "crystal/system/signal"
#
# ```
# puts "Ctrl+C still has the OS default action (stops the program)"
# sleep 3
# sleep 3.seconds
#
# Signal::INT.trap do
# puts "Gotcha!"
# end
# puts "Ctrl+C will be caught from now on"
# sleep 3
# sleep 3.seconds
#
# Signal::INT.reset
# puts "Ctrl+C is back to the OS default action"
# sleep 3
# sleep 3.seconds
# ```
#
# WARNING: An uncaught exception in a signal handler is a fatal error.
Expand Down

0 comments on commit f6e2ab3

Please sign in to comment.