eventmachine-eventmachine 0.12.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +169 -0
- data/docs/COPYING +60 -0
- data/docs/ChangeLog +183 -0
- data/docs/DEFERRABLES +138 -0
- data/docs/EPOLL +141 -0
- data/docs/GNU +281 -0
- data/docs/INSTALL +15 -0
- data/docs/KEYBOARD +38 -0
- data/docs/LEGAL +25 -0
- data/docs/LIGHTWEIGHT_CONCURRENCY +72 -0
- data/docs/PURE_RUBY +77 -0
- data/docs/README +74 -0
- data/docs/RELEASE_NOTES +96 -0
- data/docs/SMTP +9 -0
- data/docs/SPAWNED_PROCESSES +93 -0
- data/docs/TODO +10 -0
- data/ext/binder.cpp +126 -0
- data/ext/binder.h +48 -0
- data/ext/cmain.cpp +530 -0
- data/ext/cplusplus.cpp +172 -0
- data/ext/ed.cpp +1473 -0
- data/ext/ed.h +361 -0
- data/ext/em.cpp +1895 -0
- data/ext/em.h +170 -0
- data/ext/emwin.cpp +300 -0
- data/ext/emwin.h +94 -0
- data/ext/epoll.cpp +26 -0
- data/ext/epoll.h +25 -0
- data/ext/eventmachine.h +90 -0
- data/ext/eventmachine_cpp.h +94 -0
- data/ext/extconf.rb +150 -0
- data/ext/files.cpp +94 -0
- data/ext/files.h +65 -0
- data/ext/kb.cpp +368 -0
- data/ext/page.cpp +107 -0
- data/ext/page.h +51 -0
- data/ext/pipe.cpp +327 -0
- data/ext/project.h +119 -0
- data/ext/rubymain.cpp +683 -0
- data/ext/sigs.cpp +89 -0
- data/ext/sigs.h +32 -0
- data/ext/ssl.cpp +408 -0
- data/ext/ssl.h +86 -0
- data/java/src/com/rubyeventmachine/Application.java +196 -0
- data/java/src/com/rubyeventmachine/Connection.java +74 -0
- data/java/src/com/rubyeventmachine/ConnectionFactory.java +37 -0
- data/java/src/com/rubyeventmachine/DefaultConnectionFactory.java +46 -0
- data/java/src/com/rubyeventmachine/EmReactor.java +408 -0
- data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
- data/java/src/com/rubyeventmachine/EventableChannel.java +57 -0
- data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +171 -0
- data/java/src/com/rubyeventmachine/EventableSocketChannel.java +244 -0
- data/java/src/com/rubyeventmachine/PeriodicTimer.java +38 -0
- data/java/src/com/rubyeventmachine/Timer.java +54 -0
- data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +108 -0
- data/java/src/com/rubyeventmachine/tests/ConnectTest.java +124 -0
- data/java/src/com/rubyeventmachine/tests/EMTest.java +80 -0
- data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +53 -0
- data/java/src/com/rubyeventmachine/tests/TestServers.java +74 -0
- data/java/src/com/rubyeventmachine/tests/TestTimers.java +89 -0
- data/lib/em/deferrable.rb +208 -0
- data/lib/em/eventable.rb +39 -0
- data/lib/em/future.rb +62 -0
- data/lib/em/messages.rb +66 -0
- data/lib/em/processes.rb +68 -0
- data/lib/em/spawnable.rb +88 -0
- data/lib/em/streamer.rb +112 -0
- data/lib/eventmachine.rb +1763 -0
- data/lib/eventmachine_version.rb +31 -0
- data/lib/evma.rb +32 -0
- data/lib/evma/callback.rb +32 -0
- data/lib/evma/container.rb +75 -0
- data/lib/evma/factory.rb +77 -0
- data/lib/evma/protocol.rb +87 -0
- data/lib/evma/reactor.rb +48 -0
- data/lib/jeventmachine.rb +137 -0
- data/lib/pr_eventmachine.rb +1011 -0
- data/lib/protocols/buftok.rb +127 -0
- data/lib/protocols/header_and_content.rb +129 -0
- data/lib/protocols/httpcli2.rb +794 -0
- data/lib/protocols/httpclient.rb +270 -0
- data/lib/protocols/line_and_text.rb +122 -0
- data/lib/protocols/linetext2.rb +163 -0
- data/lib/protocols/postgres.rb +261 -0
- data/lib/protocols/saslauth.rb +179 -0
- data/lib/protocols/smtpclient.rb +308 -0
- data/lib/protocols/smtpserver.rb +556 -0
- data/lib/protocols/stomp.rb +130 -0
- data/lib/protocols/tcptest.rb +57 -0
- data/tasks/cpp.rake +77 -0
- data/tasks/project.rake +78 -0
- data/tasks/tests.rake +192 -0
- data/tests/test_attach.rb +66 -0
- data/tests/test_basic.rb +231 -0
- data/tests/test_defer.rb +47 -0
- data/tests/test_epoll.rb +161 -0
- data/tests/test_errors.rb +82 -0
- data/tests/test_eventables.rb +78 -0
- data/tests/test_exc.rb +58 -0
- data/tests/test_futures.rb +214 -0
- data/tests/test_hc.rb +218 -0
- data/tests/test_httpclient.rb +215 -0
- data/tests/test_httpclient2.rb +133 -0
- data/tests/test_kb.rb +61 -0
- data/tests/test_ltp.rb +192 -0
- data/tests/test_ltp2.rb +320 -0
- data/tests/test_next_tick.rb +102 -0
- data/tests/test_processes.rb +56 -0
- data/tests/test_pure.rb +129 -0
- data/tests/test_running.rb +47 -0
- data/tests/test_sasl.rb +74 -0
- data/tests/test_send_file.rb +245 -0
- data/tests/test_servers.rb +80 -0
- data/tests/test_smtpclient.rb +81 -0
- data/tests/test_smtpserver.rb +93 -0
- data/tests/test_spawn.rb +329 -0
- data/tests/test_ssl_args.rb +68 -0
- data/tests/test_timers.rb +146 -0
- data/tests/test_ud.rb +43 -0
- data/tests/testem.rb +31 -0
- metadata +197 -0
@@ -0,0 +1,215 @@
|
|
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 TestHttpClient < Test::Unit::TestCase
|
32
|
+
|
33
|
+
Localhost = "127.0.0.1"
|
34
|
+
Localport = 9801
|
35
|
+
|
36
|
+
def setup
|
37
|
+
end
|
38
|
+
|
39
|
+
def teardown
|
40
|
+
end
|
41
|
+
|
42
|
+
#-------------------------------------
|
43
|
+
|
44
|
+
def test_http_client
|
45
|
+
ok = false
|
46
|
+
EventMachine.run {
|
47
|
+
c = EventMachine::Protocols::HttpClient.send :request, :host => "www.bayshorenetworks.com", :port => 80
|
48
|
+
c.callback {
|
49
|
+
ok = true
|
50
|
+
EventMachine.stop
|
51
|
+
}
|
52
|
+
c.errback {EventMachine.stop} # necessary, otherwise a failure blocks the test suite forever.
|
53
|
+
}
|
54
|
+
assert ok
|
55
|
+
end
|
56
|
+
|
57
|
+
#-------------------------------------
|
58
|
+
|
59
|
+
def test_http_client_1
|
60
|
+
ok = false
|
61
|
+
EventMachine.run {
|
62
|
+
c = EventMachine::Protocols::HttpClient.send :request, :host => "www.bayshorenetworks.com", :port => 80
|
63
|
+
c.callback {ok = true; EventMachine.stop}
|
64
|
+
c.errback {EventMachine.stop}
|
65
|
+
}
|
66
|
+
assert ok
|
67
|
+
end
|
68
|
+
|
69
|
+
#-------------------------------------
|
70
|
+
|
71
|
+
def test_http_client_2
|
72
|
+
ok = false
|
73
|
+
EventMachine.run {
|
74
|
+
c = EventMachine::Protocols::HttpClient.send :request, :host => "www.bayshorenetworks.com", :port => 80
|
75
|
+
c.callback {|result|
|
76
|
+
ok = true;
|
77
|
+
EventMachine.stop
|
78
|
+
}
|
79
|
+
c.errback {EventMachine.stop}
|
80
|
+
}
|
81
|
+
assert ok
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
#-----------------------------------------
|
86
|
+
|
87
|
+
# Test a server that returns a page with a zero content-length.
|
88
|
+
# This caused an early version of the HTTP client not to generate a response,
|
89
|
+
# causing this test to hang. Observe, there was no problem with responses
|
90
|
+
# lacking a content-length, just when the content-length was zero.
|
91
|
+
#
|
92
|
+
class EmptyContent < EventMachine::Connection
|
93
|
+
def initialize *args
|
94
|
+
super
|
95
|
+
end
|
96
|
+
def receive_data data
|
97
|
+
send_data "HTTP/1.0 404 ...\r\nContent-length: 0\r\n\r\n"
|
98
|
+
close_connection_after_writing
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_http_empty_content
|
103
|
+
ok = false
|
104
|
+
EventMachine.run {
|
105
|
+
EventMachine.start_server "127.0.0.1", 9701, EmptyContent
|
106
|
+
c = EventMachine::Protocols::HttpClient.send :request, :host => "127.0.0.1", :port => 9701
|
107
|
+
c.callback {|result|
|
108
|
+
ok = true
|
109
|
+
EventMachine.stop
|
110
|
+
}
|
111
|
+
}
|
112
|
+
assert ok
|
113
|
+
end
|
114
|
+
|
115
|
+
|
116
|
+
#---------------------------------------
|
117
|
+
|
118
|
+
class PostContent < EventMachine::Protocols::LineAndTextProtocol
|
119
|
+
def initialize *args
|
120
|
+
super
|
121
|
+
@lines = []
|
122
|
+
end
|
123
|
+
def receive_line line
|
124
|
+
if line.length > 0
|
125
|
+
@lines << line
|
126
|
+
else
|
127
|
+
process_headers
|
128
|
+
end
|
129
|
+
end
|
130
|
+
def receive_binary_data data
|
131
|
+
@post_content = data
|
132
|
+
send_response
|
133
|
+
end
|
134
|
+
def process_headers
|
135
|
+
if @lines.first =~ /\APOST ([^\s]+) HTTP\/1.1\Z/
|
136
|
+
@uri = $1.dup
|
137
|
+
else
|
138
|
+
raise "bad request"
|
139
|
+
end
|
140
|
+
@lines.each {|line|
|
141
|
+
if line =~ /\AContent-length:\s*(\d+)\Z/i
|
142
|
+
@content_length = $1.dup.to_i
|
143
|
+
elsif line =~ /\AContent-type:\s*(\d+)\Z/i
|
144
|
+
@content_type = $1.dup
|
145
|
+
end
|
146
|
+
}
|
147
|
+
|
148
|
+
raise "invalid content length" unless @content_length
|
149
|
+
set_binary_mode @content_length
|
150
|
+
end
|
151
|
+
def send_response
|
152
|
+
send_data "HTTP/1.1 200 ...\r\nConnection: close\r\nContent-length: 10\r\nContent-type: text/html\r\n\r\n0123456789"
|
153
|
+
close_connection_after_writing
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
# TODO, this is WRONG. The handler is asserting an HTTP 1.1 request, but the client
|
158
|
+
# is sending a 1.0 request. Gotta fix the client
|
159
|
+
def test_post
|
160
|
+
response = nil
|
161
|
+
EventMachine.run {
|
162
|
+
EventMachine.start_server Localhost, Localport, PostContent
|
163
|
+
EventMachine.add_timer(2) {raise "timed out"}
|
164
|
+
c = EventMachine::Protocols::HttpClient.request :host=>Localhost,
|
165
|
+
:port=>Localport, :method=>:post, :request=>"/aaa", :content=>"XYZ",
|
166
|
+
:content_type=>"text/plain"
|
167
|
+
c.callback {|r|
|
168
|
+
response = r
|
169
|
+
EventMachine.stop
|
170
|
+
}
|
171
|
+
}
|
172
|
+
|
173
|
+
assert_equal( 200, response[:status] )
|
174
|
+
assert_equal( "0123456789", response[:content] )
|
175
|
+
end
|
176
|
+
|
177
|
+
|
178
|
+
# TODO, need a more intelligent cookie tester.
|
179
|
+
# In fact, this whole test-harness needs a beefier server implementation.
|
180
|
+
def test_cookie
|
181
|
+
ok = false
|
182
|
+
EM.run {
|
183
|
+
c = EM::Protocols::HttpClient.send :request, :host => "www.bayshorenetworks.com", :port => 80, :cookie=>"aaa=bbb"
|
184
|
+
c.callback {|result|
|
185
|
+
ok = true;
|
186
|
+
EventMachine.stop
|
187
|
+
}
|
188
|
+
c.errback {EventMachine.stop}
|
189
|
+
}
|
190
|
+
assert ok
|
191
|
+
end
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
# We can tell the client to send an HTTP/1.0 request (default is 1.1).
|
196
|
+
# This is useful for suppressing chunked responses until those are working.
|
197
|
+
def test_version_1_0
|
198
|
+
ok = false
|
199
|
+
EM.run {
|
200
|
+
c = EM::P::HttpClient.request :host => "www.bayshorenetworks.com",
|
201
|
+
:port => 80,
|
202
|
+
:version => "1.0"
|
203
|
+
c.callback {|result|
|
204
|
+
ok = true;
|
205
|
+
EventMachine.stop
|
206
|
+
}
|
207
|
+
c.errback {EventMachine.stop}
|
208
|
+
}
|
209
|
+
assert ok
|
210
|
+
end
|
211
|
+
|
212
|
+
|
213
|
+
end
|
214
|
+
|
215
|
+
|
@@ -0,0 +1,133 @@
|
|
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 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_raise( ArgumentError ) {
|
64
|
+
EM::P::HttpClient2.connect Localhost, "xxx"
|
65
|
+
}
|
66
|
+
EM.stop
|
67
|
+
}
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_bad_server
|
71
|
+
EM.run {
|
72
|
+
http = EM::P::HttpClient2.connect Localhost, 9999
|
73
|
+
d = http.get "/"
|
74
|
+
d.errback {p d.internal_error; EM.stop }
|
75
|
+
}
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_get
|
79
|
+
EM.run {
|
80
|
+
http = EM::P::HttpClient2.connect "www.bayshorenetworks.com", 80
|
81
|
+
d = http.get "/"
|
82
|
+
d.callback {
|
83
|
+
p d.content
|
84
|
+
EM.stop
|
85
|
+
}
|
86
|
+
}
|
87
|
+
end
|
88
|
+
|
89
|
+
# Not a pipelined request because we wait for one response before we request the next.
|
90
|
+
def test_get_multiple
|
91
|
+
EM.run {
|
92
|
+
http = EM::P::HttpClient2.connect "www.bayshorenetworks.com", 80
|
93
|
+
d = http.get "/"
|
94
|
+
d.callback {
|
95
|
+
e = http.get "/"
|
96
|
+
e.callback {
|
97
|
+
p e.content
|
98
|
+
EM.stop
|
99
|
+
}
|
100
|
+
}
|
101
|
+
}
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_get_pipeline
|
105
|
+
EM.run {
|
106
|
+
http = EM::P::HttpClient2.connect "www.microsoft.com", 80
|
107
|
+
d = http.get("/")
|
108
|
+
d.callback {
|
109
|
+
p d.headers
|
110
|
+
}
|
111
|
+
e = http.get("/")
|
112
|
+
e.callback {
|
113
|
+
p e.headers
|
114
|
+
}
|
115
|
+
EM::Timer.new(1) {EM.stop}
|
116
|
+
}
|
117
|
+
end
|
118
|
+
|
119
|
+
|
120
|
+
def test_authheader
|
121
|
+
EM.run {
|
122
|
+
EM.start_server Localhost, Localport, TestServer
|
123
|
+
http = EM::P::HttpClient2.connect Localhost, 18842
|
124
|
+
d = http.get :url=>"/", :authorization=>"Basic xxx"
|
125
|
+
d.callback {EM.stop}
|
126
|
+
d.errback {EM.stop}
|
127
|
+
}
|
128
|
+
end
|
129
|
+
|
130
|
+
|
131
|
+
end
|
132
|
+
|
133
|
+
|
data/tests/test_kb.rb
ADDED
@@ -0,0 +1,61 @@
|
|
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
|
+
}
|
58
|
+
end if $stdout.tty? # don't run the test unless it stands a chance of validity.
|
59
|
+
|
60
|
+
end
|
61
|
+
|
data/tests/test_ltp.rb
ADDED
@@ -0,0 +1,192 @@
|
|
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
|
+
require 'eventmachine'
|
29
|
+
require 'test/unit'
|
30
|
+
|
31
|
+
class TestLineAndTextProtocol < Test::Unit::TestCase
|
32
|
+
|
33
|
+
TestHost = "127.0.0.1"
|
34
|
+
TestPort = 8905
|
35
|
+
|
36
|
+
|
37
|
+
#--------------------------------------------------------------------
|
38
|
+
|
39
|
+
class SimpleLineTest < EventMachine::Protocols::LineAndTextProtocol
|
40
|
+
def receive_line line
|
41
|
+
@line_buffer << line
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
module StopClient
|
46
|
+
def set_receive_data(&blk)
|
47
|
+
@rdb = blk
|
48
|
+
end
|
49
|
+
|
50
|
+
def receive_data data
|
51
|
+
@rdb.call(data) if @rdb
|
52
|
+
end
|
53
|
+
|
54
|
+
def unbind
|
55
|
+
EM.add_timer(0.1) { EM.stop }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
def test_simple_lines
|
61
|
+
# THIS TEST CURRENTLY FAILS IN JRUBY.
|
62
|
+
assert( RUBY_PLATFORM !~ /java/ )
|
63
|
+
|
64
|
+
lines_received = []
|
65
|
+
Thread.abort_on_exception = true
|
66
|
+
EventMachine.run {
|
67
|
+
EventMachine.start_server( TestHost, TestPort, SimpleLineTest ) do |conn|
|
68
|
+
conn.instance_eval "@line_buffer = lines_received"
|
69
|
+
end
|
70
|
+
EventMachine.add_timer(4) {assert(false, "test timed out")}
|
71
|
+
|
72
|
+
EventMachine.connect TestHost, TestPort, StopClient do |c|
|
73
|
+
c.send_data "aaa\nbbb\r\nccc\n"
|
74
|
+
c.close_connection_after_writing
|
75
|
+
end
|
76
|
+
}
|
77
|
+
assert_equal( %w(aaa bbb ccc), lines_received )
|
78
|
+
end
|
79
|
+
|
80
|
+
#--------------------------------------------------------------------
|
81
|
+
|
82
|
+
class SimpleLineTest < EventMachine::Protocols::LineAndTextProtocol
|
83
|
+
def receive_error text
|
84
|
+
@error_message << text
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_overlength_lines
|
89
|
+
# THIS TEST CURRENTLY FAILS IN JRUBY.
|
90
|
+
assert( RUBY_PLATFORM !~ /java/ )
|
91
|
+
|
92
|
+
lines_received = []
|
93
|
+
Thread.abort_on_exception = true
|
94
|
+
EventMachine.run {
|
95
|
+
EventMachine.start_server( TestHost, TestPort, SimpleLineTest ) do |conn|
|
96
|
+
conn.instance_eval "@error_message = lines_received"
|
97
|
+
end
|
98
|
+
EventMachine.add_timer(4) {assert(false, "test timed out")}
|
99
|
+
|
100
|
+
EventMachine.connect TestHost, TestPort, StopClient do |c|
|
101
|
+
c.send_data "a" * (16*1024 + 1)
|
102
|
+
c.send_data "\n"
|
103
|
+
c.close_connection_after_writing
|
104
|
+
end
|
105
|
+
|
106
|
+
}
|
107
|
+
assert_equal( ["overlength line"], lines_received )
|
108
|
+
end
|
109
|
+
|
110
|
+
|
111
|
+
#--------------------------------------------------------------------
|
112
|
+
|
113
|
+
class LineAndTextTest < EventMachine::Protocols::LineAndTextProtocol
|
114
|
+
def post_init
|
115
|
+
end
|
116
|
+
def receive_line line
|
117
|
+
if line =~ /content-length:\s*(\d+)/i
|
118
|
+
@content_length = $1.to_i
|
119
|
+
elsif line.length == 0
|
120
|
+
set_binary_mode @content_length
|
121
|
+
end
|
122
|
+
end
|
123
|
+
def receive_binary_data text
|
124
|
+
send_data "received #{text.length} bytes"
|
125
|
+
close_connection_after_writing
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_lines_and_text
|
130
|
+
output = ''
|
131
|
+
lines_received = []
|
132
|
+
text_received = []
|
133
|
+
Thread.abort_on_exception = true
|
134
|
+
EventMachine.run {
|
135
|
+
EventMachine.start_server( TestHost, TestPort, LineAndTextTest ) do |conn|
|
136
|
+
conn.instance_eval "@lines = lines_received; @text = text_received"
|
137
|
+
end
|
138
|
+
EventMachine.add_timer(4) {assert(false, "test timed out")}
|
139
|
+
|
140
|
+
EventMachine.connect TestHost, TestPort, StopClient do |c|
|
141
|
+
c.set_receive_data { |data| output << data }
|
142
|
+
c.send_data "Content-length: 400\n"
|
143
|
+
c.send_data "\n"
|
144
|
+
c.send_data "A" * 400
|
145
|
+
EM.add_timer(0.1) { c.close_connection_after_writing }
|
146
|
+
end
|
147
|
+
}
|
148
|
+
assert_equal( "received 400 bytes", output )
|
149
|
+
end
|
150
|
+
|
151
|
+
#--------------------------------------------------------------------
|
152
|
+
|
153
|
+
|
154
|
+
class BinaryTextTest < EventMachine::Protocols::LineAndTextProtocol
|
155
|
+
def post_init
|
156
|
+
end
|
157
|
+
def receive_line line
|
158
|
+
if line =~ /content-length:\s*(\d+)/i
|
159
|
+
set_binary_mode $1.to_i
|
160
|
+
else
|
161
|
+
raise "protocol error"
|
162
|
+
end
|
163
|
+
end
|
164
|
+
def receive_binary_data text
|
165
|
+
send_data "received #{text.length} bytes"
|
166
|
+
close_connection_after_writing
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_binary_text
|
171
|
+
output = ''
|
172
|
+
lines_received = []
|
173
|
+
text_received = []
|
174
|
+
Thread.abort_on_exception = true
|
175
|
+
EventMachine.run {
|
176
|
+
EventMachine.start_server( TestHost, TestPort, BinaryTextTest ) do |conn|
|
177
|
+
conn.instance_eval "@lines = lines_received; @text = text_received"
|
178
|
+
end
|
179
|
+
EventMachine.add_timer(4) {assert(false, "test timed out")}
|
180
|
+
|
181
|
+
EventMachine.connect TestHost, TestPort, StopClient do |c|
|
182
|
+
c.set_receive_data { |data| output << data }
|
183
|
+
c.send_data "Content-length: 10000\n"
|
184
|
+
c.send_data "A" * 10000
|
185
|
+
EM.add_timer(0.2) { c.close_connection_after_writing }
|
186
|
+
end
|
187
|
+
}
|
188
|
+
assert_equal( "received 10000 bytes", output )
|
189
|
+
end
|
190
|
+
|
191
|
+
#--------------------------------------------------------------------
|
192
|
+
end
|