eventmachine 0.12.6 → 0.12.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. data/{docs/README → README} +21 -13
  2. data/Rakefile +14 -4
  3. data/docs/DEFERRABLES +0 -5
  4. data/docs/INSTALL +2 -4
  5. data/docs/LEGAL +1 -1
  6. data/docs/LIGHTWEIGHT_CONCURRENCY +0 -2
  7. data/docs/PURE_RUBY +0 -2
  8. data/docs/RELEASE_NOTES +0 -2
  9. data/docs/SMTP +0 -7
  10. data/docs/SPAWNED_PROCESSES +0 -4
  11. data/docs/TODO +0 -2
  12. data/eventmachine.gemspec +17 -8
  13. data/examples/ex_channel.rb +43 -0
  14. data/examples/ex_queue.rb +2 -0
  15. data/examples/helper.rb +2 -0
  16. data/ext/cmain.cpp +119 -20
  17. data/ext/cplusplus.cpp +15 -6
  18. data/ext/ed.cpp +303 -93
  19. data/ext/ed.h +49 -22
  20. data/ext/em.cpp +368 -42
  21. data/ext/em.h +43 -6
  22. data/ext/eventmachine.h +21 -8
  23. data/ext/eventmachine_cpp.h +1 -0
  24. data/ext/extconf.rb +4 -0
  25. data/ext/kb.cpp +1 -2
  26. data/ext/pipe.cpp +1 -3
  27. data/ext/project.h +21 -0
  28. data/ext/rubymain.cpp +232 -32
  29. data/ext/ssl.cpp +38 -1
  30. data/ext/ssl.h +5 -1
  31. data/java/src/com/rubyeventmachine/Application.java +7 -3
  32. data/java/src/com/rubyeventmachine/EmReactor.java +16 -1
  33. data/java/src/com/rubyeventmachine/tests/ConnectTest.java +25 -3
  34. data/lib/{protocols → em}/buftok.rb +16 -5
  35. data/lib/em/callback.rb +26 -0
  36. data/lib/em/channel.rb +57 -0
  37. data/lib/em/connection.rb +505 -0
  38. data/lib/em/deferrable.rb +144 -165
  39. data/lib/em/file_watch.rb +54 -0
  40. data/lib/em/future.rb +24 -25
  41. data/lib/em/messages.rb +1 -1
  42. data/lib/em/process_watch.rb +44 -0
  43. data/lib/em/processes.rb +58 -52
  44. data/lib/em/protocols.rb +35 -0
  45. data/lib/em/protocols/header_and_content.rb +138 -0
  46. data/lib/em/protocols/httpclient.rb +263 -0
  47. data/lib/em/protocols/httpclient2.rb +582 -0
  48. data/lib/{protocols → em/protocols}/line_and_text.rb +2 -2
  49. data/lib/em/protocols/linetext2.rb +160 -0
  50. data/lib/{protocols → em/protocols}/memcache.rb +37 -7
  51. data/lib/em/protocols/object_protocol.rb +39 -0
  52. data/lib/em/protocols/postgres3.rb +247 -0
  53. data/lib/em/protocols/saslauth.rb +175 -0
  54. data/lib/em/protocols/smtpclient.rb +331 -0
  55. data/lib/em/protocols/smtpserver.rb +547 -0
  56. data/lib/em/protocols/stomp.rb +200 -0
  57. data/lib/{protocols → em/protocols}/tcptest.rb +21 -25
  58. data/lib/em/queue.rb +61 -0
  59. data/lib/em/spawnable.rb +53 -56
  60. data/lib/em/streamer.rb +92 -74
  61. data/lib/em/timers.rb +55 -0
  62. data/lib/em/version.rb +3 -0
  63. data/lib/eventmachine.rb +1008 -1298
  64. data/lib/evma.rb +1 -1
  65. data/lib/jeventmachine.rb +106 -101
  66. data/lib/pr_eventmachine.rb +47 -36
  67. data/tasks/project.rake +2 -1
  68. data/tests/client.crt +31 -0
  69. data/tests/client.key +51 -0
  70. data/tests/test_attach.rb +18 -0
  71. data/tests/test_basic.rb +108 -54
  72. data/tests/test_channel.rb +63 -0
  73. data/tests/test_connection_count.rb +2 -2
  74. data/tests/test_epoll.rb +109 -110
  75. data/tests/test_errors.rb +36 -36
  76. data/tests/test_exc.rb +22 -25
  77. data/tests/test_file_watch.rb +49 -0
  78. data/tests/test_futures.rb +77 -93
  79. data/tests/test_hc.rb +2 -2
  80. data/tests/test_httpclient.rb +55 -52
  81. data/tests/test_httpclient2.rb +110 -112
  82. data/tests/test_inactivity_timeout.rb +30 -0
  83. data/tests/test_kb.rb +8 -9
  84. data/tests/test_ltp2.rb +274 -277
  85. data/tests/test_next_tick.rb +91 -65
  86. data/tests/test_object_protocol.rb +37 -0
  87. data/tests/test_process_watch.rb +48 -0
  88. data/tests/test_processes.rb +56 -23
  89. data/tests/test_proxy_connection.rb +92 -0
  90. data/tests/test_pure.rb +1 -5
  91. data/tests/test_queue.rb +44 -0
  92. data/tests/test_running.rb +9 -14
  93. data/tests/test_sasl.rb +32 -34
  94. data/tests/test_send_file.rb +175 -176
  95. data/tests/test_servers.rb +37 -41
  96. data/tests/test_smtpserver.rb +47 -55
  97. data/tests/test_spawn.rb +284 -291
  98. data/tests/test_ssl_args.rb +1 -1
  99. data/tests/test_ssl_methods.rb +1 -1
  100. data/tests/test_ssl_verify.rb +82 -0
  101. data/tests/test_timers.rb +81 -88
  102. data/tests/test_ud.rb +0 -7
  103. data/tests/testem.rb +1 -1
  104. metadata +68 -39
  105. data/lib/em/eventable.rb +0 -39
  106. data/lib/eventmachine_version.rb +0 -31
  107. data/lib/protocols/header_and_content.rb +0 -129
  108. data/lib/protocols/httpcli2.rb +0 -803
  109. data/lib/protocols/httpclient.rb +0 -270
  110. data/lib/protocols/linetext2.rb +0 -161
  111. data/lib/protocols/postgres.rb +0 -261
  112. data/lib/protocols/saslauth.rb +0 -179
  113. data/lib/protocols/smtpclient.rb +0 -308
  114. data/lib/protocols/smtpserver.rb +0 -556
  115. data/lib/protocols/stomp.rb +0 -153
  116. data/tests/test_eventables.rb +0 -77
