smparkes-eventmachine 0.12.10
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +15 -0
- data/README +81 -0
- data/Rakefile +374 -0
- data/docs/COPYING +60 -0
- data/docs/ChangeLog +211 -0
- data/docs/DEFERRABLES +133 -0
- data/docs/EPOLL +141 -0
- data/docs/GNU +281 -0
- data/docs/INSTALL +13 -0
- data/docs/KEYBOARD +38 -0
- data/docs/LEGAL +25 -0
- data/docs/LIGHTWEIGHT_CONCURRENCY +70 -0
- data/docs/PURE_RUBY +75 -0
- data/docs/RELEASE_NOTES +94 -0
- data/docs/SMTP +2 -0
- data/docs/SPAWNED_PROCESSES +89 -0
- data/docs/TODO +8 -0
- data/eventmachine.gemspec +40 -0
- data/examples/ex_channel.rb +43 -0
- data/examples/ex_queue.rb +2 -0
- data/examples/helper.rb +2 -0
- data/ext/binder.cpp +125 -0
- data/ext/binder.h +46 -0
- data/ext/cmain.cpp +827 -0
- data/ext/cplusplus.cpp +202 -0
- data/ext/ed.cpp +1901 -0
- data/ext/ed.h +424 -0
- data/ext/em.cpp +2288 -0
- data/ext/em.h +229 -0
- data/ext/emwin.cpp +300 -0
- data/ext/emwin.h +94 -0
- data/ext/epoll.cpp +26 -0
- data/ext/epoll.h +25 -0
- data/ext/eventmachine.h +122 -0
- data/ext/eventmachine_cpp.h +96 -0
- data/ext/extconf.rb +150 -0
- data/ext/fastfilereader/extconf.rb +85 -0
- data/ext/fastfilereader/mapper.cpp +214 -0
- data/ext/fastfilereader/mapper.h +59 -0
- data/ext/fastfilereader/rubymain.cpp +127 -0
- data/ext/files.cpp +94 -0
- data/ext/files.h +65 -0
- data/ext/kb.cpp +81 -0
- data/ext/page.cpp +107 -0
- data/ext/page.h +51 -0
- data/ext/pipe.cpp +349 -0
- data/ext/project.h +156 -0
- data/ext/rubymain.cpp +1194 -0
- data/ext/sigs.cpp +89 -0
- data/ext/sigs.h +32 -0
- data/ext/ssl.cpp +460 -0
- data/ext/ssl.h +94 -0
- data/java/.classpath +8 -0
- data/java/.project +17 -0
- data/java/src/com/rubyeventmachine/EmReactor.java +570 -0
- data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
- data/java/src/com/rubyeventmachine/EventableChannel.java +69 -0
- data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +189 -0
- data/java/src/com/rubyeventmachine/EventableSocketChannel.java +364 -0
- data/java/src/com/rubyeventmachine/application/Application.java +194 -0
- data/java/src/com/rubyeventmachine/application/Connection.java +74 -0
- data/java/src/com/rubyeventmachine/application/ConnectionFactory.java +37 -0
- data/java/src/com/rubyeventmachine/application/DefaultConnectionFactory.java +46 -0
- data/java/src/com/rubyeventmachine/application/PeriodicTimer.java +38 -0
- data/java/src/com/rubyeventmachine/application/Timer.java +54 -0
- data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +109 -0
- data/java/src/com/rubyeventmachine/tests/ConnectTest.java +148 -0
- data/java/src/com/rubyeventmachine/tests/EMTest.java +80 -0
- data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +53 -0
- data/java/src/com/rubyeventmachine/tests/TestServers.java +75 -0
- data/java/src/com/rubyeventmachine/tests/TestTimers.java +90 -0
- data/lib/em/buftok.rb +138 -0
- data/lib/em/callback.rb +26 -0
- data/lib/em/channel.rb +57 -0
- data/lib/em/connection.rb +564 -0
- data/lib/em/deferrable.rb +192 -0
- data/lib/em/file_watch.rb +54 -0
- data/lib/em/future.rb +61 -0
- data/lib/em/messages.rb +66 -0
- data/lib/em/process_watch.rb +44 -0
- data/lib/em/processes.rb +119 -0
- data/lib/em/protocols/header_and_content.rb +138 -0
- data/lib/em/protocols/httpclient.rb +263 -0
- data/lib/em/protocols/httpclient2.rb +590 -0
- data/lib/em/protocols/line_and_text.rb +125 -0
- data/lib/em/protocols/linetext2.rb +161 -0
- data/lib/em/protocols/memcache.rb +323 -0
- data/lib/em/protocols/object_protocol.rb +45 -0
- data/lib/em/protocols/postgres3.rb +247 -0
- data/lib/em/protocols/saslauth.rb +175 -0
- data/lib/em/protocols/smtpclient.rb +357 -0
- data/lib/em/protocols/smtpserver.rb +547 -0
- data/lib/em/protocols/socks4.rb +66 -0
- data/lib/em/protocols/stomp.rb +200 -0
- data/lib/em/protocols/tcptest.rb +53 -0
- data/lib/em/protocols.rb +36 -0
- data/lib/em/queue.rb +61 -0
- data/lib/em/spawnable.rb +85 -0
- data/lib/em/streamer.rb +130 -0
- data/lib/em/timers.rb +56 -0
- data/lib/em/version.rb +3 -0
- data/lib/eventmachine.rb +1592 -0
- data/lib/evma/callback.rb +32 -0
- data/lib/evma/container.rb +75 -0
- data/lib/evma/factory.rb +77 -0
- data/lib/evma/protocol.rb +87 -0
- data/lib/evma/reactor.rb +48 -0
- data/lib/evma.rb +32 -0
- data/lib/jeventmachine.rb +257 -0
- data/lib/pr_eventmachine.rb +1022 -0
- data/setup.rb +1585 -0
- data/tasks/cpp.rake_example +77 -0
- data/tests/client.crt +31 -0
- data/tests/client.key +51 -0
- data/tests/test_attach.rb +126 -0
- data/tests/test_basic.rb +284 -0
- data/tests/test_channel.rb +63 -0
- data/tests/test_connection_count.rb +35 -0
- data/tests/test_defer.rb +47 -0
- data/tests/test_epoll.rb +160 -0
- data/tests/test_error_handler.rb +35 -0
- data/tests/test_errors.rb +82 -0
- data/tests/test_exc.rb +55 -0
- data/tests/test_file_watch.rb +49 -0
- data/tests/test_futures.rb +198 -0
- data/tests/test_get_sock_opt.rb +30 -0
- data/tests/test_handler_check.rb +37 -0
- data/tests/test_hc.rb +218 -0
- data/tests/test_httpclient.rb +218 -0
- data/tests/test_httpclient2.rb +153 -0
- data/tests/test_inactivity_timeout.rb +50 -0
- data/tests/test_kb.rb +60 -0
- data/tests/test_ltp.rb +182 -0
- data/tests/test_ltp2.rb +317 -0
- data/tests/test_next_tick.rb +133 -0
- data/tests/test_object_protocol.rb +37 -0
- data/tests/test_pause.rb +70 -0
- data/tests/test_pending_connect_timeout.rb +48 -0
- data/tests/test_process_watch.rb +48 -0
- data/tests/test_processes.rb +128 -0
- data/tests/test_proxy_connection.rb +92 -0
- data/tests/test_pure.rb +125 -0
- data/tests/test_queue.rb +44 -0
- data/tests/test_running.rb +42 -0
- data/tests/test_sasl.rb +72 -0
- data/tests/test_send_file.rb +242 -0
- data/tests/test_servers.rb +76 -0
- data/tests/test_smtpclient.rb +83 -0
- data/tests/test_smtpserver.rb +85 -0
- data/tests/test_spawn.rb +322 -0
- data/tests/test_ssl_args.rb +79 -0
- data/tests/test_ssl_methods.rb +50 -0
- data/tests/test_ssl_verify.rb +82 -0
- data/tests/test_timers.rb +162 -0
- data/tests/test_ud.rb +36 -0
- data/tests/testem.rb +31 -0
- data/web/whatis +7 -0
- metadata +237 -0
data/tests/test_spawn.rb
ADDED
@@ -0,0 +1,322 @@
|
|
1
|
+
# $Id$
|
2
|
+
#
|
3
|
+
# Author:: Francis Cianfrocca (gmail: blackhedd)
|
4
|
+
# Homepage:: http://rubyeventmachine.com
|
5
|
+
# Date:: 25 Aug 2007
|
6
|
+
#
|
7
|
+
# See EventMachine and EventMachine::Connection for documentation and
|
8
|
+
# usage examples.
|
9
|
+
#
|
10
|
+
#----------------------------------------------------------------------------
|
11
|
+
#
|
12
|
+
# Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
13
|
+
# Gmail: blackhedd
|
14
|
+
#
|
15
|
+
# This program is free software; you can redistribute it and/or modify
|
16
|
+
# it under the terms of either: 1) the GNU General Public License
|
17
|
+
# as published by the Free Software Foundation; either version 2 of the
|
18
|
+
# License, or (at your option) any later version; or 2) Ruby's License.
|
19
|
+
#
|
20
|
+
# See the file COPYING for complete licensing information.
|
21
|
+
#
|
22
|
+
#---------------------------------------------------------------------------
|
23
|
+
#
|
24
|
+
#
|
25
|
+
#
|
26
|
+
#
|
27
|
+
|
28
|
+
|
29
|
+
$:.unshift "../lib"
|
30
|
+
require 'eventmachine'
|
31
|
+
require 'test/unit'
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
class TestSpawn < Test::Unit::TestCase
|
36
|
+
|
37
|
+
# Spawn a process that simply stops the reactor.
|
38
|
+
# Assert that the notification runs after the block that calls it.
|
39
|
+
#
|
40
|
+
def test_stop
|
41
|
+
x = nil
|
42
|
+
EM.run {
|
43
|
+
s = EM.spawn {EM.stop}
|
44
|
+
s.notify
|
45
|
+
x = true
|
46
|
+
}
|
47
|
+
assert x
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
# Pass a parameter to a spawned process.
|
52
|
+
#
|
53
|
+
def test_parms
|
54
|
+
val = 5
|
55
|
+
EM.run {
|
56
|
+
s = EM.spawn {|v| val *= v; EM.stop}
|
57
|
+
s.notify 3
|
58
|
+
}
|
59
|
+
assert_equal( 15, val )
|
60
|
+
end
|
61
|
+
|
62
|
+
# Pass multiple parameters to a spawned process.
|
63
|
+
#
|
64
|
+
def test_multiparms
|
65
|
+
val = 5
|
66
|
+
EM.run {
|
67
|
+
s = EM.spawn {|v1,v2| val *= (v1 + v2); EM.stop}
|
68
|
+
s.notify 3,4
|
69
|
+
}
|
70
|
+
assert_equal( 35, val )
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
# This test demonstrates that a notification does not happen immediately,
|
75
|
+
# but rather is scheduled sometime after the current code path completes.
|
76
|
+
#
|
77
|
+
def test_race
|
78
|
+
x = 0
|
79
|
+
EM.run {
|
80
|
+
s = EM.spawn {x *= 2; EM.stop}
|
81
|
+
s.notify
|
82
|
+
x = 2
|
83
|
+
}
|
84
|
+
assert_equal( 4, x)
|
85
|
+
end
|
86
|
+
|
87
|
+
|
88
|
+
# Spawn a process and notify it 25 times to run fibonacci
|
89
|
+
# on a pair of global variables.
|
90
|
+
#
|
91
|
+
def test_fibonacci
|
92
|
+
x = 1
|
93
|
+
y = 1
|
94
|
+
EM.run {
|
95
|
+
s = EM.spawn {x,y = y,x+y}
|
96
|
+
25.times {s.notify}
|
97
|
+
|
98
|
+
t = EM.spawn {EM.stop}
|
99
|
+
t.notify
|
100
|
+
}
|
101
|
+
assert_equal( 121393, x)
|
102
|
+
assert_equal( 196418, y)
|
103
|
+
end
|
104
|
+
|
105
|
+
# This one spawns 25 distinct processes, and notifies each one once,
|
106
|
+
# rather than notifying a single process 25 times.
|
107
|
+
#
|
108
|
+
def test_another_fibonacci
|
109
|
+
x = 1
|
110
|
+
y = 1
|
111
|
+
EM.run {
|
112
|
+
25.times {
|
113
|
+
s = EM.spawn {x,y = y,x+y}
|
114
|
+
s.notify
|
115
|
+
}
|
116
|
+
|
117
|
+
t = EM.spawn {EM.stop}
|
118
|
+
t.notify
|
119
|
+
}
|
120
|
+
assert_equal( 121393, x)
|
121
|
+
assert_equal( 196418, y)
|
122
|
+
end
|
123
|
+
|
124
|
+
|
125
|
+
# Make a chain of processes that notify each other in turn
|
126
|
+
# with intermediate fibonacci results. The final process in
|
127
|
+
# the chain stops the loop and returns the result.
|
128
|
+
#
|
129
|
+
def test_fibonacci_chain
|
130
|
+
a,b = nil
|
131
|
+
|
132
|
+
EM.run {
|
133
|
+
nextpid = EM.spawn {|x,y|
|
134
|
+
a,b = x,y
|
135
|
+
EM.stop
|
136
|
+
}
|
137
|
+
|
138
|
+
25.times {
|
139
|
+
n = nextpid
|
140
|
+
nextpid = EM.spawn {|x,y| n.notify( y, x+y )}
|
141
|
+
}
|
142
|
+
|
143
|
+
nextpid.notify( 1, 1 )
|
144
|
+
}
|
145
|
+
|
146
|
+
assert_equal( 121393, a)
|
147
|
+
assert_equal( 196418, b)
|
148
|
+
end
|
149
|
+
|
150
|
+
|
151
|
+
# EM#yield gives a spawed process to yield control to other processes
|
152
|
+
# (in other words, to stop running), and to specify a different code block
|
153
|
+
# that will run on its next notification.
|
154
|
+
#
|
155
|
+
def test_yield
|
156
|
+
a = 0
|
157
|
+
EM.run {
|
158
|
+
n = EM.spawn {
|
159
|
+
a += 10
|
160
|
+
EM.yield {
|
161
|
+
a += 20
|
162
|
+
EM.yield {
|
163
|
+
a += 30
|
164
|
+
EM.stop
|
165
|
+
}
|
166
|
+
}
|
167
|
+
}
|
168
|
+
n.notify
|
169
|
+
n.notify
|
170
|
+
n.notify
|
171
|
+
}
|
172
|
+
assert_equal( 60, a )
|
173
|
+
end
|
174
|
+
|
175
|
+
# EM#yield_and_notify behaves like EM#yield, except that it also notifies the
|
176
|
+
# yielding process. This may sound trivial, since the yield block will run very
|
177
|
+
# shortly after with no action by the program, but this actually can be very useful,
|
178
|
+
# because it causes the reactor core to execute once before the yielding process
|
179
|
+
# gets control back. So it can be used to allow heavily-used network connections
|
180
|
+
# to clear buffers, or allow other processes to process their notifications.
|
181
|
+
#
|
182
|
+
# Notice in this test code that only a simple notify is needed at the bottom
|
183
|
+
# of the initial block. Even so, all of the yielded blocks will execute.
|
184
|
+
#
|
185
|
+
def test_yield_and_notify
|
186
|
+
a = 0
|
187
|
+
EM.run {
|
188
|
+
n = EM.spawn {
|
189
|
+
a += 10
|
190
|
+
EM.yield_and_notify {
|
191
|
+
a += 20
|
192
|
+
EM.yield_and_notify {
|
193
|
+
a += 30
|
194
|
+
EM.stop
|
195
|
+
}
|
196
|
+
}
|
197
|
+
}
|
198
|
+
n.notify
|
199
|
+
}
|
200
|
+
assert_equal( 60, a )
|
201
|
+
end
|
202
|
+
|
203
|
+
# resume is an alias for notify.
|
204
|
+
#
|
205
|
+
def test_resume
|
206
|
+
EM.run {
|
207
|
+
n = EM.spawn {EM.stop}
|
208
|
+
n.resume
|
209
|
+
}
|
210
|
+
assert true
|
211
|
+
end
|
212
|
+
|
213
|
+
# run is an idiomatic alias for notify.
|
214
|
+
#
|
215
|
+
def test_run
|
216
|
+
EM.run {
|
217
|
+
(EM.spawn {EM.stop}).run
|
218
|
+
}
|
219
|
+
assert true
|
220
|
+
end
|
221
|
+
|
222
|
+
|
223
|
+
# Clones the ping-pong example from the Erlang tutorial, in much less code.
|
224
|
+
# Illustrates that a spawned block executes in the context of a SpawnableObject.
|
225
|
+
# (Meaning, we can pass self as a parameter to another process that can then
|
226
|
+
# notify us.)
|
227
|
+
#
|
228
|
+
def test_ping_pong
|
229
|
+
n_pongs = 0
|
230
|
+
EM.run {
|
231
|
+
pong = EM.spawn {|x, ping|
|
232
|
+
n_pongs += 1
|
233
|
+
ping.notify( x-1 )
|
234
|
+
}
|
235
|
+
ping = EM.spawn {|x|
|
236
|
+
if x > 0
|
237
|
+
pong.notify x, self
|
238
|
+
else
|
239
|
+
EM.stop
|
240
|
+
end
|
241
|
+
}
|
242
|
+
ping.notify 3
|
243
|
+
}
|
244
|
+
assert_equal( 3, n_pongs )
|
245
|
+
end
|
246
|
+
|
247
|
+
# Illustrates that you can call notify inside a notification, and it will cause
|
248
|
+
# the currently-executing process to be re-notified. Of course, the new notification
|
249
|
+
# won't run until sometime after the current one completes.
|
250
|
+
#
|
251
|
+
def test_self_notify
|
252
|
+
n = 0
|
253
|
+
EM.run {
|
254
|
+
pid = EM.spawn {|x|
|
255
|
+
if x > 0
|
256
|
+
n += x
|
257
|
+
notify( x-1 )
|
258
|
+
else
|
259
|
+
EM.stop
|
260
|
+
end
|
261
|
+
}
|
262
|
+
pid.notify 3
|
263
|
+
}
|
264
|
+
assert_equal( 6, n )
|
265
|
+
end
|
266
|
+
|
267
|
+
|
268
|
+
# Illustrates that the block passed to #spawn executes in the context of a
|
269
|
+
# SpawnedProcess object, NOT in the local context. This can often be deceptive.
|
270
|
+
#
|
271
|
+
class BlockScopeTest
|
272
|
+
attr_reader :var
|
273
|
+
def run
|
274
|
+
# The following line correctly raises a NameError.
|
275
|
+
# The problem is that the programmer expected the spawned block to
|
276
|
+
# execute in the local context, but it doesn't.
|
277
|
+
#
|
278
|
+
# (EM.spawn { do_something }).notify ### NO! BAD!
|
279
|
+
|
280
|
+
|
281
|
+
|
282
|
+
# The following line correctly passes self as a parameter to the
|
283
|
+
# notified process.
|
284
|
+
#
|
285
|
+
(EM.spawn {|obj| obj.do_something }).notify(self)
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
# Here's another way to do it. This works because "myself" is bound
|
290
|
+
# in the local scope, unlike "self," so the spawned block sees it.
|
291
|
+
#
|
292
|
+
myself = self
|
293
|
+
(EM.spawn { myself.do_something }).notify
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
# And we end the loop.
|
298
|
+
# This is a tangential point, but observe that #notify never blocks.
|
299
|
+
# It merely appends a message to the internal queue of a spawned process
|
300
|
+
# and returns. As it turns out, the reactor processes notifications for ALL
|
301
|
+
# spawned processes in the order that #notify is called. So there is a
|
302
|
+
# reasonable expectation that the process which stops the reactor will
|
303
|
+
# execute after the previous ones in this method. HOWEVER, this is NOT
|
304
|
+
# a documented behavior and is subject to change.
|
305
|
+
#
|
306
|
+
(EM.spawn {EM.stop}).notify
|
307
|
+
end
|
308
|
+
def do_something
|
309
|
+
@var ||= 0
|
310
|
+
@var += 100
|
311
|
+
end
|
312
|
+
end
|
313
|
+
|
314
|
+
def test_block_scope
|
315
|
+
bs = BlockScopeTest.new
|
316
|
+
EM.run {
|
317
|
+
bs.run
|
318
|
+
}
|
319
|
+
assert_equal( 200, bs.var )
|
320
|
+
end
|
321
|
+
|
322
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require 'tempfile'
|
3
|
+
|
4
|
+
$:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
|
5
|
+
require "eventmachine"
|
6
|
+
|
7
|
+
module EventMachine
|
8
|
+
def self._set_mocks
|
9
|
+
class <<self
|
10
|
+
alias set_tls_parms_old set_tls_parms
|
11
|
+
alias start_tls_old start_tls
|
12
|
+
begin
|
13
|
+
old, $VERBOSE = $VERBOSE, nil
|
14
|
+
def set_tls_parms *args; end
|
15
|
+
def start_tls *args; end
|
16
|
+
ensure
|
17
|
+
$VERBOSE = old
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self._clear_mocks
|
23
|
+
class <<self
|
24
|
+
begin
|
25
|
+
old, $VERBOSE = $VERBOSE, nil
|
26
|
+
alias set_tls_parms set_tls_parms_old
|
27
|
+
alias start_tls start_tls_old
|
28
|
+
ensure
|
29
|
+
$VERBOSE = old
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
class TestSslArgs < Test::Unit::TestCase
|
38
|
+
def setup
|
39
|
+
EventMachine._set_mocks
|
40
|
+
end
|
41
|
+
|
42
|
+
def teardown
|
43
|
+
EventMachine._clear_mocks
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_tls_params_file_doesnt_exist
|
47
|
+
priv_file, cert_file = 'foo_priv_key', 'bar_cert_file'
|
48
|
+
[priv_file, cert_file].all? do |f|
|
49
|
+
assert(!File.exists?(f), "Cert file #{f} seems to exist, and should not for the tests")
|
50
|
+
end
|
51
|
+
|
52
|
+
# associate_callback_target is a pain! (build!)
|
53
|
+
conn = EventMachine::Connection.new('foo')
|
54
|
+
|
55
|
+
assert_raises(EventMachine::FileNotFoundException) do
|
56
|
+
conn.start_tls(:private_key_file => priv_file)
|
57
|
+
end
|
58
|
+
assert_raises(EventMachine::FileNotFoundException) do
|
59
|
+
conn.start_tls(:cert_chain_file => cert_file)
|
60
|
+
end
|
61
|
+
assert_raises(EventMachine::FileNotFoundException) do
|
62
|
+
conn.start_tls(:private_key_file => priv_file, :cert_chain_file => cert_file)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_tls_params_file_does_exist
|
67
|
+
priv_file = Tempfile.new('em_test')
|
68
|
+
cert_file = Tempfile.new('em_test')
|
69
|
+
priv_file_path = priv_file.path
|
70
|
+
cert_file_path = cert_file.path
|
71
|
+
conn = EventMachine::Connection.new('foo')
|
72
|
+
params = {:private_key_file => priv_file_path, :cert_chain_file => cert_file_path}
|
73
|
+
begin
|
74
|
+
conn.start_tls params
|
75
|
+
rescue Object
|
76
|
+
assert(false, 'should not have raised an exception')
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end if EM.ssl?
|
@@ -0,0 +1,50 @@
|
|
1
|
+
$:.unshift "../lib"
|
2
|
+
require 'eventmachine'
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
class TestSSLMethods < Test::Unit::TestCase
|
6
|
+
|
7
|
+
module ServerHandler
|
8
|
+
|
9
|
+
def post_init
|
10
|
+
start_tls
|
11
|
+
end
|
12
|
+
|
13
|
+
def ssl_handshake_completed
|
14
|
+
$server_called_back = true
|
15
|
+
$server_cert_value = get_peer_cert
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
module ClientHandler
|
21
|
+
|
22
|
+
def post_init
|
23
|
+
start_tls
|
24
|
+
end
|
25
|
+
|
26
|
+
def ssl_handshake_completed
|
27
|
+
$client_called_back = true
|
28
|
+
$client_cert_value = get_peer_cert
|
29
|
+
EM.stop_event_loop
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_ssl_methods
|
35
|
+
$server_called_back, $client_called_back = false, false
|
36
|
+
$server_cert_value, $client_cert_value = nil, nil
|
37
|
+
|
38
|
+
EM.run {
|
39
|
+
EM.start_server("127.0.0.1", 9999, ServerHandler)
|
40
|
+
EM.connect("127.0.0.1", 9999, ClientHandler)
|
41
|
+
}
|
42
|
+
|
43
|
+
assert($server_called_back)
|
44
|
+
assert($client_called_back)
|
45
|
+
|
46
|
+
assert($server_cert_value.is_a?(NilClass))
|
47
|
+
assert($client_cert_value.is_a?(String))
|
48
|
+
end
|
49
|
+
|
50
|
+
end if EM.ssl?
|
@@ -0,0 +1,82 @@
|
|
1
|
+
$:.unshift "../lib"
|
2
|
+
require 'eventmachine'
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
class TestSslVerify < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
$dir = File.dirname(File.expand_path(__FILE__)) + '/'
|
9
|
+
$cert_from_file = File.read($dir+'client.crt')
|
10
|
+
end
|
11
|
+
|
12
|
+
module Client
|
13
|
+
def connection_completed
|
14
|
+
start_tls(:private_key_file => $dir+'client.key', :cert_chain_file => $dir+'client.crt')
|
15
|
+
end
|
16
|
+
|
17
|
+
def ssl_handshake_completed
|
18
|
+
$client_handshake_completed = true
|
19
|
+
close_connection
|
20
|
+
end
|
21
|
+
|
22
|
+
def unbind
|
23
|
+
EM.stop_event_loop
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module AcceptServer
|
28
|
+
def post_init
|
29
|
+
start_tls(:verify_peer => true)
|
30
|
+
end
|
31
|
+
|
32
|
+
def ssl_verify_peer(cert)
|
33
|
+
$cert_from_server = cert
|
34
|
+
true
|
35
|
+
end
|
36
|
+
|
37
|
+
def ssl_handshake_completed
|
38
|
+
$server_handshake_completed = true
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
module DenyServer
|
43
|
+
def post_init
|
44
|
+
start_tls(:verify_peer => true)
|
45
|
+
end
|
46
|
+
|
47
|
+
def ssl_verify_peer(cert)
|
48
|
+
$cert_from_server = cert
|
49
|
+
# Do not accept the peer. This should now cause the connection to shut down without the SSL handshake being completed.
|
50
|
+
false
|
51
|
+
end
|
52
|
+
|
53
|
+
def ssl_handshake_completed
|
54
|
+
$server_handshake_completed = true
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_accept_server
|
59
|
+
$client_handshake_completed, $server_handshake_completed = false, false
|
60
|
+
EM.run {
|
61
|
+
EM.start_server("127.0.0.1", 16784, AcceptServer)
|
62
|
+
EM.connect("127.0.0.1", 16784, Client).instance_variable_get("@signature")
|
63
|
+
}
|
64
|
+
|
65
|
+
assert_equal($cert_from_file, $cert_from_server)
|
66
|
+
assert($client_handshake_completed)
|
67
|
+
assert($server_handshake_completed)
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_deny_server
|
71
|
+
$client_handshake_completed, $server_handshake_completed = false, false
|
72
|
+
EM.run {
|
73
|
+
EM.start_server("127.0.0.1", 16784, DenyServer)
|
74
|
+
EM.connect("127.0.0.1", 16784, Client)
|
75
|
+
}
|
76
|
+
|
77
|
+
assert_equal($cert_from_file, $cert_from_server)
|
78
|
+
assert(!$client_handshake_completed)
|
79
|
+
assert(!$server_handshake_completed)
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
@@ -0,0 +1,162 @@
|
|
1
|
+
# $Id$
|
2
|
+
#
|
3
|
+
# Author:: Francis Cianfrocca (gmail: blackhedd)
|
4
|
+
# Homepage:: http://rubyeventmachine.com
|
5
|
+
# Date:: 8 April 2006
|
6
|
+
#
|
7
|
+
# See EventMachine and EventMachine::Connection for documentation and
|
8
|
+
# usage examples.
|
9
|
+
#
|
10
|
+
#----------------------------------------------------------------------------
|
11
|
+
#
|
12
|
+
# Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
13
|
+
# Gmail: blackhedd
|
14
|
+
#
|
15
|
+
# This program is free software; you can redistribute it and/or modify
|
16
|
+
# it under the terms of either: 1) the GNU General Public License
|
17
|
+
# as published by the Free Software Foundation; either version 2 of the
|
18
|
+
# License, or (at your option) any later version; or 2) Ruby's License.
|
19
|
+
#
|
20
|
+
# See the file COPYING for complete licensing information.
|
21
|
+
#
|
22
|
+
#---------------------------------------------------------------------------
|
23
|
+
#
|
24
|
+
#
|
25
|
+
#
|
26
|
+
#
|
27
|
+
|
28
|
+
$:.unshift "../lib"
|
29
|
+
require 'eventmachine'
|
30
|
+
require 'test/unit'
|
31
|
+
|
32
|
+
class TestTimers < Test::Unit::TestCase
|
33
|
+
|
34
|
+
def test_timer_with_block
|
35
|
+
x = false
|
36
|
+
EventMachine.run {
|
37
|
+
EventMachine::Timer.new(0.25) {
|
38
|
+
x = true
|
39
|
+
EventMachine.stop
|
40
|
+
}
|
41
|
+
}
|
42
|
+
assert x
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_timer_with_proc
|
46
|
+
x = false
|
47
|
+
EventMachine.run {
|
48
|
+
EventMachine::Timer.new(0.25, proc {
|
49
|
+
x = true
|
50
|
+
EventMachine.stop
|
51
|
+
})
|
52
|
+
}
|
53
|
+
assert x
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_timer_cancel
|
57
|
+
x = true
|
58
|
+
EventMachine.run {
|
59
|
+
timer = EventMachine::Timer.new(0.25, proc { x = false })
|
60
|
+
timer.cancel
|
61
|
+
EventMachine::Timer.new(0.5, proc {EventMachine.stop})
|
62
|
+
}
|
63
|
+
assert x
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_periodic_timer
|
67
|
+
x = 0
|
68
|
+
EventMachine.run {
|
69
|
+
EventMachine::PeriodicTimer.new(0.1) do
|
70
|
+
x += 1
|
71
|
+
EventMachine.stop if x == 4
|
72
|
+
end
|
73
|
+
}
|
74
|
+
assert( x == 4 )
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_add_periodic_timer
|
78
|
+
x = 0
|
79
|
+
EM.run {
|
80
|
+
t = EM.add_periodic_timer(0.1) do
|
81
|
+
x += 1
|
82
|
+
EM.stop if x == 4
|
83
|
+
end
|
84
|
+
assert t.respond_to?(:cancel)
|
85
|
+
}
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_periodic_timer_cancel
|
89
|
+
x = 0
|
90
|
+
EventMachine.run {
|
91
|
+
pt = EventMachine::PeriodicTimer.new(0.25, proc { x += 1 })
|
92
|
+
pt.cancel
|
93
|
+
EventMachine::Timer.new(0.5) {EventMachine.stop}
|
94
|
+
}
|
95
|
+
assert( x == 0 )
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_add_periodic_timer_cancel
|
99
|
+
x = 0
|
100
|
+
EventMachine.run {
|
101
|
+
pt = EM.add_periodic_timer(0.25) { x += 1 }
|
102
|
+
EM.cancel_timer(pt)
|
103
|
+
EM.add_timer(0.5) { EM.stop }
|
104
|
+
}
|
105
|
+
assert( x == 0 )
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_periodic_timer_self_cancel
|
109
|
+
x = 0
|
110
|
+
EventMachine.run {
|
111
|
+
pt = EventMachine::PeriodicTimer.new(0.1) {
|
112
|
+
x += 1
|
113
|
+
if x == 4
|
114
|
+
pt.cancel
|
115
|
+
EventMachine.stop
|
116
|
+
end
|
117
|
+
}
|
118
|
+
}
|
119
|
+
assert( x == 4 )
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
|
+
# This test is only applicable to compiled versions of the reactor.
|
124
|
+
# Pure ruby and java versions have no built-in limit on the number of outstanding timers.
|
125
|
+
#
|
126
|
+
def test_timer_change_max_outstanding
|
127
|
+
ten_thousand_timers = proc {
|
128
|
+
10001.times {
|
129
|
+
EM.add_timer(5) {}
|
130
|
+
}
|
131
|
+
}
|
132
|
+
|
133
|
+
EM.run {
|
134
|
+
if EM.library_type == :pure_ruby
|
135
|
+
ten_thousand_timers.call
|
136
|
+
elsif EM.library_type == :java
|
137
|
+
ten_thousand_timers.call
|
138
|
+
else
|
139
|
+
begin
|
140
|
+
assert_raises( RuntimeError ) {
|
141
|
+
ten_thousand_timers.call
|
142
|
+
}
|
143
|
+
rescue Object
|
144
|
+
p $!
|
145
|
+
assert(false, $!.message)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
EM.stop
|
149
|
+
}
|
150
|
+
|
151
|
+
assert(!EM.reactor_running?, 'Reactor running when it should not be.')
|
152
|
+
assert( EM.get_max_timers != 10001 )
|
153
|
+
EM.set_max_timers( 10001 )
|
154
|
+
assert( EM.get_max_timers == 10001 )
|
155
|
+
|
156
|
+
EM.run {
|
157
|
+
ten_thousand_timers.call
|
158
|
+
EM.stop
|
159
|
+
}
|
160
|
+
end
|
161
|
+
|
162
|
+
end
|