diff options
Diffstat (limited to 'spec/ruby/library/socket/tcpsocket')
7 files changed, 86 insertions, 43 deletions
diff --git a/spec/ruby/library/socket/tcpsocket/gethostbyname_spec.rb b/spec/ruby/library/socket/tcpsocket/gethostbyname_spec.rb index f0e98778f5..c6fe007827 100644 --- a/spec/ruby/library/socket/tcpsocket/gethostbyname_spec.rb +++ b/spec/ruby/library/socket/tcpsocket/gethostbyname_spec.rb @@ -2,7 +2,7 @@ require_relative '../spec_helper' require_relative '../fixtures/classes' # TODO: verify these for windows -describe "TCPSocket#gethostbyname" do +describe "TCPSocket.gethostbyname" do before :each do suppress_warning do @host_info = TCPSocket.gethostbyname(SocketSpecs.hostname) @@ -10,7 +10,7 @@ describe "TCPSocket#gethostbyname" do end it "returns an array elements of information on the hostname" do - @host_info.should be_kind_of(Array) + @host_info.should.is_a?(Array) end platform_is_not :windows do @@ -20,12 +20,12 @@ describe "TCPSocket#gethostbyname" do it "returns the address type as the third value" do address_type = @host_info[2] - [Socket::AF_INET, Socket::AF_INET6].include?(address_type).should be_true + [Socket::AF_INET, Socket::AF_INET6].include?(address_type).should == true end it "returns the IP address as the fourth value" do ip = @host_info[3] - ["127.0.0.1", "::1"].include?(ip).should be_true + ["127.0.0.1", "::1"].include?(ip).should == true end end @@ -48,14 +48,14 @@ describe "TCPSocket#gethostbyname" do end it "returns any aliases to the address as second value" do - @host_info[1].should be_kind_of(Array) + @host_info[1].should.is_a?(Array) end end -describe 'TCPSocket#gethostbyname' do +describe 'TCPSocket.gethostbyname' do it 'returns an Array' do suppress_warning do - TCPSocket.gethostbyname('127.0.0.1').should be_an_instance_of(Array) + TCPSocket.gethostbyname('127.0.0.1').should.instance_of?(Array) end end @@ -72,11 +72,11 @@ describe 'TCPSocket#gethostbyname' do end it 'includes an array of alternative hostnames as the 2nd value' do - @array[1].should be_an_instance_of(Array) + @array[1].should.instance_of?(Array) end it 'includes the address family as the 3rd value' do - @array[2].should be_kind_of(Integer) + @array[2].should.is_a?(Integer) end it 'includes the IP addresses as all the remaining values' do diff --git a/spec/ruby/library/socket/tcpsocket/initialize_spec.rb b/spec/ruby/library/socket/tcpsocket/initialize_spec.rb index 065c8f4190..a33d0b16ba 100644 --- a/spec/ruby/library/socket/tcpsocket/initialize_spec.rb +++ b/spec/ruby/library/socket/tcpsocket/initialize_spec.rb @@ -4,13 +4,34 @@ require_relative 'shared/new' describe 'TCPSocket#initialize' do it_behaves_like :tcpsocket_new, :new + + describe "with a running server" do + before :each do + @server = SocketSpecs::SpecTCPServer.new + @hostname = @server.hostname + end + + after :each do + if @socket + @socket.write "QUIT" + @socket.close + end + @server.shutdown + end + + it "does not use the given block and warns to use TCPSocket::open" do + -> { + @socket = TCPSocket.new(@hostname, @server.port, nil) { raise } + }.should complain(/warning: TCPSocket::new\(\) does not take block; use TCPSocket::open\(\) instead/) + end + end end describe 'TCPSocket#initialize' do SocketSpecs.each_ip_protocol do |family, ip_address| describe 'when no server is listening on the given address' do it 'raises Errno::ECONNREFUSED' do - -> { TCPSocket.new(ip_address, 666) }.should raise_error(Errno::ECONNREFUSED) + -> { TCPSocket.new(ip_address, 666) }.should.raise(Errno::ECONNREFUSED) end end @@ -27,21 +48,21 @@ describe 'TCPSocket#initialize' do it 'returns a TCPSocket when using an Integer as the port' do @client = TCPSocket.new(ip_address, @port) - @client.should be_an_instance_of(TCPSocket) + @client.should.instance_of?(TCPSocket) end it 'returns a TCPSocket when using a String as the port' do @client = TCPSocket.new(ip_address, @port.to_s) - @client.should be_an_instance_of(TCPSocket) + @client.should.instance_of?(TCPSocket) end it 'raises SocketError when the port number is a non numeric String' do - -> { TCPSocket.new(ip_address, 'cats') }.should raise_error(SocketError) + -> { TCPSocket.new(ip_address, 'cats') }.should.raise(SocketError) end it 'set the socket to binmode' do @client = TCPSocket.new(ip_address, @port) - @client.binmode?.should be_true + @client.binmode?.should == true end it 'connects to the right address' do @@ -51,6 +72,19 @@ describe 'TCPSocket#initialize' do @client.remote_address.ip_port.should == @server.local_address.ip_port end + platform_is_not :windows do + it "creates a socket which is set to nonblocking" do + require 'io/nonblock' + @client = TCPSocket.new(ip_address, @port) + @client.should.nonblock? + end + end + + it "creates a socket which is set to close on exec" do + @client = TCPSocket.new(ip_address, @port) + @client.should.close_on_exec? + end + describe 'using a local address and service' do it 'binds the client socket to the local address and service' do @client = TCPSocket.new(ip_address, @port, ip_address, 0) diff --git a/spec/ruby/library/socket/tcpsocket/local_address_spec.rb b/spec/ruby/library/socket/tcpsocket/local_address_spec.rb index ce66d5ff8f..5dcf741f29 100644 --- a/spec/ruby/library/socket/tcpsocket/local_address_spec.rb +++ b/spec/ruby/library/socket/tcpsocket/local_address_spec.rb @@ -23,7 +23,7 @@ describe 'TCPSocket#local_address' do end it 'returns an Addrinfo' do - @sock.local_address.should be_an_instance_of(Addrinfo) + @sock.local_address.should.instance_of?(Addrinfo) end describe 'the returned Addrinfo' do diff --git a/spec/ruby/library/socket/tcpsocket/open_spec.rb b/spec/ruby/library/socket/tcpsocket/open_spec.rb index 31b630a23b..0c0b579064 100644 --- a/spec/ruby/library/socket/tcpsocket/open_spec.rb +++ b/spec/ruby/library/socket/tcpsocket/open_spec.rb @@ -1,3 +1,4 @@ +require_relative "../../../spec_helper" require_relative 'shared/new' describe "TCPSocket.open" do diff --git a/spec/ruby/library/socket/tcpsocket/partially_closable_spec.rb b/spec/ruby/library/socket/tcpsocket/partially_closable_spec.rb index a381627a39..d365ecd335 100644 --- a/spec/ruby/library/socket/tcpsocket/partially_closable_spec.rb +++ b/spec/ruby/library/socket/tcpsocket/partially_closable_spec.rb @@ -16,6 +16,6 @@ describe "TCPSocket partial closability" do @s2.close end - it_should_behave_like "partially closable sockets" + it_should_behave_like :partially_closable_sockets end diff --git a/spec/ruby/library/socket/tcpsocket/remote_address_spec.rb b/spec/ruby/library/socket/tcpsocket/remote_address_spec.rb index eb9dabc075..085d57b3f9 100644 --- a/spec/ruby/library/socket/tcpsocket/remote_address_spec.rb +++ b/spec/ruby/library/socket/tcpsocket/remote_address_spec.rb @@ -23,7 +23,7 @@ describe 'TCPSocket#remote_address' do end it 'returns an Addrinfo' do - @sock.remote_address.should be_an_instance_of(Addrinfo) + @sock.remote_address.should.instance_of?(Addrinfo) end describe 'the returned Addrinfo' do diff --git a/spec/ruby/library/socket/tcpsocket/shared/new.rb b/spec/ruby/library/socket/tcpsocket/shared/new.rb index 4189acc2f8..cf4834526d 100644 --- a/spec/ruby/library/socket/tcpsocket/shared/new.rb +++ b/spec/ruby/library/socket/tcpsocket/shared/new.rb @@ -3,23 +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.0" 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) - 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 @@ -38,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 @@ -80,15 +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 - ruby_version_is "3.0" do - 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) - end + it "connects to a server when passed connect_timeout argument" do + @socket = TCPSocket.send(@method, @hostname, @server.port, connect_timeout: 1) + @socket.should.instance_of?(TCPSocket) end end end |
