eventmachine 0.12.6-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (136) hide show
  1. data/.gitignore +13 -0
  2. data/Rakefile +262 -0
  3. data/docs/COPYING +60 -0
  4. data/docs/ChangeLog +211 -0
  5. data/docs/DEFERRABLES +138 -0
  6. data/docs/EPOLL +141 -0
  7. data/docs/GNU +281 -0
  8. data/docs/INSTALL +15 -0
  9. data/docs/KEYBOARD +38 -0
  10. data/docs/LEGAL +25 -0
  11. data/docs/LIGHTWEIGHT_CONCURRENCY +72 -0
  12. data/docs/PURE_RUBY +77 -0
  13. data/docs/README +74 -0
  14. data/docs/RELEASE_NOTES +96 -0
  15. data/docs/SMTP +9 -0
  16. data/docs/SPAWNED_PROCESSES +93 -0
  17. data/docs/TODO +10 -0
  18. data/eventmachine.gemspec +32 -0
  19. data/ext/binder.cpp +126 -0
  20. data/ext/binder.h +48 -0
  21. data/ext/cmain.cpp +586 -0
  22. data/ext/cplusplus.cpp +193 -0
  23. data/ext/ed.cpp +1522 -0
  24. data/ext/ed.h +380 -0
  25. data/ext/em.cpp +1937 -0
  26. data/ext/em.h +186 -0
  27. data/ext/emwin.cpp +300 -0
  28. data/ext/emwin.h +94 -0
  29. data/ext/epoll.cpp +26 -0
  30. data/ext/epoll.h +25 -0
  31. data/ext/eventmachine.h +98 -0
  32. data/ext/eventmachine_cpp.h +95 -0
  33. data/ext/extconf.rb +129 -0
  34. data/ext/fastfilereader/extconf.rb +77 -0
  35. data/ext/fastfilereader/mapper.cpp +214 -0
  36. data/ext/fastfilereader/mapper.h +59 -0
  37. data/ext/fastfilereader/rubymain.cpp +127 -0
  38. data/ext/files.cpp +94 -0
  39. data/ext/files.h +65 -0
  40. data/ext/kb.cpp +82 -0
  41. data/ext/page.cpp +107 -0
  42. data/ext/page.h +51 -0
  43. data/ext/pipe.cpp +351 -0
  44. data/ext/project.h +119 -0
  45. data/ext/rubymain.cpp +847 -0
  46. data/ext/sigs.cpp +89 -0
  47. data/ext/sigs.h +32 -0
  48. data/ext/ssl.cpp +423 -0
  49. data/ext/ssl.h +90 -0
  50. data/java/.classpath +8 -0
  51. data/java/.project +17 -0
  52. data/java/src/com/rubyeventmachine/Application.java +196 -0
  53. data/java/src/com/rubyeventmachine/Connection.java +74 -0
  54. data/java/src/com/rubyeventmachine/ConnectionFactory.java +37 -0
  55. data/java/src/com/rubyeventmachine/DefaultConnectionFactory.java +46 -0
  56. data/java/src/com/rubyeventmachine/EmReactor.java +408 -0
  57. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  58. data/java/src/com/rubyeventmachine/EventableChannel.java +57 -0
  59. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +171 -0
  60. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +244 -0
  61. data/java/src/com/rubyeventmachine/PeriodicTimer.java +38 -0
  62. data/java/src/com/rubyeventmachine/Timer.java +54 -0
  63. data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +108 -0
  64. data/java/src/com/rubyeventmachine/tests/ConnectTest.java +124 -0
  65. data/java/src/com/rubyeventmachine/tests/EMTest.java +80 -0
  66. data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +53 -0
  67. data/java/src/com/rubyeventmachine/tests/TestServers.java +74 -0
  68. data/java/src/com/rubyeventmachine/tests/TestTimers.java +89 -0
  69. data/lib/em/deferrable.rb +208 -0
  70. data/lib/em/eventable.rb +39 -0
  71. data/lib/em/future.rb +62 -0
  72. data/lib/em/messages.rb +66 -0
  73. data/lib/em/processes.rb +113 -0
  74. data/lib/em/spawnable.rb +88 -0
  75. data/lib/em/streamer.rb +112 -0
  76. data/lib/eventmachine.rb +1926 -0
  77. data/lib/eventmachine_version.rb +31 -0
  78. data/lib/evma.rb +32 -0
  79. data/lib/evma/callback.rb +32 -0
  80. data/lib/evma/container.rb +75 -0
  81. data/lib/evma/factory.rb +77 -0
  82. data/lib/evma/protocol.rb +87 -0
  83. data/lib/evma/reactor.rb +48 -0
  84. data/lib/jeventmachine.rb +137 -0
  85. data/lib/pr_eventmachine.rb +1011 -0
  86. data/lib/protocols/buftok.rb +127 -0
  87. data/lib/protocols/header_and_content.rb +129 -0
  88. data/lib/protocols/httpcli2.rb +803 -0
  89. data/lib/protocols/httpclient.rb +270 -0
  90. data/lib/protocols/line_and_text.rb +126 -0
  91. data/lib/protocols/linetext2.rb +161 -0
  92. data/lib/protocols/memcache.rb +293 -0
  93. data/lib/protocols/postgres.rb +261 -0
  94. data/lib/protocols/saslauth.rb +179 -0
  95. data/lib/protocols/smtpclient.rb +308 -0
  96. data/lib/protocols/smtpserver.rb +556 -0
  97. data/lib/protocols/stomp.rb +153 -0
  98. data/lib/protocols/tcptest.rb +57 -0
  99. data/setup.rb +1585 -0
  100. data/tasks/cpp.rake +77 -0
  101. data/tasks/project.rake +78 -0
  102. data/tasks/tests.rake +193 -0
  103. data/tests/test_attach.rb +83 -0
  104. data/tests/test_basic.rb +231 -0
  105. data/tests/test_connection_count.rb +45 -0
  106. data/tests/test_defer.rb +47 -0
  107. data/tests/test_epoll.rb +163 -0
  108. data/tests/test_error_handler.rb +35 -0
  109. data/tests/test_errors.rb +82 -0
  110. data/tests/test_eventables.rb +77 -0
  111. data/tests/test_exc.rb +58 -0
  112. data/tests/test_futures.rb +214 -0
  113. data/tests/test_handler_check.rb +37 -0
  114. data/tests/test_hc.rb +218 -0
  115. data/tests/test_httpclient.rb +215 -0
  116. data/tests/test_httpclient2.rb +155 -0
  117. data/tests/test_kb.rb +61 -0
  118. data/tests/test_ltp.rb +188 -0
  119. data/tests/test_ltp2.rb +320 -0
  120. data/tests/test_next_tick.rb +109 -0
  121. data/tests/test_processes.rb +95 -0
  122. data/tests/test_pure.rb +129 -0
  123. data/tests/test_running.rb +47 -0
  124. data/tests/test_sasl.rb +74 -0
  125. data/tests/test_send_file.rb +243 -0
  126. data/tests/test_servers.rb +80 -0
  127. data/tests/test_smtpclient.rb +83 -0
  128. data/tests/test_smtpserver.rb +93 -0
  129. data/tests/test_spawn.rb +329 -0
  130. data/tests/test_ssl_args.rb +68 -0
  131. data/tests/test_ssl_methods.rb +50 -0
  132. data/tests/test_timers.rb +148 -0
  133. data/tests/test_ud.rb +43 -0
  134. data/tests/testem.rb +31 -0
  135. data/web/whatis +7 -0
  136. metadata +214 -0