@@ -29,127 +29,125 @@ require 'eventmachine'
29
29
  require 'test/unit'
30
30
 
31
31
  class TestHttpClient2 < Test::Unit::TestCase
32
- Localhost = "127.0.0.1"
33
- Localport = 9801
34
-
35
- def setup
36
- end
37
-
38
- def teardown
39
- end
40
-
41
-
42
- class TestServer < EM::Connection
43
- end
44
-
45
- # #connect returns an object which has made a connection to an HTTP server
46
- # and exposes methods for making HTTP requests on that connection.
47
- # #connect can take either a pair of parameters (a host and a port),
48
- # or a single parameter which is a Hash.
49
- #
50
- def test_connect
51
- EM.run {
52
- EM.start_server Localhost, Localport, TestServer
53
- http1 = EM::P::HttpClient2.connect Localhost, Localport
54
- http2 = EM::P::HttpClient2.connect( :host=>Localhost, :port=>Localport )
55
- EM.stop
56
- }
57
- end
58
-
59
-
60
- def test_bad_port
61
- EM.run {
62
- EM.start_server Localhost, Localport, TestServer
63
- assert_raises( ArgumentError ) {
64
- EM::P::HttpClient2.connect Localhost, "xxx"
65
- }
66
- EM.stop
67
- }
68
- end
69
-
70
- def test_bad_server
71
- err = nil
72
- EM.run {
73
- http = EM::P::HttpClient2.connect Localhost, 9999
74
- d = http.get "/"
75
- d.errback { err = true; d.internal_error; EM.stop }
76
- }
77
- assert(err)
78
- end
79
-
80
- def test_get
81
- content = nil
82
- EM.run {
83
- http = EM::P::HttpClient2.connect "www.bayshorenetworks.com", 80
84
- d = http.get "/"
85
- d.callback {
86
- content = d.content
87
- EM.stop
88
- }
89
- }
90
- assert(content)
91
- end
92
-
93
- # Not a pipelined request because we wait for one response before we request the next.
94
- def test_get_multiple
95
- content = nil
96
- EM.run {
97
- http = EM::P::HttpClient2.connect "www.bayshorenetworks.com", 80
98
- d = http.get "/"
99
- d.callback {
100
- e = http.get "/"
101
- e.callback {
102
- content = e.content
103
- EM.stop
104
- }
105
- }
106
- }
107
- assert(content)
108
- end
109
-
110
- def test_get_pipeline
111
- headers, headers2 = nil, nil
112
- EM.run {
113
- http = EM::P::HttpClient2.connect "www.microsoft.com", 80
114
- d = http.get("/")
115
- d.callback {
116
- headers = d.headers
117
- }
118
- e = http.get("/")
119
- e.callback {
120
- headers2 = e.headers
121
- }
122
- EM::Timer.new(1) {EM.stop}
123
- }
124
- assert(headers)
125
- assert(headers2)
126
- end
127
-
128
-
129
- def test_authheader
130
- EM.run {
131
- EM.start_server Localhost, Localport, TestServer
132
- http = EM::P::HttpClient2.connect Localhost, 18842
133
- d = http.get :url=>"/", :authorization=>"Basic xxx"
134
- d.callback {EM.stop}
135
- d.errback {EM.stop}
136
- }
137
- end
138
-
139
- def test_https_get
140
- d = nil
32
+ Localhost = "127.0.0.1"
33
+ Localport = 9801
34
+
35
+ def setup
36
+ end
37
+
38
+ def teardown
39
+ end
40
+
41
+
42
+ class TestServer < EM::Connection
43
+ end
44
+
45
+ # #connect returns an object which has made a connection to an HTTP server
46
+ # and exposes methods for making HTTP requests on that connection.
47
+ # #connect can take either a pair of parameters (a host and a port),
48
+ # or a single parameter which is a Hash.
49
+ #
50
+ def test_connect
141
51
  EM.run {
142
- http = EM::P::HttpClient2.connect :host => 'www.amazon.com', :port => 443, :ssl => true
52
+ EM.start_server Localhost, Localport, TestServer
53
+ http1 = EM::P::HttpClient2.connect Localhost, Localport
54
+ http2 = EM::P::HttpClient2.connect( :host=>Localhost, :port=>Localport )
55
+ EM.stop
56
+ }
57
+ end
58
+
59
+
60
+ def test_bad_port
61
+ EM.run {
62
+ EM.start_server Localhost, Localport, TestServer
63
+ assert_raises( ArgumentError ) {
64
+ EM::P::HttpClient2.connect Localhost, "xxx"
65
+ }
66
+ EM.stop
67
+ }
68
+ end
69
+
70
+ def test_bad_server
71
+ err = nil
72
+ EM.run {
73
+ http = EM::P::HttpClient2.connect Localhost, 9999
74
+ d = http.get "/"
75
+ d.errback { err = true; d.internal_error; EM.stop }
76
+ }
77
+ assert(err)
78
+ end
79
+
80
+ def test_get
81
+ content = nil
82
+ EM.run {
83
+ http = EM::P::HttpClient2.connect "www.bayshorenetworks.com", 80
143
84
  d = http.get "/"
144
85
  d.callback {
86
+ content = d.content
145
87
  EM.stop
146
88
  }
147
89
  }
