eventmachine 0.5.3 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,85 @@
1
+ # $Id: test_eventables.rb 252 2006-09-02 19:23:20Z blackhedd $
2
+ #
3
+ # Author:: blackhedd (gmail address: garbagecat10).
4
+ # Date:: 8 Apr 2006
5
+ #
6
+ # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
7
+ #
8
+ # This program is made available under the terms of the GPL version 2.
9
+ #
10
+ # See EventMachine and EventMachine::Connection for documentation and
11
+ # usage examples.
12
+ #
13
+ #----------------------------------------------------------------------------
14
+ #
15
+ # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
16
+ #
17
+ # Gmail: garbagecat10
18
+ #
19
+ # This program is free software; you can redistribute it and/or modify
20
+ # it under the terms of the GNU General Public License as published by
21
+ # the Free Software Foundation; either version 2 of the License, or
22
+ # (at your option) any later version.
23
+ #
24
+ # This program is distributed in the hope that it will be useful,
25
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
26
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
+ # GNU General Public License for more details.
28
+ #
29
+ # You should have received a copy of the GNU General Public License
30
+ # along with this program; if not, write to the Free Software
31
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32
+ #
33
+ #---------------------------------------------------------------------------
34
+ #
35
+ #
36
+
37
+ $:.unshift "../lib"
38
+ require 'eventmachine'
39
+
40
+ require 'test/unit/testsuite'
41
+ require 'test/unit/ui/console/testrunner'
42
+
43
+
44
+ class TestEventables < Test::Unit::TestCase
45
+
46
+ class EvTest
47
+ include EventMachine::Eventable
48
+ end
49
+
50
+ def setup
51
+ end
52
+
53
+ def teardown
54
+ end
55
+
56
+ def test_a
57
+ n = 0
58
+ tester = EvTest.new
59
+ tester.listen_event( :fire1 ) {|arg|
60
+ n = 1 if arg == "$"
61
+ EventMachine.stop
62
+ }
63
+ tester.post_event( :fire1, "$" )
64
+
65
+ EventMachine.run {
66
+ EventMachine::add_timer(1) {EventMachine.stop}
67
+ }
68
+
69
+ assert n == 1
70
+ end
71
+
72
+ end
73
+
74
+
75
+ #--------------------------------------
76
+
77
+ if __FILE__ == $0
78
+ runner = Test::Unit::UI::Console::TestRunner
79
+ suite = Test::Unit::TestSuite.new("name")
80
+ ObjectSpace.each_object(Class) do |testcase|
81
+ suite << testcase.suite if testcase < Test::Unit::TestCase
82
+ end
83
+ runner.run(suite)
84
+ end
85
+
data/tests/test_hc.rb ADDED
@@ -0,0 +1,207 @@
1
+ # $Id: test_hc.rb 281 2006-11-20 03:17:22Z blackhedd $
2
+ #
3
+ # Author:: blackhedd (gmail address: garbagecat10).
4
+ # Date:: 8 Apr 2006
5
+ #
6
+ # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
7
+ #
8
+ # This program is made available under the terms of the GPL version 2.
9
+ #
10
+ # See EventMachine and EventMachine::Connection for documentation and
11
+ # usage examples.
12
+ #
13
+ #----------------------------------------------------------------------------
14
+ #
15
+ # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
16
+ #
17
+ # Gmail: garbagecat10
18
+ #
19
+ # This program is free software; you can redistribute it and/or modify
20
+ # it under the terms of the GNU General Public License as published by
21
+ # the Free Software Foundation; either version 2 of the License, or
22
+ # (at your option) any later version.
23
+ #
24
+ # This program is distributed in the hope that it will be useful,
25
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
26
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
+ # GNU General Public License for more details.
28
+ #
29
+ # You should have received a copy of the GNU General Public License
30
+ # along with this program; if not, write to the Free Software
31
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32
+ #
33
+ #---------------------------------------------------------------------------
34
+ #
35
+ #
36
+
37
+ $:.unshift "../lib"
38
+ require 'eventmachine'
39
+
40
+ class TestHeaderAndContentProtocol < Test::Unit::TestCase
41
+
42
+ TestHost = "127.0.0.1"
43
+ TestPort = 8905
44
+
45
+
46
+ #--------------------------------------------------------------------
47
+
48
+ class SimpleTest < EventMachine::Protocols::HeaderAndContentProtocol
49
+ attr_reader :first_header, :my_headers, :request
50
+
51
+ def receive_first_header_line hdr
52
+ @first_header ||= []
53
+ @first_header << hdr
54
+ end
55
+ def receive_headers hdrs
56
+ @my_headers ||= []
57
+ @my_headers << hdrs
58
+ end
59
+ def receive_request hdrs, content
60
+ @request ||= []
61
+ @request << [hdrs, content]
62
+ end
63
+ end
64
+
65
+ def test_no_content
66
+ Thread.abort_on_exception = true
67
+ the_connection = nil
68
+ EventMachine.run {
69
+ EventMachine.start_server( TestHost, TestPort, SimpleTest ) do |conn|
70
+ the_connection = conn
71
+ end
72
+ EventMachine.add_timer(4) {raise "test timed out"}
73
+ EventMachine.defer proc {
74
+ t = TCPSocket.new TestHost, TestPort
75
+ t.write [
76
+ "aaa\n", "bbb\r\n", "ccc\n", "\n"
77
+ ].join
78
+ t.close
79
+ }, proc {
80
+ EventMachine.stop
81
+ }
82
+ }
83
+ assert_equal( ["aaa"], the_connection.first_header )
84
+ assert_equal( [%w(aaa bbb ccc)], the_connection.my_headers )
85
+ assert_equal( [[%w(aaa bbb ccc), ""]], the_connection.request )
86
+ end
87
+
88
+ def test_content
89
+ Thread.abort_on_exception = true
90
+ the_connection = nil
91
+ content = "A" * 50
92
+ headers = ["aaa", "bbb", "Content-length: #{content.length}", "ccc"]
93
+ EventMachine.run {
94
+ EventMachine.start_server( TestHost, TestPort, SimpleTest ) do |conn|
95
+ the_connection = conn
96
+ end
97
+ EventMachine.add_timer(4) {raise "test timed out"}
98
+ EventMachine.defer proc {
99
+ t = TCPSocket.new TestHost, TestPort
100
+ headers.each {|h| t.write "#{h}\r\n" }
101
+ t.write "\n"
102
+ t.write content
103
+ t.close
104
+ }, proc {
105
+ EventMachine.stop
106
+ }
107
+ }
108
+ assert_equal( ["aaa"], the_connection.first_header )
109
+ assert_equal( [headers], the_connection.my_headers )
110
+ assert_equal( [[headers, content]], the_connection.request )
111
+ end
112
+
113
+ def test_several_requests
114
+ Thread.abort_on_exception = true
115
+ the_connection = nil
116
+ content = "A" * 50
117
+ headers = ["aaa", "bbb", "Content-length: #{content.length}", "ccc"]
118
+ EventMachine.run {
119
+ EventMachine.start_server( TestHost, TestPort, SimpleTest ) do |conn|
120
+ the_connection = conn
121
+ end
122
+ EventMachine.add_timer(4) {raise "test timed out"}
123
+ EventMachine.defer proc {
124
+ t = TCPSocket.new TestHost, TestPort
125
+ 5.times {
126
+ headers.each {|h| t.write "#{h}\r\n" }
127
+ t.write "\n"
128
+ t.write content
129
+ }
130
+ t.close
131
+ }, proc {
132
+ EventMachine.stop
133
+ }
134
+ }
135
+ assert_equal( ["aaa"] * 5, the_connection.first_header )
136
+ assert_equal( [headers] * 5, the_connection.my_headers )
137
+ assert_equal( [[headers, content]] * 5, the_connection.request )
138
+ end
139
+
140
+
141
+ def x_test_multiple_content_length_headers
142
+ # This is supposed to throw a RuntimeError but it throws a C++ exception instead.
143
+ Thread.abort_on_exception = true
144
+ the_connection = nil
145
+ content = "A" * 50
146
+ headers = ["aaa", "bbb", ["Content-length: #{content.length}"]*2, "ccc"].flatten
147
+ EventMachine.run {
148
+ EventMachine.start_server( TestHost, TestPort, SimpleTest ) do |conn|
149
+ the_connection = conn
150
+ end
151
+ EventMachine.add_timer(4) {raise "test timed out"}
152
+ EventMachine.defer proc {
153
+ t = TCPSocket.new TestHost, TestPort
154
+ headers.each {|h| t.write "#{h}\r\n" }
155
+ t.write "\n"
156
+ t.write content
157
+ t.close
158
+ }, proc {
159
+ EventMachine.stop
160
+ }
161
+ }
162
+ end
163
+
164
+ def test_interpret_headers
165
+ Thread.abort_on_exception = true
166
+ the_connection = nil
167
+ content = "A" * 50
168
+ headers = [
169
+ "GET / HTTP/1.0",
170
+ "Accept: aaa",
171
+ "User-Agent: bbb",
172
+ "Host: ccc",
173
+ "x-tempest-header:ddd"
174
+ ]
175
+ EventMachine.run {
176
+ EventMachine.start_server( TestHost, TestPort, SimpleTest ) do |conn|
177
+ the_connection = conn
178
+ end
179
+ EventMachine.add_timer(4) {raise "test timed out"}
180
+ EventMachine.defer proc {
181
+ t = TCPSocket.new TestHost, TestPort
182
+ headers.each {|h| t.write "#{h}\r\n" }
183
+ t.write "\n"
184
+ t.write content
185
+ t.close
186
+ }, proc {
187
+ EventMachine.stop
188
+ }
189
+ }
190
+
191
+ hsh = the_connection.headers_2_hash( the_connection.my_headers.shift )
192
+ assert_equal(
193
+ {
194
+ :accept => "aaa",
195
+ :user_agent => "bbb",
196
+ :host => "ccc",
197
+ :x_tempest_header => "ddd"
198
+ },
199
+ hsh
200
+ )
201
+ end
202
+
203
+ #--------------------------------------------------------------------
204
+
205
+ end
206
+
207
+
@@ -0,0 +1,94 @@
1
+ # $Id: test_httpclient.rb 226 2006-08-10 08:55:49Z blackhedd $
2
+ #
3
+ # Author:: blackhedd (gmail address: garbagecat10).
4
+ # Date:: 8 Apr 2006
5
+ #
6
+ # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
7
+ #
8
+ # This program is made available under the terms of the GPL version 2.
9
+ #
10
+ # See EventMachine and EventMachine::Connection for documentation and
11
+ # usage examples.
12
+ #
13
+ #----------------------------------------------------------------------------
14
+ #
15
+ # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
16
+ #
17
+ # Gmail: garbagecat10
18
+ #
19
+ # This program is free software; you can redistribute it and/or modify
20
+ # it under the terms of the GNU General Public License as published by
21
+ # the Free Software Foundation; either version 2 of the License, or
22
+ # (at your option) any later version.
23
+ #
24
+ # This program is distributed in the hope that it will be useful,
25
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
26
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
+ # GNU General Public License for more details.
28
+ #
29
+ # You should have received a copy of the GNU General Public License
30
+ # along with this program; if not, write to the Free Software
31
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32
+ #
33
+ #---------------------------------------------------------------------------
34
+ #
35
+ #
36
+
37
+ $:.unshift "../lib"
38
+ require 'eventmachine'
39
+
40
+ class TestHttpClient < Test::Unit::TestCase
41
+
42
+ def setup
43
+ end
44
+
45
+ def teardown
46
+ end
47
+
48
+ #-------------------------------------
49
+
50
+ def test_http_client
51
+ ok = false
52
+ EventMachine.run {
53
+ c = EventMachine::Protocols::HttpClient.send :request, :host => "www.bayshorenetworks.com", :port => 80
54
+ c.callback {
55
+ ok = true
56
+ EventMachine.stop
57
+ }
58
+ c.errback {EventMachine.stop} # necessary, otherwise a failure blocks the test suite forever.
59
+ }
60
+ assert ok
61
+ end
62
+
63
+ #-------------------------------------
64
+
65
+ def test_http_client_1
66
+ ok = false
67
+ EventMachine.run {
68
+ c = EventMachine::Protocols::HttpClient.send :request, :host => "www.bayshorenetworks.com", :port => 80
69
+ c.callback {ok = true; EventMachine.stop}
70
+ c.errback {EventMachine.stop}
71
+ }
72
+ assert ok
73
+ end
74
+
75
+ #-------------------------------------
76
+
77
+ def test_http_client_2
78
+ ok = false
79
+ EventMachine.run {
80
+ c = EventMachine::Protocols::HttpClient.send :request, :host => "www.bayshorenetworks.com", :port => 80
81
+ c.callback {|result|
82
+ p result
83
+ ok = true;
84
+ EventMachine.stop
85
+ }
86
+ c.errback {EventMachine.stop}
87
+ }
88
+ assert ok
89
+ end
90
+
91
+
92
+ end
93
+
94
+
data/tests/test_ltp.rb ADDED
@@ -0,0 +1,192 @@
1
+ # $Id: test_ltp.rb 278 2006-11-16 15:54:52Z blackhedd $
2
+ #
3
+ # Author:: blackhedd (gmail address: garbagecat10).
4
+ # Date:: 8 Apr 2006
5
+ #
6
+ # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
7
+ #
8
+ # This program is made available under the terms of the GPL version 2.
9
+ #
10
+ # See EventMachine and EventMachine::Connection for documentation and
11
+ # usage examples.
12
+ #
13
+ #----------------------------------------------------------------------------
14
+ #
15
+ # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
16
+ #
17
+ # Gmail: garbagecat10
18
+ #
19
+ # This program is free software; you can redistribute it and/or modify
20
+ # it under the terms of the GNU General Public License as published by
21
+ # the Free Software Foundation; either version 2 of the License, or
22
+ # (at your option) any later version.
23
+ #
24
+ # This program is distributed in the hope that it will be useful,
25
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
26
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
+ # GNU General Public License for more details.
28
+ #
29
+ # You should have received a copy of the GNU General Public License
30
+ # along with this program; if not, write to the Free Software
31
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32
+ #
33
+ #---------------------------------------------------------------------------
34
+ #
35
+ #
36
+
37
+ $:.unshift "../lib"
38
+ require 'eventmachine'
39
+
40
+ class TestLineAndTextProtocol < Test::Unit::TestCase
41
+
42
+ TestHost = "127.0.0.1"
43
+ TestPort = 8905
44
+
45
+
46
+ #--------------------------------------------------------------------
47
+
48
+ class SimpleLineTest < EventMachine::Protocols::LineAndTextProtocol
49
+ def receive_line line
50
+ @line_buffer << line
51
+ end
52
+ end
53
+
54
+ def test_simple_lines
55
+ lines_received = []
56
+ Thread.abort_on_exception = true
57
+ EventMachine.run {
58
+ EventMachine.start_server( TestHost, TestPort, SimpleLineTest ) do |conn|
59
+ conn.instance_eval "@line_buffer = lines_received"
60
+ end
61
+ EventMachine.add_timer(4) {raise "test timed out"}
62
+ EventMachine.defer proc {
63
+ t = TCPSocket.new TestHost, TestPort
64
+ t.write [
65
+ "aaa\n", "bbb\r\n", "ccc\n"
66
+ ].join
67
+ t.close
68
+ }, proc {
69
+ EventMachine.stop
70
+ }
71
+ }
72
+ assert_equal( %w(aaa bbb ccc), lines_received )
73
+ end
74
+
75
+ #--------------------------------------------------------------------
76
+
77
+ class SimpleLineTest < EventMachine::Protocols::LineAndTextProtocol
78
+ def receive_error text
79
+ @error_message << text
80
+ end
81
+ end
82
+
83
+ def test_overlength_lines
84
+ lines_received = []
85
+ Thread.abort_on_exception = true
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) {raise "test timed out"}
91
+ EventMachine.defer proc {
92
+ t = TCPSocket.new TestHost, TestPort
93
+ t.write "a" * (16*1024 + 1)
94
+ t.write "\n"
95
+ t.close
96
+ }, proc {
97
+ EventMachine.stop
98
+ }
99
+ }
100
+ assert_equal( ["overlength line"], lines_received )
101
+ end
102
+
103
+
104
+ #--------------------------------------------------------------------
105
+
106
+ class LineAndTextTest < EventMachine::Protocols::LineAndTextProtocol
107
+ def post_init
108
+ end
109
+ def receive_line line
110
+ if line =~ /content-length:\s*(\d+)/i
111
+ @content_lenth = $1.to_i
112
+ elsif line.length == 0
113
+ set_binary_mode @content_length
114
+ end
115
+ end
116
+ def receive_binary_data text
117
+ send_data "received #{text.length} bytes"
118
+ close_connection_after_writing
119
+ end
120
+ end
121
+
122
+ def test_lines_and_text
123
+ output = nil
124
+ lines_received = []
125
+ text_received = []
126
+ Thread.abort_on_exception = true
127
+ EventMachine.run {
128
+ EventMachine.start_server( TestHost, TestPort, LineAndTextTest ) do |conn|
129
+ conn.instance_eval "@lines = lines_received; @text = text_received"
130
+ end
131
+ EventMachine.add_timer(4) {raise "test timed out"}
132
+ EventMachine.defer proc {
133
+ t = TCPSocket.new TestHost, TestPort
134
+ t.puts "Content-length: 400"
135
+ t.puts
136
+ t.write "A" * 400
137
+ output = t.read
138
+ t.close
139
+ }, proc {
140
+ EventMachine.stop
141
+ }
142
+ }
143
+ assert_equal( "received 400 bytes", output )
144
+ end
145
+
146
+ #--------------------------------------------------------------------
147
+
148
+
149
+ class BinaryTextTest < EventMachine::Protocols::LineAndTextProtocol
150
+ def post_init
151
+ end
152
+ def receive_line line
153
+ if line =~ /content-length:\s*(\d+)/i
154
+ set_binary_mode $1.to_i
155
+ else
156
+ raise "protocol error"
157
+ end
158
+ end
159
+ def receive_binary_data text
160
+ send_data "received #{text.length} bytes"
161
+ close_connection_after_writing
162
+ end
163
+ end
164
+
165
+ def test_binary_text
166
+ output = nil
167
+ lines_received = []
168
+ text_received = []
169
+ Thread.abort_on_exception = true
170
+ EventMachine.run {
171
+ EventMachine.start_server( TestHost, TestPort, BinaryTextTest ) do |conn|
172
+ conn.instance_eval "@lines = lines_received; @text = text_received"
173
+ end
174
+ EventMachine.add_timer(4) {raise "test timed out"}
175
+ EventMachine.defer proc {
176
+ t = TCPSocket.new TestHost, TestPort
177
+ t.puts "Content-length: 10000"
178
+ t.write "A" * 10000
179
+ output = t.read
180
+ t.close
181
+ }, proc {
182
+ EventMachine.stop
183
+ }
184
+ }
185
+ assert_equal( "received 10000 bytes", output )
186
+ end
187
+
188
+ #--------------------------------------------------------------------
189
+
190
+ end
191
+
192
+
data/tests/test_ud.rb ADDED
@@ -0,0 +1,52 @@
1
+ # $Id: test_ud.rb 269 2006-10-25 23:31:15Z blackhedd $
2
+ #
3
+ # Author:: blackhedd (gmail address: garbagecat10).
4
+ # Date:: 8 Apr 2006
5
+ #
6
+ # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
7
+ #
8
+ # This program is made available under the terms of the GPL version 2.
9
+ #
10
+ # See EventMachine and EventMachine::Connection for documentation and
11
+ # usage examples.
12
+ #
13
+ #----------------------------------------------------------------------------
14
+ #
15
+ # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
16
+ #
17
+ # Gmail: garbagecat10
18
+ #
19
+ # This program is free software; you can redistribute it and/or modify
20
+ # it under the terms of the GNU General Public License as published by
21
+ # the Free Software Foundation; either version 2 of the License, or
22
+ # (at your option) any later version.
23
+ #
24
+ # This program is distributed in the hope that it will be useful,
25
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
26
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
+ # GNU General Public License for more details.
28
+ #
29
+ # You should have received a copy of the GNU General Public License
30
+ # along with this program; if not, write to the Free Software
31
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32
+ #
33
+ #---------------------------------------------------------------------------
34
+ #
35
+ #
36
+
37
+ $:.unshift "../lib"
38
+ require 'eventmachine'
39
+
40
+ class TestUserDefinedEvents < Test::Unit::TestCase
41
+
42
+ def setup
43
+ end
44
+
45
+ def teardown
46
+ end
47
+
48
+ def test_a
49
+ end
50
+
51
+ end
52
+