eventmachine-with-ipv6 1.0.0.beta.4.ipv6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. data/.gitignore +21 -0
  2. data/.yardopts +7 -0
  3. data/FORK.md +47 -0
  4. data/GNU +281 -0
  5. data/Gemfile +3 -0
  6. data/LICENSE +60 -0
  7. data/README.md +109 -0
  8. data/Rakefile +20 -0
  9. data/docs/DocumentationGuidesIndex.md +27 -0
  10. data/docs/GettingStarted.md +521 -0
  11. data/docs/old/ChangeLog +211 -0
  12. data/docs/old/DEFERRABLES +246 -0
  13. data/docs/old/EPOLL +141 -0
  14. data/docs/old/INSTALL +13 -0
  15. data/docs/old/KEYBOARD +42 -0
  16. data/docs/old/LEGAL +25 -0
  17. data/docs/old/LIGHTWEIGHT_CONCURRENCY +130 -0
  18. data/docs/old/PURE_RUBY +75 -0
  19. data/docs/old/RELEASE_NOTES +94 -0
  20. data/docs/old/SMTP +4 -0
  21. data/docs/old/SPAWNED_PROCESSES +148 -0
  22. data/docs/old/TODO +8 -0
  23. data/eventmachine.gemspec +50 -0
  24. data/examples/guides/getting_started/01_eventmachine_echo_server.rb +18 -0
  25. data/examples/guides/getting_started/02_eventmachine_echo_server_that_recognizes_exit_command.rb +22 -0
  26. data/examples/guides/getting_started/03_simple_chat_server.rb +149 -0
  27. data/examples/guides/getting_started/04_simple_chat_server_step_one.rb +27 -0
  28. data/examples/guides/getting_started/05_simple_chat_server_step_two.rb +43 -0
  29. data/examples/guides/getting_started/06_simple_chat_server_step_three.rb +98 -0
  30. data/examples/guides/getting_started/07_simple_chat_server_step_four.rb +121 -0
  31. data/examples/guides/getting_started/08_simple_chat_server_step_five.rb +141 -0
  32. data/examples/old/ex_channel.rb +43 -0
  33. data/examples/old/ex_queue.rb +2 -0
  34. data/examples/old/ex_tick_loop_array.rb +15 -0
  35. data/examples/old/ex_tick_loop_counter.rb +32 -0
  36. data/examples/old/helper.rb +2 -0
  37. data/ext/binder.cpp +124 -0
  38. data/ext/binder.h +46 -0
  39. data/ext/cmain.cpp +858 -0
  40. data/ext/ed.cpp +1992 -0
  41. data/ext/ed.h +423 -0
  42. data/ext/em.cpp +2358 -0
  43. data/ext/em.h +245 -0
  44. data/ext/eventmachine.h +127 -0
  45. data/ext/extconf.rb +166 -0
  46. data/ext/fastfilereader/extconf.rb +94 -0
  47. data/ext/fastfilereader/mapper.cpp +214 -0
  48. data/ext/fastfilereader/mapper.h +59 -0
  49. data/ext/fastfilereader/rubymain.cpp +127 -0
  50. data/ext/kb.cpp +79 -0
  51. data/ext/page.cpp +107 -0
  52. data/ext/page.h +51 -0
  53. data/ext/pipe.cpp +347 -0
  54. data/ext/project.h +155 -0
  55. data/ext/rubymain.cpp +1280 -0
  56. data/ext/ssl.cpp +468 -0
  57. data/ext/ssl.h +94 -0
  58. data/java/.classpath +8 -0
  59. data/java/.project +17 -0
  60. data/java/src/com/rubyeventmachine/EmReactor.java +571 -0
  61. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  62. data/java/src/com/rubyeventmachine/EventableChannel.java +69 -0
  63. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +189 -0
  64. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +364 -0
  65. data/lib/em/buftok.rb +110 -0
  66. data/lib/em/callback.rb +58 -0
  67. data/lib/em/channel.rb +64 -0
  68. data/lib/em/completion.rb +304 -0
  69. data/lib/em/connection.rb +728 -0
  70. data/lib/em/deferrable.rb +210 -0
  71. data/lib/em/deferrable/pool.rb +2 -0
  72. data/lib/em/file_watch.rb +73 -0
  73. data/lib/em/future.rb +61 -0
  74. data/lib/em/iterator.rb +270 -0
  75. data/lib/em/messages.rb +66 -0
  76. data/lib/em/pool.rb +151 -0
  77. data/lib/em/process_watch.rb +45 -0
  78. data/lib/em/processes.rb +123 -0
  79. data/lib/em/protocols.rb +36 -0
  80. data/lib/em/protocols/header_and_content.rb +138 -0
  81. data/lib/em/protocols/httpclient.rb +279 -0
  82. data/lib/em/protocols/httpclient2.rb +600 -0
  83. data/lib/em/protocols/line_and_text.rb +125 -0
  84. data/lib/em/protocols/line_protocol.rb +29 -0
  85. data/lib/em/protocols/linetext2.rb +161 -0
  86. data/lib/em/protocols/memcache.rb +331 -0
  87. data/lib/em/protocols/object_protocol.rb +46 -0
  88. data/lib/em/protocols/postgres3.rb +246 -0
  89. data/lib/em/protocols/saslauth.rb +175 -0
  90. data/lib/em/protocols/smtpclient.rb +365 -0
  91. data/lib/em/protocols/smtpserver.rb +640 -0
  92. data/lib/em/protocols/socks4.rb +66 -0
  93. data/lib/em/protocols/stomp.rb +202 -0
  94. data/lib/em/protocols/tcptest.rb +54 -0
  95. data/lib/em/pure_ruby.rb +1017 -0
  96. data/lib/em/queue.rb +71 -0
  97. data/lib/em/resolver.rb +195 -0
  98. data/lib/em/spawnable.rb +84 -0
  99. data/lib/em/streamer.rb +118 -0
  100. data/lib/em/threaded_resource.rb +90 -0
  101. data/lib/em/tick_loop.rb +85 -0
  102. data/lib/em/timers.rb +61 -0
  103. data/lib/em/version.rb +3 -0
  104. data/lib/eventmachine.rb +1517 -0
  105. data/lib/jeventmachine.rb +279 -0
  106. data/rakelib/cpp.rake_example +77 -0
  107. data/rakelib/package.rake +98 -0
  108. data/rakelib/test.rake +8 -0
  109. data/tests/client.crt +31 -0
  110. data/tests/client.key +51 -0
  111. data/tests/em_test_helper.rb +64 -0
  112. data/tests/test_attach.rb +126 -0
  113. data/tests/test_basic.rb +294 -0
  114. data/tests/test_channel.rb +62 -0
  115. data/tests/test_completion.rb +177 -0
  116. data/tests/test_connection_count.rb +33 -0
  117. data/tests/test_defer.rb +18 -0
  118. data/tests/test_deferrable.rb +35 -0
  119. data/tests/test_epoll.rb +134 -0
  120. data/tests/test_error_handler.rb +38 -0
  121. data/tests/test_exc.rb +28 -0
  122. data/tests/test_file_watch.rb +65 -0
  123. data/tests/test_futures.rb +170 -0
  124. data/tests/test_get_sock_opt.rb +37 -0
  125. data/tests/test_handler_check.rb +35 -0
  126. data/tests/test_hc.rb +155 -0
  127. data/tests/test_httpclient.rb +190 -0
  128. data/tests/test_httpclient2.rb +128 -0
  129. data/tests/test_inactivity_timeout.rb +54 -0
  130. data/tests/test_ipv4.rb +128 -0
  131. data/tests/test_ipv6.rb +135 -0
  132. data/tests/test_kb.rb +34 -0
  133. data/tests/test_ltp.rb +138 -0
  134. data/tests/test_ltp2.rb +288 -0
  135. data/tests/test_next_tick.rb +104 -0
  136. data/tests/test_object_protocol.rb +36 -0
  137. data/tests/test_pause.rb +78 -0
  138. data/tests/test_pending_connect_timeout.rb +52 -0
  139. data/tests/test_pool.rb +194 -0
  140. data/tests/test_process_watch.rb +48 -0
  141. data/tests/test_processes.rb +133 -0
  142. data/tests/test_proxy_connection.rb +168 -0
  143. data/tests/test_pure.rb +88 -0
  144. data/tests/test_queue.rb +50 -0
  145. data/tests/test_resolver.rb +55 -0
  146. data/tests/test_running.rb +14 -0
  147. data/tests/test_sasl.rb +47 -0
  148. data/tests/test_send_file.rb +217 -0
  149. data/tests/test_servers.rb +33 -0
  150. data/tests/test_set_sock_opt.rb +41 -0
  151. data/tests/test_shutdown_hooks.rb +23 -0
  152. data/tests/test_smtpclient.rb +55 -0
  153. data/tests/test_smtpserver.rb +57 -0
  154. data/tests/test_spawn.rb +293 -0
  155. data/tests/test_ssl_args.rb +78 -0
  156. data/tests/test_ssl_methods.rb +48 -0
  157. data/tests/test_ssl_verify.rb +82 -0
  158. data/tests/test_threaded_resource.rb +53 -0
  159. data/tests/test_tick_loop.rb +59 -0
  160. data/tests/test_timers.rb +123 -0
  161. data/tests/test_ud.rb +8 -0
  162. data/tests/test_udp46.rb +54 -0
  163. data/tests/test_unbind_reason.rb +48 -0
  164. metadata +319 -0
