eventmachine 0.12.10-x86-mswin32-60 → 1.0.0.beta.2-x86-mswin32-60

Sign up to get free protection for your applications and to get access to all the features.
Files changed (150) hide show
  1. data/.gitignore +2 -0
  2. data/Gemfile +1 -0
  3. data/README +80 -81
  4. data/Rakefile +7 -370
  5. data/docs/COPYING +60 -60
  6. data/docs/ChangeLog +211 -211
  7. data/docs/DEFERRABLES +246 -133
  8. data/docs/EPOLL +141 -141
  9. data/docs/GNU +281 -281
  10. data/docs/INSTALL +13 -13
  11. data/docs/KEYBOARD +42 -38
  12. data/docs/LEGAL +25 -25
  13. data/docs/LIGHTWEIGHT_CONCURRENCY +130 -70
  14. data/docs/PURE_RUBY +75 -75
  15. data/docs/RELEASE_NOTES +94 -94
  16. data/docs/SMTP +4 -2
  17. data/docs/SPAWNED_PROCESSES +148 -89
  18. data/docs/TODO +8 -8
  19. data/eventmachine.gemspec +19 -26
  20. data/examples/ex_channel.rb +42 -42
  21. data/examples/ex_queue.rb +2 -2
  22. data/examples/ex_tick_loop_array.rb +15 -0
  23. data/examples/ex_tick_loop_counter.rb +32 -0
  24. data/examples/helper.rb +1 -1
  25. data/ext/binder.cpp +0 -1
  26. data/ext/cmain.cpp +36 -25
  27. data/ext/ed.cpp +104 -113
  28. data/ext/ed.h +24 -30
  29. data/ext/em.cpp +349 -283
  30. data/ext/em.h +25 -29
  31. data/ext/eventmachine.h +5 -4
  32. data/ext/extconf.rb +58 -49
  33. data/ext/fastfilereader/extconf.rb +5 -3
  34. data/ext/fastfilereader/mapper.cpp +214 -214
  35. data/ext/fastfilereader/mapper.h +59 -59
  36. data/ext/fastfilereader/rubymain.cpp +127 -127
  37. data/ext/kb.cpp +1 -3
  38. data/ext/page.cpp +107 -107
  39. data/ext/page.h +51 -51
  40. data/ext/pipe.cpp +9 -11
  41. data/ext/project.h +12 -8
  42. data/ext/rubymain.cpp +138 -104
  43. data/java/.classpath +8 -8
  44. data/java/.project +17 -17
  45. data/java/src/com/rubyeventmachine/EmReactor.java +1 -0
  46. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -40
  47. data/lib/em/buftok.rb +138 -138
  48. data/lib/em/callback.rb +25 -25
  49. data/lib/em/channel.rb +1 -1
  50. data/lib/em/connection.rb +6 -1
  51. data/lib/em/deferrable.rb +16 -2
  52. data/lib/em/file_watch.rb +53 -53
  53. data/lib/em/future.rb +61 -61
  54. data/lib/em/iterator.rb +270 -0
  55. data/lib/em/messages.rb +66 -66
  56. data/lib/em/process_watch.rb +43 -43
  57. data/lib/em/protocols.rb +1 -1
  58. data/lib/em/protocols/header_and_content.rb +138 -138
  59. data/lib/em/protocols/httpclient.rb +267 -262
  60. data/lib/em/protocols/line_protocol.rb +28 -0
  61. data/lib/em/protocols/memcache.rb +322 -322
  62. data/lib/em/protocols/postgres3.rb +247 -247
  63. data/lib/em/protocols/saslauth.rb +175 -175
  64. data/lib/em/protocols/smtpserver.rb +640 -547
  65. data/lib/em/protocols/stomp.rb +200 -200
  66. data/lib/em/protocols/tcptest.rb +52 -52
  67. data/lib/{pr_eventmachine.rb → em/pure_ruby.rb} +1013 -1022
  68. data/lib/em/queue.rb +1 -0
  69. data/lib/em/spawnable.rb +85 -85
  70. data/lib/em/streamer.rb +130 -130
  71. data/lib/em/tick_loop.rb +85 -0
  72. data/lib/em/timers.rb +2 -1
  73. data/lib/em/version.rb +1 -1
  74. data/lib/eventmachine.rb +40 -84
  75. data/lib/jeventmachine.rb +2 -1
  76. data/lib/rubyeventmachine.rb +2 -0
  77. data/setup.rb +1585 -1585
  78. data/tasks/doc.rake +30 -0
  79. data/tasks/package.rake +85 -0
  80. data/tasks/test.rake +6 -0
  81. data/tests/client.crt +31 -31
  82. data/tests/client.key +51 -51
  83. data/tests/test_attach.rb +13 -3
  84. data/tests/test_basic.rb +60 -95
  85. data/tests/test_channel.rb +3 -2
  86. data/tests/test_defer.rb +49 -47
  87. data/tests/test_deferrable.rb +35 -0
  88. data/tests/test_error_handler.rb +35 -35
  89. data/tests/test_errors.rb +82 -82
  90. data/tests/test_exc.rb +55 -55
  91. data/tests/test_file_watch.rb +49 -49
  92. data/tests/test_futures.rb +198 -198
  93. data/tests/test_handler_check.rb +36 -36
  94. data/tests/test_hc.rb +190 -218
  95. data/tests/test_httpclient.rb +227 -218
  96. data/tests/test_httpclient2.rb +3 -2
  97. data/tests/test_inactivity_timeout.rb +3 -3
  98. data/tests/test_kb.rb +60 -60
  99. data/tests/test_ltp.rb +13 -5
  100. data/tests/test_ltp2.rb +317 -317
  101. data/tests/test_next_tick.rb +1 -1
  102. data/tests/test_object_protocol.rb +36 -36
  103. data/tests/test_pending_connect_timeout.rb +2 -2
  104. data/tests/test_process_watch.rb +50 -48
  105. data/tests/test_proxy_connection.rb +52 -0
  106. data/tests/test_pure.rb +134 -125
  107. data/tests/test_queue.rb +44 -44
  108. data/tests/test_running.rb +42 -42
  109. data/tests/test_sasl.rb +72 -72
  110. data/tests/test_send_file.rb +251 -242
  111. data/tests/test_servers.rb +76 -76
  112. data/tests/test_smtpclient.rb +83 -83
  113. data/tests/test_smtpserver.rb +85 -85
  114. data/tests/test_spawn.rb +322 -322
  115. data/tests/test_ssl_methods.rb +49 -49
  116. data/tests/test_ssl_verify.rb +82 -82
  117. data/tests/test_tick_loop.rb +59 -0
  118. data/tests/test_timers.rb +13 -15
  119. data/tests/test_ud.rb +36 -36
  120. data/tests/testem.rb +31 -31
  121. metadata +66 -51
  122. data/ext/cplusplus.cpp +0 -202
  123. data/ext/emwin.cpp +0 -300
  124. data/ext/emwin.h +0 -94
  125. data/ext/epoll.cpp +0 -26
  126. data/ext/epoll.h +0 -25
  127. data/ext/eventmachine_cpp.h +0 -96
  128. data/ext/files.cpp +0 -94
  129. data/ext/files.h +0 -65
  130. data/ext/sigs.cpp +0 -89
  131. data/ext/sigs.h +0 -32
  132. data/java/src/com/rubyeventmachine/application/Application.java +0 -194
  133. data/java/src/com/rubyeventmachine/application/Connection.java +0 -74
  134. data/java/src/com/rubyeventmachine/application/ConnectionFactory.java +0 -37
  135. data/java/src/com/rubyeventmachine/application/DefaultConnectionFactory.java +0 -46
  136. data/java/src/com/rubyeventmachine/application/PeriodicTimer.java +0 -38
  137. data/java/src/com/rubyeventmachine/application/Timer.java +0 -54
  138. data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +0 -109
  139. data/java/src/com/rubyeventmachine/tests/ConnectTest.java +0 -148
  140. data/java/src/com/rubyeventmachine/tests/EMTest.java +0 -80
  141. data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +0 -53
  142. data/java/src/com/rubyeventmachine/tests/TestServers.java +0 -75
  143. data/java/src/com/rubyeventmachine/tests/TestTimers.java +0 -90
  144. data/lib/evma.rb +0 -32
  145. data/lib/evma/callback.rb +0 -32
  146. data/lib/evma/container.rb +0 -75
  147. data/lib/evma/factory.rb +0 -77
  148. data/lib/evma/protocol.rb +0 -87
  149. data/lib/evma/reactor.rb +0 -48
  150. data/web/whatis +0 -7
@@ -37,14 +37,14 @@ class TestInactivityTimeout < Test::Unit::TestCase
37
37
  EM.run {
38
38
  EM.heartbeat_interval = 0.1
39
39
  EM.start_server("127.0.0.1", 12345)
40
- EM.add_timer(0.2) {
40
+ EM.add_timer(0.1) {
41
41
  $start = Time.now
42
42
  c = EM.connect("127.0.0.1", 12345, TimeoutHandler)
43
- c.comm_inactivity_timeout = 2.5
43
+ c.comm_inactivity_timeout = 0.2
44
44
  }
45
45
  }
46
46
 
47
- assert_in_delta(2.5, (Time.now - $start), 0.3)
47
+ assert_in_delta(0.2, (Time.now - $start), 0.1)
48
48
  end
49
49
 
50
50
  end
@@ -1,60 +1,60 @@
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 TestKeyboardEvents < Test::Unit::TestCase
32
-
33
- def setup
34
- end
35
-
36
- def teardown
37
- end
38
-
39
- module KbHandler
40
- include EM::Protocols::LineText2
41
- def receive_line d
42
- EM::stop if d == "STOP"
43
- end
44
- end
45
-
46
- # This test doesn't actually do anything useful but is here to
47
- # illustrate the usage. If you removed the timer and ran this test
48
- # by itself on a console, and then typed into the console, it would
49
- # work.
50
- # I don't know how to get the test harness to simulate actual keystrokes.
51
- # When someone figures that out, then we can make this a real test.
52
- #
53
- def test_kb
54
- EM.run {
55
- EM.open_keyboard KbHandler
56
- EM::Timer.new(1) { EM.stop }
57
- } if $stdout.tty? # don't run the test unless it stands a chance of validity.
58
- end
59
-
60
- end
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 TestKeyboardEvents < Test::Unit::TestCase
32
+
33
+ def setup
34
+ end
35
+
36
+ def teardown
37
+ end
38
+
39
+ module KbHandler
40
+ include EM::Protocols::LineText2
41
+ def receive_line d
42
+ EM::stop if d == "STOP"
43
+ end
44
+ end
45
+
46
+ # This test doesn't actually do anything useful but is here to
47
+ # illustrate the usage. If you removed the timer and ran this test
48
+ # by itself on a console, and then typed into the console, it would
49
+ # work.
50
+ # I don't know how to get the test harness to simulate actual keystrokes.
51
+ # When someone figures that out, then we can make this a real test.
52
+ #
53
+ def test_kb
54
+ EM.run {
55
+ EM.open_keyboard KbHandler
56
+ EM::Timer.new(1) { EM.stop }
57
+ } if $stdout.tty? # don't run the test unless it stands a chance of validity.
58
+ end
59
+
60
+ end
@@ -55,7 +55,15 @@ class TestLineAndTextProtocol < Test::Unit::TestCase
55
55
  EM.add_timer(0.1) { EM.stop }
56
56
  end
57
57
  end
58
-
58
+
59
+ def setup_timeout(timeout = 4)
60
+ EM.schedule {
61
+ start_time = EM.current_time
62
+ EM.add_periodic_timer(0.01) {
63
+ raise "timeout" if EM.current_time - start_time >= timeout
64
+ }
65
+ }
66
+ end
59
67
 
60
68
  def test_simple_lines
61
69
  lines_received = []
@@ -63,7 +71,7 @@ class TestLineAndTextProtocol < Test::Unit::TestCase
63
71
  EventMachine.start_server( TestHost, TestPort, SimpleLineTest ) do |conn|
64
72
  conn.instance_eval "@line_buffer = lines_received"
65
73
  end
66
- EventMachine.add_timer(4) {assert(false, "test timed out")}
74
+ setup_timeout
67
75
 
68
76
  EventMachine.connect TestHost, TestPort, StopClient do |c|
69
77
  c.send_data "aaa\nbbb\r\nccc\n"
@@ -87,7 +95,7 @@ class TestLineAndTextProtocol < Test::Unit::TestCase
87
95
  EventMachine.start_server( TestHost, TestPort, SimpleLineTest ) do |conn|
88
96
  conn.instance_eval "@error_message = lines_received"
89
97
  end
90
- EventMachine.add_timer(4) {assert(false, "test timed out")}
98
+ setup_timeout
91
99
 
92
100
  EventMachine.connect TestHost, TestPort, StopClient do |c|
93
101
  c.send_data "a" * (16*1024 + 1)
@@ -126,7 +134,7 @@ class TestLineAndTextProtocol < Test::Unit::TestCase
126
134
  EventMachine.start_server( TestHost, TestPort, LineAndTextTest ) do |conn|
127
135
  conn.instance_eval "@lines = lines_received; @text = text_received"
128
136
  end
129
- EventMachine.add_timer(4) {assert(false, "test timed out")}
137
+ setup_timeout
130
138
 
131
139
  EventMachine.connect TestHost, TestPort, StopClient do |c|
132
140
  c.set_receive_data { |data| output << data }
@@ -166,7 +174,7 @@ class TestLineAndTextProtocol < Test::Unit::TestCase
166
174
  EventMachine.start_server( TestHost, TestPort, BinaryTextTest ) do |conn|
167
175
  conn.instance_eval "@lines = lines_received; @text = text_received"
168
176
  end
169
- EventMachine.add_timer(4) {assert(false, "test timed out")}
177
+ setup_timeout
170
178
 
171
179
  EventMachine.connect TestHost, TestPort, StopClient do |c|
172
180
  c.set_receive_data { |data| output << data }
@@ -1,317 +1,317 @@
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
-
28
- $:.unshift "../lib"
29
- require 'eventmachine'
30
- require 'test/unit'
31
-
32
- # TODO!!! Need tests for overlength headers and text bodies.
33
-
34
- class TestLineText2 < Test::Unit::TestCase
35
-
36
- # Run each of these tests two ways: passing in the whole test-dataset in one chunk,
37
- # and passing it in one character at a time.
38
-
39
- class Basic
40
- include EM::Protocols::LineText2
41
- attr_reader :lines
42
- def receive_line line
43
- (@lines ||= []) << line
44
- end
45
- end
46
- def test_basic
47
- testdata = "Line 1\nLine 2\r\nLine 3\n"
48
-
49
- a = Basic.new
50
- a.receive_data testdata
51
- assert_equal( ["Line 1", "Line 2", "Line 3"], a.lines )
52
-
53
- a = Basic.new
54
- testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
55
- assert_equal( ["Line 1", "Line 2", "Line 3"], a.lines )
56
- end
57
-
58
- class ChangeDelimiter
59
- include EM::Protocols::LineText2
60
- attr_reader :lines
61
- def initialize *args
62
- super
63
- @delim = "A"
64
- set_delimiter @delim
65
- end
66
- def receive_line line
67
- (@lines ||= []) << line
68
- set_delimiter( @delim.succ! )
69
- end
70
- end
71
-
72
- def test_change_delimiter
73
- testdata = %Q(LineaALinebBLinecCLinedD)
74
-
75
- a = ChangeDelimiter.new
76
- a.receive_data testdata
77
- assert_equal( ["Linea", "Lineb", "Linec", "Lined"], a.lines )
78
-
79
- a = ChangeDelimiter.new
80
- testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
81
- assert_equal( ["Linea", "Lineb", "Linec", "Lined"], a.lines )
82
- end
83
-
84
-
85
- #--
86
- # Test two lines followed by an empty line, ten bytes of binary data, then
87
- # two more lines.
88
-
89
- class Binary
90
- include EM::Protocols::LineText2
91
- attr_reader :lines, :body
92
- def initialize *args
93
- super
94
- @lines = []
95
- @body = nil
96
- end
97
- def receive_line ln
98
- if ln == ""
99
- set_text_mode 10
100
- else
101
- @lines << ln
102
- end
103
- end
104
- def receive_binary_data data
105
- @body = data
106
- end
107
- end
108
-
109
- def test_binary
110
- testdata = %Q(Line 1
111
- Line 2
112
-
113
- 0000000000Line 3
114
- Line 4
115
- )
116
-
117
- a = Binary.new
118
- a.receive_data testdata
119
- assert_equal( ["Line 1", "Line 2", "Line 3", "Line 4"], a.lines)
120
- assert_equal( "0000000000", a.body )
121
-
122
- a = Binary.new
123
- testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
124
- assert_equal( ["Line 1", "Line 2", "Line 3", "Line 4"], a.lines)
125
- assert_equal( "0000000000", a.body )
126
- end
127
-
128
-
129
- # Test unsized binary data. The expectation is that each chunk of it
130
- # will be passed to us as it it received.
131
- class UnsizedBinary
132
- include EM::Protocols::LineText2
133
- attr_reader :n_calls, :body
134
- def initialize *args
135
- super
136
- set_text_mode
137
- end
138
- def receive_binary_data data
139
- @n_calls ||= 0
140
- @n_calls += 1
141
- (@body ||= "") << data
142
- end
143
- end
144
-
145
- def test_unsized_binary
146
- testdata = "X\0" * 1000
147
-
148
- a = UnsizedBinary.new
149
- a.receive_data testdata
150
- assert_equal( 1, a.n_calls )
151
- assert_equal( testdata, a.body )
152
-
153
- a = UnsizedBinary.new
154
- testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
155
- assert_equal( 2000, a.n_calls )
156
- assert_equal( testdata, a.body )
157
- end
158
-
159
-
160
- # Test binary data with a "throw back" into line-mode.
161
- class ThrowBack
162
- include EM::Protocols::LineText2
163
- attr_reader :headers
164
- def initialize *args
165
- super
166
- @headers = []
167
- @n_bytes = 0
168
- set_text_mode
169
- end
170
- def receive_binary_data data
171
- wanted = 25 - @n_bytes
172
- will_take = if data.length > wanted
173
- data.length - wanted
174
- else
175
- data.length
176
- end
177
- @n_bytes += will_take
178
-
179
- if @n_bytes == 25
180
- set_line_mode( data[will_take..-1] )
181
- end
182
- end
183
- def receive_line ln
184
- @headers << ln
185
- end
186
- end
187
- def test_throw_back
188
- testdata = "Line\n" * 10
189
-
190
- a = ThrowBack.new
191
- a.receive_data testdata
192
- assert_equal( ["Line"] * 5, a.headers )
193
-
194
- a = ThrowBack.new
195
- testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
196
- assert_equal( ["Line"] * 5, a.headers )
197
- end
198
-
199
- # Test multi-character line delimiters.
200
- # Also note that the test data has a "tail" with no delimiter, that will be
201
- # discarded, but cf. the BinaryTail test.
202
- # TODO!!! This test doesn't work in the byte-by-byte case.
203
- class Multichar
204
- include EM::Protocols::LineText2
205
- attr_reader :lines
206
- def initialize *args
207
- super
208
- @lines = []
209
- set_delimiter "012"
210
- end
211
- def receive_line ln
212
- @lines << ln
213
- end
214
- end
215
- def test_multichar
216
- testdata = "Line012Line012Line012Line"
217
-
218
- a = Multichar.new
219
- a.receive_data testdata
220
- assert_equal( ["Line"]*3, a.lines )
221
-
222
- a = Multichar.new
223
- testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
224
- # DOESN'T WORK in this case. Multi-character delimiters are broken.
225
- #assert_equal( ["Line"]*3, a.lines )
226
- end
227
-
228
- # Test a binary "tail," when a sized binary transfer doesn't complete because
229
- # of an unbind. We get a partial result.
230
- class BinaryTail
231
- include EM::Protocols::LineText2
232
- attr_reader :data
233
- def initialize *args
234
- super
235
- @data = ""
236
- set_text_mode 1000
237
- end
238
- def receive_binary_data data
239
- # we expect to get all the data in one chunk, even in the byte-by-byte case,
240
- # because sized transfers by definition give us exactly one call to
241
- # #receive_binary_data.
242
- @data = data
243
- end
244
- end
245
- def test_binary_tail
246
- testdata = "0" * 500
247
-
248
- a = BinaryTail.new
249
- a.receive_data testdata
250
- a.unbind
251
- assert_equal( "0" * 500, a.data )
252
-
253
- a = BinaryTail.new
254
- testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
255
- a.unbind
256
- assert_equal( "0" * 500, a.data )
257
- end
258
-
259
-
260
- # Test an end-of-binary call. Arrange to receive binary data but don't bother counting it
261
- # as it comes. Rely on getting receive_end_of_binary_data to signal the transition back to
262
- # line mode.
263
- # At the present time, this isn't strictly necessary with sized binary chunks because by
264
- # definition we accumulate them and make exactly one call to receive_binary_data, but
265
- # we may want to support a mode in the future that would break up large chunks into multiple
266
- # calls.
267
- class LazyBinary
268
- include EM::Protocols::LineText2
269
- attr_reader :data, :end
270
- def initialize *args
271
- super
272
- @data = ""
273
- set_text_mode 1000
274
- end
275
- def receive_binary_data data
276
- # we expect to get all the data in one chunk, even in the byte-by-byte case,
277
- # because sized transfers by definition give us exactly one call to
278
- # #receive_binary_data.
279
- @data = data
280
- end
281
- def receive_end_of_binary_data
282
- @end = true
283
- end
284
- end
285
- def test_receive_end_of_binary_data
286
- testdata = "_" * 1000
287
- a = LazyBinary.new
288
- testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
289
- assert_equal( "_" * 1000, a.data )
290
- assert( a.end )
291
- end
292
-
293
-
294
- # This tests a bug fix in which calling set_text_mode failed when called
295
- # inside receive_binary_data.
296
- #
297
- class BinaryPair
298
- include EM::Protocols::LineText2
299
- attr_reader :sizes
300
- def initialize *args
301
- super
302
- set_text_mode 1
303
- @sizes = []
304
- end
305
- def receive_binary_data dt
306
- @sizes << dt.length
307
- set_text_mode( (dt.length == 1) ? 2 : 1 )
308
- end
309
- end
310
- def test_binary_pairs
311
- test_data = "123" * 5
312
- a = BinaryPair.new
313
- a.receive_data test_data
314
- assert_equal( [1,2,1,2,1,2,1,2,1,2], a.sizes )
315
- end
316
-
317
- end
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
+
28
+ $:.unshift "../lib"
29
+ require 'eventmachine'
30
+ require 'test/unit'
31
+
32
+ # TODO!!! Need tests for overlength headers and text bodies.
33
+
34
+ class TestLineText2 < Test::Unit::TestCase
35
+
36
+ # Run each of these tests two ways: passing in the whole test-dataset in one chunk,
37
+ # and passing it in one character at a time.
38
+
39
+ class Basic
40
+ include EM::Protocols::LineText2
41
+ attr_reader :lines
42
+ def receive_line line
43
+ (@lines ||= []) << line
44
+ end
45
+ end
46
+ def test_basic
47
+ testdata = "Line 1\nLine 2\r\nLine 3\n"
48
+
49
+ a = Basic.new
50
+ a.receive_data testdata
51
+ assert_equal( ["Line 1", "Line 2", "Line 3"], a.lines )
52
+
53
+ a = Basic.new
54
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
55
+ assert_equal( ["Line 1", "Line 2", "Line 3"], a.lines )
56
+ end
57
+
58
+ class ChangeDelimiter
59
+ include EM::Protocols::LineText2
60
+ attr_reader :lines
61
+ def initialize *args
62
+ super
63
+ @delim = "A"
64
+ set_delimiter @delim
65
+ end
66
+ def receive_line line
67
+ (@lines ||= []) << line
68
+ set_delimiter( @delim.succ! )
69
+ end
70
+ end
71
+
72
+ def test_change_delimiter
73
+ testdata = %Q(LineaALinebBLinecCLinedD)
74
+
75
+ a = ChangeDelimiter.new
76
+ a.receive_data testdata
77
+ assert_equal( ["Linea", "Lineb", "Linec", "Lined"], a.lines )
78
+
79
+ a = ChangeDelimiter.new
80
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
81
+ assert_equal( ["Linea", "Lineb", "Linec", "Lined"], a.lines )
82
+ end
83
+
84
+
85
+ #--
86
+ # Test two lines followed by an empty line, ten bytes of binary data, then
87
+ # two more lines.
88
+
89
+ class Binary
90
+ include EM::Protocols::LineText2
91
+ attr_reader :lines, :body
92
+ def initialize *args
93
+ super
94
+ @lines = []
95
+ @body = nil
96
+ end
97
+ def receive_line ln
98
+ if ln == ""
99
+ set_text_mode 10
100
+ else
101
+ @lines << ln
102
+ end
103
+ end
104
+ def receive_binary_data data
105
+ @body = data
106
+ end
107
+ end
108
+
109
+ def test_binary
110
+ testdata = %Q(Line 1
111
+ Line 2
112
+
113
+ 0000000000Line 3
114
+ Line 4
115
+ )
116
+
117
+ a = Binary.new
118
+ a.receive_data testdata
119
+ assert_equal( ["Line 1", "Line 2", "Line 3", "Line 4"], a.lines)
120
+ assert_equal( "0000000000", a.body )
121
+
122
+ a = Binary.new
123
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
124
+ assert_equal( ["Line 1", "Line 2", "Line 3", "Line 4"], a.lines)
125
+ assert_equal( "0000000000", a.body )
126
+ end
127
+
128
+
129
+ # Test unsized binary data. The expectation is that each chunk of it
130
+ # will be passed to us as it it received.
131
+ class UnsizedBinary
132
+ include EM::Protocols::LineText2
133
+ attr_reader :n_calls, :body
134
+ def initialize *args
135
+ super
136
+ set_text_mode
137
+ end
138
+ def receive_binary_data data
139
+ @n_calls ||= 0
140
+ @n_calls += 1
141
+ (@body ||= "") << data
142
+ end
143
+ end
144
+
145
+ def test_unsized_binary
146
+ testdata = "X\0" * 1000
147
+
148
+ a = UnsizedBinary.new
149
+ a.receive_data testdata
150
+ assert_equal( 1, a.n_calls )
151
+ assert_equal( testdata, a.body )
152
+
153
+ a = UnsizedBinary.new
154
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
155
+ assert_equal( 2000, a.n_calls )
156
+ assert_equal( testdata, a.body )
157
+ end
158
+
159
+
160
+ # Test binary data with a "throw back" into line-mode.
161
+ class ThrowBack
162
+ include EM::Protocols::LineText2
163
+ attr_reader :headers
164
+ def initialize *args
165
+ super
166
+ @headers = []
167
+ @n_bytes = 0
168
+ set_text_mode
169
+ end
170
+ def receive_binary_data data
171
+ wanted = 25 - @n_bytes
172
+ will_take = if data.length > wanted
173
+ data.length - wanted
174
+ else
175
+ data.length
176
+ end
177
+ @n_bytes += will_take
178
+
179
+ if @n_bytes == 25
180
+ set_line_mode( data[will_take..-1] )
181
+ end
182
+ end
183
+ def receive_line ln
184
+ @headers << ln
185
+ end
186
+ end
187
+ def test_throw_back
188
+ testdata = "Line\n" * 10
189
+
190
+ a = ThrowBack.new
191
+ a.receive_data testdata
192
+ assert_equal( ["Line"] * 5, a.headers )
193
+
194
+ a = ThrowBack.new
195
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
196
+ assert_equal( ["Line"] * 5, a.headers )
197
+ end
198
+
199
+ # Test multi-character line delimiters.
200
+ # Also note that the test data has a "tail" with no delimiter, that will be
201
+ # discarded, but cf. the BinaryTail test.
202
+ # TODO!!! This test doesn't work in the byte-by-byte case.
203
+ class Multichar
204
+ include EM::Protocols::LineText2
205
+ attr_reader :lines
206
+ def initialize *args
207
+ super
208
+ @lines = []
209
+ set_delimiter "012"
210
+ end
211
+ def receive_line ln
212
+ @lines << ln
213
+ end
214
+ end
215
+ def test_multichar
216
+ testdata = "Line012Line012Line012Line"
217
+
218
+ a = Multichar.new
219
+ a.receive_data testdata
220
+ assert_equal( ["Line"]*3, a.lines )
221
+
222
+ a = Multichar.new
223
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
224
+ # DOESN'T WORK in this case. Multi-character delimiters are broken.
225
+ #assert_equal( ["Line"]*3, a.lines )
226
+ end
227
+
228
+ # Test a binary "tail," when a sized binary transfer doesn't complete because
229
+ # of an unbind. We get a partial result.
230
+ class BinaryTail
231
+ include EM::Protocols::LineText2
232
+ attr_reader :data
233
+ def initialize *args
234
+ super
235
+ @data = ""
236
+ set_text_mode 1000
237
+ end
238
+ def receive_binary_data data
239
+ # we expect to get all the data in one chunk, even in the byte-by-byte case,
240
+ # because sized transfers by definition give us exactly one call to
241
+ # #receive_binary_data.
242
+ @data = data
243
+ end
244
+ end
245
+ def test_binary_tail
246
+ testdata = "0" * 500
247
+
248
+ a = BinaryTail.new
249
+ a.receive_data testdata
250
+ a.unbind
251
+ assert_equal( "0" * 500, a.data )
252
+
253
+ a = BinaryTail.new
254
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
255
+ a.unbind
256
+ assert_equal( "0" * 500, a.data )
257
+ end
258
+
259
+
260
+ # Test an end-of-binary call. Arrange to receive binary data but don't bother counting it
261
+ # as it comes. Rely on getting receive_end_of_binary_data to signal the transition back to
262
+ # line mode.
263
+ # At the present time, this isn't strictly necessary with sized binary chunks because by
264
+ # definition we accumulate them and make exactly one call to receive_binary_data, but
265
+ # we may want to support a mode in the future that would break up large chunks into multiple
266
+ # calls.
267
+ class LazyBinary
268
+ include EM::Protocols::LineText2
269
+ attr_reader :data, :end
270
+ def initialize *args
271
+ super
272
+ @data = ""
273
+ set_text_mode 1000
274
+ end
275
+ def receive_binary_data data
276
+ # we expect to get all the data in one chunk, even in the byte-by-byte case,
277
+ # because sized transfers by definition give us exactly one call to
278
+ # #receive_binary_data.
279
+ @data = data
280
+ end
281
+ def receive_end_of_binary_data
282
+ @end = true
283
+ end
284
+ end
285
+ def test_receive_end_of_binary_data
286
+ testdata = "_" * 1000
287
+ a = LazyBinary.new
288
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
289
+ assert_equal( "_" * 1000, a.data )
290
+ assert( a.end )
291
+ end
292
+
293
+
294
+ # This tests a bug fix in which calling set_text_mode failed when called
295
+ # inside receive_binary_data.
296
+ #
297
+ class BinaryPair
298
+ include EM::Protocols::LineText2
299
+ attr_reader :sizes
300
+ def initialize *args
301
+ super
302
+ set_text_mode 1
303
+ @sizes = []
304
+ end
305
+ def receive_binary_data dt
306
+ @sizes << dt.length
307
+ set_text_mode( (dt.length == 1) ? 2 : 1 )
308
+ end
309
+ end
310
+ def test_binary_pairs
311
+ test_data = "123" * 5
312
+ a = BinaryPair.new
313
+ a.receive_data test_data
314
+ assert_equal( [1,2,1,2,1,2,1,2,1,2], a.sizes )
315
+ end
316
+
317
+ end