diff options
Diffstat (limited to 'cherrypy/test/test_states.py')
| -rw-r--r-- | cherrypy/test/test_states.py | 64 |
1 files changed, 23 insertions, 41 deletions
diff --git a/cherrypy/test/test_states.py b/cherrypy/test/test_states.py index ce6830a8..9c000a5a 100644 --- a/cherrypy/test/test_states.py +++ b/cherrypy/test/test_states.py @@ -1,11 +1,13 @@ import os import signal -import socket import sys import time import unittest import warnings +import pytest +import portend + import cherrypy import cherrypy.process.servers from cherrypy._cpcompat import BadStatusLine, ntob @@ -110,7 +112,7 @@ class ServerStateTests(helper.CPWebCase): host = cherrypy.server.socket_host port = cherrypy.server.socket_port - self.assertRaises(IOError, cherrypy._cpserver.check_port, host, port) + portend.occupied(host, port, timeout=0.1) # The db_connection should be running now self.assertEqual(db_connection.running, True) @@ -288,7 +290,7 @@ class ServerStateTests(helper.CPWebCase): time.sleep(2) host = cherrypy.server.socket_host port = cherrypy.server.socket_port - cherrypy._cpserver.wait_for_occupied_port(host, port) + portend.occupied(host, port, timeout=5) self.getPage('/start') if not (float(self.body) > start): @@ -469,7 +471,7 @@ test_case_name: "test_signal_handler_unsubscribe" class WaitTests(unittest.TestCase): - def test_wait_for_occupied_port_INADDR_ANY(self): + def test_safe_wait_INADDR_ANY(self): """ Wait on INADDR_ANY should not raise IOError @@ -485,43 +487,23 @@ class WaitTests(unittest.TestCase): # Simulate the behavior we observe when no loopback interface is # present by: finding a port that's not occupied, then wait on it. - free_port = self.find_free_port() + free_port = portend.find_available_local_port() servers = cherrypy.process.servers - def with_shorter_timeouts(func): - """ - A context where occupied_port_timeout is much smaller to speed - test runs. - """ - # When we have Python 2.5, simplify using the with_statement. - orig_timeout = servers.occupied_port_timeout - servers.occupied_port_timeout = .07 - try: - func() - finally: - servers.occupied_port_timeout = orig_timeout - - def do_waiting(): - # Wait on the free port that's unbound - with warnings.catch_warnings(record=True) as w: - servers.wait_for_occupied_port('0.0.0.0', free_port) - self.assertEqual(len(w), 1) - self.assertTrue(isinstance(w[0], warnings.WarningMessage)) - self.assertTrue( - 'Unable to verify that the server is bound on ' in str(w[0])) - - # The wait should still raise an IO error if INADDR_ANY was - # not supplied. - self.assertRaises(IOError, servers.wait_for_occupied_port, - '127.0.0.1', free_port) - - with_shorter_timeouts(do_waiting) - - def find_free_port(self): - 'Find a free port by binding to port 0 then unbinding.' - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.bind(('', 0)) - free_port = sock.getsockname()[1] - sock.close() - return free_port + inaddr_any = '0.0.0.0' + + # Wait on the free port that's unbound + with warnings.catch_warnings(record=True) as w: + with servers._safe_wait(inaddr_any, free_port): + portend.occupied(inaddr_any, free_port, timeout=1) + self.assertEqual(len(w), 1) + self.assertTrue(isinstance(w[0], warnings.WarningMessage)) + self.assertTrue( + 'Unable to verify that the server is bound on ' in str(w[0])) + + # The wait should still raise an IO error if INADDR_ANY was + # not supplied. + with pytest.raises(IOError): + with servers._safe_wait('127.0.0.1', free_port): + portend.occupied('127.0.0.1', free_port, timeout=1) |
