class TestAsyncConnectionFailure(TestCase): def setUp(self): # Override setup to not start the server immediately self.purger = DefaultPurger() self.httpd, self.httpt = self.startServer(start=False) def dispatchURL(self, path, method="PURGE", port=SERVER_PORT): url = "http://localhost:%s%s" % (port, path) self.purger.purgeAsync(url, method) # Item should now be in the queue! q, w = self.purger.getQueueAndWorker(url) for i in range(10): if q.qsize() == 0: break time.sleep(0.1) else: self.fail("Nothing consumed our queued item") # Make sure the other thread has actually processed it! time.sleep(0.1) def testConnectionFailure(self): oldTimeout = self.httpd.socket.gettimeout() self.httpd.socket.settimeout(0.1) try: self.dispatchURL("/foo") time.sleep(0.2) finally: self.httpd.socket.settimeout(oldTimeout) self.httpd.queue_response(response=200) self.httpt.start() # We should have entered the 'connection retry' loop, which # will wait 2 seconds before trying again - wait at least that long. for i in range(25): if self.httpd.response_queue.empty(): break time.sleep(0.1)
class TestCase(unittest.TestCase): def setUp(self): self.purger = DefaultPurger() self.httpd, self.httpt = self.startServer() def tearDown(self): try: # If anything remains in our response queue, it means the test # failed (but - we give it a little time to stop.) if self.httpd is not None: for i in range(10): if self.httpd.response_queue.empty(): break time.sleep(0.1) self.assertTrue(self.httpd.response_queue.empty(), "response queue not consumed") if not self.purger.stopThreads(wait=True): self.fail("The purge threads did not stop") finally: if self.httpd is not None: self.httpd.shutdown() if self.httpt.isAlive(): self.httpt.join(5) if self.httpt.isAlive(): self.fail("Thread failed to shut down") self.purger = None self.httpd, self.httpt = None, None def startServer(self, port=SERVER_PORT, start=True): """Start a TestHTTPServer in a separate thread, returning a tuple (server, thread). If start is true, the thread is started. """ server_address = ('localhost', port) httpd = TestHTTPServer(server_address, TestHandler) t = threading.Thread(target=httpd.serve_forever) if start: t.start() return httpd, t
class TestCase(unittest.TestCase): def setUp(self): self.purger = DefaultPurger() self.httpd, self.httpt, self.port = self.startServer() def tearDown(self): try: # If anything remains in our response queue, it means the test # failed (but - we give it a little time to stop.) if self.httpd is not None: for i in range(10): if self.httpd.response_queue.empty(): break time.sleep(0.1) if not self.purger.stopThreads(wait=True): self.fail("The purge threads did not stop") finally: if self.httpd is not None: self.httpd.shutdown() if self.httpt.isAlive(): self.httpt.join(5) if self.httpt.isAlive(): self.fail("Thread failed to shut down") self.purger = None self.httpd.server_close() self.httpd, self.httpt = None, None def startServer(self, start=True): """Start a TestHTTPServer in a separate thread, returning a tuple (server, thread). If start is true, the thread is started. """ environment_port = int(os.environ.get("ZSERVER_PORT", 0)) server_address = ("localhost", environment_port) httpd = TestHTTPServer(server_address, TestHandler) _, actual_port = httpd.socket.getsockname() t = threading.Thread(target=httpd.serve_forever) if start: t.start() return httpd, t, actual_port
def setUp(self): self.purger = DefaultPurger() self.httpd, self.httpt = self.startServer()
def setUp(self): # Override setup to not start the server immediately self.purger = DefaultPurger() self.httpd, self.httpt = self.startServer(start=False)