PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/test_ipv4.rb

http://github.com/eventmachine/eventmachine
Ruby | 96 lines | 72 code | 16 blank | 8 comment | 2 complexity | 23a57a3e96196e80ed9981238f2ad052 MD5 | raw file
  1. require_relative 'em_test_helper'
  2. class TestIPv4 < Test::Unit::TestCase
  3. # Runs a TCP server in the local IPv4 address, connects to it and sends a specific data.
  4. # Timeout in 2 seconds.
  5. def test_ipv4_tcp_local_server
  6. omit_if(!Test::Unit::TestCase.public_ipv4?)
  7. @@received_data = nil
  8. @local_port = next_port
  9. setup_timeout(2)
  10. EM.run do
  11. EM::start_server(@@public_ipv4, @local_port) do |s|
  12. def s.receive_data data
  13. @@received_data = data
  14. EM.stop
  15. end
  16. end
  17. EM::connect(@@public_ipv4, @local_port) do |c|
  18. c.send_data "ipv4/tcp"
  19. end
  20. end
  21. assert_equal "ipv4/tcp", @@received_data
  22. end
  23. # Runs a UDP server in the local IPv4 address, connects to it and sends a specific data.
  24. # Timeout in 2 seconds.
  25. def test_ipv4_udp_local_server
  26. omit_if(!Test::Unit::TestCase.public_ipv4?)
  27. @@received_data = nil
  28. @local_port = next_port
  29. setup_timeout(2)
  30. EM.run do
  31. EM::open_datagram_socket(@@public_ipv4, @local_port) do |s|
  32. def s.receive_data data
  33. @@received_data = data
  34. EM.stop
  35. end
  36. end
  37. EM::open_datagram_socket(@@public_ipv4, next_port) do |c|
  38. c.send_datagram "ipv4/udp", @@public_ipv4, @local_port
  39. end
  40. end
  41. assert_equal "ipv4/udp", @@received_data
  42. end
  43. # Try to connect via TCP to an invalid IPv4. EM.connect should raise
  44. # EM::ConnectionError.
  45. def test_tcp_connect_to_invalid_ipv4
  46. omit_if(!Test::Unit::TestCase.public_ipv4?)
  47. pend("\nFIXME: Windows as of 2018-06-23 on 32 bit >= 2.4 (#{RUBY_VERSION} #{RUBY_PLATFORM})") if RUBY_PLATFORM[/i386-mingw/] && RUBY_VERSION >= '2.4'
  48. invalid_ipv4 = "9.9:9"
  49. EM.run do
  50. begin
  51. error = nil
  52. EM.connect(invalid_ipv4, 1234)
  53. rescue => e
  54. error = e
  55. ensure
  56. EM.stop
  57. assert_equal EM::ConnectionError, (error && error.class)
  58. end
  59. end
  60. end
  61. # Try to send a UDP datagram to an invalid IPv4. EM.send_datagram should raise
  62. # EM::ConnectionError.
  63. def test_udp_send_datagram_to_invalid_ipv4
  64. omit_if(!Test::Unit::TestCase.public_ipv4?)
  65. invalid_ipv4 = "9.9:9"
  66. EM.run do
  67. begin
  68. error = nil
  69. EM.open_datagram_socket(@@public_ipv4, next_port) do |c|
  70. c.send_datagram "hello", invalid_ipv4, 1234
  71. end
  72. rescue => e
  73. error = e
  74. ensure
  75. EM.stop
  76. assert_equal EM::ConnectionError, (error && error.class)
  77. end
  78. end
  79. end
  80. end