예제 #1
0
    def test_cancel_post_end(self):
        """Check that canceller is not called if cancelled after end."""
        self.cancelled = 0
        fs = {}
        f1 = ProgressiveFuture()
        f1.task_canceller = self.cancel_task
        f2 = ProgressiveFuture()
        f2.task_canceller = self.cancel_task
        fs[f1] = 10  # estimated duration in seconds
        fs[f2] = 15  # estimated duration in seconds
        batch_future = ProgressiveBatchFuture(fs)

        # "start" the sub-tasks; the batch task is already started at creation
        # (Note: typically only one sub-future is run at a time)
        f1.set_running_or_notify_cancel()
        f2.set_running_or_notify_cancel()
        self.assertEqual(self.cancelled, 0)

        # "end" the sub-futures
        f1.set_result(1)
        f2.set_result(2)

        # check batch future automatically finished when all sub-futures finished
        self.assertTrue(batch_future.done())
        self.assertIsNone(
            batch_future.result())  # result of successful batch future is None
        self.assertEqual(f1.result(), 1)
        self.assertEqual(f2.result(), 2)

        # try to cancel after end
        self.assertFalse(batch_future.cancel())

        self.assertFalse(
            batch_future.cancelled())  # check batch future is not cancelled
        for f in fs:  # check all sub-futures are not cancelled
            self.assertFalse(f.cancelled())

        # The result shouldn't change
        self.assertIsNone(batch_future.result())
        self.assertEqual(f1.result(), 1)
        self.assertEqual(f2.result(), 2)

        self.assertEqual(self.cancelled, 0)
예제 #2
0
    def test_cancel_while_running(self):
        """Test cancelling of future while running."""
        self.cancelled = 0
        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 + 2)
        future.task_canceller = self.cancel_task

        # "start" the task (set the future running)
        future.set_running_or_notify_cancel()

        time.sleep(0.1)  # wait a bit

        future.cancel()
        self.assertTrue(future.cancelled())
        with self.assertRaises(CancelledError):
            future.result(timeout=5)

        self.assertEqual(self.cancelled, 1)