eventmachine 1.2.0.dev.2-x64-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +105 -0
- data/GNU +281 -0
- data/LICENSE +60 -0
- data/README.md +108 -0
- data/docs/DocumentationGuidesIndex.md +27 -0
- data/docs/GettingStarted.md +521 -0
- data/docs/old/ChangeLog +211 -0
- data/docs/old/DEFERRABLES +246 -0
- data/docs/old/EPOLL +141 -0
- data/docs/old/INSTALL +13 -0
- data/docs/old/KEYBOARD +42 -0
- data/docs/old/LEGAL +25 -0
- data/docs/old/LIGHTWEIGHT_CONCURRENCY +130 -0
- data/docs/old/PURE_RUBY +75 -0
- data/docs/old/RELEASE_NOTES +94 -0
- data/docs/old/SMTP +4 -0
- data/docs/old/SPAWNED_PROCESSES +148 -0
- data/docs/old/TODO +8 -0
- data/examples/guides/getting_started/01_eventmachine_echo_server.rb +18 -0
- data/examples/guides/getting_started/02_eventmachine_echo_server_that_recognizes_exit_command.rb +22 -0
- data/examples/guides/getting_started/03_simple_chat_server.rb +149 -0
- data/examples/guides/getting_started/04_simple_chat_server_step_one.rb +27 -0
- data/examples/guides/getting_started/05_simple_chat_server_step_two.rb +43 -0
- data/examples/guides/getting_started/06_simple_chat_server_step_three.rb +98 -0
- data/examples/guides/getting_started/07_simple_chat_server_step_four.rb +121 -0
- data/examples/guides/getting_started/08_simple_chat_server_step_five.rb +141 -0
- data/examples/old/ex_channel.rb +43 -0
- data/examples/old/ex_queue.rb +2 -0
- data/examples/old/ex_tick_loop_array.rb +15 -0
- data/examples/old/ex_tick_loop_counter.rb +32 -0
- data/examples/old/helper.rb +2 -0
- data/ext/binder.cpp +124 -0
- data/ext/binder.h +46 -0
- data/ext/cmain.cpp +988 -0
- data/ext/ed.cpp +2111 -0
- data/ext/ed.h +442 -0
- data/ext/em.cpp +2379 -0
- data/ext/em.h +308 -0
- data/ext/eventmachine.h +143 -0
- data/ext/extconf.rb +270 -0
- data/ext/fastfilereader/extconf.rb +110 -0
- data/ext/fastfilereader/mapper.cpp +216 -0
- data/ext/fastfilereader/mapper.h +59 -0
- data/ext/fastfilereader/rubymain.cpp +127 -0
- data/ext/kb.cpp +79 -0
- data/ext/page.cpp +107 -0
- data/ext/page.h +51 -0
- data/ext/pipe.cpp +354 -0
- data/ext/project.h +176 -0
- data/ext/rubymain.cpp +1504 -0
- data/ext/ssl.cpp +615 -0
- data/ext/ssl.h +103 -0
- data/java/.classpath +8 -0
- data/java/.project +17 -0
- data/java/src/com/rubyeventmachine/EmReactor.java +591 -0
- data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
- data/java/src/com/rubyeventmachine/EventableChannel.java +72 -0
- data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +201 -0
- data/java/src/com/rubyeventmachine/EventableSocketChannel.java +415 -0
- data/lib/2.0/fastfilereaderext.so +0 -0
- data/lib/2.0/rubyeventmachine.so +0 -0
- data/lib/2.1/fastfilereaderext.so +0 -0
- data/lib/2.1/rubyeventmachine.so +0 -0
- data/lib/2.2/fastfilereaderext.so +0 -0
- data/lib/2.2/rubyeventmachine.so +0 -0
- data/lib/2.3/fastfilereaderext.so +0 -0
- data/lib/2.3/rubyeventmachine.so +0 -0
- data/lib/em/buftok.rb +59 -0
- data/lib/em/callback.rb +58 -0
- data/lib/em/channel.rb +69 -0
- data/lib/em/completion.rb +304 -0
- data/lib/em/connection.rb +770 -0
- data/lib/em/deferrable.rb +210 -0
- data/lib/em/deferrable/pool.rb +2 -0
- data/lib/em/file_watch.rb +73 -0
- data/lib/em/future.rb +61 -0
- data/lib/em/iterator.rb +252 -0
- data/lib/em/messages.rb +66 -0
- data/lib/em/pool.rb +151 -0
- data/lib/em/process_watch.rb +45 -0
- data/lib/em/processes.rb +123 -0
- data/lib/em/protocols.rb +37 -0
- data/lib/em/protocols/header_and_content.rb +138 -0
- data/lib/em/protocols/httpclient.rb +299 -0
- data/lib/em/protocols/httpclient2.rb +600 -0
- data/lib/em/protocols/line_and_text.rb +125 -0
- data/lib/em/protocols/line_protocol.rb +29 -0
- data/lib/em/protocols/linetext2.rb +166 -0
- data/lib/em/protocols/memcache.rb +331 -0
- data/lib/em/protocols/object_protocol.rb +46 -0
- data/lib/em/protocols/postgres3.rb +246 -0
- data/lib/em/protocols/saslauth.rb +175 -0
- data/lib/em/protocols/smtpclient.rb +394 -0
- data/lib/em/protocols/smtpserver.rb +666 -0
- data/lib/em/protocols/socks4.rb +66 -0
- data/lib/em/protocols/stomp.rb +205 -0
- data/lib/em/protocols/tcptest.rb +54 -0
- data/lib/em/pure_ruby.rb +1022 -0
- data/lib/em/queue.rb +80 -0
- data/lib/em/resolver.rb +232 -0
- data/lib/em/spawnable.rb +84 -0
- data/lib/em/streamer.rb +118 -0
- data/lib/em/threaded_resource.rb +90 -0
- data/lib/em/tick_loop.rb +85 -0
- data/lib/em/timers.rb +61 -0
- data/lib/em/version.rb +3 -0
- data/lib/eventmachine.rb +1584 -0
- data/lib/fastfilereaderext.rb +2 -0
- data/lib/jeventmachine.rb +301 -0
- data/lib/rubyeventmachine.rb +2 -0
- data/rakelib/package.rake +120 -0
- data/rakelib/test.rake +8 -0
- data/tests/client.crt +31 -0
- data/tests/client.key +51 -0
- data/tests/dhparam.pem +13 -0
- data/tests/em_test_helper.rb +151 -0
- data/tests/test_attach.rb +151 -0
- data/tests/test_basic.rb +283 -0
- data/tests/test_channel.rb +75 -0
- data/tests/test_completion.rb +178 -0
- data/tests/test_connection_count.rb +54 -0
- data/tests/test_connection_write.rb +35 -0
- data/tests/test_defer.rb +35 -0
- data/tests/test_deferrable.rb +35 -0
- data/tests/test_epoll.rb +142 -0
- data/tests/test_error_handler.rb +38 -0
- data/tests/test_exc.rb +28 -0
- data/tests/test_file_watch.rb +66 -0
- data/tests/test_fork.rb +75 -0
- data/tests/test_futures.rb +170 -0
- data/tests/test_get_sock_opt.rb +37 -0
- data/tests/test_handler_check.rb +35 -0
- data/tests/test_hc.rb +155 -0
- data/tests/test_httpclient.rb +233 -0
- data/tests/test_httpclient2.rb +128 -0
- data/tests/test_idle_connection.rb +25 -0
- data/tests/test_inactivity_timeout.rb +54 -0
- data/tests/test_ipv4.rb +125 -0
- data/tests/test_ipv6.rb +131 -0
- data/tests/test_iterator.rb +115 -0
- data/tests/test_kb.rb +28 -0
- data/tests/test_line_protocol.rb +33 -0
- data/tests/test_ltp.rb +138 -0
- data/tests/test_ltp2.rb +308 -0
- data/tests/test_many_fds.rb +22 -0
- data/tests/test_next_tick.rb +104 -0
- data/tests/test_object_protocol.rb +36 -0
- data/tests/test_pause.rb +107 -0
- data/tests/test_pending_connect_timeout.rb +52 -0
- data/tests/test_pool.rb +196 -0
- data/tests/test_process_watch.rb +50 -0
- data/tests/test_processes.rb +128 -0
- data/tests/test_proxy_connection.rb +180 -0
- data/tests/test_pure.rb +88 -0
- data/tests/test_queue.rb +64 -0
- data/tests/test_resolver.rb +104 -0
- data/tests/test_running.rb +14 -0
- data/tests/test_sasl.rb +47 -0
- data/tests/test_send_file.rb +217 -0
- data/tests/test_servers.rb +33 -0
- data/tests/test_set_sock_opt.rb +39 -0
- data/tests/test_shutdown_hooks.rb +23 -0
- data/tests/test_smtpclient.rb +75 -0
- data/tests/test_smtpserver.rb +57 -0
- data/tests/test_spawn.rb +293 -0
- data/tests/test_ssl_args.rb +78 -0
- data/tests/test_ssl_dhparam.rb +83 -0
- data/tests/test_ssl_ecdh_curve.rb +79 -0
- data/tests/test_ssl_extensions.rb +49 -0
- data/tests/test_ssl_methods.rb +65 -0
- data/tests/test_ssl_protocols.rb +246 -0
- data/tests/test_ssl_verify.rb +126 -0
- data/tests/test_stomp.rb +37 -0
- data/tests/test_system.rb +46 -0
- data/tests/test_threaded_resource.rb +61 -0
- data/tests/test_tick_loop.rb +59 -0
- data/tests/test_timers.rb +123 -0
- data/tests/test_ud.rb +8 -0
- data/tests/test_unbind_reason.rb +52 -0
- metadata +381 -0
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'em_test_helper'
|
2
|
+
|
3
|
+
class TestShutdownHooks < Test::Unit::TestCase
|
4
|
+
def test_shutdown_hooks
|
5
|
+
r = false
|
6
|
+
EM.run {
|
7
|
+
EM.add_shutdown_hook { r = true }
|
8
|
+
EM.stop
|
9
|
+
}
|
10
|
+
assert_equal( true, r )
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_hook_order
|
14
|
+
r = []
|
15
|
+
EM.run {
|
16
|
+
EM.add_shutdown_hook { r << 2 }
|
17
|
+
EM.add_shutdown_hook { r << 1 }
|
18
|
+
EM.stop
|
19
|
+
}
|
20
|
+
assert_equal( [1, 2], r )
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'em_test_helper'
|
2
|
+
|
3
|
+
class TestSmtpClient < Test::Unit::TestCase
|
4
|
+
|
5
|
+
Localhost = "127.0.0.1"
|
6
|
+
Localport = 9801
|
7
|
+
|
8
|
+
def setup
|
9
|
+
end
|
10
|
+
|
11
|
+
def teardown
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_a
|
15
|
+
# No real tests until we have a server implementation to test against.
|
16
|
+
# This is what the call looks like, though:
|
17
|
+
err = nil
|
18
|
+
EM.run {
|
19
|
+
d = EM::Protocols::SmtpClient.send :domain=>"example.com",
|
20
|
+
:host=>Localhost,
|
21
|
+
:port=>Localport, # optional, defaults 25
|
22
|
+
:starttls=>true,
|
23
|
+
:from=>"sender@example.com",
|
24
|
+
:to=> ["to_1@example.com", "to_2@example.com"],
|
25
|
+
:header=> {"Subject" => "This is a subject line"},
|
26
|
+
:body=> "This is the body of the email",
|
27
|
+
:verbose=>true
|
28
|
+
d.errback {|e|
|
29
|
+
err = e
|
30
|
+
EM.stop
|
31
|
+
}
|
32
|
+
}
|
33
|
+
assert(err)
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_content
|
37
|
+
err = nil
|
38
|
+
EM.run {
|
39
|
+
d = EM::Protocols::SmtpClient.send :domain=>"example.com",
|
40
|
+
:host=>Localhost,
|
41
|
+
:port=>Localport, # optional, defaults 25
|
42
|
+
:starttls=>true,
|
43
|
+
:from=>"sender@example.com",
|
44
|
+
:to=> ["to_1@example.com", "to_2@example.com"],
|
45
|
+
:content => ["Subject: xxx\r\n\r\ndata\r\n.\r\n"],
|
46
|
+
:verbose=>true
|
47
|
+
d.errback {|e|
|
48
|
+
err = e
|
49
|
+
EM.stop
|
50
|
+
}
|
51
|
+
}
|
52
|
+
assert(err)
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
EM::Protocols::SmtpClient.__send__(:public, :escape_leading_dots)
|
57
|
+
|
58
|
+
def test_escaping
|
59
|
+
smtp = EM::Protocols::SmtpClient.new :domain => "example.com"
|
60
|
+
|
61
|
+
expectations = {
|
62
|
+
"Hello\r\n" => "Hello\r\n",
|
63
|
+
"\r\n.whatever\r\n" => "\r\n..whatever\r\n",
|
64
|
+
"\r\n.\r\n" => "\r\n..\r\n",
|
65
|
+
"\r\n.\r\n." => "\r\n..\r\n..",
|
66
|
+
".\r\n.\r\n" => "..\r\n..\r\n",
|
67
|
+
"..\r\n" => "...\r\n"
|
68
|
+
}
|
69
|
+
|
70
|
+
expectations.each do |input, output|
|
71
|
+
assert_equal output, smtp.escape_leading_dots(input)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'em_test_helper'
|
2
|
+
|
3
|
+
class TestSmtpServer < Test::Unit::TestCase
|
4
|
+
|
5
|
+
# Don't test on port 25. It requires superuser and there's probably
|
6
|
+
# a mail server already running there anyway.
|
7
|
+
Localhost = "127.0.0.1"
|
8
|
+
Localport = 25001
|
9
|
+
|
10
|
+
# This class is an example of what you need to write in order
|
11
|
+
# to implement a mail server. You override the methods you are
|
12
|
+
# interested in. Some, but not all, of these are illustrated here.
|
13
|
+
#
|
14
|
+
class Mailserver < EM::Protocols::SmtpServer
|
15
|
+
|
16
|
+
attr_reader :my_msg_body, :my_sender, :my_recipients
|
17
|
+
|
18
|
+
def initialize *args
|
19
|
+
super
|
20
|
+
end
|
21
|
+
def receive_sender sender
|
22
|
+
@my_sender = sender
|
23
|
+
#p sender
|
24
|
+
true
|
25
|
+
end
|
26
|
+
def receive_recipient rcpt
|
27
|
+
@my_recipients ||= []
|
28
|
+
@my_recipients << rcpt
|
29
|
+
true
|
30
|
+
end
|
31
|
+
def receive_data_chunk c
|
32
|
+
@my_msg_body = c.last
|
33
|
+
end
|
34
|
+
def connection_ended
|
35
|
+
EM.stop
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_mail
|
40
|
+
c = nil
|
41
|
+
EM.run {
|
42
|
+
EM.start_server( Localhost, Localport, Mailserver ) {|conn| c = conn}
|
43
|
+
EM::Timer.new(2) {EM.stop} # prevent hanging the test suite in case of error
|
44
|
+
EM::Protocols::SmtpClient.send :host=>Localhost,
|
45
|
+
:port=>Localport,
|
46
|
+
:domain=>"bogus",
|
47
|
+
:from=>"me@example.com",
|
48
|
+
:to=>"you@example.com",
|
49
|
+
:header=> {"Subject"=>"Email subject line", "Reply-to"=>"me@example.com"},
|
50
|
+
:body=>"Not much of interest here."
|
51
|
+
|
52
|
+
}
|
53
|
+
assert_equal( "Not much of interest here.", c.my_msg_body )
|
54
|
+
assert_equal( "<me@example.com>", c.my_sender )
|
55
|
+
assert_equal( ["<you@example.com>"], c.my_recipients )
|
56
|
+
end
|
57
|
+
end
|
data/tests/test_spawn.rb
ADDED
@@ -0,0 +1,293 @@
|
|
1
|
+
|
2
|
+
require 'em_test_helper'
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
class TestSpawn < Test::Unit::TestCase
|
7
|
+
|
8
|
+
# Spawn a process that simply stops the reactor.
|
9
|
+
# Assert that the notification runs after the block that calls it.
|
10
|
+
#
|
11
|
+
def test_stop
|
12
|
+
x = nil
|
13
|
+
EM.run {
|
14
|
+
s = EM.spawn {EM.stop}
|
15
|
+
s.notify
|
16
|
+
x = true
|
17
|
+
}
|
18
|
+
assert x
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
# Pass a parameter to a spawned process.
|
23
|
+
#
|
24
|
+
def test_parms
|
25
|
+
val = 5
|
26
|
+
EM.run {
|
27
|
+
s = EM.spawn {|v| val *= v; EM.stop}
|
28
|
+
s.notify 3
|
29
|
+
}
|
30
|
+
assert_equal( 15, val )
|
31
|
+
end
|
32
|
+
|
33
|
+
# Pass multiple parameters to a spawned process.
|
34
|
+
#
|
35
|
+
def test_multiparms
|
36
|
+
val = 5
|
37
|
+
EM.run {
|
38
|
+
s = EM.spawn {|v1,v2| val *= (v1 + v2); EM.stop}
|
39
|
+
s.notify 3,4
|
40
|
+
}
|
41
|
+
assert_equal( 35, val )
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
# This test demonstrates that a notification does not happen immediately,
|
46
|
+
# but rather is scheduled sometime after the current code path completes.
|
47
|
+
#
|
48
|
+
def test_race
|
49
|
+
x = 0
|
50
|
+
EM.run {
|
51
|
+
s = EM.spawn {x *= 2; EM.stop}
|
52
|
+
s.notify
|
53
|
+
x = 2
|
54
|
+
}
|
55
|
+
assert_equal( 4, x)
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
# Spawn a process and notify it 25 times to run fibonacci
|
60
|
+
# on a pair of global variables.
|
61
|
+
#
|
62
|
+
def test_fibonacci
|
63
|
+
x = 1
|
64
|
+
y = 1
|
65
|
+
EM.run {
|
66
|
+
s = EM.spawn {x,y = y,x+y}
|
67
|
+
25.times {s.notify}
|
68
|
+
|
69
|
+
t = EM.spawn {EM.stop}
|
70
|
+
t.notify
|
71
|
+
}
|
72
|
+
assert_equal( 121393, x)
|
73
|
+
assert_equal( 196418, y)
|
74
|
+
end
|
75
|
+
|
76
|
+
# This one spawns 25 distinct processes, and notifies each one once,
|
77
|
+
# rather than notifying a single process 25 times.
|
78
|
+
#
|
79
|
+
def test_another_fibonacci
|
80
|
+
x = 1
|
81
|
+
y = 1
|
82
|
+
EM.run {
|
83
|
+
25.times {
|
84
|
+
s = EM.spawn {x,y = y,x+y}
|
85
|
+
s.notify
|
86
|
+
}
|
87
|
+
|
88
|
+
t = EM.spawn {EM.stop}
|
89
|
+
t.notify
|
90
|
+
}
|
91
|
+
assert_equal( 121393, x)
|
92
|
+
assert_equal( 196418, y)
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
# Make a chain of processes that notify each other in turn
|
97
|
+
# with intermediate fibonacci results. The final process in
|
98
|
+
# the chain stops the loop and returns the result.
|
99
|
+
#
|
100
|
+
def test_fibonacci_chain
|
101
|
+
a,b = nil
|
102
|
+
|
103
|
+
EM.run {
|
104
|
+
nextpid = EM.spawn {|x,y|
|
105
|
+
a,b = x,y
|
106
|
+
EM.stop
|
107
|
+
}
|
108
|
+
|
109
|
+
25.times {
|
110
|
+
n = nextpid
|
111
|
+
nextpid = EM.spawn {|x,y| n.notify( y, x+y )}
|
112
|
+
}
|
113
|
+
|
114
|
+
nextpid.notify( 1, 1 )
|
115
|
+
}
|
116
|
+
|
117
|
+
assert_equal( 121393, a)
|
118
|
+
assert_equal( 196418, b)
|
119
|
+
end
|
120
|
+
|
121
|
+
|
122
|
+
# EM#yield gives a spawed process to yield control to other processes
|
123
|
+
# (in other words, to stop running), and to specify a different code block
|
124
|
+
# that will run on its next notification.
|
125
|
+
#
|
126
|
+
def test_yield
|
127
|
+
a = 0
|
128
|
+
EM.run {
|
129
|
+
n = EM.spawn {
|
130
|
+
a += 10
|
131
|
+
EM.yield {
|
132
|
+
a += 20
|
133
|
+
EM.yield {
|
134
|
+
a += 30
|
135
|
+
EM.stop
|
136
|
+
}
|
137
|
+
}
|
138
|
+
}
|
139
|
+
n.notify
|
140
|
+
n.notify
|
141
|
+
n.notify
|
142
|
+
}
|
143
|
+
assert_equal( 60, a )
|
144
|
+
end
|
145
|
+
|
146
|
+
# EM#yield_and_notify behaves like EM#yield, except that it also notifies the
|
147
|
+
# yielding process. This may sound trivial, since the yield block will run very
|
148
|
+
# shortly after with no action by the program, but this actually can be very useful,
|
149
|
+
# because it causes the reactor core to execute once before the yielding process
|
150
|
+
# gets control back. So it can be used to allow heavily-used network connections
|
151
|
+
# to clear buffers, or allow other processes to process their notifications.
|
152
|
+
#
|
153
|
+
# Notice in this test code that only a simple notify is needed at the bottom
|
154
|
+
# of the initial block. Even so, all of the yielded blocks will execute.
|
155
|
+
#
|
156
|
+
def test_yield_and_notify
|
157
|
+
a = 0
|
158
|
+
EM.run {
|
159
|
+
n = EM.spawn {
|
160
|
+
a += 10
|
161
|
+
EM.yield_and_notify {
|
162
|
+
a += 20
|
163
|
+
EM.yield_and_notify {
|
164
|
+
a += 30
|
165
|
+
EM.stop
|
166
|
+
}
|
167
|
+
}
|
168
|
+
}
|
169
|
+
n.notify
|
170
|
+
}
|
171
|
+
assert_equal( 60, a )
|
172
|
+
end
|
173
|
+
|
174
|
+
# resume is an alias for notify.
|
175
|
+
#
|
176
|
+
def test_resume
|
177
|
+
EM.run {
|
178
|
+
n = EM.spawn {EM.stop}
|
179
|
+
n.resume
|
180
|
+
}
|
181
|
+
assert true
|
182
|
+
end
|
183
|
+
|
184
|
+
# run is an idiomatic alias for notify.
|
185
|
+
#
|
186
|
+
def test_run
|
187
|
+
EM.run {
|
188
|
+
(EM.spawn {EM.stop}).run
|
189
|
+
}
|
190
|
+
assert true
|
191
|
+
end
|
192
|
+
|
193
|
+
|
194
|
+
# Clones the ping-pong example from the Erlang tutorial, in much less code.
|
195
|
+
# Illustrates that a spawned block executes in the context of a SpawnableObject.
|
196
|
+
# (Meaning, we can pass self as a parameter to another process that can then
|
197
|
+
# notify us.)
|
198
|
+
#
|
199
|
+
def test_ping_pong
|
200
|
+
n_pongs = 0
|
201
|
+
EM.run {
|
202
|
+
pong = EM.spawn {|x, ping|
|
203
|
+
n_pongs += 1
|
204
|
+
ping.notify( x-1 )
|
205
|
+
}
|
206
|
+
ping = EM.spawn {|x|
|
207
|
+
if x > 0
|
208
|
+
pong.notify x, self
|
209
|
+
else
|
210
|
+
EM.stop
|
211
|
+
end
|
212
|
+
}
|
213
|
+
ping.notify 3
|
214
|
+
}
|
215
|
+
assert_equal( 3, n_pongs )
|
216
|
+
end
|
217
|
+
|
218
|
+
# Illustrates that you can call notify inside a notification, and it will cause
|
219
|
+
# the currently-executing process to be re-notified. Of course, the new notification
|
220
|
+
# won't run until sometime after the current one completes.
|
221
|
+
#
|
222
|
+
def test_self_notify
|
223
|
+
n = 0
|
224
|
+
EM.run {
|
225
|
+
pid = EM.spawn {|x|
|
226
|
+
if x > 0
|
227
|
+
n += x
|
228
|
+
notify( x-1 )
|
229
|
+
else
|
230
|
+
EM.stop
|
231
|
+
end
|
232
|
+
}
|
233
|
+
pid.notify 3
|
234
|
+
}
|
235
|
+
assert_equal( 6, n )
|
236
|
+
end
|
237
|
+
|
238
|
+
|
239
|
+
# Illustrates that the block passed to #spawn executes in the context of a
|
240
|
+
# SpawnedProcess object, NOT in the local context. This can often be deceptive.
|
241
|
+
#
|
242
|
+
class BlockScopeTest
|
243
|
+
attr_reader :var
|
244
|
+
def run
|
245
|
+
# The following line correctly raises a NameError.
|
246
|
+
# The problem is that the programmer expected the spawned block to
|
247
|
+
# execute in the local context, but it doesn't.
|
248
|
+
#
|
249
|
+
# (EM.spawn { do_something }).notify ### NO! BAD!
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
# The following line correctly passes self as a parameter to the
|
254
|
+
# notified process.
|
255
|
+
#
|
256
|
+
(EM.spawn {|obj| obj.do_something }).notify(self)
|
257
|
+
|
258
|
+
|
259
|
+
|
260
|
+
# Here's another way to do it. This works because "myself" is bound
|
261
|
+
# in the local scope, unlike "self," so the spawned block sees it.
|
262
|
+
#
|
263
|
+
myself = self
|
264
|
+
(EM.spawn { myself.do_something }).notify
|
265
|
+
|
266
|
+
|
267
|
+
|
268
|
+
# And we end the loop.
|
269
|
+
# This is a tangential point, but observe that #notify never blocks.
|
270
|
+
# It merely appends a message to the internal queue of a spawned process
|
271
|
+
# and returns. As it turns out, the reactor processes notifications for ALL
|
272
|
+
# spawned processes in the order that #notify is called. So there is a
|
273
|
+
# reasonable expectation that the process which stops the reactor will
|
274
|
+
# execute after the previous ones in this method. HOWEVER, this is NOT
|
275
|
+
# a documented behavior and is subject to change.
|
276
|
+
#
|
277
|
+
(EM.spawn {EM.stop}).notify
|
278
|
+
end
|
279
|
+
def do_something
|
280
|
+
@var ||= 0
|
281
|
+
@var += 100
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
def test_block_scope
|
286
|
+
bs = BlockScopeTest.new
|
287
|
+
EM.run {
|
288
|
+
bs.run
|
289
|
+
}
|
290
|
+
assert_equal( 200, bs.var )
|
291
|
+
end
|
292
|
+
|
293
|
+
end
|