forked from cabo/eventmachine
/
em_test_helper.rb
143 lines (122 loc) · 3.32 KB
/
em_test_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
require 'eventmachine'
require 'test/unit'
require 'rbconfig'
require 'socket'
class Test::Unit::TestCase
class EMTestTimeout < StandardError ; end
def setup_timeout(timeout = TIMEOUT_INTERVAL)
EM.schedule {
EM.add_timer(timeout) {
raise EMTestTimeout, "Test was cancelled after #{timeout} seconds."
}
}
end
def port_in_use?(port, host="127.0.0.1")
s = TCPSocket.new(host, port)
s.close
s
rescue Errno::ECONNREFUSED
false
end
def next_port
@@port ||= 9000
begin
@@port += 1
end while port_in_use?(@@port)
@@port
end
# Returns true if the host have a localhost 127.0.0.1 IPv6.
def self.local_ipv4?
return @@has_local_ipv4 if defined?(@@has_local_ipv4)
begin
get_my_ipv4_address "127.0.0.1"
@@has_local_ipv4 = true
rescue
@@has_local_ipv4 = false
end
end
# Returns true if the host have a public IPv6 and stores it in
# @@public_ipv4.
def self.public_ipv4?
return @@has_public_ipv4 if defined?(@@has_public_ipv4)
begin
@@public_ipv4 = get_my_ipv4_address "1.2.3.4"
@@has_public_ipv4 = true
rescue
@@has_public_ipv4 = false
end
end
# Returns true if the host have a localhost ::1 IPv6.
def self.local_ipv6?
return @@has_local_ipv6 if defined?(@@has_local_ipv6)
begin
get_my_ipv6_address "::1"
@@has_local_ipv6 = true
rescue
@@has_local_ipv6 = false
end
end
# Returns true if the host have a public IPv6 and stores it in
# @@public_ipv6.
def self.public_ipv6?
return @@has_public_ipv6 if defined?(@@has_public_ipv6)
begin
@@public_ipv6 = get_my_ipv6_address "2001::1"
@@has_public_ipv6 = true
rescue
@@has_public_ipv6 = false
end
end
# Returns an array with the localhost addresses (IPv4 and/or IPv6).
def local_ips
return @@local_ips if defined?(@@local_ips)
@@local_ips = []
@@local_ips << "127.0.0.1" if self.class.local_ipv4?
@@local_ips << "::1" if self.class.local_ipv6?
@@local_ips
end
def exception_class
jruby? ? NativeException : RuntimeError
end
module PlatformHelper
# http://blog.emptyway.com/2009/11/03/proper-way-to-detect-windows-platform-in-ruby/
def windows?
RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
end
# http://stackoverflow.com/questions/1342535/how-can-i-tell-if-im-running-from-jruby-vs-ruby/1685970#1685970
def jruby?
defined? JRUBY_VERSION
end
end
include PlatformHelper
extend PlatformHelper
# Tests run significantly slower on windows. YMMV
TIMEOUT_INTERVAL = windows? ? 1 : 0.5
def silent
backup, $VERBOSE = $VERBOSE, nil
begin
yield
ensure
$VERBOSE = backup
end
end
private
def self.get_my_ipv4_address ip
orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true # turn off reverse DNS resolution temporarily
UDPSocket.open(Socket::AF_INET) do |s|
s.connect ip, 1
s.addr.last
end
ensure
Socket.do_not_reverse_lookup = orig
end
def self.get_my_ipv6_address ip
orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true # turn off reverse DNS resolution temporarily
UDPSocket.open(Socket::AF_INET6) do |s|
s.connect ip, 1
s.addr.last
end
ensure
Socket.do_not_reverse_lookup = orig
end
end