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)
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())
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())