eventmachine 1.2.0.dev.2-x64-mingw32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (181) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +105 -0
  3. data/GNU +281 -0
  4. data/LICENSE +60 -0
  5. data/README.md +108 -0
  6. data/docs/DocumentationGuidesIndex.md +27 -0
  7. data/docs/GettingStarted.md +521 -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 +46 -0
  35. data/ext/cmain.cpp +988 -0
  36. data/ext/ed.cpp +2111 -0
  37. data/ext/ed.h +442 -0
  38. data/ext/em.cpp +2379 -0
  39. data/ext/em.h +308 -0
  40. data/ext/eventmachine.h +143 -0
  41. data/ext/extconf.rb +270 -0
  42. data/ext/fastfilereader/extconf.rb +110 -0
  43. data/ext/fastfilereader/mapper.cpp +216 -0
  44. data/ext/fastfilereader/mapper.h +59 -0
  45. data/ext/fastfilereader/rubymain.cpp +127 -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 +176 -0
  51. data/ext/rubymain.cpp +1504 -0
  52. data/ext/ssl.cpp +615 -0
  53. data/ext/ssl.h +103 -0
  54. data/java/.classpath +8 -0
  55. data/java/.project +17 -0
  56. data/java/src/com/rubyeventmachine/EmReactor.java +591 -0
  57. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  58. data/java/src/com/rubyeventmachine/EventableChannel.java +72 -0
  59. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +201 -0
  60. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +415 -0
  61. data/lib/2.0/fastfilereaderext.so +0 -0
  62. data/lib/2.0/rubyeventmachine.so +0 -0
  63. data/lib/2.1/fastfilereaderext.so +0 -0
  64. data/lib/2.1/rubyeventmachine.so +0 -0
  65. data/lib/2.2/fastfilereaderext.so +0 -0
  66. data/lib/2.2/rubyeventmachine.so +0 -0
  67. data/lib/2.3/fastfilereaderext.so +0 -0
  68. data/lib/2.3/rubyeventmachine.so +0 -0
  69. data/lib/em/buftok.rb +59 -0
  70. data/lib/em/callback.rb +58 -0
  71. data/lib/em/channel.rb +69 -0
  72. data/lib/em/completion.rb +304 -0
  73. data/lib/em/connection.rb +770 -0
  74. data/lib/em/deferrable.rb +210 -0
  75. data/lib/em/deferrable/pool.rb +2 -0
  76. data/lib/em/file_watch.rb +73 -0
  77. data/lib/em/future.rb +61 -0
  78. data/lib/em/iterator.rb +252 -0
  79. data/lib/em/messages.rb +66 -0
  80. data/lib/em/pool.rb +151 -0
  81. data/lib/em/process_watch.rb +45 -0
  82. data/lib/em/processes.rb +123 -0
  83. data/lib/em/protocols.rb +37 -0
  84. data/lib/em/protocols/header_and_content.rb +138 -0
  85. data/lib/em/protocols/httpclient.rb +299 -0
  86. data/lib/em/protocols/httpclient2.rb +600 -0
  87. data/lib/em/protocols/line_and_text.rb +125 -0
  88. data/lib/em/protocols/line_protocol.rb +29 -0
  89. data/lib/em/protocols/linetext2.rb +166 -0
  90. data/lib/em/protocols/memcache.rb +331 -0
  91. data/lib/em/protocols/object_protocol.rb +46 -0
  92. data/lib/em/protocols/postgres3.rb +246 -0
  93. data/lib/em/protocols/saslauth.rb +175 -0
  94. data/lib/em/protocols/smtpclient.rb +394 -0
  95. data/lib/em/protocols/smtpserver.rb +666 -0
  96. data/lib/em/protocols/socks4.rb +66 -0
  97. data/lib/em/protocols/stomp.rb +205 -0
  98. data/lib/em/protocols/tcptest.rb +54 -0
  99. data/lib/em/pure_ruby.rb +1022 -0
  100. data/lib/em/queue.rb +80 -0
  101. data/lib/em/resolver.rb +232 -0
  102. data/lib/em/spawnable.rb +84 -0
  103. data/lib/em/streamer.rb +118 -0
  104. data/lib/em/threaded_resource.rb +90 -0
  105. data/lib/em/tick_loop.rb +85 -0
  106. data/lib/em/timers.rb +61 -0
  107. data/lib/em/version.rb +3 -0
  108. data/lib/eventmachine.rb +1584 -0
  109. data/lib/fastfilereaderext.rb +2 -0
  110. data/lib/jeventmachine.rb +301 -0
  111. data/lib/rubyeventmachine.rb +2 -0
  112. data/rakelib/package.rake +120 -0
  113. data/rakelib/test.rake +8 -0
  114. data/tests/client.crt +31 -0
  115. data/tests/client.key +51 -0
  116. data/tests/dhparam.pem +13 -0
  117. data/tests/em_test_helper.rb +151 -0
  118. data/tests/test_attach.rb +151 -0
  119. data/tests/test_basic.rb +283 -0
  120. data/tests/test_channel.rb +75 -0
  121. data/tests/test_completion.rb +178 -0
  122. data/tests/test_connection_count.rb +54 -0
  123. data/tests/test_connection_write.rb +35 -0
  124. data/tests/test_defer.rb +35 -0
  125. data/tests/test_deferrable.rb +35 -0
  126. data/tests/test_epoll.rb +142 -0
  127. data/tests/test_error_handler.rb +38 -0
  128. data/tests/test_exc.rb +28 -0
  129. data/tests/test_file_watch.rb +66 -0
  130. data/tests/test_fork.rb +75 -0
  131. data/tests/test_futures.rb +170 -0
  132. data/tests/test_get_sock_opt.rb +37 -0
  133. data/tests/test_handler_check.rb +35 -0
  134. data/tests/test_hc.rb +155 -0
  135. data/tests/test_httpclient.rb +233 -0
  136. data/tests/test_httpclient2.rb +128 -0
  137. data/tests/test_idle_connection.rb +25 -0
  138. data/tests/test_inactivity_timeout.rb +54 -0
  139. data/tests/test_ipv4.rb +125 -0
  140. data/tests/test_ipv6.rb +131 -0
  141. data/tests/test_iterator.rb +115 -0
  142. data/tests/test_kb.rb +28 -0
  143. data/tests/test_line_protocol.rb +33 -0
  144. data/tests/test_ltp.rb +138 -0
  145. data/tests/test_ltp2.rb +308 -0
  146. data/tests/test_many_fds.rb +22 -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 +107 -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 +128 -0
  154. data/tests/test_proxy_connection.rb +180 -0
  155. data/tests/test_pure.rb +88 -0
  156. data/tests/test_queue.rb +64 -0
  157. data/tests/test_resolver.rb +104 -0
  158. data/tests/test_running.rb +14 -0
  159. data/tests/test_sasl.rb +47 -0
  160. data/tests/test_send_file.rb +217 -0
  161. data/tests/test_servers.rb +33 -0
  162. data/tests/test_set_sock_opt.rb +39 -0
  163. data/tests/test_shutdown_hooks.rb +23 -0
  164. data/tests/test_smtpclient.rb +75 -0
  165. data/tests/test_smtpserver.rb +57 -0
  166. data/tests/test_spawn.rb +293 -0
  167. data/tests/test_ssl_args.rb +78 -0
  168. data/tests/test_ssl_dhparam.rb +83 -0
  169. data/tests/test_ssl_ecdh_curve.rb +79 -0
  170. data/tests/test_ssl_extensions.rb +49 -0
  171. data/tests/test_ssl_methods.rb +65 -0
  172. data/tests/test_ssl_protocols.rb +246 -0
  173. data/tests/test_ssl_verify.rb +126 -0
  174. data/tests/test_stomp.rb +37 -0
  175. data/tests/test_system.rb +46 -0
  176. data/tests/test_threaded_resource.rb +61 -0
  177. data/tests/test_tick_loop.rb +59 -0
  178. data/tests/test_timers.rb +123 -0
  179. data/tests/test_ud.rb +8 -0
  180. data/tests/test_unbind_reason.rb +52 -0
  181. metadata +381 -0
