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.
Files changed (66) hide show
  1. data/.gitignore +14 -13
  2. data/Rakefile +374 -264
  3. data/eventmachine.gemspec +4 -5
  4. data/ext/binder.cpp +125 -126
  5. data/ext/binder.h +46 -48
  6. data/ext/cmain.cpp +184 -42
  7. data/ext/cplusplus.cpp +202 -202
  8. data/ext/ed.cpp +242 -81
  9. data/ext/ed.h +39 -22
  10. data/ext/em.cpp +127 -108
  11. data/ext/em.h +27 -18
  12. data/ext/emwin.cpp +3 -3
  13. data/ext/eventmachine.h +49 -38
  14. data/ext/eventmachine_cpp.h +96 -96
  15. data/ext/extconf.rb +147 -132
  16. data/ext/fastfilereader/extconf.rb +82 -76
  17. data/ext/project.h +151 -140
  18. data/ext/rubymain.cpp +222 -103
  19. data/ext/ssl.cpp +460 -460
  20. data/ext/ssl.h +94 -94
  21. data/java/src/com/rubyeventmachine/EmReactor.java +570 -423
  22. data/java/src/com/rubyeventmachine/EventableChannel.java +69 -57
  23. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +189 -171
  24. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +364 -244
  25. data/java/src/com/rubyeventmachine/{Application.java → application/Application.java} +194 -200
  26. data/java/src/com/rubyeventmachine/{Connection.java → application/Connection.java} +74 -74
  27. data/java/src/com/rubyeventmachine/{ConnectionFactory.java → application/ConnectionFactory.java} +36 -36
  28. data/java/src/com/rubyeventmachine/{DefaultConnectionFactory.java → application/DefaultConnectionFactory.java} +46 -46
  29. data/java/src/com/rubyeventmachine/{PeriodicTimer.java → application/PeriodicTimer.java} +38 -38
  30. data/java/src/com/rubyeventmachine/{Timer.java → application/Timer.java} +54 -54
  31. data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +109 -108
  32. data/java/src/com/rubyeventmachine/tests/ConnectTest.java +148 -146
  33. data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +53 -53
  34. data/java/src/com/rubyeventmachine/tests/TestServers.java +75 -74
  35. data/java/src/com/rubyeventmachine/tests/TestTimers.java +90 -89
  36. data/lib/em/connection.rb +71 -12
  37. data/lib/em/deferrable.rb +191 -186
  38. data/lib/em/protocols.rb +36 -35
  39. data/lib/em/protocols/httpclient2.rb +590 -582
  40. data/lib/em/protocols/line_and_text.rb +125 -126
  41. data/lib/em/protocols/linetext2.rb +161 -160
  42. data/lib/em/protocols/object_protocol.rb +45 -39
  43. data/lib/em/protocols/smtpclient.rb +357 -331
  44. data/lib/em/protocols/socks4.rb +66 -0
  45. data/lib/em/queue.rb +60 -60
  46. data/lib/em/timers.rb +56 -55
  47. data/lib/em/version.rb +1 -1
  48. data/lib/eventmachine.rb +125 -169
  49. data/lib/jeventmachine.rb +257 -142
  50. data/tasks/{cpp.rake → cpp.rake_example} +76 -76
  51. data/tests/test_attach.rb +125 -100
  52. data/tests/test_basic.rb +1 -2
  53. data/tests/test_connection_count.rb +34 -44
  54. data/tests/test_epoll.rb +0 -2
  55. data/tests/test_get_sock_opt.rb +30 -0
  56. data/tests/test_httpclient2.rb +3 -3
  57. data/tests/test_inactivity_timeout.rb +21 -1
  58. data/tests/test_ltp.rb +182 -188
  59. data/tests/test_next_tick.rb +0 -2
  60. data/tests/test_pause.rb +70 -0
  61. data/tests/test_pending_connect_timeout.rb +48 -0
  62. data/tests/test_ssl_args.rb +78 -67
  63. data/tests/test_timers.rb +162 -141
  64. metadata +13 -11
  65. data/tasks/project.rake +0 -79
  66. data/tasks/tests.rake +0 -193
@@ -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
- $sock.write("abc\n")
38
- end
39
-
40
- def notify_readable
41
- $read = $sock.readline
42
- $fd = detach
43
- end
44
-
45
- def unbind
46
- EM.next_tick do
47
- $sock.write("def\n")
48
- EM.add_timer(0.5){ EM.stop }
49
- end
50
- end
51
- end
52
-
53
- def test_attach
54
- EM.run{
55
- EM.start_server Host, Port, EchoServer
56
- $sock = TCPSocket.new Host, Port
57
- EM.attach $sock, EchoClient
58
- }
59
-
60
- assert_equal $read, "abc\n"
61
- assert_equal $fd, $sock.fileno
62
- assert_equal false, $sock.closed?
63
- assert_equal $sock.readline, "def\n"
64
- end
65
-
66
-
67
- module PipeWatch
68
- def notify_readable
69
- $read = $r.readline
70
- EM.stop
71
- end
72
- end
73
-
74
- def test_attach_pipe
75
- EM.run{
76
- $r, $w = IO.pipe
77
- EM.attach $r, PipeWatch
78
- $w.write("ghi\n")
79
- }
80
-
81
- assert_equal $read, "ghi\n"
82
- end
83
-
84
- module PipeReader
85
- def receive_data data
86
- $read = data
87
- EM.stop
88
- end
89
- end
90
-
91
- def test_read_write_pipe
92
- EM.run{
93
- $r, $w = IO.pipe
94
- EM.attach $r, PipeReader
95
- writer = EM.attach($w)
96
- writer.send_data 'ghi'
97
- }
98
-
99
- assert_equal $read, "ghi"
100
- end
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
@@ -266,8 +266,7 @@ class TestBasic < Test::Unit::TestCase
266
266
  x = false
267
267
  assert !x
268
268
  EM.run do
269
- Thread.new { EM.schedule { x = true } }.join
270
- EM.stop
269
+ Thread.new { EM.schedule { x = true; EM.stop } }.join
271
270
  end
272
271
  assert x
273
272
  end
@@ -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
- EM.next_tick{
18
- $client_connected = EM.connection_count
19
- EM.stop
20
- }
21
- end
22
- end
23
-
24
- module Server
25
- def post_init
26
- $server_connected = EM.connection_count
27
- end
28
- end
29
-
30
- def test_with_some_connections
31
- EM.run {
32
- $initial = EM.connection_count
33
- EM.start_server("127.0.0.1", 9999, Server)
34
- $server_started = EM.connection_count
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
@@ -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
@@ -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 "www.bayshorenetworks.com", 80
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 "www.bayshorenetworks.com", 80
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 "www.microsoft.com", 80
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 test_with_set
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
@@ -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
- # 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
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