eventmachine-mkroman 1.3.0.dev.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (182) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +179 -0
  3. data/GNU +281 -0
  4. data/LICENSE +60 -0
  5. data/README.md +110 -0
  6. data/docs/DocumentationGuidesIndex.md +27 -0
  7. data/docs/GettingStarted.md +520 -0
  8. data/docs/old/ChangeLog +211 -0
  9. data/docs/old/DEFERRABLES +246 -0
  10. data/docs/old/EPOLL +141 -0
  11. data/docs/old/INSTALL +13 -0
  12. data/docs/old/KEYBOARD +42 -0
  13. data/docs/old/LEGAL +25 -0
  14. data/docs/old/LIGHTWEIGHT_CONCURRENCY +130 -0
  15. data/docs/old/PURE_RUBY +75 -0
  16. data/docs/old/RELEASE_NOTES +94 -0
  17. data/docs/old/SMTP +4 -0
  18. data/docs/old/SPAWNED_PROCESSES +148 -0
  19. data/docs/old/TODO +8 -0
  20. data/examples/guides/getting_started/01_eventmachine_echo_server.rb +18 -0
  21. data/examples/guides/getting_started/02_eventmachine_echo_server_that_recognizes_exit_command.rb +22 -0
  22. data/examples/guides/getting_started/03_simple_chat_server.rb +149 -0
  23. data/examples/guides/getting_started/04_simple_chat_server_step_one.rb +27 -0
  24. data/examples/guides/getting_started/05_simple_chat_server_step_two.rb +43 -0
  25. data/examples/guides/getting_started/06_simple_chat_server_step_three.rb +98 -0
  26. data/examples/guides/getting_started/07_simple_chat_server_step_four.rb +121 -0
  27. data/examples/guides/getting_started/08_simple_chat_server_step_five.rb +141 -0
  28. data/examples/old/ex_channel.rb +43 -0
  29. data/examples/old/ex_queue.rb +2 -0
  30. data/examples/old/ex_tick_loop_array.rb +15 -0
  31. data/examples/old/ex_tick_loop_counter.rb +32 -0
  32. data/examples/old/helper.rb +2 -0
  33. data/ext/binder.cpp +124 -0
  34. data/ext/binder.h +52 -0
  35. data/ext/cmain.cpp +1046 -0
  36. data/ext/ed.cpp +2243 -0
  37. data/ext/ed.h +463 -0
  38. data/ext/em.cpp +2378 -0
  39. data/ext/em.h +266 -0
  40. data/ext/eventmachine.h +152 -0
  41. data/ext/extconf.rb +291 -0
  42. data/ext/fastfilereader/extconf.rb +120 -0
  43. data/ext/fastfilereader/mapper.cpp +214 -0
  44. data/ext/fastfilereader/mapper.h +59 -0
  45. data/ext/fastfilereader/rubymain.cpp +126 -0
  46. data/ext/kb.cpp +79 -0
  47. data/ext/page.cpp +107 -0
  48. data/ext/page.h +51 -0
  49. data/ext/pipe.cpp +354 -0
  50. data/ext/project.h +174 -0
  51. data/ext/rubymain.cpp +1643 -0
  52. data/ext/ssl.cpp +701 -0
  53. data/ext/ssl.h +103 -0
  54. data/ext/wait_for_single_fd.h +36 -0
  55. data/java/.classpath +8 -0
  56. data/java/.project +17 -0
  57. data/java/src/com/rubyeventmachine/EmReactor.java +625 -0
  58. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  59. data/java/src/com/rubyeventmachine/EmReactorInterface.java +70 -0
  60. data/java/src/com/rubyeventmachine/EventableChannel.java +72 -0
  61. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +201 -0
  62. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +415 -0
  63. data/java/src/com/rubyeventmachine/NullEmReactor.java +157 -0
  64. data/java/src/com/rubyeventmachine/NullEventableChannel.java +81 -0
  65. data/lib/em/buftok.rb +59 -0
  66. data/lib/em/callback.rb +58 -0
  67. data/lib/em/channel.rb +69 -0
  68. data/lib/em/completion.rb +307 -0
  69. data/lib/em/connection.rb +802 -0
  70. data/lib/em/deferrable/pool.rb +2 -0
  71. data/lib/em/deferrable.rb +210 -0
  72. data/lib/em/file_watch.rb +73 -0
  73. data/lib/em/future.rb +61 -0
  74. data/lib/em/io_streamer.rb +68 -0
  75. data/lib/em/iterator.rb +252 -0
  76. data/lib/em/messages.rb +66 -0
  77. data/lib/em/pool.rb +151 -0
  78. data/lib/em/process_watch.rb +45 -0
  79. data/lib/em/processes.rb +123 -0
  80. data/lib/em/protocols/header_and_content.rb +138 -0
  81. data/lib/em/protocols/httpclient.rb +303 -0
  82. data/lib/em/protocols/httpclient2.rb +602 -0
  83. data/lib/em/protocols/line_and_text.rb +125 -0
  84. data/lib/em/protocols/line_protocol.rb +33 -0
  85. data/lib/em/protocols/linetext2.rb +179 -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 +394 -0
  91. data/lib/em/protocols/smtpserver.rb +666 -0
  92. data/lib/em/protocols/socks4.rb +66 -0
  93. data/lib/em/protocols/stomp.rb +205 -0
  94. data/lib/em/protocols/tcptest.rb +54 -0
  95. data/lib/em/protocols.rb +37 -0
  96. data/lib/em/pure_ruby.rb +1300 -0
  97. data/lib/em/queue.rb +80 -0
  98. data/lib/em/resolver.rb +232 -0
  99. data/lib/em/spawnable.rb +84 -0
  100. data/lib/em/streamer.rb +118 -0
  101. data/lib/em/threaded_resource.rb +90 -0
  102. data/lib/em/tick_loop.rb +85 -0
  103. data/lib/em/timers.rb +61 -0
  104. data/lib/em/version.rb +3 -0
  105. data/lib/eventmachine.rb +1602 -0
  106. data/lib/jeventmachine.rb +319 -0
  107. data/rakelib/package.rake +120 -0
  108. data/rakelib/test.rake +6 -0
  109. data/rakelib/test_pure.rake +11 -0
  110. data/tests/client.crt +31 -0
  111. data/tests/client.key +51 -0
  112. data/tests/dhparam.pem +13 -0
  113. data/tests/em_ssl_handlers.rb +165 -0
  114. data/tests/em_test_helper.rb +198 -0
  115. data/tests/encoded_client.key +54 -0
  116. data/tests/jruby/test_jeventmachine.rb +38 -0
  117. data/tests/test_attach.rb +199 -0
  118. data/tests/test_basic.rb +321 -0
  119. data/tests/test_channel.rb +75 -0
  120. data/tests/test_completion.rb +178 -0
  121. data/tests/test_connection_count.rb +83 -0
  122. data/tests/test_connection_write.rb +35 -0
  123. data/tests/test_defer.rb +35 -0
  124. data/tests/test_deferrable.rb +35 -0
  125. data/tests/test_epoll.rb +141 -0
  126. data/tests/test_error_handler.rb +38 -0
  127. data/tests/test_exc.rb +37 -0
  128. data/tests/test_file_watch.rb +86 -0
  129. data/tests/test_fork.rb +75 -0
  130. data/tests/test_futures.rb +170 -0
  131. data/tests/test_handler_check.rb +35 -0
  132. data/tests/test_hc.rb +155 -0
  133. data/tests/test_httpclient.rb +238 -0
  134. data/tests/test_httpclient2.rb +132 -0
  135. data/tests/test_idle_connection.rb +31 -0
  136. data/tests/test_inactivity_timeout.rb +102 -0
  137. data/tests/test_io_streamer.rb +48 -0
  138. data/tests/test_ipv4.rb +96 -0
  139. data/tests/test_ipv6.rb +107 -0
  140. data/tests/test_iterator.rb +122 -0
  141. data/tests/test_kb.rb +28 -0
  142. data/tests/test_keepalive.rb +113 -0
  143. data/tests/test_line_protocol.rb +33 -0
  144. data/tests/test_ltp.rb +155 -0
  145. data/tests/test_ltp2.rb +332 -0
  146. data/tests/test_many_fds.rb +21 -0
  147. data/tests/test_next_tick.rb +104 -0
  148. data/tests/test_object_protocol.rb +36 -0
  149. data/tests/test_pause.rb +109 -0
  150. data/tests/test_pending_connect_timeout.rb +52 -0
  151. data/tests/test_pool.rb +196 -0
  152. data/tests/test_process_watch.rb +50 -0
  153. data/tests/test_processes.rb +147 -0
  154. data/tests/test_proxy_connection.rb +180 -0
  155. data/tests/test_pure.rb +156 -0
  156. data/tests/test_queue.rb +64 -0
  157. data/tests/test_resolver.rb +129 -0
  158. data/tests/test_running.rb +14 -0
  159. data/tests/test_sasl.rb +46 -0
  160. data/tests/test_send_file.rb +217 -0
  161. data/tests/test_servers.rb +32 -0
  162. data/tests/test_shutdown_hooks.rb +23 -0
  163. data/tests/test_smtpclient.rb +75 -0
  164. data/tests/test_smtpserver.rb +90 -0
  165. data/tests/test_sock_opt.rb +53 -0
  166. data/tests/test_spawn.rb +290 -0
  167. data/tests/test_ssl_args.rb +70 -0
  168. data/tests/test_ssl_dhparam.rb +57 -0
  169. data/tests/test_ssl_ecdh_curve.rb +57 -0
  170. data/tests/test_ssl_extensions.rb +24 -0
  171. data/tests/test_ssl_inline_cert.rb +222 -0
  172. data/tests/test_ssl_methods.rb +31 -0
  173. data/tests/test_ssl_protocols.rb +190 -0
  174. data/tests/test_ssl_verify.rb +108 -0
  175. data/tests/test_stomp.rb +38 -0
  176. data/tests/test_system.rb +46 -0
  177. data/tests/test_threaded_resource.rb +68 -0
  178. data/tests/test_tick_loop.rb +58 -0
  179. data/tests/test_timers.rb +150 -0
  180. data/tests/test_ud.rb +8 -0
  181. data/tests/test_unbind_reason.rb +40 -0
  182. metadata +389 -0
