5

I'm writing a simple integration test which consists of the following:

  1. Starting a simple HTTP server which serves up a page to be tested
  2. Starting various different browsers which load this page to confirm that everything works.

I know #2 works and I have no problem with this.

However, #1 only works for the first few browsers and then just hangs, causing subsequent browser tests to fail. I really don't understand why.

In my code here's what I'm doing:

class Test:  
    def setup_class(cls):  
        ready_signal = threading.Event()  
        cls.port = 9000  
        cls.docroot = /tmp  
        cls.http_wrapper = SimpleHTTPWrap(port=cls.port, docroot=cls.docroot, ready_signal)  
        background_thread = threading.Thread(target=http_wrapper.start)  
        background_thread.daemon = True  
        background_thread.start()  
        ready_signal.wait(10) # Wait for thread to be ready (or proceed after timeout)  
    def test(self):  
        ip = get_my_ip_address()  
        self.browser = webdriver.Remote(_some_parameters_go_here)  
        self.browser.get('http://' + ip + ":" + cls.port + cls.docroot + '/test.html')  
        # This hangs after similar tests have already run and succeeded  
    def teardown_class(cls):  
        cls.http_wrapper.stop()    

class SimpleHTTPWrap(object):  
    def __init__(self, port, docroot, ready_signal):  
        self.port = port  
        self.docroot = docroot  
        self.request_handler = SimpleHTTPServer.SimpleHTTPRequestHandler  
        self.server = None  
        self.ready_signal = ready_signal  
    def start(self):  
        os.chdir(self.docroot)  
        try:  
            log.info("Attempting to bind to port: " + str(self.port))  
            self.server = SocketServer.TCPServer(("", self.port), self.request_handler)  
            self.server.allow_reuse_address = True  
            log.info("Success.")  
            self.ready_signal.set() # Signal the main thread that we're ready  
            self.server.serve_forever()  
        except socket.error:  
            log.error("Could not bind to address (port already in use)" + str(self.port))  

I think it's in some sort of deadlock situation because when I tried calling cls.http_wrapper.server.shutdown() it just hung.

However, I have no idea why. There is only ever one thread running here in the background and other tests seem to be fine. But after a few, everything hangs.
From the browser perspective, it tries loading the page for a while until it finally times out due to a connection refused.

Would anyone happen to know what might be causing this or what I can do to further investigate? At this point I'm at a loss and considering just trying something else.

  • Did you ever find a solution for this? My work-around was to use handle_request() in a while loop and then kill the loop, but the loop would only exit after making an additional request to the server after it was killed. Having a working shutdown method would be nice – spencer Feb 14 '18 at 15:52
  • please check https://bugs.python.org/issue31639 – Mountain Jun 01 '18 at 18:14

0 Answers0