@@ -0,0 +1,80 @@
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
+ $:.unshift "../lib"
28
+ require 'eventmachine'
29
+ require 'socket'
30
+ require 'test/unit'
31
+
32
+
33
+ class TestServers < Test::Unit::TestCase
34
+
35
+ Host = "127.0.0.1"
36
+ Port = 9555
37
+
38
+ module NetstatHelper
39
+ GlobalUdp4Rexp = /udp.*\s+(?:\*|(?:0\.){3}0)[:.](\d+)\s/i
40
+ GlobalTcp4Rexp = /tcp.*\s+(?:\*|(?:0\.){3}0)[:.](\d+)\s/i
41
+ LocalUdpRexp = /udp.*\s+(?:127\.0\.0\.1|::1)[:.](\d+)\s/i
42
+ LocalTcpRexp = /tcp.*\s+(?:127\.0\.0\.1|::1)[:.](\d+)\s/i
43
+ def grep_netstat(pattern)
44
+ `netstat -an`.scan(/^.*$/).grep(pattern)
45
+ end
46
+ end
47
+ include NetstatHelper
48
+
49
+ class TestStopServer < EM::Connection
50
+ def initialize *args
51
+ super
52
+ end
53
+ def post_init
54
+ # TODO,sucks that this isn't OOPy enough.
55
+ EM.stop_server @server_instance
56
+ end
57
+ end
58
+
59
+ def run_test_stop_server
60
+ EM.run {
61
+ sig = EM.start_server(Host, Port)
62
+ assert(grep_netstat(LocalTcpRexp).grep(%r(#{Port})).size >= 1, "Server didn't start")
63
+ EM.stop_server sig
64
+ # Give the server some time to shutdown.
65
+ EM.add_timer(0.1) {
66
+ assert(grep_netstat(LocalTcpRexp).grep(%r(#{Port})).empty?, "Servers didn't stop")
67
+ EM.stop
68
+ }
69
+ }
70
+ end
71
+ def test_stop_server
72
+ assert(grep_netstat(LocalTcpRexp).grep(Port).empty?, "Port already in use")
73
+ 5.times {run_test_stop_server}
74
+ assert(grep_netstat(LocalTcpRexp).grep(%r(#{Port})).empty?, "Servers didn't stop")
75
+ end
76
+
77
+
78
+ end
79
+
80
+
@@ -0,0 +1,83 @@
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
+ $:.unshift "../lib"
28
+ require 'eventmachine'
29
+ require 'test/unit'
30
+
31
+ class TestSmtpClient < Test::Unit::TestCase
32
+
33
+ Localhost = "127.0.0.1"
34
+ Localport = 9801
35
+
36
+ def setup
37
+ end
38
+
39
+ def teardown
40
+ end
41
+
42
+ def test_a
43
+ # No real tests until we have a server implementation to test against.
44
+ # This is what the call looks like, though:
45
+ err = nil
46
+ EM.run {
47
+ d = EM::Protocols::SmtpClient.send :domain=>"example.com",
48
+ :host=>Localhost,
49
+ :port=>Localport, # optional, defaults 25
50
+ :starttls=>true,
51
+ :from=>"sender@example.com",
52
+ :to=> ["to_1@example.com", "to_2@example.com"],
53
+ :header=> {"Subject" => "This is a subject line"},
54
+ :body=> "This is the body of the email",
55
+ :verbose=>true
56
+ d.errback {|e|
57
+ err = e
58
+ EM.stop
59
+ }
60
+ }
61
+ assert(err)
62
+ end
63
+
64
+ def test_content
65
+ err = nil
66
+ EM.run {
67
+ d = EM::Protocols::SmtpClient.send :domain=>"example.com",
68
+ :host=>Localhost,
69
+ :port=>Localport, # optional, defaults 25
70
+ :starttls=>true,
71
+ :from=>"sender@example.com",
72
+ :to=> ["to_1@example.com", "to_2@example.com"],
73
+ :content => ["Subject: xxx\r\n\r\ndata\r\n.\r\n"],
74
+ :verbose=>true
75
+ d.errback {|e|
76
+ err = e
77
+ EM.stop
78
+ }
79
+ }
80
+ assert(err)
81
+ end
82
+
83
+ end
@@ -0,0 +1,93 @@
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
+ $:.unshift "../lib"
28
+ require 'eventmachine'
29
+ require 'test/unit'
30
+
31
+ class TestSmtpServer < Test::Unit::TestCase
32
+
33
+ # Don't test on port 25. It requires superuser and there's probably
34
+ # a mail server already running there anyway.
35
+ Localhost = "127.0.0.1"
36
+ Localport = 25001
37
+
38
+ # This class is an example of what you need to write in order
39
+ # to implement a mail server. You override the methods you are
40
+ # interested in. Some, but not all, of these are illustrated here.
41
+ #
42
+ class Mailserver < EM::Protocols::SmtpServer
43
+
44
+ attr_reader :my_msg_body, :my_sender, :my_recipients
45
+
46
+ def initialize *args
47
+ super
48
+ end
49
+ def receive_sender sender
50
+ @my_sender = sender
51
+ #p sender
52
+ true
53
+ end
54
+ def receive_recipient rcpt
55
+ @my_recipients ||= []
56
+ @my_recipients << rcpt
57
+ true
58
+ end
59
+ def receive_data_chunk c
60
+ @my_msg_body = c.last
61
+ end
62
+ def connection_ended
63
+ EM.stop
64
+ end
65
+ end
66
+
67
+
68
+ def setup
69
+ end
70
+
71
+ def teardown
72
+ end
73
+
74
+ def test_mail
75
+ c = nil
76
+ EM.run {
77
+ EM.start_server( Localhost, Localport, Mailserver ) {|conn| c = conn}
78
+ EM::Timer.new(2) {EM.stop} # prevent hanging the test suite in case of error
79
+ EM::Protocols::SmtpClient.send :host=>Localhost,
80
+ :port=>Localport,
81
+ :domain=>"bogus",
82
+ :from=>"me@example.com",
83
+ :to=>"you@example.com",
84
+ :header=> {"Subject"=>"Email subject line", "Reply-to"=>"me@example.com"},
85
+ :body=>"Not much of interest here."
86
+
87
+ }
88
+ assert_equal( "Not much of interest here.", c.my_msg_body )
89
+ assert_equal( "<me@example.com>", c.my_sender )
90
+ assert_equal( ["<you@example.com>"], c.my_recipients )
91
+ end
92
+ end
93
+
@@ -0,0 +1,329 @@
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
+ def setup
38
+ end
39
+
40
+ def teardown
41
+ end
42
+
43
+
44
+ # Spawn a process that simply stops the reactor.
45
+ # Assert that the notification runs after the block that calls it.
46
+ #
47
+ def test_stop
48
+ x = nil
49
+ EM.run {
50
+ s = EM.spawn {EM.stop}
51
+ s.notify
52
+ x = true
53
+ }
54
+ assert x
55
+ end
56
+
57
+
58
+ # Pass a parameter to a spawned process.
59
+ #
60
+ def test_parms
61
+ val = 5
62
+ EM.run {
63
+ s = EM.spawn {|v| val *= v; EM.stop}
64
+ s.notify 3
65
+ }
66
+ assert_equal( 15, val )
67
+ end
68
+
69
+ # Pass multiple parameters to a spawned process.
70
+ #
71
+ def test_multiparms
72
+ val = 5
73
+ EM.run {
74
+ s = EM.spawn {|v1,v2| val *= (v1 + v2); EM.stop}
75
+ s.notify 3,4
76
+ }
77
+ assert_equal( 35, val )
78
+ end
79
+
80
+
81
+ # This test demonstrates that a notification does not happen immediately,
82
+ # but rather is scheduled sometime after the current code path completes.
83
+ #
84
+ def test_race
85
+ x = 0
86
+ EM.run {
87
+ s = EM.spawn {x *= 2; EM.stop}
88
+ s.notify
89
+ x = 2
90
+ }
91
+ assert_equal( 4, x)
92
+ end
93
+
94
+
95
+ # Spawn a process and notify it 25 times to run fibonacci
96
+ # on a pair of global variables.
97
+ #
98
+ def test_fibonacci
99
+ x = 1
100
+ y = 1
101
+ EM.run {
102
+ s = EM.spawn {x,y = y,x+y}
103
+ 25.times {s.notify}
104
+
105
+ t = EM.spawn {EM.stop}
106
+ t.notify
107
+ }
108
+ assert_equal( 121393, x)
109
+ assert_equal( 196418, y)
110
+ end
111
+
112
+ # This one spawns 25 distinct processes, and notifies each one once,
113
+ # rather than notifying a single process 25 times.
114
+ #
115
+ def test_another_fibonacci
116
+ x = 1
117
+ y = 1
118
+ EM.run {
119
+ 25.times {
120
+ s = EM.spawn {x,y = y,x+y}
121
+ s.notify
122
+ }
123
+
124
+ t = EM.spawn {EM.stop}
125
+ t.notify
126
+ }
127
+ assert_equal( 121393, x)
128
+ assert_equal( 196418, y)
129
+ end
130
+
131
+
132
+ # Make a chain of processes that notify each other in turn
133
+ # with intermediate fibonacci results. The final process in
134
+ # the chain stops the loop and returns the result.
135
+ #
136
+ def test_fibonacci_chain
137
+ a,b = nil
138
+
139
+ EM.run {
140
+ nextpid = EM.spawn {|x,y|
141
+ a,b = x,y
142
+ EM.stop
143
+ }
144
+
145
+ 25.times {
146
+ n = nextpid
147
+ nextpid = EM.spawn {|x,y| n.notify( y, x+y )}
148
+ }
149
+
150
+ nextpid.notify( 1, 1 )
151
+ }
152
+
153
+ assert_equal( 121393, a)
154
+ assert_equal( 196418, b)
155
+ end
156
+
157
+
158
+ # EM#yield gives a spawed process to yield control to other processes
159
+ # (in other words, to stop running), and to specify a different code block
160
+ # that will run on its next notification.
161
+ #
162
+ def test_yield
163
+ a = 0
164
+ EM.run {
165
+ n = EM.spawn {
166
+ a += 10
167
+ EM.yield {
168
+ a += 20
169
+ EM.yield {
170
+ a += 30
171
+ EM.stop
172
+ }
173
+ }
174
+ }
175
+ n.notify
176
+ n.notify
177
+ n.notify
178
+ }
179
+ assert_equal( 60, a )
180
+ end
181
+
182
+ # EM#yield_and_notify behaves like EM#yield, except that it also notifies the
183
+ # yielding process. This may sound trivial, since the yield block will run very
184
+ # shortly after with no action by the program, but this actually can be very useful,
185
+ # because it causes the reactor core to execute once before the yielding process
186
+ # gets control back. So it can be used to allow heavily-used network connections
187
+ # to clear buffers, or allow other processes to process their notifications.
188
+ #
189
+ # Notice in this test code that only a simple notify is needed at the bottom
190
+ # of the initial block. Even so, all of the yielded blocks will execute.
191
+ #
192
+ def test_yield_and_notify
193
+ a = 0
194
+ EM.run {
195
+ n = EM.spawn {
196
+ a += 10
197
+ EM.yield_and_notify {
198
+ a += 20
199
+ EM.yield_and_notify {
200
+ a += 30
201
+ EM.stop
202
+ }
203
+ }
204
+ }
205
+ n.notify
206
+ }
207
+ assert_equal( 60, a )
208
+ end
209
+
210
+ # resume is an alias for notify.
211
+ #
212
+ def test_resume
213
+ EM.run {
214
+ n = EM.spawn {EM.stop}
215
+ n.resume
216
+ }
217
+ assert true
218
+ end
219
+
220
+ # run is an idiomatic alias for notify.
221
+ #
222
+ def test_run
223
+ EM.run {
224
+ (EM.spawn {EM.stop}).run
225
+ }
226
+ assert true
227
+ end
228
+
229
+
230
+ # Clones the ping-pong example from the Erlang tutorial, in much less code.
231
+ # Illustrates that a spawned block executes in the context of a SpawnableObject.
232
+ # (Meaning, we can pass self as a parameter to another process that can then
233
+ # notify us.)
234
+ #
235
+ def test_ping_pong
236
+ n_pongs = 0
237
+ EM.run {
238
+ pong = EM.spawn {|x, ping|
239
+ n_pongs += 1
240
+ ping.notify( x-1 )
241
+ }
242
+ ping = EM.spawn {|x|
243
+ if x > 0
244
+ pong.notify x, self
245
+ else
246
+ EM.stop
247
+ end
248
+ }
249
+ ping.notify 3
250
+ }
251
+ assert_equal( 3, n_pongs )
252
+ end
253
+
254
+ # Illustrates that you can call notify inside a notification, and it will cause
255
+ # the currently-executing process to be re-notified. Of course, the new notification
256
+ # won't run until sometime after the current one completes.
257
+ #
258
+ def test_self_notify
259
+ n = 0
260
+ EM.run {
261
+ pid = EM.spawn {|x|
262
+ if x > 0
263
+ n += x
264
+ notify( x-1 )
265
+ else
266
+ EM.stop
267
+ end
268
+ }
269
+ pid.notify 3
270
+ }
271
+ assert_equal( 6, n )
272
+ end
273
+
274
+
275
+ # Illustrates that the block passed to #spawn executes in the context of a
276
+ # SpawnedProcess object, NOT in the local context. This can often be deceptive.
277
+ #
278
+ class BlockScopeTest
279
+ attr_reader :var
280
+ def run
281
+ # The following line correctly raises a NameError.
282
+ # The problem is that the programmer expected the spawned block to
283
+ # execute in the local context, but it doesn't.
284
+ #
285
+ # (EM.spawn { do_something }).notify ### NO! BAD!
286
+
287
+
288
+
289
+ # The following line correctly passes self as a parameter to the
290
+ # notified process.
291
+ #
292
+ (EM.spawn {|obj| obj.do_something }).notify(self)
293
+
294
+
295
+
296
+ # Here's another way to do it. This works because "myself" is bound
297
+ # in the local scope, unlike "self," so the spawned block sees it.
298
+ #
299
+ myself = self
300
+ (EM.spawn { myself.do_something }).notify
301
+
302
+
303
+
304
+ # And we end the loop.
305
+ # This is a tangential point, but observe that #notify never blocks.
306
+ # It merely appends a message to the internal queue of a spawned process
307
+ # and returns. As it turns out, the reactor processes notifications for ALL
308
+ # spawned processes in the order that #notify is called. So there is a
309
+ # reasonable expectation that the process which stops the reactor will
310
+ # execute after the previous ones in this method. HOWEVER, this is NOT
311
+ # a documented behavior and is subject to change.
312
+ #
313
+ (EM.spawn {EM.stop}).notify
314
+ end
315
+ def do_something
316
+ @var ||= 0
317
+ @var += 100
318
+ end
319
+ end
320
+
321
+ def test_block_scope
322
+ bs = BlockScopeTest.new
323
+ EM.run {
324
+ bs.run
325
+ }
326
+ assert_equal( 200, bs.var )
327
+ end
328
+
329
+ end