eventmachine 0.12.6-java
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +13 -0
- data/Rakefile +262 -0
- data/docs/COPYING +60 -0
- data/docs/ChangeLog +211 -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/eventmachine.gemspec +32 -0
- data/ext/binder.cpp +126 -0
- data/ext/binder.h +48 -0
- data/ext/cmain.cpp +586 -0
- data/ext/cplusplus.cpp +193 -0
- data/ext/ed.cpp +1522 -0
- data/ext/ed.h +380 -0
- data/ext/em.cpp +1937 -0
- data/ext/em.h +186 -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 +98 -0
- data/ext/eventmachine_cpp.h +95 -0
- data/ext/extconf.rb +129 -0
- data/ext/fastfilereader/extconf.rb +77 -0
- data/ext/fastfilereader/mapper.cpp +214 -0
- data/ext/fastfilereader/mapper.h +59 -0
- data/ext/fastfilereader/rubymain.cpp +127 -0
- data/ext/files.cpp +94 -0
- data/ext/files.h +65 -0
- data/ext/kb.cpp +82 -0
- data/ext/page.cpp +107 -0
- data/ext/page.h +51 -0
- data/ext/pipe.cpp +351 -0
- data/ext/project.h +119 -0
- data/ext/rubymain.cpp +847 -0
- data/ext/sigs.cpp +89 -0
- data/ext/sigs.h +32 -0
- data/ext/ssl.cpp +423 -0
- data/ext/ssl.h +90 -0
- data/java/.classpath +8 -0
- data/java/.project +17 -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 +113 -0
- data/lib/em/spawnable.rb +88 -0
- data/lib/em/streamer.rb +112 -0
- data/lib/eventmachine.rb +1926 -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 +803 -0
- data/lib/protocols/httpclient.rb +270 -0
- data/lib/protocols/line_and_text.rb +126 -0
- data/lib/protocols/linetext2.rb +161 -0
- data/lib/protocols/memcache.rb +293 -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 +153 -0
- data/lib/protocols/tcptest.rb +57 -0
- data/setup.rb +1585 -0
- data/tasks/cpp.rake +77 -0
- data/tasks/project.rake +78 -0
- data/tasks/tests.rake +193 -0
- data/tests/test_attach.rb +83 -0
- data/tests/test_basic.rb +231 -0
- data/tests/test_connection_count.rb +45 -0
- data/tests/test_defer.rb +47 -0
- data/tests/test_epoll.rb +163 -0
- data/tests/test_error_handler.rb +35 -0
- data/tests/test_errors.rb +82 -0
- data/tests/test_eventables.rb +77 -0
- data/tests/test_exc.rb +58 -0
- data/tests/test_futures.rb +214 -0
- data/tests/test_handler_check.rb +37 -0
- data/tests/test_hc.rb +218 -0
- data/tests/test_httpclient.rb +215 -0
- data/tests/test_httpclient2.rb +155 -0
- data/tests/test_kb.rb +61 -0
- data/tests/test_ltp.rb +188 -0
- data/tests/test_ltp2.rb +320 -0
- data/tests/test_next_tick.rb +109 -0
- data/tests/test_processes.rb +95 -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 +243 -0
- data/tests/test_servers.rb +80 -0
- data/tests/test_smtpclient.rb +83 -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_ssl_methods.rb +50 -0
- data/tests/test_timers.rb +148 -0
- data/tests/test_ud.rb +43 -0
- data/tests/testem.rb +31 -0
- data/web/whatis +7 -0
- metadata +214 -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,155 @@
|
|
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_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
|
141
|
+
EM.run {
|
142
|
+
http = EM::P::HttpClient2.connect :host => 'www.amazon.com', :port => 443, :ssl => true
|
143
|
+
d = http.get "/"
|
144
|
+
d.callback {
|
145
|
+
EM.stop
|
146
|
+
}
|
147
|
+
}
|
148
|
+
assert_equal(200, d.status)
|
149
|
+
end
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
end
|
154
|
+
|
155
|
+
|
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
|
+
} if $stdout.tty? # don't run the test unless it stands a chance of validity.
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
data/tests/test_ltp.rb
ADDED
@@ -0,0 +1,188 @@
|
|
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
|
+
EventMachine.run {
|
66
|
+
EventMachine.start_server( TestHost, TestPort, SimpleLineTest ) do |conn|
|
67
|
+
conn.instance_eval "@line_buffer = lines_received"
|
68
|
+
end
|
69
|
+
EventMachine.add_timer(4) {assert(false, "test timed out")}
|
70
|
+
|
71
|
+
EventMachine.connect TestHost, TestPort, StopClient do |c|
|
72
|
+
c.send_data "aaa\nbbb\r\nccc\n"
|
73
|
+
c.close_connection_after_writing
|
74
|
+
end
|
75
|
+
}
|
76
|
+
assert_equal( %w(aaa bbb ccc), lines_received )
|
77
|
+
end
|
78
|
+
|
79
|
+
#--------------------------------------------------------------------
|
80
|
+
|
81
|
+
class SimpleLineTest < EventMachine::Protocols::LineAndTextProtocol
|
82
|
+
def receive_error text
|
83
|
+
@error_message << text
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_overlength_lines
|
88
|
+
# THIS TEST CURRENTLY FAILS IN JRUBY.
|
89
|
+
assert( RUBY_PLATFORM !~ /java/ )
|
90
|
+
|
91
|
+
lines_received = []
|
92
|
+
EventMachine.run {
|
93
|
+
EventMachine.start_server( TestHost, TestPort, SimpleLineTest ) do |conn|
|
94
|
+
conn.instance_eval "@error_message = lines_received"
|
95
|
+
end
|
96
|
+
EventMachine.add_timer(4) {assert(false, "test timed out")}
|
97
|
+
|
98
|
+
EventMachine.connect TestHost, TestPort, StopClient do |c|
|
99
|
+
c.send_data "a" * (16*1024 + 1)
|
100
|
+
c.send_data "\n"
|
101
|
+
c.close_connection_after_writing
|
102
|
+
end
|
103
|
+
|
104
|
+
}
|
105
|
+
assert_equal( ["overlength line"], lines_received )
|
106
|
+
end
|
107
|
+
|
108
|
+
|
109
|
+
#--------------------------------------------------------------------
|
110
|
+
|
111
|
+
class LineAndTextTest < EventMachine::Protocols::LineAndTextProtocol
|
112
|
+
def post_init
|
113
|
+
end
|
114
|
+
def receive_line line
|
115
|
+
if line =~ /content-length:\s*(\d+)/i
|
116
|
+
@content_length = $1.to_i
|
117
|
+
elsif line.length == 0
|
118
|
+
set_binary_mode @content_length
|
119
|
+
end
|
120
|
+
end
|
121
|
+
def receive_binary_data text
|
122
|
+
send_data "received #{text.length} bytes"
|
123
|
+
close_connection_after_writing
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def test_lines_and_text
|
128
|
+
output = ''
|
129
|
+
lines_received = []
|
130
|
+
text_received = []
|
131
|
+
EventMachine.run {
|
132
|
+
EventMachine.start_server( TestHost, TestPort, LineAndTextTest ) do |conn|
|
133
|
+
conn.instance_eval "@lines = lines_received; @text = text_received"
|
134
|
+
end
|
135
|
+
EventMachine.add_timer(4) {assert(false, "test timed out")}
|
136
|
+
|
137
|
+
EventMachine.connect TestHost, TestPort, StopClient do |c|
|
138
|
+
c.set_receive_data { |data| output << data }
|
139
|
+
c.send_data "Content-length: 400\n"
|
140
|
+
c.send_data "\n"
|
141
|
+
c.send_data "A" * 400
|
142
|
+
EM.add_timer(0.1) { c.close_connection_after_writing }
|
143
|
+
end
|
144
|
+
}
|
145
|
+
assert_equal( "received 400 bytes", output )
|
146
|
+
end
|
147
|
+
|
148
|
+
#--------------------------------------------------------------------
|
149
|
+
|
150
|
+
|
151
|
+
class BinaryTextTest < EventMachine::Protocols::LineAndTextProtocol
|
152
|
+
def post_init
|
153
|
+
end
|
154
|
+
def receive_line line
|
155
|
+
if line =~ /content-length:\s*(\d+)/i
|
156
|
+
set_binary_mode $1.to_i
|
157
|
+
else
|
158
|
+
raise "protocol error"
|
159
|
+
end
|
160
|
+
end
|
161
|
+
def receive_binary_data text
|
162
|
+
send_data "received #{text.length} bytes"
|
163
|
+
close_connection_after_writing
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
def test_binary_text
|
168
|
+
output = ''
|
169
|
+
lines_received = []
|
170
|
+
text_received = []
|
171
|
+
EventMachine.run {
|
172
|
+
EventMachine.start_server( TestHost, TestPort, BinaryTextTest ) do |conn|
|
173
|
+
conn.instance_eval "@lines = lines_received; @text = text_received"
|
174
|
+
end
|
175
|
+
EventMachine.add_timer(4) {assert(false, "test timed out")}
|
176
|
+
|
177
|
+
EventMachine.connect TestHost, TestPort, StopClient do |c|
|
178
|
+
c.set_receive_data { |data| output << data }
|
179
|
+
c.send_data "Content-length: 10000\n"
|
180
|
+
c.send_data "A" * 10000
|
181
|
+
EM.add_timer(0.2) { c.close_connection_after_writing }
|
182
|
+
end
|
183
|
+
}
|
184
|
+
assert_equal( "received 10000 bytes", output )
|
185
|
+
end
|
186
|
+
|
187
|
+
#--------------------------------------------------------------------
|
188
|
+
end
|