@@ -0,0 +1,28 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestKeyboardEvents < Test::Unit::TestCase
4
+
5
+ module KbHandler
6
+ include EM::Protocols::LineText2
7
+ def receive_line d
8
+ EM::stop if d == "STOP"
9
+ end
10
+ end
11
+
12
+ # This test doesn't actually do anything useful but is here to
13
+ # illustrate the usage. If you removed the timer and ran this test
14
+ # by itself on a console, and then typed into the console, it would
15
+ # work.
16
+ # I don't know how to get the test harness to simulate actual keystrokes.
17
+ # When someone figures that out, then we can make this a real test.
18
+ #
19
+ def test_kb
20
+ omit_if(jruby?)
21
+ omit_if(!$stdout.tty?) # don't run the test unless it stands a chance of validity.
22
+ EM.run do
23
+ EM.open_keyboard KbHandler
24
+ EM::Timer.new(1) { EM.stop }
25
+ end
26
+ end
27
+
28
+ end
@@ -0,0 +1,33 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestLineProtocol < Test::Unit::TestCase
4
+ class LineProtocolTestClass
5
+ include EM::Protocols::LineProtocol
6
+
7
+ def lines
8
+ @lines ||= []
9
+ end
10
+
11
+ def receive_line(line)
12
+ lines << line
13
+ end
14
+ end
15
+
16
+ def setup
17
+ @proto = LineProtocolTestClass.new
18
+ end
19
+
20
+ def test_simple_split_line
21
+ @proto.receive_data("this is")
22
+ assert_equal([], @proto.lines)
23
+
24
+ @proto.receive_data(" a test\n")
25
+ assert_equal(["this is a test"], @proto.lines)
26
+ end
27
+
28
+ def test_simple_lines
29
+ @proto.receive_data("aaa\nbbb\r\nccc\nddd")
30
+ assert_equal(%w(aaa bbb ccc), @proto.lines)
31
+ end
32
+
33
+ end
@@ -0,0 +1,138 @@
1
+ require 'em_test_helper'
2
+
3
+ class TestLineAndTextProtocol < Test::Unit::TestCase
4
+
5
+ class SimpleLineTest < EM::P::LineAndTextProtocol
6
+ def receive_line line
7
+ @line_buffer << line
8
+ end
9
+ end
10
+
11
+ module StopClient
12
+ def set_receive_data(&blk)
13
+ @rdb = blk
14
+ end
15
+
16
+ def receive_data data
17
+ @rdb.call(data) if @rdb
18
+ end
19
+
20
+ def unbind
21
+ EM.add_timer(0.1) { EM.stop }
22
+ end
23
+ end
24
+
25
+ def setup
26
+ @port = next_port
27
+ end
28
+
29
+ def test_simple_lines
30
+ lines_received = []
31
+ EM.run {
32
+ EM.start_server( "127.0.0.1", @port, SimpleLineTest ) do |conn|
33
+ conn.instance_eval "@line_buffer = lines_received"
34
+ end
35
+ setup_timeout
36
+
37
+ EM.connect "127.0.0.1", @port, StopClient do |c|
38
+ c.send_data "aaa\nbbb\r\nccc\n"
39
+ c.close_connection_after_writing
40
+ end
41
+ }
42
+ assert_equal( %w(aaa bbb ccc), lines_received )
43
+ end
44
+
45
+ #--------------------------------------------------------------------
46
+
47
+ class SimpleLineTest < EM::P::LineAndTextProtocol
48
+ def receive_error text
49
+ @error_message << text
50
+ end
51
+ end
52
+
53
+ def test_overlength_lines
54
+ lines_received = []
55
+ EM.run {
56
+ EM.start_server( "127.0.0.1", @port, SimpleLineTest ) do |conn|
57
+ conn.instance_eval "@error_message = lines_received"
58
+ end
59
+ setup_timeout
60
+
61
+ EM.connect "127.0.0.1", @port, StopClient do |c|
62
+ c.send_data "a" * (16*1024 + 1)
63
+ c.send_data "\n"
64
+ c.close_connection_after_writing
65
+ end
66
+
67
+ }
68
+ assert_equal( ["overlength line"], lines_received )
69
+ end
70
+
71
+
72
+ #--------------------------------------------------------------------
73
+
74
+ class LineAndTextTest < EM::P::LineAndTextProtocol
75
+ def receive_line line
76
+ if line =~ /content-length:\s*(\d+)/i
77
+ @content_length = $1.to_i
78
+ elsif line.length == 0
79
+ set_binary_mode @content_length
80
+ end
81
+ end
82
+ def receive_binary_data text
83
+ send_data "received #{text.length} bytes"
84
+ close_connection_after_writing
85
+ end
86
+ end
87
+
88
+ def test_lines_and_text
89
+ output = ''
90
+ EM.run {
91
+ EM.start_server( "127.0.0.1", @port, LineAndTextTest )
92
+ setup_timeout
93
+
94
+ EM.connect "127.0.0.1", @port, StopClient do |c|
95
+ c.set_receive_data { |data| output << data }
96
+ c.send_data "Content-length: 400\n"
97
+ c.send_data "\n"
98
+ c.send_data "A" * 400
99
+ EM.add_timer(0.1) { c.close_connection_after_writing }
100
+ end
101
+ }
102
+ assert_equal( "received 400 bytes", output )
103
+ end
104
+
105
+ #--------------------------------------------------------------------
106
+
107
+
108
+ class BinaryTextTest < EM::P::LineAndTextProtocol
109
+ def receive_line line
110
+ if line =~ /content-length:\s*(\d+)/i
111
+ set_binary_mode $1.to_i
112
+ else
113
+ raise "protocol error"
114
+ end
115
+ end
116
+ def receive_binary_data text
117
+ send_data "received #{text.length} bytes"
118
+ close_connection_after_writing
119
+ end
120
+ end
121
+
122
+ def test_binary_text
123
+ output = ''
124
+ EM.run {
125
+ EM.start_server( "127.0.0.1", @port, BinaryTextTest )
126
+ setup_timeout
127
+
128
+ EM.connect "127.0.0.1", @port, StopClient do |c|
129
+ c.set_receive_data { |data| output << data }
130
+ c.send_data "Content-length: 10000\n"
131
+ c.send_data "A" * 10000
132
+ EM.add_timer(0.1) { c.close_connection_after_writing }
133
+ end
134
+ }
135
+ assert_equal( "received 10000 bytes", output )
136
+ end
137
+
138
+ end
@@ -0,0 +1,308 @@
1
+ require '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
+
76
+ #--
77
+ # Test two lines followed by an empty line, ten bytes of binary data, then
78
+ # two more lines.
79
+
80
+ class Binary
81
+ include EM::Protocols::LineText2
82
+ attr_reader :lines, :body
83
+ def initialize *args
84
+ super
85
+ @lines = []
86
+ @body = nil
87
+ end
88
+ def receive_line ln
89
+ if ln == ""
90
+ set_text_mode 10
91
+ else
92
+ @lines << ln
93
+ end
94
+ end
95
+ def receive_binary_data data
96
+ @body = data
97
+ end
98
+ end
99
+
100
+ def test_binary
101
+ testdata = %Q(Line 1
102
+ Line 2
103
+
104
+ 0000000000Line 3
105
+ Line 4
106
+ )
107
+
108
+ a = Binary.new
109
+ a.receive_data testdata
110
+ assert_equal( ["Line 1", "Line 2", "Line 3", "Line 4"], a.lines)
111
+ assert_equal( "0000000000", a.body )
112
+
113
+ a = Binary.new
114
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
115
+ assert_equal( ["Line 1", "Line 2", "Line 3", "Line 4"], a.lines)
116
+ assert_equal( "0000000000", a.body )
117
+ end
118
+
119
+
120
+ # Test unsized binary data. The expectation is that each chunk of it
121
+ # will be passed to us as it it received.
122
+ class UnsizedBinary
123
+ include EM::Protocols::LineText2
124
+ attr_reader :n_calls, :body
125
+ def initialize *args
126
+ super
127
+ set_text_mode
128
+ end
129
+ def receive_binary_data data
130
+ @n_calls ||= 0
131
+ @n_calls += 1
132
+ (@body ||= "") << data
133
+ end
134
+ end
135
+
136
+ def test_unsized_binary
137
+ testdata = "X\0" * 1000
138
+
139
+ a = UnsizedBinary.new
140
+ a.receive_data testdata
141
+ assert_equal( 1, a.n_calls )
142
+ assert_equal( testdata, a.body )
143
+
144
+ a = UnsizedBinary.new
145
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
146
+ assert_equal( 2000, a.n_calls )
147
+ assert_equal( testdata, a.body )
148
+ end
149
+
150
+
151
+ # Test binary data with a "throw back" into line-mode.
152
+ class ThrowBack
153
+ include EM::Protocols::LineText2
154
+ attr_reader :headers
155
+ def initialize *args
156
+ super
157
+ @headers = []
158
+ @n_bytes = 0
159
+ set_text_mode
160
+ end
161
+ def receive_binary_data data
162
+ wanted = 25 - @n_bytes
163
+ will_take = if data.length > wanted
164
+ data.length - wanted
165
+ else
166
+ data.length
167
+ end
168
+ @n_bytes += will_take
169
+
170
+ if @n_bytes == 25
171
+ set_line_mode( data[will_take..-1] )
172
+ end
173
+ end
174
+ def receive_line ln
175
+ @headers << ln
176
+ end
177
+ end
178
+ def test_throw_back
179
+ testdata = "Line\n" * 10
180
+
181
+ a = ThrowBack.new
182
+ a.receive_data testdata
183
+ assert_equal( ["Line"] * 5, a.headers )
184
+
185
+ a = ThrowBack.new
186
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
187
+ assert_equal( ["Line"] * 5, a.headers )
188
+ end
189
+
190
+ # Test multi-character line delimiters.
191
+ # Also note that the test data has a "tail" with no delimiter, that will be
192
+ # discarded, but cf. the BinaryTail test.
193
+ # TODO!!! This test doesn't work in the byte-by-byte case.
194
+ class Multichar
195
+ include EM::Protocols::LineText2
196
+ attr_reader :lines
197
+ def initialize *args
198
+ super
199
+ @lines = []
200
+ set_delimiter "012"
201
+ end
202
+ def receive_line ln
203
+ @lines << ln
204
+ end
205
+ end
206
+ def test_multichar
207
+ testdata = "Line012Line012Line012Line"
208
+
209
+ a = Multichar.new
210
+ a.receive_data testdata
211
+ assert_equal( ["Line"]*3, a.lines )
212
+
213
+ a = Multichar.new
214
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
215
+ # DOESN'T WORK in this case. Multi-character delimiters are broken.
216
+ #assert_equal( ["Line"]*3, a.lines )
217
+ end
218
+
219
+ # Test a binary "tail," when a sized binary transfer doesn't complete because
220
+ # of an unbind. We get a partial result.
221
+ class BinaryTail
222
+ include EM::Protocols::LineText2
223
+ attr_reader :data
224
+ def initialize *args
225
+ super
226
+ @data = ""
227
+ set_text_mode 1000
228
+ end
229
+ def receive_binary_data data
230
+ # we expect to get all the data in one chunk, even in the byte-by-byte case,
231
+ # because sized transfers by definition give us exactly one call to
232
+ # #receive_binary_data.
233
+ @data = data
234
+ end
235
+ end
236
+ def test_binary_tail
237
+ testdata = "0" * 500
238
+
239
+ a = BinaryTail.new
240
+ a.receive_data testdata
241
+ a.unbind
242
+ assert_equal( "0" * 500, a.data )
243
+
244
+ a = BinaryTail.new
245
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
246
+ a.unbind
247
+ assert_equal( "0" * 500, a.data )
248
+ end
249
+
250
+
251
+ # Test an end-of-binary call. Arrange to receive binary data but don't bother counting it
252
+ # as it comes. Rely on getting receive_end_of_binary_data to signal the transition back to
253
+ # line mode.
254
+ # At the present time, this isn't strictly necessary with sized binary chunks because by
255
+ # definition we accumulate them and make exactly one call to receive_binary_data, but
256
+ # we may want to support a mode in the future that would break up large chunks into multiple
257
+ # calls.
258
+ class LazyBinary
259
+ include EM::Protocols::LineText2
260
+ attr_reader :data, :end
261
+ def initialize *args
262
+ super
263
+ @data = ""
264
+ set_text_mode 1000
265
+ end
266
+ def receive_binary_data data
267
+ # we expect to get all the data in one chunk, even in the byte-by-byte case,
268
+ # because sized transfers by definition give us exactly one call to
269
+ # #receive_binary_data.
270
+ @data = data
271
+ end
272
+ def receive_end_of_binary_data
273
+ @end = true
274
+ end
275
+ end
276
+ def test_receive_end_of_binary_data
277
+ testdata = "_" * 1000
278
+ a = LazyBinary.new
279
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
280
+ assert_equal( "_" * 1000, a.data )
281
+ assert( a.end )
282
+ end
283
+
284
+
285
+ # This tests a bug fix in which calling set_text_mode failed when called
286
+ # inside receive_binary_data.
287
+ #
288
+ class BinaryPair
289
+ include EM::Protocols::LineText2
290
+ attr_reader :sizes
291
+ def initialize *args
292
+ super
293
+ set_text_mode 1
294
+ @sizes = []
295
+ end
296
+ def receive_binary_data dt
297
+ @sizes << dt.length
298
+ set_text_mode( (dt.length == 1) ? 2 : 1 )
299
+ end
300
+ end
301
+ def test_binary_pairs
302
+ test_data = "123" * 5
303
+ a = BinaryPair.new
304
+ a.receive_data test_data
305
+ assert_equal( [1,2,1,2,1,2,1,2,1,2], a.sizes )
306
+ end
307
+
308
+ end