예제 #1
0
    def testProgressiveFuture(self):
        """
        Only tests a simple ProgressiveFuture
        """
        self.cancelled = 0
        self.start = None
        self.end = None
        future = ProgressiveFuture()
        future.task_canceller = self.cancel_task

        now = time.time()
        # try to update progress
        future.set_progress(end=now + 1)
        future.add_update_callback(self.on_progress_update)
        future.set_progress(end=now + 2)  # should say about 2 s left
        self.assertEqual(self.end, now + 2)
        # before being started, start should always be (a bit) in the future
        self.assertGreaterEqual(self.start, now)

        # "start" the task
        future.set_running_or_notify_cancel()
        self.assertTrue(0 <= time.time() - self.start < 0.1)
        time.sleep(0.1)

        now = time.time()
        future.set_progress(end=now + 1)
        self.assertTrue(0.9 <= self.end - time.time() < 1)

        # try to cancel while running
        future.cancel()
        self.assertTrue(future.cancelled())
        self.assertRaises(CancelledError, future.result, 1)
        self.assertLessEqual(self.end, time.time())
        self.assertEqual(self.cancelled, 1)
예제 #2
0
    def testProgressiveFuture(self):
        """
        Only tests a simple ProgressiveFuture
        """
        self.cancelled = 0
        self.start = None
        self.end = None
        future = ProgressiveFuture()
        future.task_canceller = self.cancel_task

        now = time.time()
        # try to update progress
        future.set_progress(end=now + 1)
        future.add_update_callback(self.on_progress_update)
        future.set_progress(end=now + 2) # should say about 2 s left
        self.assertEqual(self.end, now + 2)
        # before being started, start should always be (a bit) in the future
        self.assertGreaterEqual(self.start, now)

        # "start" the task
        future.set_running_or_notify_cancel()
        self.assertTrue(0 <= time.time() - self.start < 0.1)
        time.sleep(0.1)

        now = time.time()
        future.set_progress(end=now + 1)
        self.assertTrue(0.9 <= self.end - time.time() < 1)

        # try to cancel while running
        future.cancel()
        self.assertTrue(future.cancelled())
        self.assertRaises(CancelledError, future.result, 1)
        self.assertLessEqual(self.end, time.time())
        self.assertEqual(self.cancelled, 1)
예제 #3
0
    def test_progress(self):
        """Test progress update for future."""
        self.start = None  # for the caller
        self.end = None  # for the caller
        now = time.time()
        # start is default time now if not specified
        future = ProgressiveFuture(end=now + 1)
        future.task_canceller = self.cancel_task

        # caller request future to indicate change in time estimation
        future.add_update_callback(self.on_progress_update)
        # update the progress
        future.set_progress(end=now + 2)
        # check end time was updated
        self.assertEqual(self.end, now + 2)
        # future not running yet, so start should be after "now"
        self.assertGreaterEqual(self.start, now)

        # "start" the task (set the future running)
        future.set_running_or_notify_cancel()
        # the progress should be updated and thus .start should be updated now with the current time
        expected_start = time.time()
        self.assertTrue(expected_start - 0.1 <= self.start <= expected_start)

        time.sleep(0.1)  # wait a bit

        now = time.time()
        # while running, update the estimated ending time
        future.set_progress(end=now + 1)
        # the progress should be updated and thus .end should be in 1 sec from now
        expected_end = now + 1
        self.assertTrue(expected_end - 0.1 <= self.end <= expected_end)
예제 #4
0
    def testProgressiveFuture(self):
        """
        Only tests a simple ProgressiveFuture
        """
        self.cancelled = 0
        self.past = None
        self.left = None
        future = ProgressiveFuture()
        future.task_canceller = self.cancel_task

        now = time.time()
        # try to update progress
        future.set_end_time(now + 1)
        future.add_update_callback(self.on_progress_update)
        future.set_end_time(now + 2) # should say about 2 s left
        self.assertTrue(1.9 <= self.left < 2)
        self.assertLessEqual(self.past, 0)

        # "start" the task
        future.set_running_or_notify_cancel()
        self.assertTrue(0 <= self.past < 0.1)
        time.sleep(0.1)

        now = time.time()
        future.set_end_time(now + 1)
        self.assertTrue(0.9 <= self.left < 1)


        # try to cancel while running
        future.cancel()
        self.assertTrue(future.cancelled())
        self.assertRaises(CancelledError, future.result, 1)
        self.assertEqual(self.left, 0)
        self.assertEqual(self.cancelled, 1)
예제 #5
0
    def testProgressiveFuture(self):
        """
        Only tests a simple ProgressiveFuture
        """
        self.cancelled = 0
        self.past = None
        self.left = None
        future = ProgressiveFuture()
        future.task_canceller = self.cancel_task

        now = time.time()
        # try to update progress
        future.set_end_time(now + 1)
        future.add_update_callback(self.on_progress_update)
        future.set_end_time(now + 2)  # should say about 2 s left
        self.assertTrue(1.9 <= self.left < 2)
        self.assertLessEqual(self.past, 0)

        # "start" the task
        future.set_running_or_notify_cancel()
        self.assertTrue(0 <= self.past < 0.1)
        time.sleep(0.1)

        now = time.time()
        future.set_end_time(now + 1)
        self.assertTrue(0.9 <= self.left < 1)

        # try to cancel while running
        future.cancel()
        self.assertTrue(future.cancelled())
        self.assertRaises(CancelledError, future.result, 1)
        self.assertEqual(self.left, 0)
        self.assertEqual(self.cancelled, 1)