def test_timeout_cancel(self): (yield gen.Callback('key'))('result') # called immediately try: result = yield yieldpoints.WithTimeout( timedelta(seconds=0.1), 'key', self.io_loop) except yieldpoints.TimeoutException: self.fail("TimeoutException unexpectedly raised") self.assertEqual('result', result)
def get(self): urls = set([ 'http://google.com', 'http://apple.com', 'http://microsoft.com', 'http://amazon.com' ]) self.write('<table border="1">') start = time.time() def duration(): return time.time() - start # Set max_clients so all fetches can happen at once client = httpclient.AsyncHTTPClient(max_clients=len(urls)) # Start all the fetches for url in urls: client.fetch(url, callback=(yield gen.Callback(url))) # Handle them as they complete pending_urls = urls.copy() while pending_urls: try: url, response = yield yieldpoints.WithTimeout( start + 0.5, yieldpoints.WaitAny(pending_urls)) except yieldpoints.TimeoutException: self.finish(""" </table> <p>These URLs did not complete after %.1f seconds: %s</p> """ % (duration(), ', '.join(pending_urls))) # Avoid LeakedCallbackError yield yieldpoints.CancelAll() return pending_urls.remove(url) self.write(""" <tr> <td>%s</td> <td>HTTP %s</td> <td>%.1f seconds</td> </tr> """ % (url, response.code, duration())) yield gen.Task(self.flush) self.finish(""" </table> <p>Completed all in %.1f seconds</p> """ % duration())
def test_timeout_and_task(self): # Make sure WithTimeout and gen.Task are composable try: yield yieldpoints.WithTimeout( timedelta(seconds=0.01), gen.Task(self.io_loop.add_callback), self.io_loop) except yieldpoints.TimeoutException: self.fail("TimeoutException unexpectedly raised") try: yield yieldpoints.WithTimeout( timedelta(seconds=0.01), gen.Task(self.io_loop.add_timeout, timedelta(seconds=0.1)), self.io_loop) except yieldpoints.TimeoutException: # Expected pass else: self.fail("No TimeoutException raised")
def test_callback_after_timeout(self): # Check that a canceled callback can still be run without error callback = yield gen.Callback('key') try: yield yieldpoints.WithTimeout( timedelta(seconds=0.1), 'key', self.io_loop) except yieldpoints.TimeoutException: # Expected pass else: self.fail("No TimeoutException raised") callback() # No error
def test_timeout_and_wait_any(self): # Make sure WithTimeout and WaitAny are composable yield gen.Callback('key') try: yield yieldpoints.WithTimeout( timedelta(seconds=0.01), yieldpoints.WaitAny(['key']), self.io_loop) except yieldpoints.TimeoutException: # Expected pass else: self.fail("No TimeoutException raised") yield yieldpoints.Cancel('key')
def test_timeout_cancel_with_delay(self): callback = yield gen.Callback('key') start = time.time() self.io_loop.add_timeout( timedelta(seconds=0.1), partial(callback, 'result')) try: result = yield yieldpoints.WithTimeout( timedelta(seconds=0.2), 'key', self.io_loop) except yieldpoints.TimeoutException: self.fail("TimeoutException unexpectedly raised") duration = time.time() - start self.assertTrue(abs(duration - 0.1) < 0.01) self.assertEqual('result', result)
def test_timeout(self): yield gen.Callback('key') start = time.time() try: yield yieldpoints.WithTimeout( timedelta(seconds=0.1), 'key', self.io_loop) except yieldpoints.TimeoutException: # Expected pass else: self.fail("No TimeoutException raised") duration = time.time() - start # assertAlmostEquals with 'delta' not available until Python 2.7 self.assertTrue(abs(duration - 0.1) < 0.01) yield yieldpoints.Cancel('key') # avoid LeakedCallbackError