root/trunk/tests/test_epoll.rb

Revision 802, 3.9 kB (checked in by raggi, 8 months ago)

ruby 1.9 compatibility enhancements

  • Property svn:keywords set to Id
Line 
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 # TODO, and I know this doesn't belong here, but if a datagram calls
27 # send_data outside of a receive_data, there is no return address, and
28 # the result is a very confusing error message.
29 #
30
31 require 'eventmachine'
32 require 'test/unit'
33
34
35 class TestEpoll < Test::Unit::TestCase
36
37         module TestEchoServer
38                 def receive_data data
39                         send_data data
40                         close_connection_after_writing
41                 end
42         end
43
44         module TestEchoClient
45                 def connection_completed
46                         send_data "ABCDE"
47                         $max += 1
48                 end
49                 def receive_data data
50                         raise "bad response" unless data == "ABCDE"
51                 end
52                 def unbind
53                         $n -= 1
54                         EM.stop if $n == 0
55                 end
56         end
57
58
59         # We can set the rlimit/nofile of a process but we can only set it
60         # higher if we're running as root.
61         # On most systems, the default value is 1024.
62         # Java doesn't (currently) implement this.
63         def test_rlimit
64                 unless RUBY_PLATFORM =~ /java/
65                         a = EM.set_descriptor_table_size
66                         assert( a <= 1024 )
67                         a = EM.set_descriptor_table_size( 1024 )
68                         assert( a == 1024 )
69                 end
70         end
71
72         # Run a high-volume version of this test by kicking the number of connections
73         # up past 512. (Each connection uses two sockets, a client and a server.)
74         # (Will require running the test as root)
75         # This test exercises TCP clients and servers.
76         #
77         def test_descriptors
78                 EM.epoll
79                 s = EM.set_descriptor_table_size 60000
80                 EM.run {
81                         EM.start_server "127.0.0.1", 9800, TestEchoServer
82                         $n = 0
83                         $max = 0
84                         100.times {
85                                 EM.connect("127.0.0.1", 9800, TestEchoClient) {$n += 1}
86                         }
87                 }
88                 assert_equal(0, $n)
89                 assert_equal(100, $max)
90         end
91
92   def test_defer
93     n = 0
94     work_proc = proc {n += 1}
95     callback_proc = proc {EM.stop}
96     EM.epoll
97     EM.run {
98       EM.defer work_proc, callback_proc
99     }
100     assert_equal( 1, n )
101   end unless RUBY_VERSION >= '1.9.0'
102
103
104         module TestDatagramServer
105                 def receive_data dgm
106                         $in = dgm
107                         send_data "abcdefghij"
108                 end
109         end
110         module TestDatagramClient
111                 def post_init
112                         send_datagram "1234567890", "127.0.0.1", 9500
113                 end
114                 def receive_data dgm
115                         $out = dgm
116                         EM.stop
117                 end
118         end
119
120         def test_datagrams
121                 $in = $out = ""
122                 EM.epoll
123                 EM.run {
124                         EM.open_datagram_socket "127.0.0.1", 9500, TestDatagramServer
125                         EM.open_datagram_socket "127.0.0.1", 0, TestDatagramClient
126                 }
127                 assert_equal( "1234567890", $in )
128                 assert_equal( "abcdefghij", $out )
129         end
130
131
132
133
134         def test_unix_domain
135                 EM.epoll
136                 s = EM.set_descriptor_table_size 60000
137                 EM.run {
138                         # The pure-Ruby version won't let us open the socket if the node already exists.
139                         # Not sure, that actually may be correct and the compiled version is wrong.
140                         # Pure Ruby also oddly won't let us make that many connections. This test used
141                         # to run 100 times. Not sure where that lower connection-limit is coming from in
142                         # pure Ruby.
143                         # Let's not sweat the Unix-ness of the filename, since this test can't possibly
144                         # work on Windows anyway.
145                         #
146                         fn = "/tmp/xxx.chain"
147                         File.unlink(fn) if File.exist?(fn)
148                         EM.start_unix_domain_server fn, TestEchoServer
149                         $n = 0
150                         $max = 0
151                         50.times {
152                                 EM.connect_unix_domain(fn, TestEchoClient) {$n += 1}
153                         }
154                         EM::add_timer(1) { $stderr.puts("test_unix_domain timed out!"); EM::stop }
155                 }
156                 assert_equal(0, $n)
157                 assert_equal(50, $max)
158         end
159
160 end
161
Note: See TracBrowser for help on using the browser.