PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/library/socket/tcpserver/new_spec.rb

https://github.com/gilmation/rubyspec
Ruby | 78 lines | 60 code | 11 blank | 7 comment | 16 complexity | f5656f9990e2f8d09781c003d1f46b96 MD5 | raw file
  1. require File.expand_path('../../../../spec_helper', __FILE__)
  2. require File.expand_path('../../fixtures/classes', __FILE__)
  3. describe "TCPServer.new" do
  4. after(:each) do
  5. @server.close if @server && !@server.closed?
  6. end
  7. it "binds to a host and a port" do
  8. @server = TCPServer.new('127.0.0.1', SocketSpecs.port)
  9. addr = @server.addr
  10. addr[0].should == 'AF_INET'
  11. addr[1].should be_kind_of(Fixnum)
  12. # on some platforms (Mac), MRI
  13. # returns comma at the end.
  14. addr[2].should =~ /^#{SocketSpecs.hostname}\b/
  15. addr[3].should == '127.0.0.1'
  16. end
  17. it "binds to localhost and a port with either IPv4 or IPv6" do
  18. @server = TCPServer.new(SocketSpecs.hostname, SocketSpecs.port)
  19. addr = @server.addr
  20. if addr[0] == 'AF_INET'
  21. addr[1].should == SocketSpecs.port
  22. addr[2].should =~ /^#{SocketSpecs.hostname}\b/
  23. addr[3].should == '127.0.0.1'
  24. else
  25. addr[1].should == SocketSpecs.port
  26. addr[2].should =~ /^#{SocketSpecs.hostnamev6}\b/
  27. addr[3].should == '::1'
  28. end
  29. end
  30. it "binds to INADDR_ANY if the hostname is empty" do
  31. @server = TCPServer.new('', SocketSpecs.port)
  32. addr = @server.addr
  33. addr[0].should == 'AF_INET'
  34. addr[1].should == SocketSpecs.port
  35. addr[2].should == '0.0.0.0'
  36. addr[3].should == '0.0.0.0'
  37. end
  38. it "coerces port to string, then determines port from that number or service name" do
  39. t = Object.new
  40. lambda { TCPServer.new(SocketSpecs.hostname, t) }.should raise_error(TypeError)
  41. def t.to_str; SocketSpecs.port.to_s; end
  42. @server = TCPServer.new(SocketSpecs.hostname, t)
  43. addr = @server.addr
  44. addr[1].should == SocketSpecs.port
  45. # TODO: This should also accept strings like 'https', but I don't know how to
  46. # pick such a service port that will be able to reliably bind...
  47. end
  48. it "raises Errno::EADDRNOTAVAIL when the adress is unknown" do
  49. lambda { TCPServer.new("1.2.3.4", 4000) }.should raise_error(Errno::EADDRNOTAVAIL)
  50. end
  51. # There is no way to make this fail-proof on all machines, because
  52. # DNS servers like opendns return A records for ANY host, including
  53. # traditionally invalidly named ones.
  54. quarantine! do
  55. it "raises a SocketError when the host is unknown" do
  56. lambda {
  57. TCPServer.new("--notavalidname", 4000)
  58. }.should raise_error(SocketError)
  59. end
  60. end
  61. it "raises Errno::EADDRINUSE when address is already in use" do
  62. lambda {
  63. @server = TCPServer.new('127.0.0.1', SocketSpecs.port)
  64. @server = TCPServer.new('127.0.0.1', SocketSpecs.port)
  65. }.should raise_error(Errno::EADDRINUSE)
  66. end
  67. end