eventmachine 1.2.1-x86-mingw32 → 1.2.2-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,25 +1,31 @@
1
1
  require 'em_test_helper'
2
2
 
3
3
  class TestIdleConnection < Test::Unit::TestCase
4
- if EM.respond_to?(:get_idle_time)
5
- def test_idle_time
6
- EM.run{
7
- conn = EM.connect 'www.google.com', 80
8
- EM.add_timer(3){
9
- $idle_time = conn.get_idle_time
10
- conn.send_data "GET / HTTP/1.0\r\n\r\n"
11
- EM.next_tick{
12
- EM.next_tick{
13
- $idle_time_after_send = conn.get_idle_time
14
- conn.close_connection
15
- EM.stop
16
- }
17
- }
18
- }
19
- }
4
+ def setup
5
+ @port = next_port
6
+ end
7
+
8
+ def test_idle_time
9
+ omit_if(!EM.respond_to?(:get_idle_time))
20
10
 
21
- assert_in_delta 3, $idle_time, 0.2
22
- assert_in_delta 0, $idle_time_after_send, 0.1
11
+ a, b = nil, nil
12
+ EM.run do
13
+ EM.start_server '127.0.0.1', @port, Module.new
14
+ conn = EM.connect '127.0.0.1', @port
15
+ EM.add_timer(0.3) do
16
+ a = conn.get_idle_time
17
+ conn.send_data 'a'
18
+ EM.next_tick do
19
+ EM.next_tick do
20
+ b = conn.get_idle_time
21
+ conn.close_connection
22
+ EM.stop
23
+ end
24
+ end
25
+ end
23
26
  end
27
+
28
+ assert_in_delta 0.3, a, 0.1
29
+ assert_in_delta 0, b, 0.1
24
30
  end
25
31
  end
data/tests/test_ipv4.rb CHANGED
@@ -1,125 +1,95 @@
1
1
  require 'em_test_helper'
2
- require 'socket'
3
2
 
4
3
  class TestIPv4 < Test::Unit::TestCase
5
-
6
- if Test::Unit::TestCase.public_ipv4?
7
-
8
- # Tries to connect to www.google.com port 80 via TCP.
9
- # Timeout in 2 seconds.
10
- def test_ipv4_tcp_client
11
- conn = nil
12
- setup_timeout(2)
13
-
14
- EM.run do
15
- conn = EM::connect("www.google.com", 80) do |c|
16
- def c.connected
17
- @connected
18
- end
19
-
20
- def c.connection_completed
21
- @connected = true
22
- EM.stop
23
- end
4
+ # Runs a TCP server in the local IPv4 address, connects to it and sends a specific data.
5
+ # Timeout in 2 seconds.
6
+ def test_ipv4_tcp_local_server
7
+ omit_if(!Test::Unit::TestCase.public_ipv4?)
8
+
9
+ @@received_data = nil
10
+ @local_port = next_port
11
+ setup_timeout(2)
12
+
13
+ EM.run do
14
+ EM::start_server(@@public_ipv4, @local_port) do |s|
15
+ def s.receive_data data
16
+ @@received_data = data
17
+ EM.stop
24
18
  end
25
19
  end
26
20
 
27
- assert conn.connected
28
- end
29
-
30
- # Runs a TCP server in the local IPv4 address, connects to it and sends a specific data.
31
- # Timeout in 2 seconds.
32
- def test_ipv4_tcp_local_server
33
- @@received_data = nil
34
- @local_port = next_port
35
- setup_timeout(2)
36
-
37
- EM.run do
38
- EM::start_server(@@public_ipv4, @local_port) do |s|
39
- def s.receive_data data
40
- @@received_data = data
41
- EM.stop
42
- end
43
- end
44
-
45
- EM::connect(@@public_ipv4, @local_port) do |c|
46
- c.send_data "ipv4/tcp"
47
- end
21
+ EM::connect(@@public_ipv4, @local_port) do |c|
22
+ c.send_data "ipv4/tcp"
48
23
  end
49
-
50
- assert_equal "ipv4/tcp", @@received_data
51
24
  end
52
25
 
