Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into feature/windows-i…
Browse files Browse the repository at this point in the history
…nterpreter
  • Loading branch information
HertzDevil committed Sep 5, 2024
2 parents 342bc4e + f6e2ab3 commit 48110f0
Show file tree
Hide file tree
Showing 26 changed files with 167 additions and 53 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 @@ -12,7 +12,7 @@ require "log/spec"
{% skip_file %}
{% end %}

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 @@ -318,12 +318,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 @@ -344,7 +344,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 @@ -354,7 +354,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 @@ -368,7 +368,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 @@ -378,7 +378,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 @@ -71,14 +71,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
9 changes: 9 additions & 0 deletions spec/std/io/buffered_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ describe "IO::Buffered" do
end
end

it "can set buffer_size to the same value after first use" do
io = BufferedWrapper.new(IO::Memory.new("hello\r\nworld\n"))
io.buffer_size = 16_384
io.gets

io.buffer_size = 16_384
io.buffer_size.should eq(16_384)
end

it "does gets" do
io = BufferedWrapper.new(IO::Memory.new("hello\r\nworld\n"))
io.gets.should eq("hello")
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 @@ -136,7 +136,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
9 changes: 9 additions & 0 deletions src/crystal/system/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ module Crystal::System::FileDescriptor
# Also used in `IO::FileDescriptor#finalize`.
# def file_descriptor_close

# Returns `true` or `false` if this file descriptor pretends to block or not
# to block the caller thread regardless of the underlying internal file
# descriptor's implementation. Returns `nil` if nothing needs to be done, i.e.
# `#blocking` is identical to `#system_blocking?`.
#
# Currently used by console STDIN on Windows.
private def emulated_blocking? : Bool?
end

private def system_read(slice : Bytes) : Int32
event_loop.read(self, slice)
end
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/event_loop_iocp.cr
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class Crystal::IOCP::EventLoop < Crystal::EventLoop
end

def connect(socket : ::Socket, address : ::Socket::Addrinfo | ::Socket::Address, timeout : ::Time::Span?) : IO::Error?
socket.overlapped_connect(socket.fd, "ConnectEx") do |overlapped|
socket.overlapped_connect(socket.fd, "ConnectEx", timeout) do |overlapped|
# This is: LibC.ConnectEx(fd, address, address.size, nil, 0, nil, overlapped)
Crystal::System::Socket.connect_ex.call(socket.fd, address.to_unsafe, address.size, Pointer(Void).null, 0_u32, Pointer(UInt32).null, overlapped.to_unsafe)
end
Expand Down
Loading

0 comments on commit 48110f0

Please sign in to comment.