eventmachine-le 1.1.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (129) hide show
  1. data/.gitignore +21 -0
  2. data/.yardopts +7 -0
  3. data/GNU +281 -0
  4. data/LICENSE +60 -0
  5. data/README.md +80 -0
  6. data/Rakefile +19 -0
  7. data/eventmachine-le.gemspec +42 -0
  8. data/ext/binder.cpp +124 -0
  9. data/ext/binder.h +46 -0
  10. data/ext/cmain.cpp +841 -0
  11. data/ext/ed.cpp +1995 -0
  12. data/ext/ed.h +424 -0
  13. data/ext/em.cpp +2377 -0
  14. data/ext/em.h +243 -0
  15. data/ext/eventmachine.h +126 -0
  16. data/ext/extconf.rb +166 -0
  17. data/ext/fastfilereader/extconf.rb +94 -0
  18. data/ext/fastfilereader/mapper.cpp +214 -0
  19. data/ext/fastfilereader/mapper.h +59 -0
  20. data/ext/fastfilereader/rubymain.cpp +127 -0
  21. data/ext/kb.cpp +79 -0
  22. data/ext/page.cpp +107 -0
  23. data/ext/page.h +51 -0
  24. data/ext/pipe.cpp +347 -0
  25. data/ext/project.h +155 -0
  26. data/ext/rubymain.cpp +1269 -0
  27. data/ext/ssl.cpp +468 -0
  28. data/ext/ssl.h +94 -0
  29. data/lib/em/buftok.rb +110 -0
  30. data/lib/em/callback.rb +58 -0
  31. data/lib/em/channel.rb +64 -0
  32. data/lib/em/completion.rb +304 -0
  33. data/lib/em/connection.rb +728 -0
  34. data/lib/em/deferrable.rb +210 -0
  35. data/lib/em/deferrable/pool.rb +2 -0
  36. data/lib/em/file_watch.rb +73 -0
  37. data/lib/em/future.rb +61 -0
  38. data/lib/em/iterator.rb +313 -0
  39. data/lib/em/messages.rb +66 -0
  40. data/lib/em/pool.rb +151 -0
  41. data/lib/em/process_watch.rb +45 -0
  42. data/lib/em/processes.rb +123 -0
  43. data/lib/em/protocols.rb +37 -0
  44. data/lib/em/protocols/header_and_content.rb +138 -0
  45. data/lib/em/protocols/httpclient.rb +279 -0
  46. data/lib/em/protocols/httpclient2.rb +600 -0
  47. data/lib/em/protocols/line_and_text.rb +125 -0
  48. data/lib/em/protocols/line_protocol.rb +29 -0
  49. data/lib/em/protocols/linetext2.rb +161 -0
  50. data/lib/em/protocols/memcache.rb +331 -0
  51. data/lib/em/protocols/object_protocol.rb +46 -0
  52. data/lib/em/protocols/postgres3.rb +246 -0
  53. data/lib/em/protocols/saslauth.rb +175 -0
  54. data/lib/em/protocols/smtpclient.rb +365 -0
  55. data/lib/em/protocols/smtpserver.rb +663 -0
  56. data/lib/em/protocols/socks4.rb +66 -0
  57. data/lib/em/protocols/stomp.rb +202 -0
  58. data/lib/em/protocols/tcptest.rb +54 -0
  59. data/lib/em/queue.rb +71 -0
  60. data/lib/em/resolver.rb +195 -0
  61. data/lib/em/spawnable.rb +84 -0
  62. data/lib/em/streamer.rb +118 -0
  63. data/lib/em/threaded_resource.rb +90 -0
  64. data/lib/em/tick_loop.rb +85 -0
  65. data/lib/em/timers.rb +106 -0
  66. data/lib/em/version.rb +3 -0
  67. data/lib/eventmachine-le.rb +10 -0
  68. data/lib/eventmachine.rb +1548 -0
  69. data/rakelib/cpp.rake_example +77 -0
  70. data/rakelib/package.rake +98 -0
  71. data/rakelib/test.rake +8 -0
  72. data/tests/client.crt +31 -0
  73. data/tests/client.key +51 -0
  74. data/tests/em_test_helper.rb +143 -0
  75. data/tests/test_attach.rb +148 -0
  76. data/tests/test_basic.rb +294 -0
  77. data/tests/test_channel.rb +62 -0
  78. data/tests/test_completion.rb +177 -0
  79. data/tests/test_connection_count.rb +33 -0
  80. data/tests/test_defer.rb +18 -0
  81. data/tests/test_deferrable.rb +35 -0
  82. data/tests/test_epoll.rb +134 -0
  83. data/tests/test_error_handler.rb +38 -0
  84. data/tests/test_exc.rb +28 -0
  85. data/tests/test_file_watch.rb +65 -0
  86. data/tests/test_futures.rb +170 -0
  87. data/tests/test_get_sock_opt.rb +37 -0
  88. data/tests/test_handler_check.rb +35 -0
  89. data/tests/test_hc.rb +155 -0
  90. data/tests/test_httpclient.rb +190 -0
  91. data/tests/test_httpclient2.rb +128 -0
  92. data/tests/test_inactivity_timeout.rb +54 -0
  93. data/tests/test_ipv4.rb +125 -0
  94. data/tests/test_ipv6.rb +131 -0
  95. data/tests/test_iterator.rb +110 -0
  96. data/tests/test_kb.rb +34 -0
  97. data/tests/test_line_protocol.rb +33 -0
  98. data/tests/test_ltp.rb +138 -0
  99. data/tests/test_ltp2.rb +288 -0
  100. data/tests/test_next_tick.rb +104 -0
  101. data/tests/test_object_protocol.rb +36 -0
  102. data/tests/test_pause.rb +78 -0
  103. data/tests/test_pending_connect_timeout.rb +52 -0
  104. data/tests/test_pool.rb +196 -0
  105. data/tests/test_process_watch.rb +48 -0
  106. data/tests/test_processes.rb +133 -0
  107. data/tests/test_proxy_connection.rb +168 -0
  108. data/tests/test_pure.rb +88 -0
  109. data/tests/test_queue.rb +50 -0
  110. data/tests/test_resolver.rb +55 -0
  111. data/tests/test_running.rb +14 -0
  112. data/tests/test_sasl.rb +47 -0
  113. data/tests/test_send_file.rb +217 -0
  114. data/tests/test_servers.rb +33 -0
  115. data/tests/test_set_sock_opt.rb +41 -0
  116. data/tests/test_shutdown_hooks.rb +23 -0
  117. data/tests/test_smtpclient.rb +55 -0
  118. data/tests/test_smtpserver.rb +120 -0
  119. data/tests/test_spawn.rb +293 -0
  120. data/tests/test_ssl_args.rb +78 -0
  121. data/tests/test_ssl_methods.rb +48 -0
  122. data/tests/test_ssl_verify.rb +82 -0
  123. data/tests/test_threaded_resource.rb +55 -0
  124. data/tests/test_tick_loop.rb +59 -0
  125. data/tests/test_timers.rb +180 -0
  126. data/tests/test_ud.rb +8 -0
  127. data/tests/test_udp46.rb +53 -0
  128. data/tests/test_unbind_reason.rb +48 -0
  129. metadata +390 -0
