#!/usr/bin/env python3.2 # -*- coding: UTF-8 -*- """ Tests if future.result(timeout) really waits too long. DISCOVERED-IN: Backported library on MACOSX 10.6 PROBLEM: future.result(timeout) waits at least until function is finished. It raises a timeout only after the command execution has finished. """ from concurrent import futures import time import unittest DELAYED_CALL_DURATION = 10 def delayed_call(duration=DELAYED_CALL_DURATION): print("delayed_call ...") time.sleep(duration) print("DONE.") return "Hello World." def timed_call(f, *args): t0 = time.time() try: f(*args) except Exception as e: print("TIMED-CALL: Exception %s" % e) finally: pass return time.time() - t0 class FuturesMoreTest(unittest.TestCase): """ OOPS: Waits longer than future.result(wait_duration) until call command has finished. """ WAIT_DURATION = 2 def setUp(self): if not hasattr(self, "executor"): self.executor = futures.ThreadPoolExecutor(max_workers=1) def do_future_wait(self, wait_duration): future = self.executor.submit(delayed_call) future.result(wait_duration) def test_result_with_timout_waitsUntilCommandHasFinished(self): duration = timed_call(self.do_future_wait, self.WAIT_DURATION) print("DURATION: %s (expected: %s)" % (duration, self.WAIT_DURATION)) self.assertTrue(self.WAIT_DURATION < DELAYED_CALL_DURATION) self.assertAlmostEqual(self.WAIT_DURATION, duration, 1) # -- AUTO-MAIN: if __name__ == '__main__': unittest.main()