@@ -0,0 +1,332 @@
1
+ require_relative 'em_test_helper'
2
+
3
+ # TODO!!! Need tests for overlength headers and text bodies.
4
+
5
+ class TestLineText2 < Test::Unit::TestCase
6
+
7
+ # Run each of these tests two ways: passing in the whole test-dataset in one chunk,
8
+ # and passing it in one character at a time.
9
+
10
+ class Basic
11
+ include EM::Protocols::LineText2
12
+ attr_reader :lines
13
+ def receive_line line
14
+ (@lines ||= []) << line
15
+ end
16
+ end
17
+ def test_basic
18
+ testdata = "Line 1\nLine 2\r\nLine 3\n"
19
+
20
+ a = Basic.new
21
+ a.receive_data testdata
22
+ assert_equal( ["Line 1", "Line 2", "Line 3"], a.lines )
23
+
24
+ a = Basic.new
25
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
26
+ assert_equal( ["Line 1", "Line 2", "Line 3"], a.lines )
27
+ end
28
+
29
+ # The basic test above shows that extra newlines are chomped
30
+ # This test shows that newlines are preserved if the delimiter isn't \n
31
+ class PreserveNewlines
32
+ include EM::Protocols::LineText2
33
+ attr_reader :lines
34
+ def initialize *args
35
+ super
36
+ @delim = "|"
37
+ set_delimiter @delim
38
+ end
39
+ def receive_line line
40
+ (@lines ||= []) << line
41
+ end
42
+ end
43
+ def test_preserve_newlines
44
+ a = PreserveNewlines.new
45
+ a.receive_data "aaa|bbb|ccc|\n|\r\n| \t ||"
46
+ assert_equal( ["aaa", "bbb", "ccc", "\n", "\r\n", " \t ", ""], a.lines )
47
+ end
48
+
49
+ class ChangeDelimiter
50
+ include EM::Protocols::LineText2
51
+ attr_reader :lines
52
+ def initialize *args
53
+ super
54
+ @delim = "A"
55
+ set_delimiter @delim
56
+ end
57
+ def receive_line line
58
+ (@lines ||= []) << line
59
+ set_delimiter( @delim.succ! )
60
+ end
61
+ end
62
+
63
+ def test_change_delimiter
64
+ testdata = %Q(LineaALinebBLinecCLinedD)
65
+
66
+ a = ChangeDelimiter.new
67
+ a.receive_data testdata
68
+ assert_equal( ["Linea", "Lineb", "Linec", "Lined"], a.lines )
69
+
70
+ a = ChangeDelimiter.new
71
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
72
+ assert_equal( ["Linea", "Lineb", "Linec", "Lined"], a.lines )
73
+ end
74
+
75
+ class RegexDelimiter
76
+ include EM::Protocols::LineText2
77
+ attr_reader :lines
78
+ def initialize *args
79
+ super
80
+ @delim = /[A-D]/
81
+ set_delimiter @delim
82
+ end
83
+ def receive_line line
84
+ (@lines ||= []) << line
85
+ end
86
+ end
87
+
88
+ def test_regex_delimiter
89
+ testdata = %Q(LineaALinebBLinecCLinedD)
90
+
91
+ a = RegexDelimiter.new
92
+ a.receive_data testdata
93
+ assert_equal( ["Linea", "Lineb", "Linec", "Lined"], a.lines )
94
+
95
+ a = RegexDelimiter.new
96
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
97
+ assert_equal( ["Linea", "Lineb", "Linec", "Lined"], a.lines )
98
+ end
99
+
100
+ #--
101
+ # Test two lines followed by an empty line, ten bytes of binary data, then
102
+ # two more lines.
103
+
104
+ class Binary
105
+ include EM::Protocols::LineText2
106
+ attr_reader :lines, :body
107
+ def initialize *args
108
+ super
109
+ @lines = []
110
+ @body = nil
111
+ end
112
+ def receive_line ln
113
+ if ln == ""
114
+ set_text_mode 10
115
+ else
116
+ @lines << ln
117
+ end
118
+ end
119
+ def receive_binary_data data
120
+ @body = data
121
+ end
122
+ end
123
+
124
+ def test_binary
125
+ testdata = %Q(Line 1
126
+ Line 2
127
+
128
+ 0000000000Line 3
129
+ Line 4
130
+ )
131
+
132
+ a = Binary.new
133
+ a.receive_data testdata
134
+ assert_equal( ["Line 1", "Line 2", "Line 3", "Line 4"], a.lines)
135
+ assert_equal( "0000000000", a.body )
136
+
137
+ a = Binary.new
138
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
139
+ assert_equal( ["Line 1", "Line 2", "Line 3", "Line 4"], a.lines)
140
+ assert_equal( "0000000000", a.body )
141
+ end
142
+
143
+
144
+ # Test unsized binary data. The expectation is that each chunk of it
145
+ # will be passed to us as it it received.
146
+ class UnsizedBinary
147
+ include EM::Protocols::LineText2
148
+ attr_reader :n_calls, :body
149
+ def initialize *args
150
+ super
151
+ set_text_mode
152
+ end
153
+ def receive_binary_data data
154
+ @n_calls ||= 0
155
+ @n_calls += 1
156
+ (@body ||= "") << data
157
+ end
158
+ end
159
+
160
+ def test_unsized_binary
161
+ testdata = "X\0" * 1000
162
+
163
+ a = UnsizedBinary.new
164
+ a.receive_data testdata
165
+ assert_equal( 1, a.n_calls )
166
+ assert_equal( testdata, a.body )
167
+
168
+ a = UnsizedBinary.new
169
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
170
+ assert_equal( 2000, a.n_calls )
171
+ assert_equal( testdata, a.body )
172
+ end
173
+
174
+
175
+ # Test binary data with a "throw back" into line-mode.
176
+ class ThrowBack
177
+ include EM::Protocols::LineText2
178
+ attr_reader :headers
179
+ def initialize *args
180
+ super
181
+ @headers = []
182
+ @n_bytes = 0
183
+ set_text_mode
184
+ end
185
+ def receive_binary_data data
186
+ wanted = 25 - @n_bytes
187
+ will_take = if data.length > wanted
188
+ data.length - wanted
189
+ else
190
+ data.length
191
+ end
192
+ @n_bytes += will_take
193
+
194
+ if @n_bytes == 25
195
+ set_line_mode( data[will_take..-1] )
196
+ end
197
+ end
198
+ def receive_line ln
199
+ @headers << ln
200
+ end
201
+ end
202
+ def test_throw_back
203
+ testdata = "Line\n" * 10
204
+
205
+ a = ThrowBack.new
206
+ a.receive_data testdata
207
+ assert_equal( ["Line"] * 5, a.headers )
208
+
209
+ a = ThrowBack.new
210
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
211
+ assert_equal( ["Line"] * 5, a.headers )
212
+ end
213
+
214
+ # Test multi-character line delimiters.
215
+ # Also note that the test data has a "tail" with no delimiter, that will be
216
+ # discarded, but cf. the BinaryTail test.
217
+ # TODO!!! This test doesn't work in the byte-by-byte case.
218
+ class Multichar
219
+ include EM::Protocols::LineText2
220
+ attr_reader :lines
221
+ def initialize *args
222
+ super
223
+ @lines = []
224
+ set_delimiter "012"
225
+ end
226
+ def receive_line ln
227
+ @lines << ln
228
+ end
229
+ end
230
+ def test_multichar
231
+ testdata = "Line012Line012Line012Line"
232
+
233
+ a = Multichar.new
234
+ a.receive_data testdata
235
+ assert_equal( ["Line"]*3, a.lines )
236
+
237
+ a = Multichar.new
238
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
239
+ # DOESN'T WORK in this case. Multi-character delimiters are broken.
240
+ #assert_equal( ["Line"]*3, a.lines )
241
+ end
242
+
243
+ # Test a binary "tail," when a sized binary transfer doesn't complete because
244
+ # of an unbind. We get a partial result.
245
+ class BinaryTail
246
+ include EM::Protocols::LineText2
247
+ attr_reader :data
248
+ def initialize *args
249
+ super
250
+ @data = ""
251
+ set_text_mode 1000
252
+ end
253
+ def receive_binary_data data
254
+ # we expect to get all the data in one chunk, even in the byte-by-byte case,
255
+ # because sized transfers by definition give us exactly one call to
256
+ # #receive_binary_data.
257
+ @data = data
258
+ end
259
+ end
260
+ def test_binary_tail
261
+ testdata = "0" * 500
262
+
263
+ a = BinaryTail.new
264
+ a.receive_data testdata
265
+ a.unbind
266
+ assert_equal( "0" * 500, a.data )
267
+
268
+ a = BinaryTail.new
269
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
270
+ a.unbind
271
+ assert_equal( "0" * 500, a.data )
272
+ end
273
+
274
+
275
+ # Test an end-of-binary call. Arrange to receive binary data but don't bother counting it
276
+ # as it comes. Rely on getting receive_end_of_binary_data to signal the transition back to
277
+ # line mode.
278
+ # At the present time, this isn't strictly necessary with sized binary chunks because by
279
+ # definition we accumulate them and make exactly one call to receive_binary_data, but
280
+ # we may want to support a mode in the future that would break up large chunks into multiple
281
+ # calls.
282
+ class LazyBinary
283
+ include EM::Protocols::LineText2
284
+ attr_reader :data, :end
285
+ def initialize *args
286
+ super
287
+ @data = ""
288
+ set_text_mode 1000
289
+ end
290
+ def receive_binary_data data
291
+ # we expect to get all the data in one chunk, even in the byte-by-byte case,
292
+ # because sized transfers by definition give us exactly one call to
293
+ # #receive_binary_data.
294
+ @data = data
295
+ end
296
+ def receive_end_of_binary_data
297
+ @end = true
298
+ end
299
+ end
300
+ def test_receive_end_of_binary_data
301
+ testdata = "_" * 1000
302
+ a = LazyBinary.new
303
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
304
+ assert_equal( "_" * 1000, a.data )
305
+ assert( a.end )
306
+ end
307
+
308
+
309
+ # This tests a bug fix in which calling set_text_mode failed when called
310
+ # inside receive_binary_data.
311
+ #
312
+ class BinaryPair
313
+ include EM::Protocols::LineText2
314
+ attr_reader :sizes
315
+ def initialize *args
316
+ super
317
+ set_text_mode 1
318
+ @sizes = []
319
+ end
320
+ def receive_binary_data dt
321
+ @sizes << dt.length
322
+ set_text_mode( (dt.length == 1) ? 2 : 1 )
323
+ end
324
+ end
325
+ def test_binary_pairs
326
+ test_data = "123" * 5
327
+ a = BinaryPair.new
328
+ a.receive_data test_data
329
+ assert_equal( [1,2,1,2,1,2,1,2,1,2], a.sizes )
330
+ end
331
+
332
+ end
@@ -0,0 +1,21 @@
1
+ require_relative 'em_test_helper'
2
+
3
+ class TestManyFDs < Test::Unit::TestCase
4
+ def setup
5
+ @port = next_port
6
+ end
7
+
8
+ def test_connection_class_cache
9
+ mod = Module.new
10
+ a = nil
11
+ Process.setrlimit(Process::RLIMIT_NOFILE, 4096) rescue nil
12
+ EM.run {
13
+ EM.start_server '127.0.0.1', @port, mod
14
+ 1100.times do
15
+ a = EM.connect '127.0.0.1', @port, mod
16
+ assert_kind_of EM::Connection, a
17
+ end
18
+ EM.stop
19
+ }
20
+ end
21
+ end
@@ -0,0 +1,104 @@
1
+ require_relative 'em_test_helper'
2
+
3
+ class TestNextTick < Test::Unit::TestCase
4
+
5
+ def test_tick_arg
6
+ pr = proc {EM.stop}
7
+ EM.run {
8
+ EM.next_tick pr
9
+ }
10
+ assert true
11
+ end
12
+
13
+ def test_tick_block
14
+ EM.run {
15
+ EM.next_tick {EM.stop}
16
+ }
17
+ assert true
18
+ end
19
+
20
+ # This illustrates the solution to a long-standing problem.
21
+ # It's now possible to correctly nest calls to EM#run.
22
+ # See the source code commentary for EM#run for more info.
23
+ #
24
+ def test_run_run
25
+ EM.run {
26
+ EM.run {
27
+ EM.next_tick {EM.stop}
28
+ }
29
+ }
30
+ end
31
+
32
+ def test_pre_run_queue
33
+ x = false
34
+ EM.next_tick { EM.stop; x = true }
35
+ EM.run { EM.add_timer(0.01) { EM.stop } }
36
+ assert x
37
+ end
38
+
39
+ def test_cleanup_after_stop
40
+ x = true
41
+ EM.run{
42
+ EM.next_tick{
43
+ EM.stop
44
+ EM.next_tick{ x=false }
45
+ }
46
+ }
47
+ EM.run{
48
+ EM.next_tick{ EM.stop }
49
+ }
50
+ assert x
51
+ end
52
+
53
+ # We now support an additional parameter for EM#run.
54
+ # You can pass two procs to EM#run now. The first is executed as the normal
55
+ # run block. The second (if given) is scheduled for execution after the
56
+ # reactor loop completes.
57
+ # The reason for supporting this is subtle. There has always been an expectation
58
+ # that EM#run doesn't return until after the reactor loop ends. But now it's
59
+ # possible to nest calls to EM#run, which means that a nested call WILL
60
+ # RETURN. In order to write code that will run correctly either way, it's
61
+ # recommended to put any code which must execute after the reactor completes
62
+ # in the second parameter.
63
+ #
64
+ def test_run_run_2
65
+ a = proc {EM.stop}
66
+ b = proc {assert true}
67
+ EM.run a, b
68
+ end
69
+
70
+
71
+ # This illustrates that EM#run returns when it's called nested.
72
+ # This isn't a feature, rather it's something to be wary of when writing code
73
+ # that must run correctly even if EM#run is called while a reactor is already
74
+ # running.
75
+ def test_run_run_3
76
+ a = []
77
+ EM.run {
78
+ EM.run proc {EM.stop}, proc {a << 2}
79
+ a << 1
80
+ }
81
+ assert_equal( [1,2], a )
82
+ end
83
+
84
+
85
+ def test_schedule_on_reactor_thread
86
+ x = false
87
+ EM.run do
88
+ EM.schedule { x = true }
89
+ EM.stop
90
+ end
91
+ assert x
92
+ end
93
+
94
+ def test_schedule_from_thread
95
+ x = false
96
+ EM.run do
97
+ Thread.new { EM.schedule { x = true } }.join
98
+ assert !x
99
+ EM.next_tick { EM.stop }
100
+ end
101
+ assert x
102
+ end
103
+
104
+ end
@@ -0,0 +1,36 @@
1
+ require_relative 'em_test_helper'
2
+
3
+ class TestObjectProtocol < Test::Unit::TestCase
4
+ module Server
5
+ include EM::P::ObjectProtocol
6
+ def post_init
7
+ send_object :hello=>'world'
8
+ end
9
+ def receive_object obj
10
+ $server = obj
11
+ EM.stop
12
+ end
13
+ end
14
+
15
+ module Client
16
+ include EM::P::ObjectProtocol
17
+ def receive_object obj
18
+ $client = obj
19
+ send_object 'you_said'=>obj
20
+ end
21
+ end
22
+
23
+ def setup
24
+ @port = next_port
25
+ end
26
+
27
+ def test_send_receive
28
+ EM.run{
29
+ EM.start_server "127.0.0.1", @port, Server
30
+ EM.connect "127.0.0.1", @port, Client
31
+ }
32
+
33
+ assert($client == {:hello=>'world'})
34
+ assert($server == {'you_said'=>{:hello=>'world'}})
35
+ end
36
+ end
@@ -0,0 +1,109 @@
1
+ require_relative 'em_test_helper'
2
+
3
+ class TestPause < Test::Unit::TestCase
4
+ if EM.respond_to? :pause_connection
5
+ def setup
6
+ @port = next_port
7
+ end
8
+
9
+ def teardown
10
+ assert(!EM.reactor_running?)
11
+ end
12
+
13
+ def test_pause_resume
14
+ server = nil
15
+
16
+ s_rx = c_rx = 0
17
+
18
+ test_server = Module.new do
19
+ define_method :post_init do
20
+ server = self
21
+ end
22
+
23
+ define_method :receive_data do |data|
24
+ s_rx += 1
25
+
26
+ EM.add_periodic_timer(0.01) { send_data 'hi' }
27
+ send_data 'hi'
28
+
29
+ # pause server, now no outgoing data will actually
30
+ # be sent and no more incoming data will be received
31
+ pause
32
+ end
33
+ end
34
+
35
+ test_client = Module.new do
36
+ def post_init
37
+ EM.add_periodic_timer(0.01) do
38
+ send_data 'hello'
39
+ end
40
+ end
41
+
42
+ define_method :receive_data do |data|
43
+ c_rx += 1
44
+ end
45
+ end
46
+
47
+ EM.run do
48
+ EM.start_server "127.0.0.1", @port, test_server
49
+ EM.connect "127.0.0.1", @port, test_client
50
+
51
+ tmr = darwin? ? 0.10 : 0.05
52
+
53
+ EM.add_timer(tmr) do
54
+ assert_equal 1, s_rx
55
+ assert_equal 0, c_rx
56
+ assert server.paused?
57
+
58
+ # resume server, queued outgoing and incoming data will be flushed
59
+ server.resume
60
+
61
+ assert !server.paused?
62
+
63
+ EM.add_timer(tmr) do
64
+ assert server.paused?
65
+ assert s_rx > 1
66
+ assert c_rx > 0
67
+ EM.stop
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ def test_pause_in_receive_data
74
+ incoming = []
75
+
76
+ test_server = Module.new do
77
+ define_method(:receive_data) do |data|
78
+ incoming << data
79
+ pause
80
+ EM.add_timer(0.5){ close_connection }
81
+ end
82
+ define_method(:unbind) do
83
+ EM.stop
84
+ end
85
+ end
86
+
87
+ buf = 'a' * 1024
88
+
89
+ EM.run do
90
+ EM.start_server "127.0.0.1", @port, test_server
91
+ cli = EM.connect "127.0.0.1", @port
92
+ 128.times do
93
+ cli.send_data buf
94
+ end
95
+ end
96
+
97
+ assert_equal 1, incoming.size
98
+ assert incoming[0].bytesize > buf.bytesize
99
+ assert incoming[0].bytesize < buf.bytesize * 128
100
+ end
101
+ else
102
+ warn "EM.pause_connection not implemented, skipping tests in #{__FILE__}"
103
+
104
+ # Because some rubies will complain if a TestCase class has no tests
105
+ def test_em_pause_connection_not_implemented
106
+ assert true
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,52 @@
1
+ require_relative 'em_test_helper'
2
+
3
+ class TestPendingConnectTimeout < Test::Unit::TestCase
4
+
5
+ if EM.respond_to? :get_pending_connect_timeout
6
+ def test_default
7
+ EM.run {
8
+ c = EM.connect("127.0.0.1", 54321)
9
+ assert_equal 20.0, c.pending_connect_timeout
10
+ EM.stop
11
+ }
12
+ end
13
+
14
+ def test_set_and_get
15
+ EM.run {
16
+ c = EM.connect("127.0.0.1", 54321)
17
+ c.pending_connect_timeout = 2.5
18
+ assert_equal 2.5, c.pending_connect_timeout
19
+ EM.stop
20
+ }
21
+ end
22
+
23
+ def test_for_real
24
+ start, finish = nil
25
+
26
+ timeout_handler = Module.new do
27
+ define_method :unbind do
28
+ finish = EM.current_time
29
+ EM.stop
30
+ end
31
+ end
32
+
33
+ EM.run {
34
+ setup_timeout 0.4
35
+ EM.heartbeat_interval = 0.1
36
+ start = EM.current_time
37
+ c = EM.connect('192.0.2.0', 54321, timeout_handler)
38
+ c.pending_connect_timeout = 0.2
39
+ }
40
+ # Travis can vary from 0.10 to 0.40
41
+ assert_in_delta(0.25, (finish - start), 0.15)
42
+ end
43
+ else
44
+ warn "EM.pending_connect_timeout not implemented, skipping tests in #{__FILE__}"
45
+
46
+ # Because some rubies will complain if a TestCase class has no tests
47
+ def test_em_pending_connect_timeout_not_implemented
48
+ assert true
49
+ end
50
+ end
51
+
52
+ end