@@ -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
data/tests/test_ltp.rb ADDED
@@ -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,288 @@
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
+ class ChangeDelimiter
30
+ include EM::Protocols::LineText2
31
+ attr_reader :lines
32
+ def initialize *args
33
+ super
34
+ @delim = "A"
35
+ set_delimiter @delim
36
+ end
37
+ def receive_line line
38
+ (@lines ||= []) << line
39
+ set_delimiter( @delim.succ! )
40
+ end
41
+ end
42
+
43
+ def test_change_delimiter
44
+ testdata = %Q(LineaALinebBLinecCLinedD)
45
+
46
+ a = ChangeDelimiter.new
47
+ a.receive_data testdata
48
+ assert_equal( ["Linea", "Lineb", "Linec", "Lined"], a.lines )
49
+
50
+ a = ChangeDelimiter.new
51
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
52
+ assert_equal( ["Linea", "Lineb", "Linec", "Lined"], a.lines )
53
+ end
54
+
55
+
56
+ #--
57
+ # Test two lines followed by an empty line, ten bytes of binary data, then
58
+ # two more lines.
59
+
60
+ class Binary
61
+ include EM::Protocols::LineText2
62
+ attr_reader :lines, :body
63
+ def initialize *args
64
+ super
65
+ @lines = []
66
+ @body = nil
67
+ end
68
+ def receive_line ln
69
+ if ln == ""
70
+ set_text_mode 10
71
+ else
72
+ @lines << ln
73
+ end
74
+ end
75
+ def receive_binary_data data
76
+ @body = data
77
+ end
78
+ end
79
+
80
+ def test_binary
81
+ testdata = %Q(Line 1
82
+ Line 2
83
+
84
+ 0000000000Line 3
85
+ Line 4
86
+ )
87
+
88
+ a = Binary.new
89
+ a.receive_data testdata
90
+ assert_equal( ["Line 1", "Line 2", "Line 3", "Line 4"], a.lines)
91
+ assert_equal( "0000000000", a.body )
92
+
93
+ a = Binary.new
94
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
95
+ assert_equal( ["Line 1", "Line 2", "Line 3", "Line 4"], a.lines)
96
+ assert_equal( "0000000000", a.body )
97
+ end
98
+
99
+
100
+ # Test unsized binary data. The expectation is that each chunk of it
101
+ # will be passed to us as it it received.
102
+ class UnsizedBinary
103
+ include EM::Protocols::LineText2
104
+ attr_reader :n_calls, :body
105
+ def initialize *args
106
+ super
107
+ set_text_mode
108
+ end
109
+ def receive_binary_data data
110
+ @n_calls ||= 0
111
+ @n_calls += 1
112
+ (@body ||= "") << data
113
+ end
114
+ end
115
+
116
+ def test_unsized_binary
117
+ testdata = "X\0" * 1000
118
+
119
+ a = UnsizedBinary.new
120
+ a.receive_data testdata
121
+ assert_equal( 1, a.n_calls )
122
+ assert_equal( testdata, a.body )
123
+
124
+ a = UnsizedBinary.new
125
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
126
+ assert_equal( 2000, a.n_calls )
127
+ assert_equal( testdata, a.body )
128
+ end
129
+
130
+
131
+ # Test binary data with a "throw back" into line-mode.
132
+ class ThrowBack
133
+ include EM::Protocols::LineText2
134
+ attr_reader :headers
135
+ def initialize *args
136
+ super
137
+ @headers = []
138
+ @n_bytes = 0
139
+ set_text_mode
140
+ end
141
+ def receive_binary_data data
142
+ wanted = 25 - @n_bytes
143
+ will_take = if data.length > wanted
144
+ data.length - wanted
145
+ else
146
+ data.length
147
+ end
148
+ @n_bytes += will_take
149
+
150
+ if @n_bytes == 25
151
+ set_line_mode( data[will_take..-1] )
152
+ end
153
+ end
154
+ def receive_line ln
155
+ @headers << ln
156
+ end
157
+ end
158
+ def test_throw_back
159
+ testdata = "Line\n" * 10
160
+
161
+ a = ThrowBack.new
162
+ a.receive_data testdata
163
+ assert_equal( ["Line"] * 5, a.headers )
164
+
165
+ a = ThrowBack.new
166
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
167
+ assert_equal( ["Line"] * 5, a.headers )
168
+ end
169
+
170
+ # Test multi-character line delimiters.
171
+ # Also note that the test data has a "tail" with no delimiter, that will be
172
+ # discarded, but cf. the BinaryTail test.
173
+ # TODO!!! This test doesn't work in the byte-by-byte case.
174
+ class Multichar
175
+ include EM::Protocols::LineText2
176
+ attr_reader :lines
177
+ def initialize *args
178
+ super
179
+ @lines = []
180
+ set_delimiter "012"
181
+ end
182
+ def receive_line ln
183
+ @lines << ln
184
+ end
185
+ end
186
+ def test_multichar
187
+ testdata = "Line012Line012Line012Line"
188
+
189
+ a = Multichar.new
190
+ a.receive_data testdata
191
+ assert_equal( ["Line"]*3, a.lines )
192
+
193
+ a = Multichar.new
194
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
195
+ # DOESN'T WORK in this case. Multi-character delimiters are broken.
196
+ #assert_equal( ["Line"]*3, a.lines )
197
+ end
198
+
199
+ # Test a binary "tail," when a sized binary transfer doesn't complete because
200
+ # of an unbind. We get a partial result.
201
+ class BinaryTail
202
+ include EM::Protocols::LineText2
203
+ attr_reader :data
204
+ def initialize *args
205
+ super
206
+ @data = ""
207
+ set_text_mode 1000
208
+ end
209
+ def receive_binary_data data
210
+ # we expect to get all the data in one chunk, even in the byte-by-byte case,
211
+ # because sized transfers by definition give us exactly one call to
212
+ # #receive_binary_data.
213
+ @data = data
214
+ end
215
+ end
216
+ def test_binary_tail
217
+ testdata = "0" * 500
218
+
219
+ a = BinaryTail.new
220
+ a.receive_data testdata
221
+ a.unbind
222
+ assert_equal( "0" * 500, a.data )
223
+
224
+ a = BinaryTail.new
225
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
226
+ a.unbind
227
+ assert_equal( "0" * 500, a.data )
228
+ end
229
+
230
+
231
+ # Test an end-of-binary call. Arrange to receive binary data but don't bother counting it
232
+ # as it comes. Rely on getting receive_end_of_binary_data to signal the transition back to
233
+ # line mode.
234
+ # At the present time, this isn't strictly necessary with sized binary chunks because by
235
+ # definition we accumulate them and make exactly one call to receive_binary_data, but
236
+ # we may want to support a mode in the future that would break up large chunks into multiple
237
+ # calls.
238
+ class LazyBinary
239
+ include EM::Protocols::LineText2
240
+ attr_reader :data, :end
241
+ def initialize *args
242
+ super
243
+ @data = ""
244
+ set_text_mode 1000
245
+ end
246
+ def receive_binary_data data
247
+ # we expect to get all the data in one chunk, even in the byte-by-byte case,
248
+ # because sized transfers by definition give us exactly one call to
249
+ # #receive_binary_data.
250
+ @data = data
251
+ end
252
+ def receive_end_of_binary_data
253
+ @end = true
254
+ end
255
+ end
256
+ def test_receive_end_of_binary_data
257
+ testdata = "_" * 1000
258
+ a = LazyBinary.new
259
+ testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
260
+ assert_equal( "_" * 1000, a.data )
261
+ assert( a.end )
262
+ end
263
+
264
+
265
+ # This tests a bug fix in which calling set_text_mode failed when called
266
+ # inside receive_binary_data.
267
+ #
268
+ class BinaryPair
269
+ include EM::Protocols::LineText2
270
+ attr_reader :sizes
271
+ def initialize *args
272
+ super
273
+ set_text_mode 1
274
+ @sizes = []
275
+ end
276
+ def receive_binary_data dt
277
+ @sizes << dt.length
278
+ set_text_mode( (dt.length == 1) ? 2 : 1 )
279
+ end
280
+ end
281
+ def test_binary_pairs
282
+ test_data = "123" * 5
283
+ a = BinaryPair.new
284
+ a.receive_data test_data
285
+ assert_equal( [1,2,1,2,1,2,1,2,1,2], a.sizes )
286
+ end
287
+
288
+ end