PageRenderTime 64ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/em_test_helper.rb

http://github.com/eventmachine/eventmachine
Ruby | 194 lines | 152 code | 28 blank | 14 comment | 14 complexity | 484de575b7ef106802c834f1b67befac MD5 | raw file
  1. require 'em/pure_ruby' if ENV['EM_PURE_RUBY']
  2. require 'eventmachine'
  3. require 'rbconfig'
  4. require 'socket'
  5. # verbose fun is to stop warnings when loading test-unit 3.2.9 in trunk
  6. verbose, $VERBOSE = $VERBOSE, nil
  7. require 'test/unit'
  8. $VERBOSE = verbose
  9. class Test::Unit::TestCase
  10. # below outputs to console on load
  11. # SSL_AVAIL is used by SSL tests
  12. puts "", RUBY_DESCRIPTION
  13. puts "\nEM.library_type #{EM.library_type.to_s.ljust(16)} EM.ssl? #{EM.ssl?}"
  14. if EM.ssl?
  15. require 'openssl'
  16. ssl_lib_vers = OpenSSL.const_defined?(:OPENSSL_LIBRARY_VERSION) ?
  17. OpenSSL::OPENSSL_LIBRARY_VERSION : 'na'
  18. puts "OpenSSL OPENSSL_LIBRARY_VERSION: #{ssl_lib_vers}\n" \
  19. " OPENSSL_VERSION: #{OpenSSL::OPENSSL_VERSION}\n" \
  20. " EM OPENSSL_LIBRARY_VERSION: #{EM::OPENSSL_LIBRARY_VERSION}\n" \
  21. " OPENSSL_VERSION: #{EM::OPENSSL_VERSION}"
  22. # assumes all 2.x versions include support for TLSv1_2
  23. temp = []
  24. temp << 'SSLv2' unless EM::OPENSSL_NO_SSL2
  25. temp << 'SSLv3' unless EM::OPENSSL_NO_SSL3
  26. temp += %w[TLSv1 TLSv1_1 TLSv1_2]
  27. temp << 'TLSv1_3' if EM.const_defined? :EM_PROTO_TLSv1_3
  28. temp.sort!
  29. puts " SSL_AVAIL: #{temp.join(' ')}", ""
  30. SSL_AVAIL = temp.freeze
  31. else
  32. puts "\nEventMachine is not built with OpenSSL support, skipping tests in",
  33. "files tests/test_ssl_*.rb"
  34. end
  35. class EMTestTimeout < StandardError ; end
  36. def setup_timeout(timeout = TIMEOUT_INTERVAL)
  37. EM.schedule {
  38. EM.add_timer(timeout) {
  39. raise EMTestTimeout, "Test was cancelled after #{timeout} seconds."
  40. }
  41. }
  42. end
  43. def port_in_use?(port, host="127.0.0.1")
  44. s = TCPSocket.new(host, port)
  45. s.close
  46. s
  47. rescue Errno::ECONNREFUSED
  48. false
  49. end
  50. def next_port
  51. @@port ||= 9000
  52. begin
  53. @@port += 1
  54. end while port_in_use?(@@port)
  55. @@port
  56. end
  57. # Returns true if the host have a localhost 127.0.0.1 IPv4.
  58. def self.local_ipv4?
  59. return @@has_local_ipv4 if defined?(@@has_local_ipv4)
  60. begin
  61. get_my_ipv4_address "127.0.0.1"
  62. @@has_local_ipv4 = true
  63. rescue
  64. @@has_local_ipv4 = false
  65. end
  66. end
  67. # Returns true if the host have a public IPv4 and stores it in
  68. # @@public_ipv4.
  69. def self.public_ipv4?
  70. return @@has_public_ipv4 if defined?(@@has_public_ipv4)
  71. begin
  72. @@public_ipv4 = get_my_ipv4_address "1.2.3.4"
  73. @@has_public_ipv4 = true
  74. rescue
  75. @@has_public_ipv4 = false
  76. end
  77. end
  78. # Returns true if the host have a localhost ::1 IPv6.
  79. def self.local_ipv6?
  80. return @@has_local_ipv6 if defined?(@@has_local_ipv6)
  81. begin
  82. get_my_ipv6_address "::1"
  83. @@has_local_ipv6 = true
  84. rescue
  85. @@has_local_ipv6 = false
  86. end
  87. end
  88. # Returns true if the host have a public IPv6 and stores it in
  89. # @@public_ipv6.
  90. def self.public_ipv6?
  91. return @@has_public_ipv6 if defined?(@@has_public_ipv6)
  92. begin
  93. @@public_ipv6 = get_my_ipv6_address "2001::1"
  94. @@has_public_ipv6 = true
  95. rescue
  96. @@has_public_ipv6 = false
  97. end
  98. end
  99. # Returns an array with the localhost addresses (IPv4 and/or IPv6).
  100. def local_ips
  101. return @@local_ips if defined?(@@local_ips)
  102. @@local_ips = []
  103. @@local_ips << "127.0.0.1" if self.class.local_ipv4?
  104. @@local_ips << "::1" if self.class.local_ipv6?
  105. @@local_ips
  106. end
  107. def exception_class
  108. jruby? ? NativeException : RuntimeError
  109. end
  110. module PlatformHelper
  111. # http://blog.emptyway.com/2009/11/03/proper-way-to-detect-windows-platform-in-ruby/
  112. def windows?
  113. RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
  114. end
  115. def solaris?
  116. RUBY_PLATFORM =~ /solaris/
  117. end
  118. # http://stackoverflow.com/questions/1342535/how-can-i-tell-if-im-running-from-jruby-vs-ruby/1685970#1685970
  119. def jruby?
  120. defined? JRUBY_VERSION
  121. end
  122. def rbx?
  123. defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
  124. end
  125. end
  126. include PlatformHelper
  127. extend PlatformHelper
  128. # Tests may run slower on windows or Appveyor. YMMV
  129. TIMEOUT_INTERVAL = windows? ? 0.25 : 0.25
  130. module EMTestCasePrepend
  131. def setup
  132. EM.stop while EM.reactor_running?
  133. end
  134. def teardown
  135. EM.cleanup_machine
  136. end
  137. end
  138. prepend EMTestCasePrepend
  139. def silent
  140. backup, $VERBOSE = $VERBOSE, nil
  141. begin
  142. yield
  143. ensure
  144. $VERBOSE = backup
  145. end
  146. end
  147. private
  148. def self.get_my_ipv4_address ip
  149. orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true # turn off reverse DNS resolution temporarily
  150. UDPSocket.open(Socket::AF_INET) do |s|
  151. s.connect ip, 1
  152. s.addr.last
  153. end
  154. ensure
  155. Socket.do_not_reverse_lookup = orig
  156. end
  157. def self.get_my_ipv6_address ip
  158. orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true # turn off reverse DNS resolution temporarily
  159. UDPSocket.open(Socket::AF_INET6) do |s|
  160. s.connect ip, 1
  161. s.addr.last
  162. end
  163. ensure
  164. Socket.do_not_reverse_lookup = orig
  165. end
  166. end