Skip to content

Commit

Permalink
Contain fibers and open ports in Socket and TCPSocket specs
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Mar 3, 2019
1 parent bbf4202 commit 932035a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
31 changes: 23 additions & 8 deletions spec/std/socket/socket_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,32 @@ describe Socket do
end

it ".accept" do
client_done = Channel(Nil).new
server = Socket.new(Socket::Family::INET, Socket::Type::STREAM, Socket::Protocol::TCP)
port = unused_local_port
server.bind("0.0.0.0", port)
server.listen

spawn { TCPSocket.new("127.0.0.1", port).close }
begin
port = unused_local_port
server.bind("0.0.0.0", port)
server.listen

spawn do
TCPSocket.new("127.0.0.1", port).close
ensure
client_done.send nil
end

client = server.accept
client.family.should eq(Socket::Family::INET)
client.type.should eq(Socket::Type::STREAM)
client.protocol.should eq(Socket::Protocol::TCP)
client = server.accept
begin
client.family.should eq(Socket::Family::INET)
client.type.should eq(Socket::Type::STREAM)
client.protocol.should eq(Socket::Protocol::TCP)
ensure
client.close
end
ensure
server.close
client_done.receive
end
end

it "sends messages" do
Expand Down
11 changes: 8 additions & 3 deletions spec/std/socket/tcp_server_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ describe TCPServer do
it "binds to port 0" do
server = TCPServer.new(address, 0)

server.local_address.address.should eq(address)
server.local_address.port.should be > 0
begin
server.local_address.address.should eq(address)
server.local_address.port.should be > 0
ensure
server.close
end
end

it "raises when port is negative" do
Expand Down Expand Up @@ -74,7 +78,8 @@ describe TCPServer do

describe "address resolution" do
it "binds to localhost" do
TCPServer.new("localhost", unused_local_port)
server = TCPServer.new("localhost", unused_local_port)
server.close
end

it "raises when host doesn't exist" do
Expand Down

0 comments on commit 932035a

Please sign in to comment.