forked from eventmachine/eventmachine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_basic.rb
249 lines (219 loc) · 5.55 KB
/
test_basic.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# $Id$
#
# Author:: Francis Cianfrocca (gmail: blackhedd)
# Homepage:: http://rubyeventmachine.com
# Date:: 8 April 2006
#
# See EventMachine and EventMachine::Connection for documentation and
# usage examples.
#
#----------------------------------------------------------------------------
#
# Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
# Gmail: blackhedd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of either: 1) the GNU General Public License
# as published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version; or 2) Ruby's License.
#
# See the file COPYING for complete licensing information.
#
#---------------------------------------------------------------------------
#
#
#
$:.unshift File.expand_path(File.dirname(__FILE__) + "/../lib")
require 'eventmachine'
require 'socket'
require 'test/unit'
class TestBasic < Test::Unit::TestCase
def test_connection_class_cache
mod = Module.new
a, b = nil, nil
EM.run {
EM.start_server '127.0.0.1', 9999, mod
a = EM.connect '127.0.0.1', 9999, mod
b = EM.connect '127.0.0.1', 9999, mod
EM.stop
}
assert_equal a.class, b.class
assert_kind_of EM::Connection, a
end
#-------------------------------------
def test_em
EventMachine.run {
EventMachine.add_timer 0 do
EventMachine.stop
end
}
end
#-------------------------------------
def test_timer
n = 0
EventMachine.run {
EventMachine.add_periodic_timer(0.1) {
n += 1
EventMachine.stop if n == 2
}
}
end
#-------------------------------------
# This test once threw an already-running exception.
module Trivial
def post_init
EventMachine.stop
end
end
def test_server
EventMachine.run {
EventMachine.start_server "localhost", 9000, Trivial
EventMachine.connect "localhost", 9000
}
assert( true ) # make sure it halts
end
#--------------------------------------
# EventMachine#run_block starts the reactor loop, runs the supplied block, and then STOPS
# the loop automatically. Contrast with EventMachine#run, which keeps running the reactor
# even after the supplied block completes.
def test_run_block
assert !EM.reactor_running?
a = nil
EM.run_block { a = "Worked" }
assert a
assert !EM.reactor_running?
end
TestHost = "127.0.0.1"
TestPort = 9070
class UnbindError < EM::Connection
ERR = Class.new(StandardError)
def initialize *args
super
end
def connection_completed
close_connection_after_writing
end
def unbind
raise ERR
end
end
def test_unbind_error
assert_raises( UnbindError::ERR ) {
EM.run {
EM.start_server TestHost, TestPort
EM.connect TestHost, TestPort, UnbindError
}
}
end
module BrsTestSrv
def receive_data data
$received << data
end
def unbind
EM.stop
end
end
module BrsTestCli
def post_init
send_data $sent
close_connection_after_writing
end
end
def setup_timeout(timeout = 4)
EM.schedule {
start_time = EM.current_time
EM.add_periodic_timer(0.01) {
raise "timeout" if EM.current_time - start_time >= timeout
}
}
end
# From ticket #50
def test_byte_range_send
$received = ''
$sent = (0..255).to_a.pack('C*')
EM::run {
EM::start_server TestHost, TestPort, BrsTestSrv
EM::connect TestHost, TestPort, BrsTestCli
setup_timeout
}
assert_equal($sent, $received)
end
def test_bind_connect
local_ip = UDPSocket.open {|s| s.connect('google.com', 80); s.addr.last }
bind_port = rand(33333)+1025
test = self
EM.run do
EM.start_server(TestHost, TestPort, Module.new do
define_method :post_init do
begin
test.assert_equal bind_port, Socket.unpack_sockaddr_in(get_peername).first
test.assert_equal local_ip, Socket.unpack_sockaddr_in(get_peername).last
ensure
EM.stop_event_loop
end
end
end)
EM.bind_connect local_ip, bind_port, TestHost, TestPort
end
end
def test_reactor_thread?
assert !EM.reactor_thread?
EM.run { assert EM.reactor_thread?; EM.stop }
assert !EM.reactor_thread?
end
def test_schedule_on_reactor_thread
x = false
EM.run do
EM.schedule { x = true }
EM.stop
end
assert x
end
def test_schedule_from_thread
x = false
assert !x
EM.run do
Thread.new { EM.schedule { x = true; EM.stop } }.join
end
assert x
end
def test_set_heartbeat_interval
interval = 0.5
EM.run {
EM.set_heartbeat_interval interval
$interval = EM.get_heartbeat_interval
EM.stop
}
assert_equal(interval, $interval)
end
module PostInitRaiser
ERR = Class.new(StandardError)
def post_init
raise ERR
end
end
def test_bubble_errors_from_post_init
localhost, port = '127.0.0.1', 9000
assert_raises(PostInitRaiser::ERR) do
EM.run do
EM.start_server localhost, port
EM.connect localhost, port, PostInitRaiser
end
end
end
module InitializeRaiser
ERR = Class.new(StandardError)
def initialize
raise ERR
end
end
def test_bubble_errors_from_initialize
localhost, port = '127.0.0.1', 9000
assert_raises(InitializeRaiser::ERR) do
EM.run do
EM.start_server localhost, port
EM.connect localhost, port, InitializeRaiser
end
end
end
end