forked from eventmachine/eventmachine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_epoll.rb
163 lines (145 loc) · 4 KB
/
test_epoll.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
62
63
64
65
66
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
# $Id$
#
# Author:: Francis Cianfrocca (gmail: blackhedd)
# Homepage:: http://rubyeventmachine.com
# Date:: 8 April 2006
#
# See EventMachine and EventMachine::Connection for documentation and
# usage examples.
#
#----------------------------------------------------------------------------
#
# Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
# Gmail: blackhedd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of either: 1) the GNU General Public License
# as published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version; or 2) Ruby's License.
#
# See the file COPYING for complete licensing information.
#
#---------------------------------------------------------------------------
#
#
#
# TODO, and I know this doesn't belong here, but if a datagram calls
# send_data outside of a receive_data, there is no return address, and
# the result is a very confusing error message.
#
require 'eventmachine'
require 'test/unit'
class TestEpoll < Test::Unit::TestCase
module TestEchoServer
def receive_data data
send_data data
close_connection_after_writing
end
end
module TestEchoClient
def connection_completed
send_data "ABCDE"
$max += 1
end
def receive_data data
raise "bad response" unless data == "ABCDE"
end
def unbind
$n -= 1
EM.stop if $n == 0
end
end
# We can set the rlimit/nofile of a process but we can only set it
# higher if we're running as root.
# On most systems, the default value is 1024.
# Java doesn't (currently) implement this.
def test_rlimit
unless RUBY_PLATFORM =~ /java/ or EM.set_descriptor_table_size >= 1024
a = EM.set_descriptor_table_size
assert( a <= 1024 )
a = EM.set_descriptor_table_size( 1024 )
assert( a == 1024 )
end
end
# Run a high-volume version of this test by kicking the number of connections
# up past 512. (Each connection uses two sockets, a client and a server.)
# (Will require running the test as root)
# This test exercises TCP clients and servers.
#
def test_descriptors
EM.epoll
s = EM.set_descriptor_table_size 60000
EM.run {
EM.start_server "127.0.0.1", 9800, TestEchoServer
$n = 0
$max = 0
100.times {
EM.connect("127.0.0.1", 9800, TestEchoClient) {$n += 1}
}
}
assert_equal(0, $n)
assert_equal(100, $max)
end
def test_defer
n = 0
work_proc = proc {n += 1}
callback_proc = proc {EM.stop}
EM.epoll
EM.run {
EM.defer work_proc, callback_proc
}
assert_equal( 1, n )
end unless RUBY_VERSION >= '1.9.0'
module TestDatagramServer
def receive_data dgm
$in = dgm
send_data "abcdefghij"
end
end
module TestDatagramClient
def post_init
send_datagram "1234567890", "127.0.0.1", 9500
end
def receive_data dgm
$out = dgm
EM.stop
end
end
def test_datagrams
$in = $out = ""
EM.epoll
EM.run {
EM.open_datagram_socket "127.0.0.1", 9500, TestDatagramServer
EM.open_datagram_socket "127.0.0.1", 0, TestDatagramClient
}
assert_equal( "1234567890", $in )
assert_equal( "abcdefghij", $out )
end
def test_unix_domain
fn = "/tmp/xxx.chain"
EM.epoll
s = EM.set_descriptor_table_size 60000
EM.run {
# The pure-Ruby version won't let us open the socket if the node already exists.
# Not sure, that actually may be correct and the compiled version is wrong.
# Pure Ruby also oddly won't let us make that many connections. This test used
# to run 100 times. Not sure where that lower connection-limit is coming from in
# pure Ruby.
# Let's not sweat the Unix-ness of the filename, since this test can't possibly
# work on Windows anyway.
#
File.unlink(fn) if File.exist?(fn)
EM.start_unix_domain_server fn, TestEchoServer
$n = 0
$max = 0
50.times {
EM.connect_unix_domain(fn, TestEchoClient) {$n += 1}
}
EM::add_timer(1) { $stderr.puts("test_unix_domain timed out!"); EM::stop }
}
assert_equal(0, $n)
assert_equal(50, $max)
ensure
File.unlink(fn) if File.exist?(fn)
end
end