53
- # Runs a UDP server in the local IPv4 address, connects to it and sends a specific data.
54
- # Timeout in 2 seconds.
55
- def test_ipv4_udp_local_server
56
- @@received_data = nil
57
- @local_port = next_port
58
- setup_timeout(2)
59
-
60
- EM.run do
61
- EM::open_datagram_socket(@@public_ipv4, @local_port) do |s|
62
- def s.receive_data data
63
- @@received_data = data
64
- EM.stop
65
- end
66
- end
26
+ assert_equal "ipv4/tcp", @@received_data
27
+ end
67
28
 
68
- EM::open_datagram_socket(@@public_ipv4, next_port) do |c|
69
- c.send_datagram "ipv4/udp", @@public_ipv4, @local_port
70
- end
71
- end
29
+ # Runs a UDP server in the local IPv4 address, connects to it and sends a specific data.
30
+ # Timeout in 2 seconds.
31
+ def test_ipv4_udp_local_server
32
+ omit_if(!Test::Unit::TestCase.public_ipv4?)
72
33
 
73
- assert_equal "ipv4/udp", @@received_data
74
- end
34
+ @@received_data = nil
35
+ @local_port = next_port
36
+ setup_timeout(2)
75
37
 
76
- # Try to connect via TCP to an invalid IPv4. EM.connect should raise
77
- # EM::ConnectionError.
78
- def test_tcp_connect_to_invalid_ipv4
79
- invalid_ipv4 = "9.9:9"
80
-
81
- EM.run do
82
- begin
83
- error = nil
84
- EM.connect(invalid_ipv4, 1234)
85
- rescue => e
86
- error = e
87
- ensure
38
+ EM.run do
39
+ EM::open_datagram_socket(@@public_ipv4, @local_port) do |s|
40
+ def s.receive_data data
41
+ @@received_data = data
88
42
  EM.stop
89
- assert_equal EM::ConnectionError, (error && error.class)
90
43
  end
91
44
  end
45
+
46
+ EM::open_datagram_socket(@@public_ipv4, next_port) do |c|
47
+ c.send_datagram "ipv4/udp", @@public_ipv4, @local_port
48
+ end
92
49
  end
93
50
 
94
- # Try to send a UDP datagram to an invalid IPv4. EM.send_datagram should raise
95
- # EM::ConnectionError.
96
- def test_udp_send_datagram_to_invalid_ipv4
97
- invalid_ipv4 = "9.9:9"
98
-
99
- EM.run do
100
- begin
101
- error = nil
102
- EM.open_datagram_socket(@@public_ipv4, next_port) do |c|
103
- c.send_datagram "hello", invalid_ipv4, 1234
104
- end
105
- rescue => e
106
- error = e
107
- ensure
108
- EM.stop
109
- assert_equal EM::ConnectionError, (error && error.class)
110
- end
51
+ assert_equal "ipv4/udp", @@received_data
52
+ end
53
+
54
+ # Try to connect via TCP to an invalid IPv4. EM.connect should raise
55
+ # EM::ConnectionError.
56
+ def test_tcp_connect_to_invalid_ipv4
57
+ omit_if(!Test::Unit::TestCase.public_ipv4?)
58
+
59
+ invalid_ipv4 = "9.9:9"
60
+
61
+ EM.run do
62
+ begin
63
+ error = nil
64
+ EM.connect(invalid_ipv4, 1234)
65
+ rescue => e
66
+ error = e
67
+ ensure
68
+ EM.stop
69
+ assert_equal EM::ConnectionError, (error && error.class)
111
70
  end
112
71
  end
72
+ end
113
73
 
74
+ # Try to send a UDP datagram to an invalid IPv4. EM.send_datagram should raise
75
+ # EM::ConnectionError.
76
+ def test_udp_send_datagram_to_invalid_ipv4
77
+ omit_if(!Test::Unit::TestCase.public_ipv4?)
114
78
 
115
- else
116
- warn "no IPv4 in this host, skipping tests in #{__FILE__}"
79
+ invalid_ipv4 = "9.9:9"
117
80
 
118
- # Because some rubies will complain if a TestCase class has no tests
119
- def test_ipv4_unavailable
120
- assert true
81
+ EM.run do
82
+ begin
83
+ error = nil
84
+ EM.open_datagram_socket(@@public_ipv4, next_port) do |c|
85
+ c.send_datagram "hello", invalid_ipv4, 1234
86
+ end
87
+ rescue => e
88
+ error = e
89
+ ensure
90
+ EM.stop
91
+ assert_equal EM::ConnectionError, (error && error.class)
92
+ end
121
93
  end
