smparkes-eventmachine 0.12.10
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +15 -0
- data/README +81 -0
- data/Rakefile +374 -0
- data/docs/COPYING +60 -0
- data/docs/ChangeLog +211 -0
- data/docs/DEFERRABLES +133 -0
- data/docs/EPOLL +141 -0
- data/docs/GNU +281 -0
- data/docs/INSTALL +13 -0
- data/docs/KEYBOARD +38 -0
- data/docs/LEGAL +25 -0
- data/docs/LIGHTWEIGHT_CONCURRENCY +70 -0
- data/docs/PURE_RUBY +75 -0
- data/docs/RELEASE_NOTES +94 -0
- data/docs/SMTP +2 -0
- data/docs/SPAWNED_PROCESSES +89 -0
- data/docs/TODO +8 -0
- data/eventmachine.gemspec +40 -0
- data/examples/ex_channel.rb +43 -0
- data/examples/ex_queue.rb +2 -0
- data/examples/helper.rb +2 -0
- data/ext/binder.cpp +125 -0
- data/ext/binder.h +46 -0
- data/ext/cmain.cpp +827 -0
- data/ext/cplusplus.cpp +202 -0
- data/ext/ed.cpp +1901 -0
- data/ext/ed.h +424 -0
- data/ext/em.cpp +2288 -0
- data/ext/em.h +229 -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 +122 -0
- data/ext/eventmachine_cpp.h +96 -0
- data/ext/extconf.rb +150 -0
- data/ext/fastfilereader/extconf.rb +85 -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 +81 -0
- data/ext/page.cpp +107 -0
- data/ext/page.h +51 -0
- data/ext/pipe.cpp +349 -0
- data/ext/project.h +156 -0
- data/ext/rubymain.cpp +1194 -0
- data/ext/sigs.cpp +89 -0
- data/ext/sigs.h +32 -0
- data/ext/ssl.cpp +460 -0
- data/ext/ssl.h +94 -0
- data/java/.classpath +8 -0
- data/java/.project +17 -0
- data/java/src/com/rubyeventmachine/EmReactor.java +570 -0
- data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
- data/java/src/com/rubyeventmachine/EventableChannel.java +69 -0
- data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +189 -0
- data/java/src/com/rubyeventmachine/EventableSocketChannel.java +364 -0
- data/java/src/com/rubyeventmachine/application/Application.java +194 -0
- data/java/src/com/rubyeventmachine/application/Connection.java +74 -0
- data/java/src/com/rubyeventmachine/application/ConnectionFactory.java +37 -0
- data/java/src/com/rubyeventmachine/application/DefaultConnectionFactory.java +46 -0
- data/java/src/com/rubyeventmachine/application/PeriodicTimer.java +38 -0
- data/java/src/com/rubyeventmachine/application/Timer.java +54 -0
- data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +109 -0
- data/java/src/com/rubyeventmachine/tests/ConnectTest.java +148 -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 +75 -0
- data/java/src/com/rubyeventmachine/tests/TestTimers.java +90 -0
- data/lib/em/buftok.rb +138 -0
- data/lib/em/callback.rb +26 -0
- data/lib/em/channel.rb +57 -0
- data/lib/em/connection.rb +564 -0
- data/lib/em/deferrable.rb +192 -0
- data/lib/em/file_watch.rb +54 -0
- data/lib/em/future.rb +61 -0
- data/lib/em/messages.rb +66 -0
- data/lib/em/process_watch.rb +44 -0
- data/lib/em/processes.rb +119 -0
- data/lib/em/protocols/header_and_content.rb +138 -0
- data/lib/em/protocols/httpclient.rb +263 -0
- data/lib/em/protocols/httpclient2.rb +590 -0
- data/lib/em/protocols/line_and_text.rb +125 -0
- data/lib/em/protocols/linetext2.rb +161 -0
- data/lib/em/protocols/memcache.rb +323 -0
- data/lib/em/protocols/object_protocol.rb +45 -0
- data/lib/em/protocols/postgres3.rb +247 -0
- data/lib/em/protocols/saslauth.rb +175 -0
- data/lib/em/protocols/smtpclient.rb +357 -0
- data/lib/em/protocols/smtpserver.rb +547 -0
- data/lib/em/protocols/socks4.rb +66 -0
- data/lib/em/protocols/stomp.rb +200 -0
- data/lib/em/protocols/tcptest.rb +53 -0
- data/lib/em/protocols.rb +36 -0
- data/lib/em/queue.rb +61 -0
- data/lib/em/spawnable.rb +85 -0
- data/lib/em/streamer.rb +130 -0
- data/lib/em/timers.rb +56 -0
- data/lib/em/version.rb +3 -0
- data/lib/eventmachine.rb +1592 -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/evma.rb +32 -0
- data/lib/jeventmachine.rb +257 -0
- data/lib/pr_eventmachine.rb +1022 -0
- data/setup.rb +1585 -0
- data/tasks/cpp.rake_example +77 -0
- data/tests/client.crt +31 -0
- data/tests/client.key +51 -0
- data/tests/test_attach.rb +126 -0
- data/tests/test_basic.rb +284 -0
- data/tests/test_channel.rb +63 -0
- data/tests/test_connection_count.rb +35 -0
- data/tests/test_defer.rb +47 -0
- data/tests/test_epoll.rb +160 -0
- data/tests/test_error_handler.rb +35 -0
- data/tests/test_errors.rb +82 -0
- data/tests/test_exc.rb +55 -0
- data/tests/test_file_watch.rb +49 -0
- data/tests/test_futures.rb +198 -0
- data/tests/test_get_sock_opt.rb +30 -0
- data/tests/test_handler_check.rb +37 -0
- data/tests/test_hc.rb +218 -0
- data/tests/test_httpclient.rb +218 -0
- data/tests/test_httpclient2.rb +153 -0
- data/tests/test_inactivity_timeout.rb +50 -0
- data/tests/test_kb.rb +60 -0
- data/tests/test_ltp.rb +182 -0
- data/tests/test_ltp2.rb +317 -0
- data/tests/test_next_tick.rb +133 -0
- data/tests/test_object_protocol.rb +37 -0
- data/tests/test_pause.rb +70 -0
- data/tests/test_pending_connect_timeout.rb +48 -0
- data/tests/test_process_watch.rb +48 -0
- data/tests/test_processes.rb +128 -0
- data/tests/test_proxy_connection.rb +92 -0
- data/tests/test_pure.rb +125 -0
- data/tests/test_queue.rb +44 -0
- data/tests/test_running.rb +42 -0
- data/tests/test_sasl.rb +72 -0
- data/tests/test_send_file.rb +242 -0
- data/tests/test_servers.rb +76 -0
- data/tests/test_smtpclient.rb +83 -0
- data/tests/test_smtpserver.rb +85 -0
- data/tests/test_spawn.rb +322 -0
- data/tests/test_ssl_args.rb +79 -0
- data/tests/test_ssl_methods.rb +50 -0
- data/tests/test_ssl_verify.rb +82 -0
- data/tests/test_timers.rb +162 -0
- data/tests/test_ud.rb +36 -0
- data/tests/testem.rb +31 -0
- data/web/whatis +7 -0
- metadata +237 -0
data/tests/test_ltp.rb
ADDED
@@ -0,0 +1,182 @@
|
|
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
|
+
lines_received = []
|
62
|
+
EventMachine.run {
|
63
|
+
EventMachine.start_server( TestHost, TestPort, SimpleLineTest ) do |conn|
|
64
|
+
conn.instance_eval "@line_buffer = lines_received"
|
65
|
+
end
|
66
|
+
EventMachine.add_timer(4) {assert(false, "test timed out")}
|
67
|
+
|
68
|
+
EventMachine.connect TestHost, TestPort, StopClient do |c|
|
69
|
+
c.send_data "aaa\nbbb\r\nccc\n"
|
70
|
+
c.close_connection_after_writing
|
71
|
+
end
|
72
|
+
}
|
73
|
+
assert_equal( %w(aaa bbb ccc), lines_received )
|
74
|
+
end
|
75
|
+
|
76
|
+
#--------------------------------------------------------------------
|
77
|
+
|
78
|
+
class SimpleLineTest < EventMachine::Protocols::LineAndTextProtocol
|
79
|
+
def receive_error text
|
80
|
+
@error_message << text
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_overlength_lines
|
85
|
+
lines_received = []
|
86
|
+
EventMachine.run {
|
87
|
+
EventMachine.start_server( TestHost, TestPort, SimpleLineTest ) do |conn|
|
88
|
+
conn.instance_eval "@error_message = lines_received"
|
89
|
+
end
|
90
|
+
EventMachine.add_timer(4) {assert(false, "test timed out")}
|
91
|
+
|
92
|
+
EventMachine.connect TestHost, TestPort, StopClient do |c|
|
93
|
+
c.send_data "a" * (16*1024 + 1)
|
94
|
+
c.send_data "\n"
|
95
|
+
c.close_connection_after_writing
|
96
|
+
end
|
97
|
+
|
98
|
+
}
|
99
|
+
assert_equal( ["overlength line"], lines_received )
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
#--------------------------------------------------------------------
|
104
|
+
|
105
|
+
class LineAndTextTest < EventMachine::Protocols::LineAndTextProtocol
|
106
|
+
def post_init
|
107
|
+
end
|
108
|
+
def receive_line line
|
109
|
+
if line =~ /content-length:\s*(\d+)/i
|
110
|
+
@content_length = $1.to_i
|
111
|
+
elsif line.length == 0
|
112
|
+
set_binary_mode @content_length
|
113
|
+
end
|
114
|
+
end
|
115
|
+
def receive_binary_data text
|
116
|
+
send_data "received #{text.length} bytes"
|
117
|
+
close_connection_after_writing
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_lines_and_text
|
122
|
+
output = ''
|
123
|
+
lines_received = []
|
124
|
+
text_received = []
|
125
|
+
EventMachine.run {
|
126
|
+
EventMachine.start_server( TestHost, TestPort, LineAndTextTest ) do |conn|
|
127
|
+
conn.instance_eval "@lines = lines_received; @text = text_received"
|
128
|
+
end
|
129
|
+
EventMachine.add_timer(4) {assert(false, "test timed out")}
|
130
|
+
|
131
|
+
EventMachine.connect TestHost, TestPort, StopClient do |c|
|
132
|
+
c.set_receive_data { |data| output << data }
|
133
|
+
c.send_data "Content-length: 400\n"
|
134
|
+
c.send_data "\n"
|
135
|
+
c.send_data "A" * 400
|
136
|
+
EM.add_timer(0.1) { c.close_connection_after_writing }
|
137
|
+
end
|
138
|
+
}
|
139
|
+
assert_equal( "received 400 bytes", output )
|
140
|
+
end
|
141
|
+
|
142
|
+
#--------------------------------------------------------------------
|
143
|
+
|
144
|
+
|
145
|
+
class BinaryTextTest < EventMachine::Protocols::LineAndTextProtocol
|
146
|
+
def post_init
|
147
|
+
end
|
148
|
+
def receive_line line
|
149
|
+
if line =~ /content-length:\s*(\d+)/i
|
150
|
+
set_binary_mode $1.to_i
|
151
|
+
else
|
152
|
+
raise "protocol error"
|
153
|
+
end
|
154
|
+
end
|
155
|
+
def receive_binary_data text
|
156
|
+
send_data "received #{text.length} bytes"
|
157
|
+
close_connection_after_writing
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_binary_text
|
162
|
+
output = ''
|
163
|
+
lines_received = []
|
164
|
+
text_received = []
|
165
|
+
EventMachine.run {
|
166
|
+
EventMachine.start_server( TestHost, TestPort, BinaryTextTest ) do |conn|
|
167
|
+
conn.instance_eval "@lines = lines_received; @text = text_received"
|
168
|
+
end
|
169
|
+
EventMachine.add_timer(4) {assert(false, "test timed out")}
|
170
|
+
|
171
|
+
EventMachine.connect TestHost, TestPort, StopClient do |c|
|
172
|
+
c.set_receive_data { |data| output << data }
|
173
|
+
c.send_data "Content-length: 10000\n"
|
174
|
+
c.send_data "A" * 10000
|
175
|
+
EM.add_timer(0.2) { c.close_connection_after_writing }
|
176
|
+
end
|
177
|
+
}
|
178
|
+
assert_equal( "received 10000 bytes", output )
|
179
|
+
end
|
180
|
+
|
181
|
+
#--------------------------------------------------------------------
|
182
|
+
end
|
data/tests/test_ltp2.rb
ADDED
@@ -0,0 +1,317 @@
|
|
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
|
+
$:.unshift "../lib"
|
29
|
+
require 'eventmachine'
|
30
|
+
require 'test/unit'
|
31
|
+
|
32
|
+
# TODO!!! Need tests for overlength headers and text bodies.
|
33
|
+
|
34
|
+
class TestLineText2 < Test::Unit::TestCase
|
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
|
+
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
|
111
|
+
Line 2
|
112
|
+
|
113
|
+
0000000000Line 3
|
114
|
+
Line 4
|
115
|
+
)
|
116
|
+
|
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
|
316
|
+
|
317
|
+
end
|
@@ -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
|
+
|
28
|
+
$:.unshift "../lib"
|
29
|
+
require 'eventmachine'
|
30
|
+
require 'test/unit'
|
31
|
+
|
32
|
+
class TestNextTick < Test::Unit::TestCase
|
33
|
+
|
34
|
+
def test_tick_arg
|
35
|
+
pr = proc {EM.stop}
|
36
|
+
EM.run {
|
37
|
+
EM.next_tick pr
|
38
|
+
}
|
39
|
+
assert true
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_tick_block
|
43
|
+
EM.run {
|
44
|
+
EM.next_tick {EM.stop}
|
45
|
+
}
|
46
|
+
assert true
|
47
|
+
end
|
48
|
+
|
49
|
+
# This illustrates the solution to a long-standing problem.
|
50
|
+
# It's now possible to correctly nest calls to EM#run.
|
51
|
+
# See the source code commentary for EM#run for more info.
|
52
|
+
#
|
53
|
+
def test_run_run
|
54
|
+
EM.run {
|
55
|
+
EM.run {
|
56
|
+
EM.next_tick {EM.stop}
|
57
|
+
}
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_pre_run_queue
|
62
|
+
x = false
|
63
|
+
EM.next_tick { EM.stop; x = true }
|
64
|
+
EM.run { EM.add_timer(0.2) { EM.stop } }
|
65
|
+
assert x
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_cleanup_after_stop
|
69
|
+
x = true
|
70
|
+
EM.run{
|
71
|
+
EM.next_tick{
|
72
|
+
EM.stop
|
73
|
+
EM.next_tick{ x=false }
|
74
|
+
}
|
75
|
+
}
|
76
|
+
EM.run{
|
77
|
+
EM.next_tick{ EM.stop }
|
78
|
+
}
|
79
|
+
assert x
|
80
|
+
end
|
81
|
+
|
82
|
+
# We now support an additional parameter for EM#run.
|
83
|
+
# You can pass two procs to EM#run now. The first is executed as the normal
|
84
|
+
# run block. The second (if given) is scheduled for execution after the
|
85
|
+
# reactor loop completes.
|
86
|
+
# The reason for supporting this is subtle. There has always been an expectation
|
87
|
+
# that EM#run doesn't return until after the reactor loop ends. But now it's
|
88
|
+
# possible to nest calls to EM#run, which means that a nested call WILL
|
89
|
+
# RETURN. In order to write code that will run correctly either way, it's
|
90
|
+
# recommended to put any code which must execute after the reactor completes
|
91
|
+
# in the second parameter.
|
92
|
+
#
|
93
|
+
def test_run_run_2
|
94
|
+
a = proc {EM.stop}
|
95
|
+
b = proc {assert true}
|
96
|
+
EM.run a, b
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
# This illustrates that EM#run returns when it's called nested.
|
101
|
+
# This isn't a feature, rather it's something to be wary of when writing code
|
102
|
+
# that must run correctly even if EM#run is called while a reactor is already
|
103
|
+
# running.
|
104
|
+
def test_run_run_3
|
105
|
+
a = []
|
106
|
+
EM.run {
|
107
|
+
EM.run proc {EM.stop}, proc {a << 2}
|
108
|
+
a << 1
|
109
|
+
}
|
110
|
+
assert_equal( [1,2], a )
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
def test_schedule_on_reactor_thread
|
115
|
+
x = false
|
116
|
+
EM.run do
|
117
|
+
EM.schedule { x = true }
|
118
|
+
EM.stop
|
119
|
+
end
|
120
|
+
assert x
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_schedule_from_thread
|
124
|
+
x = false
|
125
|
+
EM.run do
|
126
|
+
Thread.new { EM.schedule { x = true } }.join
|
127
|
+
assert !x
|
128
|
+
EM.next_tick { EM.stop }
|
129
|
+
end
|
130
|
+
assert x
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
$:.unshift "../lib"
|
2
|
+
require 'eventmachine'
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
class TestObjectProtocol < Test::Unit::TestCase
|
6
|
+
Host = "127.0.0.1"
|
7
|
+
Port = 9550
|
8
|
+
|
9
|
+
module Server
|
10
|
+
include EM::P::ObjectProtocol
|
11
|
+
def post_init
|
12
|
+
send_object :hello=>'world'
|
13
|
+
end
|
14
|
+
def receive_object obj
|
15
|
+
$server = obj
|
16
|
+
EM.stop
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module Client
|
21
|
+
include EM::P::ObjectProtocol
|
22
|
+
def receive_object obj
|
23
|
+
$client = obj
|
24
|
+
send_object 'you_said'=>obj
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_send_receive
|
29
|
+
EM.run{
|
30
|
+
EM.start_server Host, Port, Server
|
31
|
+
EM.connect Host, Port, Client
|
32
|
+
}
|
33
|
+
|
34
|
+
assert($client == {:hello=>'world'})
|
35
|
+
assert($server == {'you_said'=>{:hello=>'world'}})
|
36
|
+
end
|
37
|
+
end
|