148
- assert_equal(200, d.status)
90
+ assert(content)
149
91
  end
150
92
 
93
+ # Not a pipelined request because we wait for one response before we request the next.
94
+ # XXX this test is broken because it sends the second request to the first connection
95
+ # XXX right before the connection closes
96
+ def _test_get_multiple
97
+ content = nil
98
+ EM.run {
99
+ http = EM::P::HttpClient2.connect "www.bayshorenetworks.com", 80
100
+ d = http.get "/"
101
+ d.callback {
102
+ e = http.get "/"
103
+ e.callback {
104
+ content = e.content
105
+ EM.stop
106
+ }
107
+ }
108
+ }
109
+ assert(content)
110
+ end
151
111
 
112
+ def test_get_pipeline
113
+ headers, headers2 = nil, nil
114
+ EM.run {
115
+ http = EM::P::HttpClient2.connect "www.microsoft.com", 80
116
+ d = http.get("/")
117
+ d.callback {
118
+ headers = d.headers
119
+ }
120
+ e = http.get("/")
121
+ e.callback {
122
+ headers2 = e.headers
123
+ }
124
+ EM::Timer.new(1) {EM.stop}
125
+ }
126
+ assert(headers)
127
+ assert(headers2)
128
+ end
152
129
 
153
- end
154
130
 