122
-
123
94
  end
124
-
125
95
  end
data/tests/test_ipv6.rb CHANGED
@@ -4,32 +4,6 @@ class TestIPv6 < Test::Unit::TestCase
4
4
 
5
5
  if Test::Unit::TestCase.public_ipv6?
6
6
 
7
- # Tries to connect to ipv6.google.com (2607:f8b0:4010:800::1006) port 80 via TCP.
8
- # Timeout in 6 seconds.
9
- def test_ipv6_tcp_client_with_ipv6_google_com
10
- conn = nil
11
- setup_timeout(6)
12
-
13
- EM.run do
14
- conn = EM::connect("2607:f8b0:4010:800::1006", 80) do |c|
15
- def c.connected
16
- @connected
17
- end
18
-
19
- def c.unbind(reason)
20
- warn "unbind: #{reason.inspect}" if reason # XXX at least find out why it failed
21
- end
22
-
23
- def c.connection_completed
24
- @connected = true
25
- EM.stop
26
- end
27
- end
28
- end
29
-
30
- assert conn.connected
31
- end
32
-
33
7
  # Runs a TCP server in the local IPv6 address, connects to it and sends a specific data.
34
8
  # Timeout in 2 seconds.
35
9
  def test_ipv6_tcp_local_server
@@ -2,8 +2,12 @@ require 'em_test_helper'
2
2
 
3
3
  class TestIterator < Test::Unit::TestCase
4
4
 
5
- def get_time
6
- EM.current_time.strftime('%H:%M:%S')
5
+ # By default, format the time with tenths-of-seconds.
6
+ # Some tests should ask for extra decimal places to ensure
7
+ # that delays between iterations will receive a changed time.
8
+ def get_time(n=1)
9
+ time = EM.current_time
10
+ time.strftime('%H:%M:%S.') + time.tv_usec.to_s[0, n]
7
11
  end
8
12
 
9
13
  def test_default_concurrency
@@ -11,10 +15,10 @@ class TestIterator < Test::Unit::TestCase
11
15
  list = 1..10
12
16
  EM.run {
13
17
  EM::Iterator.new(list).each( proc {|num,iter|
14
- time = get_time
18
+ time = get_time(3)
15
19
  items[time] ||= []
16
20
  items[time] << num
17
- EM::Timer.new(1) {iter.next}
21
+ EM::Timer.new(0.02) {iter.next}
18
22
  }, proc {EM.stop})
19
23
  }
20
24
  assert_equal(10, items.keys.size)
@@ -27,10 +31,10 @@ class TestIterator < Test::Unit::TestCase
27
31
  original_list = list.dup
28
32
  EM.run {
29
33
  EM::Iterator.new(proc{list.pop || EM::Iterator::Stop}).each( proc {|num,iter|
30
- time = get_time
34
+ time = get_time(3)
31
35
  items[time] ||= []
32
36
  items[time] << num
33
- EM::Timer.new(1) {iter.next}
37
+ EM::Timer.new(0.02) {iter.next}
34
38
  }, proc {EM.stop})
35
39
  }
36
40
  assert_equal(10, items.keys.size)
@@ -52,26 +56,25 @@ class TestIterator < Test::Unit::TestCase
52
56
  assert_equal(list.to_a.sort, items.values.flatten.sort)
53
57
  end
54
58
 
55
-
56
59
  def test_changing_concurrency_affects_active_iteration
57
60
  items = {}
58
61
  list = 1..25
62
+ seen = 0
59
63
  EM.run {
60
- i = EM::Iterator.new(list,5)
64
+ i = EM::Iterator.new(list,1)
61
65
  i.each(proc {|num,iter|
62
66
  time = get_time
63
67
  items[time] ||= []
64
68
  items[time] << num
65
- EM::Timer.new(1) {iter.next}
69
+ if (seen += 1) == 5
70
+ # The first 5 items will be distinct times
71
+ # The next 20 items will happen in 2 bursts
72
+ i.concurrency = 10
73
+ end
74
+ EM::Timer.new(0.2) {iter.next}
66
75
  }, proc {EM.stop})
67
- EM.add_timer(1){
68
- i.concurrency = 1
69
- }
70
- EM.add_timer(3){
71
- i.concurrency = 3
72
- }
73
76
  }
