PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/rklemme/ruby
Ruby | 98 lines | 79 code | 18 blank | 1 comment | 7 complexity | e77361f3a06967ef13fbd6ebd0f51fdd MD5 | raw file
  1. require_relative '../spec_helper'
  2. require_relative '../fixtures/classes'
  3. describe "UNIXSocket#recvfrom" do
  4. platform_is_not :windows do
  5. before :each do
  6. @path = SocketSpecs.socket_path
  7. @server = UNIXServer.open(@path)
  8. @client = UNIXSocket.open(@path)
  9. end
  10. after :each do
  11. @client.close
  12. @server.close
  13. SocketSpecs.rm_socket @path
  14. end
  15. it "receives len bytes from sock" do
  16. @client.send("foobar", 0)
  17. sock = @server.accept
  18. sock.recvfrom(6).first.should == "foobar"
  19. sock.close
  20. end
  21. it "returns an array with data and information on the sender" do
  22. @client.send("foobar", 0)
  23. sock = @server.accept
  24. data = sock.recvfrom(6)
  25. data.first.should == "foobar"
  26. data.last.should == ["AF_UNIX", ""]
  27. sock.close
  28. end
  29. it "uses different message options" do
  30. @client.send("foobar", Socket::MSG_PEEK)
  31. sock = @server.accept
  32. peek_data = sock.recvfrom(6, Socket::MSG_PEEK) # Does not retrieve the message
  33. real_data = sock.recvfrom(6)
  34. real_data.should == peek_data
  35. peek_data.should == ["foobar", ["AF_UNIX", ""]]
  36. sock.close
  37. end
  38. end
  39. end
  40. with_feature :unix_socket do
  41. describe 'UNIXSocket#recvfrom' do
  42. describe 'using a socket pair' do
  43. before do
  44. @client, @server = UNIXSocket.socketpair
  45. @client.write('hello')
  46. end
  47. after do
  48. @client.close
  49. @server.close
  50. end
  51. it 'returns an Array containing the data and address information' do
  52. @server.recvfrom(5).should == ['hello', ['AF_UNIX', '']]
  53. end
  54. end
  55. # These specs are taken from the rdoc examples on UNIXSocket#recvfrom.
  56. describe 'using a UNIX socket constructed using UNIXSocket.for_fd' do
  57. before do
  58. @path1 = SocketSpecs.socket_path
  59. @path2 = SocketSpecs.socket_path.chop + '2'
  60. rm_r(@path2)
  61. @client_raw = Socket.new(:UNIX, :DGRAM)
  62. @client_raw.bind(Socket.sockaddr_un(@path1))
  63. @server_raw = Socket.new(:UNIX, :DGRAM)
  64. @server_raw.bind(Socket.sockaddr_un(@path2))
  65. @socket = UNIXSocket.for_fd(@server_raw.fileno)
  66. @socket.autoclose = false
  67. end
  68. after do
  69. @client_raw.close
  70. @server_raw.close # also closes @socket
  71. rm_r @path1
  72. rm_r @path2
  73. end
  74. it 'returns an Array containing the data and address information' do
  75. @client_raw.send('hello', 0, Socket.sockaddr_un(@path2))
  76. @socket.recvfrom(5).should == ['hello', ['AF_UNIX', @path1]]
  77. end
  78. end
  79. end
  80. end