eventmachine 0.12.8-x86-mswin32-60 → 0.12.10-x86-mswin32-60
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +14 -13
- data/Rakefile +374 -264
- data/eventmachine.gemspec +4 -5
- data/ext/binder.cpp +125 -126
- data/ext/binder.h +46 -48
- data/ext/cmain.cpp +184 -42
- data/ext/cplusplus.cpp +202 -202
- data/ext/ed.cpp +242 -81
- data/ext/ed.h +39 -22
- data/ext/em.cpp +127 -108
- data/ext/em.h +27 -18
- data/ext/emwin.cpp +3 -3
- data/ext/eventmachine.h +49 -38
- data/ext/eventmachine_cpp.h +96 -96
- data/ext/extconf.rb +147 -132
- data/ext/fastfilereader/extconf.rb +82 -76
- data/ext/project.h +151 -140
- data/ext/rubymain.cpp +222 -103
- data/ext/ssl.cpp +460 -460
- data/ext/ssl.h +94 -94
- data/java/src/com/rubyeventmachine/EmReactor.java +570 -423
- data/java/src/com/rubyeventmachine/EventableChannel.java +69 -57
- data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +189 -171
- data/java/src/com/rubyeventmachine/EventableSocketChannel.java +364 -244
- data/java/src/com/rubyeventmachine/{Application.java → application/Application.java} +194 -200
- data/java/src/com/rubyeventmachine/{Connection.java → application/Connection.java} +74 -74
- data/java/src/com/rubyeventmachine/{ConnectionFactory.java → application/ConnectionFactory.java} +36 -36
- data/java/src/com/rubyeventmachine/{DefaultConnectionFactory.java → application/DefaultConnectionFactory.java} +46 -46
- data/java/src/com/rubyeventmachine/{PeriodicTimer.java → application/PeriodicTimer.java} +38 -38
- data/java/src/com/rubyeventmachine/{Timer.java → application/Timer.java} +54 -54
- data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +109 -108
- data/java/src/com/rubyeventmachine/tests/ConnectTest.java +148 -146
- data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +53 -53
- data/java/src/com/rubyeventmachine/tests/TestServers.java +75 -74
- data/java/src/com/rubyeventmachine/tests/TestTimers.java +90 -89
- data/lib/em/connection.rb +71 -12
- data/lib/em/deferrable.rb +191 -186
- data/lib/em/protocols.rb +36 -35
- data/lib/em/protocols/httpclient2.rb +590 -582
- data/lib/em/protocols/line_and_text.rb +125 -126
- data/lib/em/protocols/linetext2.rb +161 -160
- data/lib/em/protocols/object_protocol.rb +45 -39
- data/lib/em/protocols/smtpclient.rb +357 -331
- data/lib/em/protocols/socks4.rb +66 -0
- data/lib/em/queue.rb +60 -60
- data/lib/em/timers.rb +56 -55
- data/lib/em/version.rb +1 -1
- data/lib/eventmachine.rb +125 -169
- data/lib/jeventmachine.rb +257 -142
- data/tasks/{cpp.rake → cpp.rake_example} +76 -76
- data/tests/test_attach.rb +125 -100
- data/tests/test_basic.rb +1 -2
- data/tests/test_connection_count.rb +34 -44
- data/tests/test_epoll.rb +0 -2
- data/tests/test_get_sock_opt.rb +30 -0
- data/tests/test_httpclient2.rb +3 -3
- data/tests/test_inactivity_timeout.rb +21 -1
- data/tests/test_ltp.rb +182 -188
- data/tests/test_next_tick.rb +0 -2
- data/tests/test_pause.rb +70 -0
- data/tests/test_pending_connect_timeout.rb +48 -0
- data/tests/test_ssl_args.rb +78 -67
- data/tests/test_timers.rb +162 -141
- metadata +13 -11
- data/tasks/project.rake +0 -79
- data/tasks/tests.rake +0 -193
data/tests/test_attach.rb
CHANGED
@@ -1,101 +1,126 @@
|
|
1
|
-
# $Id$
|
2
|
-
#
|
3
|
-
#----------------------------------------------------------------------------
|
4
|
-
#
|
5
|
-
# Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
6
|
-
# Gmail: blackhedd
|
7
|
-
#
|
8
|
-
# This program is free software; you can redistribute it and/or modify
|
9
|
-
# it under the terms of either: 1) the GNU General Public License
|
10
|
-
# as published by the Free Software Foundation; either version 2 of the
|
11
|
-
# License, or (at your option) any later version; or 2) Ruby's License.
|
12
|
-
#
|
13
|
-
# See the file COPYING for complete licensing information.
|
14
|
-
#
|
15
|
-
#---------------------------------------------------------------------------
|
16
|
-
#
|
17
|
-
|
18
|
-
$:.unshift "../lib"
|
19
|
-
require 'eventmachine'
|
20
|
-
require 'socket'
|
21
|
-
require 'test/unit'
|
22
|
-
|
23
|
-
|
24
|
-
class TestAttach < Test::Unit::TestCase
|
25
|
-
|
26
|
-
Host = "127.0.0.1"
|
27
|
-
Port = 9550
|
28
|
-
|
29
|
-
class EchoServer < EM::Connection
|
30
|
-
def receive_data data
|
31
|
-
send_data data
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
class EchoClient < EM::Connection
|
36
|
-
def initialize
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
$
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
assert_equal $
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
$w.
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
1
|
+
# $Id$
|
2
|
+
#
|
3
|
+
#----------------------------------------------------------------------------
|
4
|
+
#
|
5
|
+
# Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
6
|
+
# Gmail: blackhedd
|
7
|
+
#
|
8
|
+
# This program is free software; you can redistribute it and/or modify
|
9
|
+
# it under the terms of either: 1) the GNU General Public License
|
10
|
+
# as published by the Free Software Foundation; either version 2 of the
|
11
|
+
# License, or (at your option) any later version; or 2) Ruby's License.
|
12
|
+
#
|
13
|
+
# See the file COPYING for complete licensing information.
|
14
|
+
#
|
15
|
+
#---------------------------------------------------------------------------
|
16
|
+
#
|
17
|
+
|
18
|
+
$:.unshift "../lib"
|
19
|
+
require 'eventmachine'
|
20
|
+
require 'socket'
|
21
|
+
require 'test/unit'
|
22
|
+
|
23
|
+
|
24
|
+
class TestAttach < Test::Unit::TestCase
|
25
|
+
|
26
|
+
Host = "127.0.0.1"
|
27
|
+
Port = 9550
|
28
|
+
|
29
|
+
class EchoServer < EM::Connection
|
30
|
+
def receive_data data
|
31
|
+
send_data data
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class EchoClient < EM::Connection
|
36
|
+
def initialize
|
37
|
+
self.notify_readable = true
|
38
|
+
$sock.write("abc\n")
|
39
|
+
end
|
40
|
+
|
41
|
+
def notify_readable
|
42
|
+
$read = $sock.readline
|
43
|
+
$fd = detach
|
44
|
+
end
|
45
|
+
|
46
|
+
def unbind
|
47
|
+
EM.next_tick do
|
48
|
+
$sock.write("def\n")
|
49
|
+
EM.add_timer(0.5){ EM.stop }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_attach
|
55
|
+
EM.run{
|
56
|
+
EM.start_server Host, Port, EchoServer
|
57
|
+
$sock = TCPSocket.new Host, Port
|
58
|
+
EM.watch $sock, EchoClient
|
59
|
+
}
|
60
|
+
|
61
|
+
assert_equal $read, "abc\n"
|
62
|
+
unless defined? JRuby # jruby filenos are not real
|
63
|
+
assert_equal $fd, $sock.fileno
|
64
|
+
end
|
65
|
+
assert_equal false, $sock.closed?
|
66
|
+
assert_equal $sock.readline, "def\n"
|
67
|
+
end
|
68
|
+
|
69
|
+
module PipeWatch
|
70
|
+
def notify_readable
|
71
|
+
$read = $r.readline
|
72
|
+
EM.stop
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_attach_pipe
|
77
|
+
EM.run{
|
78
|
+
$r, $w = IO.pipe
|
79
|
+
EM.watch $r, PipeWatch do |c|
|
80
|
+
c.notify_readable = true
|
81
|
+
end
|
82
|
+
$w.write("ghi\n")
|
83
|
+
}
|
84
|
+
|
85
|
+
assert_equal $read, "ghi\n"
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_set_readable
|
89
|
+
EM.run{
|
90
|
+
$r, $w = IO.pipe
|
91
|
+
c = EM.watch $r, PipeWatch do |c|
|
92
|
+
c.notify_readable = false
|
93
|
+
end
|
94
|
+
|
95
|
+
EM.next_tick{
|
96
|
+
$before = c.notify_readable?
|
97
|
+
c.notify_readable = true
|
98
|
+
$after = c.notify_readable?
|
99
|
+
}
|
100
|
+
|
101
|
+
$w.write("jkl\n")
|
102
|
+
}
|
103
|
+
|
104
|
+
assert !$before
|
105
|
+
assert $after
|
106
|
+
assert_equal $read, "jkl\n"
|
107
|
+
end
|
108
|
+
|
109
|
+
module PipeReader
|
110
|
+
def receive_data data
|
111
|
+
$read = data
|
112
|
+
EM.stop
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_read_write_pipe
|
117
|
+
EM.run{
|
118
|
+
$r, $w = IO.pipe
|
119
|
+
EM.attach $r, PipeReader
|
120
|
+
writer = EM.attach($w)
|
121
|
+
writer.send_data 'ghi'
|
122
|
+
}
|
123
|
+
|
124
|
+
assert_equal $read, "ghi"
|
125
|
+
end
|
101
126
|
end
|
data/tests/test_basic.rb
CHANGED
@@ -1,45 +1,35 @@
|
|
1
|
-
$:.unshift "../lib"
|
2
|
-
require 'eventmachine'
|
3
|
-
require 'test/unit'
|
4
|
-
|
5
|
-
class TestConnectionCount < Test::Unit::TestCase
|
6
|
-
def test_idle_connection_count
|
7
|
-
EM.run {
|
8
|
-
$count = EM.connection_count
|
9
|
-
EM.stop_event_loop
|
10
|
-
}
|
11
|
-
|
12
|
-
assert_equal(0, $count)
|
13
|
-
end
|
14
|
-
|
15
|
-
module Client
|
16
|
-
def connection_completed
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
EM.next_tick{
|
36
|
-
EM.connect("127.0.0.1", 9999, Client)
|
37
|
-
}
|
38
|
-
}
|
39
|
-
|
40
|
-
assert_equal(0, $initial)
|
41
|
-
assert_equal(1, $server_started)
|
42
|
-
assert_equal(2, $server_connected)
|
43
|
-
assert_equal(3, $client_connected)
|
44
|
-
end
|
1
|
+
$:.unshift "../lib"
|
2
|
+
require 'eventmachine'
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
class TestConnectionCount < Test::Unit::TestCase
|
6
|
+
def test_idle_connection_count
|
7
|
+
EM.run {
|
8
|
+
$count = EM.connection_count
|
9
|
+
EM.stop_event_loop
|
10
|
+
}
|
11
|
+
|
12
|
+
assert_equal(0, $count)
|
13
|
+
end
|
14
|
+
|
15
|
+
module Client
|
16
|
+
def connection_completed
|
17
|
+
$client_conns += 1
|
18
|
+
EM.stop if $client_conns == 3
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_with_some_connections
|
23
|
+
EM.run {
|
24
|
+
$client_conns = 0
|
25
|
+
$initial_conns = EM.connection_count
|
26
|
+
EM.start_server("127.0.0.1", 9999)
|
27
|
+
$server_conns = EM.connection_count
|
28
|
+
3.times { EM.connect("127.0.0.1", 9999, Client) }
|
29
|
+
}
|
30
|
+
|
31
|
+
assert_equal(0, $initial_conns)
|
32
|
+
assert_equal(1, $server_conns)
|
33
|
+
assert_equal(4, $client_conns + $server_conns)
|
34
|
+
end
|
45
35
|
end
|
data/tests/test_epoll.rb
CHANGED
@@ -94,7 +94,6 @@ class TestEpoll < Test::Unit::TestCase
|
|
94
94
|
n = 0
|
95
95
|
work_proc = proc {n += 1}
|
96
96
|
callback_proc = proc {EM.stop}
|
97
|
-
EM.epoll
|
98
97
|
EM.run {
|
99
98
|
EM.defer work_proc, callback_proc
|
100
99
|
}
|
@@ -120,7 +119,6 @@ class TestEpoll < Test::Unit::TestCase
|
|
120
119
|
|
121
120
|
def test_datagrams
|
122
121
|
$in = $out = ""
|
123
|
-
EM.epoll
|
124
122
|
EM.run {
|
125
123
|
EM.open_datagram_socket "127.0.0.1", 9500, TestDatagramServer
|
126
124
|
EM.open_datagram_socket "127.0.0.1", 0, TestDatagramClient
|
@@ -0,0 +1,30 @@
|
|
1
|
+
$:.unshift File.expand_path(File.dirname(__FILE__) + "/../lib")
|
2
|
+
require 'eventmachine'
|
3
|
+
require 'socket'
|
4
|
+
require 'test/unit'
|
5
|
+
|
6
|
+
class TestGetSockOpt < Test::Unit::TestCase
|
7
|
+
|
8
|
+
def setup
|
9
|
+
assert(!EM.reactor_running?)
|
10
|
+
end
|
11
|
+
|
12
|
+
def teardown
|
13
|
+
assert(!EM.reactor_running?)
|
14
|
+
end
|
15
|
+
|
16
|
+
#-------------------------------------
|
17
|
+
|
18
|
+
def test_get_sock_opt
|
19
|
+
test = self
|
20
|
+
EM.run do
|
21
|
+
EM.connect 'google.com', 80, Module.new {
|
22
|
+
define_method :connection_completed do
|
23
|
+
val = get_sock_opt Socket::SOL_SOCKET, Socket::SO_ERROR
|
24
|
+
test.assert_equal "\0\0\0\0", val
|
25
|
+
EM.stop
|
26
|
+
end
|
27
|
+
}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/tests/test_httpclient2.rb
CHANGED
@@ -80,7 +80,7 @@ class TestHttpClient2 < Test::Unit::TestCase
|
|
80
80
|
def test_get
|
81
81
|
content = nil
|
82
82
|
EM.run {
|
83
|
-
http = EM::P::HttpClient2.connect "
|
83
|
+
http = EM::P::HttpClient2.connect "google.com", 80
|
84
84
|
d = http.get "/"
|
85
85
|
d.callback {
|
86
86
|
content = d.content
|
@@ -96,7 +96,7 @@ class TestHttpClient2 < Test::Unit::TestCase
|
|
96
96
|
def _test_get_multiple
|
97
97
|
content = nil
|
98
98
|
EM.run {
|
99
|
-
http = EM::P::HttpClient2.connect "
|
99
|
+
http = EM::P::HttpClient2.connect "google.com", 80
|
100
100
|
d = http.get "/"
|
101
101
|
d.callback {
|
102
102
|
e = http.get "/"
|
@@ -112,7 +112,7 @@ class TestHttpClient2 < Test::Unit::TestCase
|
|
112
112
|
def test_get_pipeline
|
113
113
|
headers, headers2 = nil, nil
|
114
114
|
EM.run {
|
115
|
-
http = EM::P::HttpClient2.connect "
|
115
|
+
http = EM::P::HttpClient2.connect "google.com", 80
|
116
116
|
d = http.get("/")
|
117
117
|
d.callback {
|
118
118
|
headers = d.headers
|
@@ -15,7 +15,7 @@ class TestInactivityTimeout < Test::Unit::TestCase
|
|
15
15
|
assert_equal(0.0, $timeout)
|
16
16
|
end
|
17
17
|
|
18
|
-
def
|
18
|
+
def test_set_and_get
|
19
19
|
$timeout = nil
|
20
20
|
EM.run {
|
21
21
|
c = EM.connect("127.0.0.1", 54321)
|
@@ -27,4 +27,24 @@ class TestInactivityTimeout < Test::Unit::TestCase
|
|
27
27
|
assert_equal(2.5, $timeout)
|
28
28
|
end
|
29
29
|
|
30
|
+
module TimeoutHandler
|
31
|
+
def unbind
|
32
|
+
EM.stop
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_for_real
|
37
|
+
EM.run {
|
38
|
+
EM.heartbeat_interval = 0.1
|
39
|
+
EM.start_server("127.0.0.1", 12345)
|
40
|
+
EM.add_timer(0.2) {
|
41
|
+
$start = Time.now
|
42
|
+
c = EM.connect("127.0.0.1", 12345, TimeoutHandler)
|
43
|
+
c.comm_inactivity_timeout = 2.5
|
44
|
+
}
|
45
|
+
}
|
46
|
+
|
47
|
+
assert_in_delta(2.5, (Time.now - $start), 0.3)
|
48
|
+
end
|
49
|
+
|
30
50
|
end
|
data/tests/test_ltp.rb
CHANGED
@@ -1,188 +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
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
EventMachine.
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
}
|
184
|
-
assert_equal( "received 10000 bytes", output )
|
185
|
-
end
|
186
|
-
|
187
|
-
#--------------------------------------------------------------------
|
188
|
-
end
|
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
|