74
- assert_equal(9, items.keys.size)
77
+ assert_in_delta(7, items.keys.size, 1)
75
78
  assert_equal(list.to_a.sort, items.values.flatten.sort)
76
79
  end
77
80
 
data/tests/test_ltp.rb CHANGED
@@ -2,7 +2,14 @@ require 'em_test_helper'
2
2
 
3
3
  class TestLineAndTextProtocol < Test::Unit::TestCase
4
4
 
5
- class SimpleLineTest < EM::P::LineAndTextProtocol
5
+ class TLP_LineBuffer < EM::P::LineAndTextProtocol
6
+ attr_reader :line_buffer
7
+
8
+ def initialize
9
+ super
10
+ @line_buffer = []
11
+ end
12
+
6
13
  def receive_line line
7
14
  @line_buffer << line
8
15
  end
@@ -27,10 +34,10 @@ class TestLineAndTextProtocol < Test::Unit::TestCase
27
34
  end
28
35
 
29
36
  def test_simple_lines
30
- lines_received = []
37
+ conn = nil
31
38
  EM.run {
32
- EM.start_server( "127.0.0.1", @port, SimpleLineTest ) do |conn|
33
- conn.instance_eval "@line_buffer = lines_received"
39
+ EM.start_server( "127.0.0.1", @port, TLP_LineBuffer ) do |c|
40
+ conn = c
34
41
  end
35
42
  setup_timeout
36
43
 
@@ -39,25 +46,35 @@ class TestLineAndTextProtocol < Test::Unit::TestCase
39
46
  c.close_connection_after_writing
40
47
  end
41
48
  }
42
- assert_equal( %w(aaa bbb ccc), lines_received )
49
+ assert_equal( %w(aaa bbb ccc), conn.line_buffer)
43
50
  end
44
51
 
45
52
  #--------------------------------------------------------------------
46
53
 
47
- class SimpleLineTest < EM::P::LineAndTextProtocol
54
+ class TLP_ErrorMessage < EM::P::LineAndTextProtocol
55
+ attr_reader :error_message
56
+
57
+ def initialize
58
+ super
59
+ @error_message = []
60
+ end
61
+
62
+ def receive_line text
63
+ raise
64
+ end
65
+
48
66
  def receive_error text
49
67
  @error_message << text
50
68
  end
51
69
  end
52
70
 
53
71
  def test_overlength_lines
54
- lines_received = []
72
+ conn = nil
55
73
  EM.run {
56
- EM.start_server( "127.0.0.1", @port, SimpleLineTest ) do |conn|
57
- conn.instance_eval "@error_message = lines_received"
74
+ EM.start_server( "127.0.0.1", @port, TLP_ErrorMessage ) do |c|
75
+ conn = c
58
76
  end
59
77
  setup_timeout
60
-
61
78
  EM.connect "127.0.0.1", @port, StopClient do |c|
62
79
  c.send_data "a" * (16*1024 + 1)
63
80
  c.send_data "\n"
@@ -65,7 +82,7 @@ class TestLineAndTextProtocol < Test::Unit::TestCase
65
82
  end
66
83
 
67
84
  }
68
- assert_equal( ["overlength line"], lines_received )
85
+ assert_equal( ["overlength line"], conn.error_message )
69
86
  end
70
87
 
71
88
 
@@ -25,7 +25,7 @@ class TestPendingConnectTimeout < Test::Unit::TestCase
25
25
 
26
26
  timeout_handler = Module.new do
27
27
  define_method :unbind do
28
- finish = Time.now
28
+ finish = EM.current_time
29
29
  EM.stop
30
30
  end
31
31
  end
@@ -33,8 +33,8 @@ class TestPendingConnectTimeout < Test::Unit::TestCase
33
33
  EM.run {
34
34
  setup_timeout
35
35
  EM.heartbeat_interval = 0.1
36
- start = Time.now
37
- c = EM.connect("1.2.3.4", 54321, timeout_handler)
36
+ start = EM.current_time
37
+ c = EM.connect('192.0.2.0', 54321, timeout_handler)
38
38
  c.pending_connect_timeout = 0.2
39
39
  }
40
40