/spec/ruby/library/socket/ipsocket/recvfrom_spec.rb

https://github.com/zenspider/ruby · Ruby · 123 lines · 94 code · 26 blank · 3 comment · 8 complexity · 28cffaff58db374181eb3365569d1a2b MD5 · raw file

  1. require_relative '../spec_helper'
  2. require_relative '../fixtures/classes'
  3. describe "Socket::IPSocket#recvfrom" do
  4. before :each do
  5. @server = TCPServer.new("127.0.0.1", 0)
  6. @port = @server.addr[1]
  7. @client = TCPSocket.new("127.0.0.1", @port)
  8. end
  9. after :each do
  10. @server.close unless @server.closed?
  11. @client.close unless @client.closed?
  12. end
  13. it "reads data from the connection" do
  14. data = nil
  15. t = Thread.new do
  16. client = @server.accept
  17. begin
  18. data = client.recvfrom(6)
  19. ensure
  20. client.close
  21. end
  22. end
  23. @client.send('hello', 0)
  24. @client.shutdown rescue nil
  25. # shutdown may raise Errno::ENOTCONN when sent data is pending.
  26. t.join
  27. data.first.should == 'hello'
  28. end
  29. it "reads up to len bytes" do
  30. data = nil
  31. t = Thread.new do
  32. client = @server.accept
  33. begin
  34. data = client.recvfrom(3)
  35. ensure
  36. client.close
  37. end
  38. end
  39. @client.send('hello', 0)
  40. @client.shutdown rescue nil
  41. t.join
  42. data.first.should == 'hel'
  43. end
  44. it "returns an array with the data and connection info" do
  45. data = nil
  46. t = Thread.new do
  47. client = @server.accept
  48. data = client.recvfrom(3)
  49. client.close
  50. end
  51. @client.send('hello', 0)
  52. @client.shutdown rescue nil
  53. t.join
  54. data.size.should == 2
  55. data.first.should == "hel"
  56. # This does not apply to every platform, dependant on recvfrom(2)
  57. # data.last.should == nil
  58. end
  59. end
  60. describe 'Socket::IPSocket#recvfrom' do
  61. SocketSpecs.each_ip_protocol do |family, ip_address, family_name|
  62. before do
  63. @server = UDPSocket.new(family)
  64. @client = UDPSocket.new(family)
  65. @server.bind(ip_address, 0)
  66. @client.connect(ip_address, @server.connect_address.ip_port)
  67. @hostname = Socket.getaddrinfo(ip_address, nil)[0][2]
  68. end
  69. after do
  70. @client.close
  71. @server.close
  72. end
  73. it 'returns an Array containing up to N bytes and address information' do
  74. @client.write('hello')
  75. port = @client.local_address.ip_port
  76. ret = @server.recvfrom(2)
  77. ret.should == ['he', [family_name, port, @hostname, ip_address]]
  78. end
  79. it 'allows specifying of flags when receiving data' do
  80. @client.write('hello')
  81. @server.recvfrom(2, Socket::MSG_PEEK)[0].should == 'he'
  82. @server.recvfrom(2)[0].should == 'he'
  83. end
  84. describe 'using reverse lookups' do
  85. before do
  86. @server.do_not_reverse_lookup = false
  87. @hostname = Socket.getaddrinfo(ip_address, nil, 0, 0, 0, 0, true)[0][2]
  88. end
  89. it 'includes the hostname in the address Array' do
  90. @client.write('hello')
  91. port = @client.local_address.ip_port
  92. ret = @server.recvfrom(2)
  93. ret.should == ['he', [family_name, port, @hostname, ip_address]]
  94. end
  95. end
  96. end
  97. end