summaryrefslogtreecommitdiff
path: root/spec/ruby/library/socket/tcpserver
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/library/socket/tcpserver')
-rw-r--r--spec/ruby/library/socket/tcpserver/accept_nonblock_spec.rb57
-rw-r--r--spec/ruby/library/socket/tcpserver/accept_spec.rb78
-rw-r--r--spec/ruby/library/socket/tcpserver/gets_spec.rb6
-rw-r--r--spec/ruby/library/socket/tcpserver/initialize_spec.rb101
-rw-r--r--spec/ruby/library/socket/tcpserver/listen_spec.rb28
-rw-r--r--spec/ruby/library/socket/tcpserver/new_spec.rb71
-rw-r--r--spec/ruby/library/socket/tcpserver/sysaccept_spec.rb46
7 files changed, 334 insertions, 53 deletions
diff --git a/spec/ruby/library/socket/tcpserver/accept_nonblock_spec.rb b/spec/ruby/library/socket/tcpserver/accept_nonblock_spec.rb
index d6f7448084..ac08fe37c6 100644
--- a/spec/ruby/library/socket/tcpserver/accept_nonblock_spec.rb
+++ b/spec/ruby/library/socket/tcpserver/accept_nonblock_spec.rb
@@ -1,5 +1,5 @@
-require File.expand_path('../../../../spec_helper', __FILE__)
-require File.expand_path('../../fixtures/classes', __FILE__)
+require_relative '../spec_helper'
+require_relative '../fixtures/classes'
describe "Socket::TCPServer.accept_nonblock" do
before :each do
@@ -13,19 +13,19 @@ describe "Socket::TCPServer.accept_nonblock" do
it "accepts non blocking connections" do
@server.listen(5)
- lambda {
+ -> {
@server.accept_nonblock
- }.should raise_error(IO::WaitReadable)
+ }.should.raise(IO::WaitReadable)
c = TCPSocket.new("127.0.0.1", @port)
- sleep 0.1
+ IO.select([@server])
s = @server.accept_nonblock
port, address = Socket.unpack_sockaddr_in(s.getsockname)
port.should == @port
address.should == "127.0.0.1"
- s.should be_kind_of(TCPSocket)
+ s.should.is_a?(TCPSocket)
c.close
s.close
@@ -33,17 +33,52 @@ describe "Socket::TCPServer.accept_nonblock" do
it "raises an IOError if the socket is closed" do
@server.close
- lambda { @server.accept }.should raise_error(IOError)
+ -> { @server.accept }.should.raise(IOError)
end
describe 'without a connected client' do
it 'raises error' do
- lambda { @server.accept_nonblock }.should raise_error(IO::WaitReadable)
+ -> { @server.accept_nonblock }.should.raise(IO::WaitReadable)
end
- ruby_version_is '2.3' do
- it 'returns :wait_readable in exceptionless mode' do
- @server.accept_nonblock(exception: false).should == :wait_readable
+ it 'returns :wait_readable in exceptionless mode' do
+ @server.accept_nonblock(exception: false).should == :wait_readable
+ end
+ end
+end
+
+describe 'TCPServer#accept_nonblock' do
+ SocketSpecs.each_ip_protocol do |family, ip_address|
+ before do
+ @server = TCPServer.new(ip_address, 0)
+ end
+
+ after do
+ @server.close
+ end
+
+ describe 'without a connected client' do
+ it 'raises IO::WaitReadable' do
+ -> { @server.accept_nonblock }.should.raise(IO::WaitReadable)
+ end
+ end
+
+ platform_is_not :windows do # spurious
+ describe 'with a connected client' do
+ before do
+ @client = TCPSocket.new(ip_address, @server.connect_address.ip_port)
+ end
+
+ after do
+ @socket.close if @socket
+ @client.close
+ end
+
+ it 'returns a TCPSocket' do
+ IO.select([@server])
+ @socket = @server.accept_nonblock
+ @socket.should.instance_of?(TCPSocket)
+ end
end
end
end
diff --git a/spec/ruby/library/socket/tcpserver/accept_spec.rb b/spec/ruby/library/socket/tcpserver/accept_spec.rb
index e7a1d87dbe..f2aa0bf8e1 100644
--- a/spec/ruby/library/socket/tcpserver/accept_spec.rb
+++ b/spec/ruby/library/socket/tcpserver/accept_spec.rb
@@ -1,6 +1,5 @@
-require File.expand_path('../../../../spec_helper', __FILE__)
-require File.expand_path('../../fixtures/classes', __FILE__)
-
+require_relative '../spec_helper'
+require_relative '../fixtures/classes'
describe "TCPServer#accept" do
before :each do
@@ -16,7 +15,7 @@ describe "TCPServer#accept" do
data = nil
t = Thread.new do
client = @server.accept
- client.should be_kind_of(TCPSocket)
+ client.should.is_a?(TCPSocket)
data = client.read(5)
client << "goodbye"
client.close
@@ -51,7 +50,7 @@ describe "TCPServer#accept" do
t = Thread.new {
-> {
@server.accept
- }.should raise_error(Exception, "interrupted")
+ }.should.raise(Exception, "interrupted")
}
Thread.pass while t.status and t.status != "sleep"
@@ -59,8 +58,75 @@ describe "TCPServer#accept" do
t.join
end
+ it "is automatically retried when interrupted by SIGVTALRM" do
+ t = Thread.new do
+ client = @server.accept
+ value = client.read(2)
+ client.close
+ value
+ end
+
+ Thread.pass while t.status and t.status != "sleep"
+ # Thread#backtrace uses SIGVTALRM on TruffleRuby and potentially other implementations.
+ # Sending a signal to a thread is not possible with Ruby APIs.
+ t.backtrace.join("\n").should =~ /in [`'](?:TCPServer#)?accept'/
+
+ socket = TCPSocket.new('127.0.0.1', @port)
+ socket.write("OK")
+ socket.close
+
+ t.value.should == "OK"
+ end
+
it "raises an IOError if the socket is closed" do
@server.close
- lambda { @server.accept }.should raise_error(IOError)
+ -> { @server.accept }.should.raise(IOError)
+ end
+end
+
+describe 'TCPServer#accept' do
+ SocketSpecs.each_ip_protocol do |family, ip_address|
+ before do
+ @server = TCPServer.new(ip_address, 0)
+ end
+
+ after do
+ @server.close
+ end
+
+ describe 'without a connected client' do
+ it 'blocks the caller' do
+ -> { @server.accept }.should block_caller
+ end
+ end
+
+ describe 'with a connected client' do
+ before do
+ @client = TCPSocket.new(ip_address, @server.connect_address.ip_port)
+ end
+
+ after do
+ @socket.close if @socket
+ @client.close
+ end
+
+ it 'returns a TCPSocket' do
+ @socket = @server.accept
+ @socket.should.instance_of?(TCPSocket)
+ end
+
+ platform_is_not :windows do
+ it "returns a TCPSocket which is set to nonblocking" do
+ require 'io/nonblock'
+ @socket = @server.accept
+ @socket.should.nonblock?
+ end
+ end
+
+ it "returns a TCPSocket which is set to close on exec" do
+ @socket = @server.accept
+ @socket.should.close_on_exec?
+ end
+ end
end
end
diff --git a/spec/ruby/library/socket/tcpserver/gets_spec.rb b/spec/ruby/library/socket/tcpserver/gets_spec.rb
index 86ba65eae2..72a72fa2dc 100644
--- a/spec/ruby/library/socket/tcpserver/gets_spec.rb
+++ b/spec/ruby/library/socket/tcpserver/gets_spec.rb
@@ -1,5 +1,5 @@
-require File.expand_path('../../../../spec_helper', __FILE__)
-require File.expand_path('../../fixtures/classes', __FILE__)
+require_relative '../spec_helper'
+require_relative '../fixtures/classes'
describe "TCPServer#gets" do
before :each do
@@ -11,6 +11,6 @@ describe "TCPServer#gets" do
end
it "raises Errno::ENOTCONN on gets" do
- lambda { @server.gets }.should raise_error(Errno::ENOTCONN)
+ -> { @server.gets }.should.raise(Errno::ENOTCONN)
end
end
diff --git a/spec/ruby/library/socket/tcpserver/initialize_spec.rb b/spec/ruby/library/socket/tcpserver/initialize_spec.rb
new file mode 100644
index 0000000000..517b014edc
--- /dev/null
+++ b/spec/ruby/library/socket/tcpserver/initialize_spec.rb
@@ -0,0 +1,101 @@
+require_relative '../spec_helper'
+require_relative '../fixtures/classes'
+
+describe 'TCPServer#initialize' do
+ describe 'with a single Integer argument' do
+ before do
+ @server = TCPServer.new(0)
+ end
+
+ after do
+ @server.close
+ end
+
+ it 'sets the port to the given argument' do
+ @server.local_address.ip_port.should.is_a?(Integer)
+ @server.local_address.ip_port.should > 0
+ end
+
+ platform_is_not :windows do
+ it 'sets the hostname to 0.0.0.0 or ::' do
+ a = @server.local_address
+ a.ip_address.should == (a.ipv6? ? '::' : '0.0.0.0')
+ end
+ end
+
+ it "sets the socket to binmode" do
+ @server.binmode?.should == true
+ end
+ end
+
+ describe 'with a single String argument containing a numeric value' do
+ before do
+ @server = TCPServer.new('0')
+ end
+
+ after do
+ @server.close
+ end
+
+ it 'sets the port to the given argument' do
+ @server.local_address.ip_port.should.is_a?(Integer)
+ @server.local_address.ip_port.should > 0
+ end
+
+ platform_is_not :windows do
+ it 'sets the hostname to 0.0.0.0 or ::' do
+ a = @server.local_address
+ a.ip_address.should == (a.ipv6? ? '::' : '0.0.0.0')
+ end
+ end
+ end
+
+ describe 'with a single String argument containing a non numeric value' do
+ it 'raises SocketError' do
+ -> { TCPServer.new('cats') }.should.raise(SocketError)
+ end
+ end
+
+ describe 'with a String and an Integer' do
+ SocketSpecs.each_ip_protocol do |family, ip_address|
+ before do
+ @server = TCPServer.new(ip_address, 0)
+ end
+
+ after do
+ @server.close
+ end
+
+ it 'sets the port to the given port argument' do
+ @server.local_address.ip_port.should.is_a?(Integer)
+ @server.local_address.ip_port.should > 0
+ end
+
+ it 'sets the hostname to the given host argument' do
+ @server.local_address.ip_address.should == ip_address
+ end
+ end
+ end
+
+ describe 'with a String and a custom object' do
+ before do
+ dummy = mock(:dummy)
+ dummy.stub!(:to_str).and_return('0')
+
+ @server = TCPServer.new('127.0.0.1', dummy)
+ end
+
+ after do
+ @server.close
+ end
+
+ it 'sets the port to the given port argument' do
+ @server.local_address.ip_port.should.is_a?(Integer)
+ @server.local_address.ip_port.should > 0
+ end
+
+ it 'sets the hostname to the given host argument' do
+ @server.local_address.ip_address.should == '127.0.0.1'
+ end
+ end
+end
diff --git a/spec/ruby/library/socket/tcpserver/listen_spec.rb b/spec/ruby/library/socket/tcpserver/listen_spec.rb
index d764b4ce70..5b046ef6f7 100644
--- a/spec/ruby/library/socket/tcpserver/listen_spec.rb
+++ b/spec/ruby/library/socket/tcpserver/listen_spec.rb
@@ -1,18 +1,22 @@
-require File.expand_path('../../../../spec_helper', __FILE__)
-require File.expand_path('../../fixtures/classes', __FILE__)
-
-require 'socket'
+require_relative '../spec_helper'
+require_relative '../fixtures/classes'
describe 'TCPServer#listen' do
- before :each do
- @server = TCPServer.new(SocketSpecs.hostname, 0)
- end
+ SocketSpecs.each_ip_protocol do |family, ip_address|
+ before do
+ @server = TCPServer.new(ip_address, 0)
+ end
- after :each do
- @server.close unless @server.closed?
- end
+ after do
+ @server.close
+ end
+
+ it 'returns 0' do
+ @server.listen(1).should == 0
+ end
- it 'returns 0' do
- @server.listen(10).should == 0
+ it "raises when the given argument can't be coerced to an Integer" do
+ -> { @server.listen('cats') }.should.raise(TypeError)
+ end
end
end
diff --git a/spec/ruby/library/socket/tcpserver/new_spec.rb b/spec/ruby/library/socket/tcpserver/new_spec.rb
index adbc3f303e..70b8d4352e 100644
--- a/spec/ruby/library/socket/tcpserver/new_spec.rb
+++ b/spec/ruby/library/socket/tcpserver/new_spec.rb
@@ -1,5 +1,5 @@
-require File.expand_path('../../../../spec_helper', __FILE__)
-require File.expand_path('../../fixtures/classes', __FILE__)
+require_relative '../spec_helper'
+require_relative '../fixtures/classes'
describe "TCPServer.new" do
after :each do
@@ -10,7 +10,7 @@ describe "TCPServer.new" do
@server = TCPServer.new('127.0.0.1', 0)
addr = @server.addr
addr[0].should == 'AF_INET'
- addr[1].should be_kind_of(Fixnum)
+ addr[1].should.is_a?(Integer)
# on some platforms (Mac), MRI
# returns comma at the end.
addr[2].should =~ /^#{SocketSpecs.hostname}\b/
@@ -20,12 +20,12 @@ describe "TCPServer.new" do
it "binds to localhost and a port with either IPv4 or IPv6" do
@server = TCPServer.new(SocketSpecs.hostname, 0)
addr = @server.addr
- addr[1].should be_kind_of(Fixnum)
+ addr[1].should.is_a?(Integer)
if addr[0] == 'AF_INET'
addr[2].should =~ /^#{SocketSpecs.hostname}\b/
addr[3].should == '127.0.0.1'
else
- addr[2].should =~ /^#{SocketSpecs.hostnamev6}\b/
+ addr[2].should =~ /^#{SocketSpecs.hostname('::1')}\b/
addr[3].should == '::1'
end
end
@@ -34,36 +34,77 @@ describe "TCPServer.new" do
@server = TCPServer.new('', 0)
addr = @server.addr
addr[0].should == 'AF_INET'
- addr[1].should be_kind_of(Fixnum)
+ addr[1].should.is_a?(Integer)
addr[2].should == '0.0.0.0'
addr[3].should == '0.0.0.0'
end
it "binds to INADDR_ANY if the hostname is empty and the port is a string" do
- @server = TCPServer.new('', 0)
+ @server = TCPServer.new('', '0')
+ addr = @server.addr
+ addr[0].should == 'AF_INET'
+ addr[1].should.is_a?(Integer)
+ addr[2].should == '0.0.0.0'
+ addr[3].should == '0.0.0.0'
+ end
+
+ it "binds to a port if the port is explicitly nil" do
+ @server = TCPServer.new('', nil)
+ addr = @server.addr
+ addr[0].should == 'AF_INET'
+ addr[1].should.is_a?(Integer)
+ addr[2].should == '0.0.0.0'
+ addr[3].should == '0.0.0.0'
+ end
+
+ it "binds to a port if the port is an empty string" do
+ @server = TCPServer.new('', '')
addr = @server.addr
addr[0].should == 'AF_INET'
- addr[1].should be_kind_of(Fixnum)
+ addr[1].should.is_a?(Integer)
addr[2].should == '0.0.0.0'
addr[3].should == '0.0.0.0'
end
it "coerces port to string, then determines port from that number or service name" do
- lambda { TCPServer.new(SocketSpecs.hostname, Object.new) }.should raise_error(TypeError)
+ -> { TCPServer.new(SocketSpecs.hostname, Object.new) }.should.raise(TypeError)
port = Object.new
port.should_receive(:to_str).and_return("0")
@server = TCPServer.new(SocketSpecs.hostname, port)
addr = @server.addr
- addr[1].should be_kind_of(Fixnum)
+ addr[1].should.is_a?(Integer)
# TODO: This should also accept strings like 'https', but I don't know how to
# pick such a service port that will be able to reliably bind...
end
+ it "has a single argument form and treats it as a port number" do
+ @server = TCPServer.new(0)
+ addr = @server.addr
+ addr[1].should.is_a?(Integer)
+ end
+
+ it "coerces port to a string when it is the only argument" do
+ -> { TCPServer.new(Object.new) }.should.raise(TypeError)
+
+ port = Object.new
+ port.should_receive(:to_str).and_return("0")
+
+ @server = TCPServer.new(port)
+ addr = @server.addr
+ addr[1].should.is_a?(Integer)
+ end
+
+ it "does not use the given block and warns to use TCPServer::open" do
+ -> {
+ @server = TCPServer.new(0) { raise }
+ }.should complain(/warning: TCPServer::new\(\) does not take block; use TCPServer::open\(\) instead/)
+ end
+
it "raises Errno::EADDRNOTAVAIL when the address is unknown" do
- lambda { TCPServer.new("1.2.3.4", 0) }.should raise_error(Errno::EADDRNOTAVAIL)
+ -> { TCPServer.new("1.2.3.4", 0) }.should.raise(Errno::EADDRNOTAVAIL)
end
# There is no way to make this fail-proof on all machines, because
@@ -71,17 +112,17 @@ describe "TCPServer.new" do
# traditionally invalidly named ones.
quarantine! do
it "raises a SocketError when the host is unknown" do
- lambda {
+ -> {
TCPServer.new("--notavalidname", 0)
- }.should raise_error(SocketError)
+ }.should.raise(SocketError)
end
end
it "raises Errno::EADDRINUSE when address is already in use" do
@server = TCPServer.new('127.0.0.1', 0)
- lambda {
+ -> {
@server = TCPServer.new('127.0.0.1', @server.addr[1])
- }.should raise_error(Errno::EADDRINUSE)
+ }.should.raise(Errno::EADDRINUSE)
end
platform_is_not :windows, :aix do
diff --git a/spec/ruby/library/socket/tcpserver/sysaccept_spec.rb b/spec/ruby/library/socket/tcpserver/sysaccept_spec.rb
index 93c1ffe152..ed23bced23 100644
--- a/spec/ruby/library/socket/tcpserver/sysaccept_spec.rb
+++ b/spec/ruby/library/socket/tcpserver/sysaccept_spec.rb
@@ -1,7 +1,5 @@
-require File.expand_path('../../../../spec_helper', __FILE__)
-require File.expand_path('../../fixtures/classes', __FILE__)
-
-require 'socket'
+require_relative '../spec_helper'
+require_relative '../fixtures/classes'
describe "TCPServer#sysaccept" do
before :each do
@@ -14,7 +12,7 @@ describe "TCPServer#sysaccept" do
end
it 'blocks if no connections' do
- lambda { @server.sysaccept }.should block_caller
+ -> { @server.sysaccept }.should block_caller
end
it 'returns file descriptor of an accepted connection' do
@@ -23,10 +21,46 @@ describe "TCPServer#sysaccept" do
fd = @server.sysaccept
- fd.should be_an_instance_of(Fixnum)
+ fd.should.is_a?(Integer)
ensure
sock.close if sock && !sock.closed?
IO.for_fd(fd).close if fd
end
end
end
+
+describe 'TCPServer#sysaccept' do
+ SocketSpecs.each_ip_protocol do |family, ip_address|
+ before do
+ @server = TCPServer.new(ip_address, 0)
+ end
+
+ after do
+ @server.close
+ end
+
+ describe 'without a connected client' do
+ it 'blocks the caller' do
+ -> { @server.sysaccept }.should block_caller
+ end
+ end
+
+ describe 'with a connected client' do
+ before do
+ @client = TCPSocket.new(ip_address, @server.connect_address.ip_port)
+ end
+
+ after do
+ Socket.for_fd(@fd).close if @fd
+ @client.close
+ end
+
+ it 'returns a new file descriptor as an Integer' do
+ @fd = @server.sysaccept
+
+ @fd.should.is_a?(Integer)
+ @fd.should_not == @client.fileno
+ end
+ end
+ end
+end