131
+ def test_authheader
132
+ EM.run {
133
+ EM.start_server Localhost, Localport, TestServer
134
+ http = EM::P::HttpClient2.connect Localhost, 18842
135
+ d = http.get :url=>"/", :authorization=>"Basic xxx"
136
+ d.callback {EM.stop}
137
+ d.errback {EM.stop}
138
+ }
139
+ end
155
140
 
141
+ def test_https_get
142
+ d = nil
143
+ EM.run {
144
+ http = EM::P::HttpClient2.connect :host => 'www.amazon.com', :port => 443, :ssl => true
145
+ d = http.get "/"
146
+ d.callback {
147
+ EM.stop
148
+ }
149
+ }
150
+ assert_equal(200, d.status)
151
+ end if EM.ssl?
152
+
153
+ end
@@ -0,0 +1,30 @@
1
+ $:.unshift "../lib"
2
+ require 'eventmachine'
3
+ require 'test/unit'
4
+
5
+ class TestInactivityTimeout < Test::Unit::TestCase
6
+
7
+ def test_default
8
+ $timeout = nil
9
+ EM.run {
10
+ c = EM.connect("127.0.0.1", 54321)
11
+ $timeout = c.comm_inactivity_timeout
12
+ EM.stop
13
+ }
14
+
15
+ assert_equal(0.0, $timeout)
16
+ end
17
+
18
+ def test_with_set
19
+ $timeout = nil
20
+ EM.run {
21
+ c = EM.connect("127.0.0.1", 54321)
22
+ c.comm_inactivity_timeout = 2.5
23
+ $timeout = c.comm_inactivity_timeout
24
+ EM.stop
25
+ }
26
+
27
+ assert_equal(2.5, $timeout)
28
+ end
29
+
30
+ end
@@ -37,10 +37,10 @@ class TestKeyboardEvents < Test::Unit::TestCase
37
37
  end
38
38
 
39
39
  module KbHandler
40
- include EM::Protocols::LineText2
41
- def receive_line d
42
- EM::stop if d == "STOP"
43
- end
40
+ include EM::Protocols::LineText2
41
+ def receive_line d
42
+ EM::stop if d == "STOP"
43
+ end
44
44
  end
45
45
 
46
46
  # This test doesn't actually do anything useful but is here to
@@ -51,11 +51,10 @@ class TestKeyboardEvents < Test::Unit::TestCase
51
51
  # When someone figures that out, then we can make this a real test.
52
52
  #
53
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.
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
58
  end
59
59
 
60
60
  end
61
-
@@ -33,288 +33,285 @@ require 'test/unit'
33
33
 
34
34
  class TestLineText2 < Test::Unit::TestCase