@@ -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
@@ -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
@@ -0,0 +1,78 @@
1
+ require "test/unit"
2
+ require 'tempfile'
3
+
4
+ require 'em_test_helper'
5
+
6
+ module EM
7
+ def self._set_mocks
8
+ class <<self
9
+ alias set_tls_parms_old set_tls_parms
10
+ alias start_tls_old start_tls
11
+ begin
12
+ old, $VERBOSE = $VERBOSE, nil
13
+ def set_tls_parms *args; end
14
+ def start_tls *args; end
15
+ ensure
16
+ $VERBOSE = old
17
+ end
18
+ end
19
+ end
20
+
21
+ def self._clear_mocks
22
+ class <<self
23
+ begin
24
+ old, $VERBOSE = $VERBOSE, nil
25
+ alias set_tls_parms set_tls_parms_old
26
+ alias start_tls start_tls_old
27
+ ensure
28
+ $VERBOSE = old
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+
35
+
36
+ class TestSslArgs < Test::Unit::TestCase
37
+ def setup
38
+ EM._set_mocks
39
+ end
40
+
41
+ def teardown
42
+ EM._clear_mocks
43
+ end
44
+
45
+ def test_tls_params_file_doesnt_exist
46
+ priv_file, cert_file = 'foo_priv_key', 'bar_cert_file'
47
+ [priv_file, cert_file].all? do |f|
48
+ assert(!File.exists?(f), "Cert file #{f} seems to exist, and should not for the tests")
49
+ end
50
+
51
+ # associate_callback_target is a pain! (build!)
52
+ conn = EM::Connection.new('foo')
53
+
54
+ assert_raises(EM::FileNotFoundException) do
55
+ conn.start_tls(:private_key_file => priv_file)
56
+ end
57
+ assert_raises(EM::FileNotFoundException) do
58
+ conn.start_tls(:cert_chain_file => cert_file)
59
+ end
60
+ assert_raises(EM::FileNotFoundException) do
61
+ conn.start_tls(:private_key_file => priv_file, :cert_chain_file => cert_file)
62
+ end
63
+ end
64
+
65
+ def test_tls_params_file_does_exist
66
+ priv_file = Tempfile.new('em_test')
67
+ cert_file = Tempfile.new('em_test')
68
+ priv_file_path = priv_file.path
69
+ cert_file_path = cert_file.path
70
+ conn = EM::Connection.new('foo')
71
+ params = {:private_key_file => priv_file_path, :cert_chain_file => cert_file_path}
72
+ begin
73
+ conn.start_tls params
74
+ rescue Object
75
+ assert(false, 'should not have raised an exception')
76
+ end
77
+ end
78
+ end if EM.ssl?
@@ -0,0 +1,48 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestSSLMethods < Test::Unit::TestCase
4
+
5
+ module ServerHandler
6
+
7
+ def post_init
8
+ start_tls
9
+ end
10
+
11
+ def ssl_handshake_completed
12
+ $server_called_back = true
13
+ $server_cert_value = get_peer_cert
14
+ end
15
+
16
+ end
17
+
18
+ module ClientHandler
19
+
20
+ def post_init
21
+ start_tls
22
+ end
23
+
24
+ def ssl_handshake_completed
25
+ $client_called_back = true
26
+ $client_cert_value = get_peer_cert
27
+ EM.stop_event_loop
28
+ end
29
+
30
+ end
31
+
32
+ def test_ssl_methods
33
+ $server_called_back, $client_called_back = false, false
34
+ $server_cert_value, $client_cert_value = nil, nil
35
+
36
+ EM.run {
37
+ EM.start_server("127.0.0.1", 9999, ServerHandler)
38
+ EM.connect("127.0.0.1", 9999, ClientHandler)
39
+ }
40
+
41
+ assert($server_called_back)
42
+ assert($client_called_back)
43
+
44
+ assert($server_cert_value.is_a?(NilClass))
45
+ assert($client_cert_value.is_a?(String))
46
+ end
47
+
48
+ end if EM.ssl?