Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test_server spec helper to not leak fiber #7612

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions spec/std/http/client/client_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,34 @@ require "http/server"

private def test_server(host, port, read_time = 0, content_type = "text/plain", write_response = true)
server = TCPServer.new(host, port)
begin
spawn do
io = server.accept
server_done = Channel(Exception?).new

spawn do
io = server.accept
begin
sleep read_time
if write_response
response = HTTP::Client::Response.new(200, headers: HTTP::Headers{"Content-Type" => content_type}, body: "OK")
response.to_io(io)
io.flush
end
ensure
io.close
end
rescue exc
server_done.send exc
else
server_done.send nil
end

begin
yield server
ensure
server.close

if exc = server_done.receive
raise exc
end
end
end

Expand Down Expand Up @@ -163,15 +177,17 @@ 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 = nil

test_server("localhost", 0, 0) do |server|
client = Client.new("localhost", server.local_address.port)
break client.get("/")
resp_get = client.get("/")
end

test_server("localhost", 0, 0) do |server|
client = Client.new("localhost", server.local_address.port)
resp_head = client.head("/")
resp_head.headers.should eq(resp_get.headers)
resp_head.headers.should eq(resp_get.try &.headers)
resp_head.body.should eq("")
end
end
Expand Down