mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Move spec/rubyspec to spec/ruby for consistency
* Other ruby implementations use the spec/ruby directory. [Misc #13792] [ruby-core:82287] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
75bfc6440d
commit
1d15d5f080
4370 changed files with 0 additions and 0 deletions
50
spec/ruby/library/socket/tcpserver/accept_nonblock_spec.rb
Normal file
50
spec/ruby/library/socket/tcpserver/accept_nonblock_spec.rb
Normal file
|
@ -0,0 +1,50 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../../fixtures/classes', __FILE__)
|
||||
|
||||
describe "Socket::TCPServer.accept_nonblock" do
|
||||
before :each do
|
||||
@server = TCPServer.new("127.0.0.1", 0)
|
||||
@port = @server.addr[1]
|
||||
end
|
||||
|
||||
after :each do
|
||||
@server.close unless @server.closed?
|
||||
end
|
||||
|
||||
it "accepts non blocking connections" do
|
||||
@server.listen(5)
|
||||
lambda {
|
||||
@server.accept_nonblock
|
||||
}.should raise_error(IO::WaitReadable)
|
||||
|
||||
c = TCPSocket.new("127.0.0.1", @port)
|
||||
sleep 0.1
|
||||
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)
|
||||
|
||||
c.close
|
||||
s.close
|
||||
end
|
||||
|
||||
it "raises an IOError if the socket is closed" do
|
||||
@server.close
|
||||
lambda { @server.accept }.should raise_error(IOError)
|
||||
end
|
||||
|
||||
describe 'without a connected client' do
|
||||
it 'raises error' do
|
||||
lambda { @server.accept_nonblock }.should raise_error(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
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
65
spec/ruby/library/socket/tcpserver/accept_spec.rb
Normal file
65
spec/ruby/library/socket/tcpserver/accept_spec.rb
Normal file
|
@ -0,0 +1,65 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../../fixtures/classes', __FILE__)
|
||||
|
||||
|
||||
describe "TCPServer#accept" do
|
||||
before :each do
|
||||
@server = TCPServer.new("127.0.0.1", 0)
|
||||
@port = @server.addr[1]
|
||||
end
|
||||
|
||||
after :each do
|
||||
@server.close unless @server.closed?
|
||||
end
|
||||
|
||||
it "accepts a connection and returns a TCPSocket" do
|
||||
data = nil
|
||||
t = Thread.new do
|
||||
client = @server.accept
|
||||
client.should be_kind_of(TCPSocket)
|
||||
data = client.read(5)
|
||||
client << "goodbye"
|
||||
client.close
|
||||
end
|
||||
Thread.pass while t.status and t.status != "sleep"
|
||||
|
||||
socket = TCPSocket.new('127.0.0.1', @port)
|
||||
socket.write('hello')
|
||||
socket.shutdown(1) # we are done with sending
|
||||
socket.read.should == 'goodbye'
|
||||
t.join
|
||||
data.should == 'hello'
|
||||
socket.close
|
||||
end
|
||||
|
||||
it "can be interrupted by Thread#kill" do
|
||||
t = Thread.new { @server.accept }
|
||||
|
||||
Thread.pass while t.status and t.status != "sleep"
|
||||
|
||||
# kill thread, ensure it dies in a reasonable amount of time
|
||||
t.kill
|
||||
a = 0
|
||||
while t.alive? and a < 5000
|
||||
sleep 0.001
|
||||
a += 1
|
||||
end
|
||||
a.should < 5000
|
||||
end
|
||||
|
||||
it "can be interrupted by Thread#raise" do
|
||||
t = Thread.new { @server.accept }
|
||||
|
||||
Thread.pass while t.status and t.status != "sleep"
|
||||
|
||||
# raise in thread, ensure the raise happens
|
||||
ex = Exception.new
|
||||
t.raise ex
|
||||
lambda { t.join }.should raise_error(Exception)
|
||||
end
|
||||
|
||||
it "raises an IOError if the socket is closed" do
|
||||
@server.close
|
||||
lambda { @server.accept }.should raise_error(IOError)
|
||||
end
|
||||
end
|
16
spec/ruby/library/socket/tcpserver/gets_spec.rb
Normal file
16
spec/ruby/library/socket/tcpserver/gets_spec.rb
Normal file
|
@ -0,0 +1,16 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../../fixtures/classes', __FILE__)
|
||||
|
||||
describe "TCPServer#gets" do
|
||||
before :each do
|
||||
@server = TCPServer.new(SocketSpecs.hostname, 0)
|
||||
end
|
||||
|
||||
after :each do
|
||||
@server.close
|
||||
end
|
||||
|
||||
it "raises Errno::ENOTCONN on gets" do
|
||||
lambda { @server.gets }.should raise_error(Errno::ENOTCONN)
|
||||
end
|
||||
end
|
18
spec/ruby/library/socket/tcpserver/listen_spec.rb
Normal file
18
spec/ruby/library/socket/tcpserver/listen_spec.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../../fixtures/classes', __FILE__)
|
||||
|
||||
require 'socket'
|
||||
|
||||
describe 'TCPServer#listen' do
|
||||
before :each do
|
||||
@server = TCPServer.new(SocketSpecs.hostname, 0)
|
||||
end
|
||||
|
||||
after :each do
|
||||
@server.close unless @server.closed?
|
||||
end
|
||||
|
||||
it 'returns 0' do
|
||||
@server.listen(10).should == 0
|
||||
end
|
||||
end
|
96
spec/ruby/library/socket/tcpserver/new_spec.rb
Normal file
96
spec/ruby/library/socket/tcpserver/new_spec.rb
Normal file
|
@ -0,0 +1,96 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../../fixtures/classes', __FILE__)
|
||||
|
||||
describe "TCPServer.new" do
|
||||
after :each do
|
||||
@server.close if @server && !@server.closed?
|
||||
end
|
||||
|
||||
it "binds to a host and a port" do
|
||||
@server = TCPServer.new('127.0.0.1', 0)
|
||||
addr = @server.addr
|
||||
addr[0].should == 'AF_INET'
|
||||
addr[1].should be_kind_of(Fixnum)
|
||||
# on some platforms (Mac), MRI
|
||||
# returns comma at the end.
|
||||
addr[2].should =~ /^#{SocketSpecs.hostname}\b/
|
||||
addr[3].should == '127.0.0.1'
|
||||
end
|
||||
|
||||
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)
|
||||
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[3].should == '::1'
|
||||
end
|
||||
end
|
||||
|
||||
it "binds to INADDR_ANY if the hostname is empty" do
|
||||
@server = TCPServer.new('', 0)
|
||||
addr = @server.addr
|
||||
addr[0].should == 'AF_INET'
|
||||
addr[1].should be_kind_of(Fixnum)
|
||||
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)
|
||||
addr = @server.addr
|
||||
addr[0].should == 'AF_INET'
|
||||
addr[1].should be_kind_of(Fixnum)
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
# 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 "raises Errno::EADDRNOTAVAIL when the adress is unknown" do
|
||||
lambda { TCPServer.new("1.2.3.4", 0) }.should raise_error(Errno::EADDRNOTAVAIL)
|
||||
end
|
||||
|
||||
# There is no way to make this fail-proof on all machines, because
|
||||
# DNS servers like opendns return A records for ANY host, including
|
||||
# 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)
|
||||
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)
|
||||
end
|
||||
|
||||
platform_is_not :windows, :aix do
|
||||
# A known bug in AIX. getsockopt(2) does not properly set
|
||||
# the fifth argument for SO_REUSEADDR.
|
||||
it "sets SO_REUSEADDR on the resulting server" do
|
||||
@server = TCPServer.new('127.0.0.1', 0)
|
||||
@server.getsockopt(:SOCKET, :REUSEADDR).data.should_not == "\x00\x00\x00\x00"
|
||||
@server.getsockopt(:SOCKET, :REUSEADDR).int.should_not == 0
|
||||
end
|
||||
end
|
||||
end
|
32
spec/ruby/library/socket/tcpserver/sysaccept_spec.rb
Normal file
32
spec/ruby/library/socket/tcpserver/sysaccept_spec.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
require File.expand_path('../../../../spec_helper', __FILE__)
|
||||
require File.expand_path('../../fixtures/classes', __FILE__)
|
||||
|
||||
require 'socket'
|
||||
|
||||
describe "TCPServer#sysaccept" do
|
||||
before :each do
|
||||
@server = TCPServer.new(SocketSpecs.hostname, 0)
|
||||
@port = @server.addr[1]
|
||||
end
|
||||
|
||||
after :each do
|
||||
@server.close unless @server.closed?
|
||||
end
|
||||
|
||||
it 'blocks if no connections' do
|
||||
lambda { @server.sysaccept }.should block_caller
|
||||
end
|
||||
|
||||
it 'returns file descriptor of an accepted connection' do
|
||||
begin
|
||||
sock = TCPSocket.new(SocketSpecs.hostname, @port)
|
||||
|
||||
fd = @server.sysaccept
|
||||
|
||||
fd.should be_an_instance_of(Fixnum)
|
||||
ensure
|
||||
sock.close if sock && !sock.closed?
|
||||
IO.for_fd(fd).close if fd
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue