OSDN Git Service

Replaced mongrel with thin
[redminele/redminele.git] / ruby / lib / ruby / gems / 1.8 / gems / eventmachine-0.12.10-x86-mswin32-60 / tests / test_ltp2.rb
1 # $Id$\r
2 #\r
3 # Author:: Francis Cianfrocca (gmail: blackhedd)\r
4 # Homepage::  http://rubyeventmachine.com\r
5 # Date:: 8 April 2006\r
6\r
7 # See EventMachine and EventMachine::Connection for documentation and\r
8 # usage examples.\r
9 #\r
10 #----------------------------------------------------------------------------\r
11 #\r
12 # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.\r
13 # Gmail: blackhedd\r
14\r
15 # This program is free software; you can redistribute it and/or modify\r
16 # it under the terms of either: 1) the GNU General Public License\r
17 # as published by the Free Software Foundation; either version 2 of the\r
18 # License, or (at your option) any later version; or 2) Ruby's License.\r
19\r
20 # See the file COPYING for complete licensing information.\r
21 #\r
22 #---------------------------------------------------------------------------\r
23 #\r
24 #\r
25 #\r
26 #\r
27 \r
28 $:.unshift "../lib"\r
29 require 'eventmachine'\r
30 require 'test/unit'\r
31 \r
32 # TODO!!! Need tests for overlength headers and text bodies.\r
33 \r
34 class TestLineText2 < Test::Unit::TestCase\r
35 \r
36   # Run each of these tests two ways: passing in the whole test-dataset in one chunk,\r
37   # and passing it in one character at a time.\r
38 \r
39   class Basic\r
40     include EM::Protocols::LineText2\r
41     attr_reader :lines\r
42     def receive_line line\r
43       (@lines ||= []) << line\r
44     end\r
45   end\r
46   def test_basic\r
47     testdata = "Line 1\nLine 2\r\nLine 3\n"\r
48 \r
49     a = Basic.new\r
50     a.receive_data testdata\r
51     assert_equal( ["Line 1", "Line 2", "Line 3"], a.lines )\r
52 \r
53     a = Basic.new\r
54     testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }\r
55     assert_equal( ["Line 1", "Line 2", "Line 3"], a.lines )\r
56   end\r
57 \r
58   class ChangeDelimiter\r
59     include EM::Protocols::LineText2\r
60     attr_reader :lines\r
61     def initialize *args\r
62       super\r
63       @delim = "A"\r
64       set_delimiter @delim\r
65     end\r
66     def receive_line line\r
67       (@lines ||= []) << line\r
68       set_delimiter( @delim.succ! )\r
69     end\r
70   end\r
71 \r
72   def test_change_delimiter\r
73     testdata = %Q(LineaALinebBLinecCLinedD)\r
74 \r
75     a = ChangeDelimiter.new\r
76     a.receive_data testdata\r
77     assert_equal( ["Linea", "Lineb", "Linec", "Lined"], a.lines )\r
78 \r
79     a = ChangeDelimiter.new\r
80     testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }\r
81     assert_equal( ["Linea", "Lineb", "Linec", "Lined"], a.lines )\r
82   end\r
83 \r
84 \r
85   #--\r
86   # Test two lines followed by an empty line, ten bytes of binary data, then\r
87   # two more lines.\r
88 \r
89   class Binary\r
90     include EM::Protocols::LineText2\r
91     attr_reader :lines, :body\r
92     def initialize *args\r
93       super\r
94       @lines = []\r
95       @body = nil\r
96     end\r
97     def receive_line ln\r
98       if ln == ""\r
99         set_text_mode 10\r
100       else\r
101         @lines << ln\r
102       end\r
103     end\r
104     def receive_binary_data data\r
105       @body = data\r
106     end\r
107   end\r
108 \r
109   def test_binary\r
110     testdata = %Q(Line 1\r
111 Line 2\r
112 \r
113 0000000000Line 3\r
114 Line 4\r
115 )\r
116 \r
117     a = Binary.new\r
118     a.receive_data testdata\r
119     assert_equal( ["Line 1", "Line 2", "Line 3", "Line 4"], a.lines)\r
120     assert_equal( "0000000000", a.body )\r
121 \r
122     a = Binary.new\r
123     testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }\r
124     assert_equal( ["Line 1", "Line 2", "Line 3", "Line 4"], a.lines)\r
125     assert_equal( "0000000000", a.body )\r
126   end\r
127 \r
128 \r
129   # Test unsized binary data. The expectation is that each chunk of it\r
130   # will be passed to us as it it received.\r
131   class UnsizedBinary\r
132     include EM::Protocols::LineText2\r
133     attr_reader :n_calls, :body\r
134     def initialize *args\r
135       super\r
136       set_text_mode\r
137     end\r
138     def receive_binary_data data\r
139       @n_calls ||= 0\r
140       @n_calls += 1\r
141       (@body ||= "") << data\r
142     end\r
143   end\r
144 \r
145   def test_unsized_binary\r
146     testdata = "X\0" * 1000\r
147 \r
148     a = UnsizedBinary.new\r
149     a.receive_data testdata\r
150     assert_equal( 1, a.n_calls )\r
151     assert_equal( testdata, a.body )\r
152 \r
153     a = UnsizedBinary.new\r
154     testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }\r
155     assert_equal( 2000, a.n_calls )\r
156     assert_equal( testdata, a.body )\r
157   end\r
158 \r
159 \r
160   # Test binary data with a "throw back" into line-mode.\r
161   class ThrowBack\r
162     include EM::Protocols::LineText2\r
163     attr_reader :headers\r
164     def initialize *args\r
165       super\r
166       @headers = []\r
167       @n_bytes = 0\r
168       set_text_mode\r
169     end\r
170     def receive_binary_data data\r
171       wanted = 25 - @n_bytes\r
172       will_take = if data.length > wanted\r
173                     data.length - wanted\r
174                   else\r
175                     data.length\r
176                   end\r
177       @n_bytes += will_take\r
178 \r
179       if @n_bytes == 25\r
180         set_line_mode( data[will_take..-1] )\r
181       end\r
182     end\r
183     def receive_line ln\r
184       @headers << ln\r
185     end\r
186   end\r
187   def test_throw_back\r
188     testdata = "Line\n" * 10\r
189 \r
190     a = ThrowBack.new\r
191     a.receive_data testdata\r
192     assert_equal( ["Line"] * 5, a.headers )\r
193 \r
194     a = ThrowBack.new\r
195     testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }\r
196     assert_equal( ["Line"] * 5, a.headers )\r
197   end\r
198 \r
199   # Test multi-character line delimiters.\r
200   # Also note that the test data has a "tail" with no delimiter, that will be\r
201   # discarded, but cf. the BinaryTail test.\r
202   # TODO!!! This test doesn't work in the byte-by-byte case.\r
203   class Multichar\r
204     include EM::Protocols::LineText2\r
205     attr_reader :lines\r
206     def initialize *args\r
207       super\r
208       @lines = []\r
209       set_delimiter "012"\r
210     end\r
211     def receive_line ln\r
212       @lines << ln\r
213     end\r
214   end\r
215   def test_multichar\r
216     testdata = "Line012Line012Line012Line"\r
217 \r
218     a = Multichar.new\r
219     a.receive_data testdata\r
220     assert_equal( ["Line"]*3, a.lines )\r
221 \r
222     a = Multichar.new\r
223     testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }\r
224     # DOESN'T WORK in this case. Multi-character delimiters are broken.\r
225     #assert_equal( ["Line"]*3, a.lines )\r
226   end\r
227 \r
228   # Test a binary "tail," when a sized binary transfer doesn't complete because\r
229   # of an unbind. We get a partial result.\r
230   class BinaryTail\r
231     include EM::Protocols::LineText2\r
232     attr_reader :data\r
233     def initialize *args\r
234       super\r
235       @data = ""\r
236       set_text_mode 1000\r
237     end\r
238     def receive_binary_data data\r
239       # we expect to get all the data in one chunk, even in the byte-by-byte case,\r
240       # because sized transfers by definition give us exactly one call to\r
241       # #receive_binary_data.\r
242       @data = data\r
243     end\r
244   end\r
245   def test_binary_tail\r
246     testdata = "0" * 500\r
247 \r
248     a = BinaryTail.new\r
249     a.receive_data testdata\r
250     a.unbind\r
251     assert_equal( "0" * 500, a.data )\r
252 \r
253     a = BinaryTail.new\r
254     testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }\r
255     a.unbind\r
256     assert_equal( "0" * 500, a.data )\r
257   end\r
258 \r
259 \r
260   # Test an end-of-binary call. Arrange to receive binary data but don't bother counting it\r
261   # as it comes. Rely on getting receive_end_of_binary_data to signal the transition back to\r
262   # line mode.\r
263   # At the present time, this isn't strictly necessary with sized binary chunks because by\r
264   # definition we accumulate them and make exactly one call to receive_binary_data, but\r
265   # we may want to support a mode in the future that would break up large chunks into multiple\r
266   # calls.\r
267   class LazyBinary\r
268     include EM::Protocols::LineText2\r
269     attr_reader :data, :end\r
270     def initialize *args\r
271       super\r
272       @data = ""\r
273       set_text_mode 1000\r
274     end\r
275     def receive_binary_data data\r
276       # we expect to get all the data in one chunk, even in the byte-by-byte case,\r
277       # because sized transfers by definition give us exactly one call to\r
278       # #receive_binary_data.\r
279       @data = data\r
280     end\r
281     def receive_end_of_binary_data\r
282       @end = true\r
283     end\r
284   end\r
285   def test_receive_end_of_binary_data\r
286     testdata = "_" * 1000\r
287     a = LazyBinary.new\r
288     testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }\r
289     assert_equal( "_" * 1000, a.data )\r
290     assert( a.end )\r
291   end\r
292 \r
293 \r
294   # This tests a bug fix in which calling set_text_mode failed when called\r
295   # inside receive_binary_data.\r
296   #\r
297   class BinaryPair\r
298     include EM::Protocols::LineText2\r
299     attr_reader :sizes\r
300     def initialize *args\r
301       super\r
302       set_text_mode 1\r
303       @sizes = []\r
304     end\r
305     def receive_binary_data dt\r
306       @sizes <<  dt.length\r
307       set_text_mode( (dt.length == 1) ? 2 : 1 )\r
308     end\r
309   end\r
310   def test_binary_pairs\r
311     test_data = "123" * 5\r
312     a = BinaryPair.new\r
313     a.receive_data test_data\r
314     assert_equal( [1,2,1,2,1,2,1,2,1,2], a.sizes )\r
315   end\r
316 \r
317 end\r