35
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
-
59
- class ChangeDelimiter
60
- include EM::Protocols::LineText2
61
- attr_reader :lines
62
- def initialize *args
63
- super
64
- @delim = "A"
65
- set_delimiter @delim
66
- end
67
- def receive_line line
68
- (@lines ||= []) << line
69
- set_delimiter( @delim.succ! )
70
- end
71
- end
72
-
73
- def test_change_delimiter
74
- testdata = %Q(LineaALinebBLinecCLinedD)
75
-
76
- a = ChangeDelimiter.new
77
- a.receive_data testdata
78
- assert_equal( ["Linea", "Lineb", "Linec", "Lined"], a.lines )
79
-
80
- a = ChangeDelimiter.new
81
- testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
82
- assert_equal( ["Linea", "Lineb", "Linec", "Lined"], a.lines )
83
- end
84
-
85
-
86
- #--
87
- # Test two lines followed by an empty line, ten bytes of binary data, then
88
- # two more lines.
89
-
90
- class Binary
91
- include EM::Protocols::LineText2
92
- attr_reader :lines, :body
93
- def initialize *args
94
- super
95
- @lines = []
96
- @body = nil
97
- end
98
- def receive_line ln
99
- if ln == ""
100
- set_text_mode 10
101
- else
102
- @lines << ln
103
- end
104
- end
105
- def receive_binary_data data
106
- @body = data
107
- end
108
- end
109
-
110
- def test_binary
111
- testdata = %Q(Line 1
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
112
111
  Line 2
113
112
 
114
113
  0000000000Line 3
115
114
  Line 4
116
115
  )
117
116
 
