root/trunk/tests/test_servers.rb

Revision 802, 2.1 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
27 $:.unshift "../lib"
28 require 'eventmachine'
29 require 'socket'
30 require 'test/unit'
31
32
33 class TestServers < Test::Unit::TestCase
34
35         Host = "127.0.0.1"
36         Port = 9555
37        
38         module NetstatHelper
39           GlobalUdp4Rexp = /udp.*\s+(?:\*|(?:0\.){3}0)[:.](\d+)\s/i
40           GlobalTcp4Rexp = /tcp.*\s+(?:\*|(?:0\.){3}0)[:.](\d+)\s/i
41           LocalUdpRexp = /udp.*\s+(?:127\.0\.0\.1|::1)[:.](\d+)\s/i
42           LocalTcpRexp = /tcp.*\s+(?:127\.0\.0\.1|::1)[:.](\d+)\s/i
43           def grep_netstat(pattern)
44             `netstat -an`.scan(/^.*$/).grep(pattern)
45     end
46   end
47         include NetstatHelper
48
49         class TestStopServer < EM::Connection
50                 def initialize *args
51                         super
52                 end
53                 def post_init
54                         # TODO,sucks that this isn't OOPy enough.
55                         EM.stop_server @server_instance
56                 end
57         end
58        
59         def run_test_stop_server
60           EM.run {
61             sig = EM.start_server(Host, Port)
62             assert(grep_netstat(LocalTcpRexp).grep(%r(#{Port})).size >= 1, "Server didn't start")
63             EM.stop_server sig
64             # Give the server some time to shutdown.
65             EM.add_timer(0.1) {
66                 assert(grep_netstat(LocalTcpRexp).grep(%r(#{Port})).empty?, "Servers didn't stop")         
67             EM.stop
68             }
69           }
70         end
71         def test_stop_server
72           assert(grep_netstat(LocalTcpRexp).grep(Port).empty?, "Port already in use")
73                 5.times {run_test_stop_server}
74                 assert(grep_netstat(LocalTcpRexp).grep(%r(#{Port})).empty?, "Servers didn't stop")
75         end
76
77
78 end
79
80
Note: See TracBrowser for help on using the browser.