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