118
- a = Binary.new
119
- a.receive_data testdata
120
- assert_equal( ["Line 1", "Line 2", "Line 3", "Line 4"], a.lines)
121
- assert_equal( "0000000000", a.body )
122
-
123
- a = Binary.new
124
- testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
125
- assert_equal( ["Line 1", "Line 2", "Line 3", "Line 4"], a.lines)
126
- assert_equal( "0000000000", a.body )
127
- end
128
-
129
-
130
- # Test unsized binary data. The expectation is that each chunk of it
131
- # will be passed to us as it it received.
132
- class UnsizedBinary
133
- include EM::Protocols::LineText2
134
- attr_reader :n_calls, :body
135
- def initialize *args
136
- super
137
- set_text_mode
138
- end
139
- def receive_binary_data data
140
- @n_calls ||= 0
141
- @n_calls += 1
142
- (@body ||= "") << data
143
- end
144
- end
145
-
146
- def test_unsized_binary
147
- testdata = "X\0" * 1000
148
-
149
- a = UnsizedBinary.new
150
- a.receive_data testdata
151
- assert_equal( 1, a.n_calls )
152
- assert_equal( testdata, a.body )
153
-
154
- a = UnsizedBinary.new
155
- testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
156
- assert_equal( 2000, a.n_calls )
157
- assert_equal( testdata, a.body )
158
- end
159
-
160
-
161
- # Test binary data with a "throw back" into line-mode.
162
- class ThrowBack
163
- include EM::Protocols::LineText2
164
- attr_reader :headers
165
- def initialize *args
166
- super
167
- @headers = []
168
- @n_bytes = 0
169
- set_text_mode
170
- end
171
- def receive_binary_data data
172
- wanted = 25 - @n_bytes
173
- will_take = if data.length > wanted
174
- data.length - wanted
175
- else
176
- data.length
177
- end
178
- @n_bytes += will_take
179
-
180
- if @n_bytes == 25
181
- set_line_mode( data[will_take..-1] )
182
- end
183
- end
184
- def receive_line ln
185
- @headers << ln
186
- end
187
- end
188
- def test_throw_back
189
- testdata = "Line\n" * 10
190
-
191
- a = ThrowBack.new
192
- a.receive_data testdata
193
- assert_equal( ["Line"] * 5, a.headers )
194
-
195
- a = ThrowBack.new
196
- testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
197
- assert_equal( ["Line"] * 5, a.headers )
198
- end
199
-
200
- # Test multi-character line delimiters.
201
- # Also note that the test data has a "tail" with no delimiter, that will be
202
- # discarded, but cf. the BinaryTail test.
203
- # TODO!!! This test doesn't work in the byte-by-byte case.
204
- class Multichar
205
- include EM::Protocols::LineText2
206
- attr_reader :lines
207
- def initialize *args
208
- super
209
- @lines = []
210
- set_delimiter "012"
211
- end
212
- def receive_line ln
213
- @lines << ln
214
- end
215
- end
216
- def test_multichar
217
- testdata = "Line012Line012Line012Line"
218
-
219
- a = Multichar.new
220
- a.receive_data testdata
221
- assert_equal( ["Line"]*3, a.lines )
222
-
223
- a = Multichar.new
224
- testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
225
- # DOESN'T WORK in this case. Multi-character delimiters are broken.
226
- #assert_equal( ["Line"]*3, a.lines )
227
- end
228
-
229
- # Test a binary "tail," when a sized binary transfer doesn't complete because
230
- # of an unbind. We get a partial result.
231
- class BinaryTail
232
- include EM::Protocols::LineText2
233
- attr_reader :data
234
- def initialize *args
235
- super
236
- @data = ""
237
- set_text_mode 1000
238
- end
239
- def receive_binary_data data
240
- # we expect to get all the data in one chunk, even in the byte-by-byte case,
241
- # because sized transfers by definition give us exactly one call to
242
- # #receive_binary_data.
243
- @data = data
244
- end
245
- end
246
- def test_binary_tail
247
- testdata = "0" * 500
248
-
249
- a = BinaryTail.new
250
- a.receive_data testdata
251
- a.unbind
252
- assert_equal( "0" * 500, a.data )
253
-
254
- a = BinaryTail.new
255
- testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
256
- a.unbind
257
- assert_equal( "0" * 500, a.data )
258
- end
259
-
260
-
261
-
262
- # Test an end-of-binary call. Arrange to receive binary data but don't bother counting it
263
- # as it comes. Rely on getting receive_end_of_binary_data to signal the transition back to
264
- # line mode.
265
- # At the present time, this isn't strictly necessary with sized binary chunks because by
266
- # definition we accumulate them and make exactly one call to receive_binary_data, but
267
- # we may want to support a mode in the future that would break up large chunks into multiple
268
- # calls.
269
- class LazyBinary
270
- include EM::Protocols::LineText2
271
- attr_reader :data, :end
272
- def initialize *args
273
- super
274
- @data = ""
275
- set_text_mode 1000
276
- end
277
- def receive_binary_data data
278
- # we expect to get all the data in one chunk, even in the byte-by-byte case,
279
- # because sized transfers by definition give us exactly one call to
280
- # #receive_binary_data.
281
- @data = data
282
- end
283
- def receive_end_of_binary_data
284
- @end = true
285
- end
286
- end
287
- def test_receive_end_of_binary_data
288
- testdata = "_" * 1000
289
- a = LazyBinary.new
290
- testdata.length.times {|i| a.receive_data( testdata[i...i+1] ) }
291
- assert_equal( "_" * 1000, a.data )
292
- assert( a.end )
293
- end
294
-
295
-
296
- # This tests a bug fix in which calling set_text_mode failed when called
297
- # inside receive_binary_data.
298
- #
299
- class BinaryPair
300
- include EM::Protocols::LineText2
301
- attr_reader :sizes
302
- def initialize *args
303
- super
304
- set_text_mode 1
305
- @sizes = []
306
- end
307
- def receive_binary_data dt
308
- @sizes << dt.length
309
- set_text_mode( (dt.length == 1) ? 2 : 1 )
310
- end
311
- end
312
- def test_binary_pairs
313
- test_data = "123" * 5
314
- a = BinaryPair.new
315
- a.receive_data test_data
316
- assert_equal( [1,2,1,2,1,2,1,2,1,2], a.sizes )
317
- end
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
318
316
 
319
317
  end
320
-