Ejemplo n.º 1
0
    def test_multiple_parallel_cancelled(self):
        """
        Try to cancel multiple running futures in parallel
        """
        random.seed(0)
        self.executor = ParallelThreadPoolExecutor()

        # Put several task with random sets
        fs = []
        self.called = 0
        for i in range(10):
            f = CancellableFuture()
            f.task_canceller = self._canceller
            f._must_stop = threading.Event()
            r_letter1, r_letter2 = random.choice('abcxyz'), random.choice('abcxyz')
            f = self.executor.submitf(set([r_letter1, r_letter2]), f, self._cancellable_task, f, 2)
            f.add_done_callback(self._on_end_task)
            fs.append(f)
        time.sleep(0.1)
        # Cancel half of the tasks
        for f in fs[1::2]:
            f.cancel()
        time.sleep(10 * 2 + 1)  # in the worst case, there is a dependency between every task, so 2*10
        self.assertEqual(self.called, 10)  # done callback is called for all futures

        for f in fs[1::2]:
            self.assertTrue(f.cancelled())
            self.assertRaises(CancelledError, f.result)
        for f in fs[0::2]:
            self.assertIsInstance(f.result(), int)
            self.assertTrue(f.done())
Ejemplo n.º 2
0
    def testCancelPostEnd(self):
        """
        Check that canceller is not called if cancelled after end
        """
        self.cancelled = 0
        future = CancellableFuture()
        future.task_canceller = self.cancel_task
        self.assertEqual(self.cancelled, 0)

        # "start" the task
        future.set_running_or_notify_cancel()

        # "end" the task
        future.set_result("boo")

        self.assertEqual(future.result(), "boo")

        # try to cancel after end
        self.assertFalse(future.cancel())
        self.assertFalse(future.cancelled())

        # The result shouldn't change
        self.assertEqual(future.result(), "boo")

        self.assertEqual(self.cancelled, 0)
Ejemplo n.º 3
0
    def test_multiple_parallel(self):
        """
        Try to cancel multiple running futures in parallel
        """
        random.seed(0)
        self.executor = ParallelThreadPoolExecutor()

        # Put several task with random sets
        fs = []
        self.called = 0
        for i in range(10):
            f = CancellableFuture()
            f.task_canceller = self._canceller
            f._must_stop = threading.Event()
            r_letter1, r_letter2 = random.choice('abcxyz'), random.choice(
                'abcxyz')
            f = self.executor.submitf(set([r_letter1, r_letter2]), f,
                                      self._cancellable_task, f, 2)
            f.add_done_callback(self._on_end_task)
            fs.append(f)
        time.sleep(
            10 * 2 + 1
        )  # in the worst case, there is a dependency between every task, so 2*10
        self.assertEqual(self.called, 10)

        for f in fs:
            self.assertIsInstance(f.result(), int)
            self.assertTrue(f.done())
Ejemplo n.º 4
0
    def testCancelPreStart(self):
        """
        Check that canceller is not called if cancelled before starting
        """
        self.cancelled = 0
        future = CancellableFuture()
        future.task_canceller = self.cancel_task
        self.assertEqual(self.cancelled, 0)

        # try to cancel before running
        self.assertTrue(future.cancel())
        self.assertTrue(future.cancelled())
        self.assertTrue(future.cancel()) # it's ok to cancel twice
        self.assertRaises(CancelledError, future.result, 1)

        self.assertEqual(self.cancelled, 0)
Ejemplo n.º 5
0
    def test_multiple_cancellable(self):
        """
        Try to cancel multiple running cancellable futures
        """
        self.executor = CancellableThreadPoolExecutor(max_workers=10)

        # Put several long task, and cancel all of them
        fs = []
        for i in range(20):
            f = CancellableFuture()
            f.task_canceller = self._canceller
            f._must_stop = threading.Event()
            f = self.executor.submitf(f, self._cancellable_task, f, 3 + i)
            fs.append(f)
        time.sleep(0.1)
        self.executor.cancel()
        for f in fs:
            self.assertTrue(f.cancelled())
            self.assertRaises(CancelledError, f.result)
Ejemplo n.º 6
0
    def test_multiple_cancellable(self):
        """
        Try to cancel multiple running cancellable futures
        """
        self.executor = CancellableThreadPoolExecutor(max_workers=10)

        # Put several long task, and cancel all of them
        fs = []
        for i in range(20):
            f = CancellableFuture()
            f.task_canceller = self._canceller
            f._must_stop = threading.Event()
            f = self.executor.submitf(f, self._cancellable_task, f, 3 + i)
            fs.append(f)
        time.sleep(0.1)
        self.executor.cancel()
        for f in fs:
            self.assertTrue(f.cancelled())
            self.assertRaises(CancelledError, f.result)
