diff options
Diffstat (limited to 'spec/ruby/library/socket/tcpsocket/shared')
| -rw-r--r-- | spec/ruby/library/socket/tcpsocket/shared/new.rb | 68 |
1 files changed, 32 insertions, 36 deletions
diff --git a/spec/ruby/library/socket/tcpsocket/shared/new.rb b/spec/ruby/library/socket/tcpsocket/shared/new.rb index 90f8d7e6a2..cf4834526d 100644 --- a/spec/ruby/library/socket/tcpsocket/shared/new.rb +++ b/spec/ruby/library/socket/tcpsocket/shared/new.rb @@ -3,37 +3,24 @@ require_relative '../../fixtures/classes' describe :tcpsocket_new, shared: true do it "requires a hostname and a port as arguments" do - -> { TCPSocket.send(@method) }.should raise_error(ArgumentError) + -> { TCPSocket.send(@method) }.should.raise(ArgumentError) end it "refuses the connection when there is no server to connect to" do -> do TCPSocket.send(@method, SocketSpecs.hostname, SocketSpecs.reserved_unused_port) - end.should raise_error(SystemCallError) {|e| - [Errno::ECONNREFUSED, Errno::EADDRNOTAVAIL].should include(e.class) + end.should.raise(SystemCallError) {|e| + [Errno::ECONNREFUSED, Errno::EADDRNOTAVAIL].should.include?(e.class) } end - ruby_version_is ""..."3.2" do - it 'raises Errno::ETIMEDOUT with :connect_timeout when no server is listening on the given address' do - -> { - TCPSocket.send(@method, "192.0.2.1", 80, connect_timeout: 0) - }.should raise_error(Errno::ETIMEDOUT) - rescue Errno::ENETUNREACH - # In the case all network interfaces down. - # raise_error cannot deal with multiple expected exceptions - end - end - - ruby_version_is "3.2" do - it 'raises IO::TimeoutError with :connect_timeout when no server is listening on the given address' do - -> { - TCPSocket.send(@method, "192.0.2.1", 80, connect_timeout: 0) - }.should raise_error(IO::TimeoutError) - rescue Errno::ENETUNREACH - # In the case all network interfaces down. - # raise_error cannot deal with multiple expected exceptions - end + it 'raises IO::TimeoutError with :connect_timeout when no server is listening on the given address' do + -> { + TCPSocket.send(@method, "192.0.2.1", 80, connect_timeout: 0) + }.should.raise(IO::TimeoutError) + rescue Errno::ENETUNREACH + # In the case all network interfaces down. + # raise_error cannot deal with multiple expected exceptions end describe "with a running server" do @@ -52,34 +39,43 @@ describe :tcpsocket_new, shared: true do it "silently ignores 'nil' as the third parameter" do @socket = TCPSocket.send(@method, @hostname, @server.port, nil) - @socket.should be_an_instance_of(TCPSocket) + @socket.should.instance_of?(TCPSocket) end it "connects to a listening server with host and port" do @socket = TCPSocket.send(@method, @hostname, @server.port) - @socket.should be_an_instance_of(TCPSocket) + @socket.should.instance_of?(TCPSocket) end it "connects to a server when passed local_host argument" do @socket = TCPSocket.send(@method, @hostname, @server.port, @hostname) - @socket.should be_an_instance_of(TCPSocket) + @socket.should.instance_of?(TCPSocket) end it "connects to a server when passed local_host and local_port arguments" do - server = TCPServer.new(SocketSpecs.hostname, 0) + retries = 0 + max_retries = 3 + begin - available_port = server.addr[1] - ensure - server.close + retries += 1 + server = TCPServer.new(SocketSpecs.hostname, 0) + begin + available_port = server.addr[1] + ensure + server.close + end + @socket = TCPSocket.send(@method, @hostname, @server.port, + @hostname, available_port) + rescue Errno::EADDRINUSE + raise if retries >= max_retries + retry end - @socket = TCPSocket.send(@method, @hostname, @server.port, - @hostname, available_port) - @socket.should be_an_instance_of(TCPSocket) + @socket.should.instance_of?(TCPSocket) end it "has an address once it has connected to a listening server" do @socket = TCPSocket.send(@method, @hostname, @server.port) - @socket.should be_an_instance_of(TCPSocket) + @socket.should.instance_of?(TCPSocket) # TODO: Figure out how to abstract this. You can get AF_INET # from 'Socket.getaddrinfo(hostname, nil)[0][3]' but socket.addr @@ -94,13 +90,13 @@ describe :tcpsocket_new, shared: true do @socket.addr[3].should == SocketSpecs.addr(:ipv6) end - @socket.addr[1].should be_kind_of(Integer) + @socket.addr[1].should.is_a?(Integer) @socket.addr[2].should =~ /^#{@hostname}/ end it "connects to a server when passed connect_timeout argument" do @socket = TCPSocket.send(@method, @hostname, @server.port, connect_timeout: 1) - @socket.should be_an_instance_of(TCPSocket) + @socket.should.instance_of?(TCPSocket) end end end |