Ejemplo n.º 7
0
 def _createFuture(self):
     """
     Return (CancellableFuture): a future that can be used to manage a move
     """
     f = CancellableFuture()
     f._moving_lock = threading.Lock()  # taken while moving
     f._must_stop = threading.Event(
     )  # cancel of the current future requested
     f._was_stopped = threading.Event()  # if cancel was successful
     f.task_canceller = self._cancelCurrentMove
     return f
Ejemplo n.º 8
0
    def test_cancel_while_running(self):
        """Cancel future while running."""
        self.cancelled = 0
        future = CancellableFuture()
        future.task_canceller = self.cancel_task

        # "start" the task (set the future running)
        future.set_running_or_notify_cancel()
        self.assertEqual(self.cancelled, 0)

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

        self.assertEqual(self.cancelled, 1)
Ejemplo n.º 9
0
    def test_cancel_pre_start(self):
        """Check that canceller is not called if cancelled before starting."""
        self.cancelled = 0
        future = CancellableFuture()
        future.task_canceller = self.cancel_task
        self.assertEqual(self.cancelled, 0)

        # try to cancel before running
        self.assertTrue(future.cancel())
        self.assertTrue(future.cancelled())
        self.assertTrue(future.cancel())  # it's ok to cancel twice
        self.assertRaises(CancelledError, future.result, 1)

        self.assertEqual(self.cancelled, 0)  # canceller should not be called
Ejemplo n.º 10
0
    def testCancelWhileRunning(self):
        """
        Only tests a simple CancellableFuture
        """
        self.cancelled = 0
        future = CancellableFuture()
        future.task_canceller = self.cancel_task

        # "start" the task
        future.set_running_or_notify_cancel()
        self.assertEqual(self.cancelled, 0)

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

        self.assertEqual(self.cancelled, 1)
Ejemplo n.º 11
0
    def testCancelWhileRunning(self):
        """
        Only tests a simple CancellableFuture
        """
        self.cancelled = 0
        future = CancellableFuture()
        future.task_canceller = self.cancel_task

        # "start" the task
        future.set_running_or_notify_cancel()
        self.assertEqual(self.cancelled, 0)

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

        self.assertEqual(self.cancelled, 1)
Ejemplo n.º 12
0
    def test_one_cancellable(self):
        """
        Test cancelling multiple cancellable futures running one at a time
        """
        self.executor = CancellableThreadPoolExecutor(max_workers=1)

        self.called = 0
        # Put several long task, and cancel all of them
        fs = []
        for i in range(20):
            f = CancellableFuture()
            f.task_canceller = self._canceller
            f._must_stop = threading.Event()
            f = self.executor.submitf(f, self._cancellable_task, f, 3 + i)
            f.add_done_callback(self._on_end_task)
            fs.append(f)

        time.sleep(0.1)
        self.executor.cancel()
        self.assertEquals(self.called, 20)
        for f in fs:
            self.assertTrue(f.cancelled())
            self.assertRaises(CancelledError, f.result)
Ejemplo n.º 13
0
    def test_one_cancellable(self):
        """
        Test cancelling multiple cancellable futures running one at a time
        """
        self.executor = CancellableThreadPoolExecutor(max_workers=1)

        self.called = 0
        # Put several long task, and cancel all of them
        fs = []
        for i in range(20):
            f = CancellableFuture()
            f.task_canceller = self._canceller
            f._must_stop = threading.Event()
            f = self.executor.submitf(f, self._cancellable_task, f, 3 + i)
            f.add_done_callback(self._on_end_task)
            fs.append(f)

        time.sleep(0.1)
        self.executor.cancel()
        self.assertEquals(self.called, 20)
        for f in fs:
            self.assertTrue(f.cancelled())
            self.assertRaises(CancelledError, f.result)
Ejemplo n.º 14
0
    def testCancelPostEnd(self):
        """
        Check that canceller is not called if cancelled after end
        """
        self.cancelled = 0
        future = CancellableFuture()
        future.task_canceller = self.cancel_task
        self.assertEqual(self.cancelled, 0)

        # "start" the task
        future.set_running_or_notify_cancel()

        # "end" the task
        future.set_result("boo")

        self.assertEqual(future.result(), "boo")

        # try to cancel after end
        self.assertFalse(future.cancel())
        self.assertFalse(future.cancelled())

        # The result shouldn't change
        self.assertEqual(future.result(), "boo")

        self.assertEqual(self.cancelled, 0)