def test_root_perform_no_wait_single(self): """Test running a simple task not waiting on a single pool. """ root = self.root event1 = threading.Event() event2 = threading.Event() par = CheckTask(name='test', custom=lambda t, x: event1.wait()) par.parallel = {'activated': True, 'pool': 'test'} aux = CheckTask(name='signal', custom=lambda t, x: event2.set()) aux.parallel = {'activated': True, 'pool': 'aux'} wait = CheckTask(name='wait') wait.wait = {'activated': True, 'wait': ['test']} root.add_child_task(0, par) root.add_child_task(1, aux) root.add_child_task(2, wait) root.check() t = threading.Thread(target=root.perform) t.start() event2.wait() event1.set() t.join() assert not root.should_pause.is_set() assert not root.should_stop.is_set() assert par.perform_called == 1 assert aux.perform_called == 1 assert wait.perform_called == 1 assert not root.resources['threads']['test'] assert root.resources['threads']['aux']
def test_root_perform_wait_single(self): """Test running a simple task waiting on a single pool. Notes ----- When starting par will be executed in its own thread, which will allow par2 to start (also in its own thread) as a consequence aux will be run. The test will wait for aux to set its flag. At this step wait should be waiting as one thread in test pool is active. After checking that, we set the flag on which par2 is waiting. This should allow wait to run. Once we have checked it is so, we let par complete. """ root = self.root event1 = threading.Event() event2 = threading.Event() event3 = threading.Event() event4 = threading.Event() par = CheckTask(name='test', custom=lambda t, x: event1.wait()) par.parallel = {'activated': True, 'pool': 'aux'} par2 = CheckTask(name='test', custom=lambda t, x: event2.wait()) par2.parallel = {'activated': True, 'pool': 'test'} aux = CheckTask(name='signal', custom=lambda t, x: event3.set()) wait = CheckTask(name='wait', custom=lambda t, x: event4.set()) wait.wait = {'activated': True, 'wait': ['test']} root.add_child_task(0, par) root.add_child_task(1, par2) root.add_child_task(2, aux) root.add_child_task(3, wait) root.check() t = threading.Thread(target=root.perform) t.start() event3.wait() sleep(1) assert not wait.perform_called assert root.resources['active_threads']['test'] assert root.resources['active_threads']['aux'] event2.set() event4.wait() assert wait.perform_called assert root.resources['active_threads']['aux'] assert not root.resources['active_threads']['test'] event1.set() t.join() assert not root.should_pause.is_set() assert not root.should_stop.is_set() assert par.perform_called == 1 assert par2.perform_called == 1 assert aux.perform_called == 1 assert wait.perform_called == 1 assert not root.resources['active_threads']['test'] assert not root.resources['active_threads']['aux']
def test_handle_task_exception_in_thread(self): """Test handling an exception occuring in a thread (test smooth_crash). """ def raiser(task, value): raise Exception() root = self.root aux = CheckTask(name='test', custom=raiser) aux.parallel = {'activated': True, 'pool': 'test'} root.add_child_task(0, aux) root.add_child_task(1, CheckTask()) root.check() root.perform() assert not root.should_pause.is_set() assert root.should_stop.is_set() assert aux.perform_called == 1
def test_handle_task_exception_in_thread(self): """Test handling an exception occuring in a thread (test smooth_crash). """ def raiser(task, value): raise Exception root = self.root aux = CheckTask(name='test', custom=raiser) aux.parallel = {'activated': True, 'pool': 'test'} root.add_child_task(0, aux) root.add_child_task(1, CheckTask()) root.check() root.perform() assert not root.should_pause.is_set() assert root.should_stop.is_set() assert aux.perform_called == 1
def test_root_perform_wait_all(self): """Test running a simple task waiting on all pools. Notes ----- When starting par will be executed in its own thread, which will allow aux to run. The test will wait for aux to set its flag. At this step wait should be waiting as one pool is active. After checking that we can set the flag on which par is waiting and let the execution complete. """ root = self.root event1 = threading.Event() event2 = threading.Event() par = CheckTask(name='test', custom=lambda t, x: event1.wait()) par.parallel = {'activated': True, 'pool': 'test'} aux = CheckTask(name='signal', custom=lambda t, x: event2.set()) wait = CheckTask(name='wait') wait.wait = {'activated': True} root.add_child_task(0, par) root.add_child_task(1, aux) root.add_child_task(2, wait) root.check() t = threading.Thread(target=root.perform) t.start() event2.wait() sleep(1) assert not wait.perform_called assert root.resources['active_threads']['test'] event1.set() t.join() assert not root.should_pause.is_set() assert not root.should_stop.is_set() assert par.perform_called == 1 assert aux.perform_called == 1 assert wait.perform_called == 1 assert not root.resources['active_threads']['test']
def test_root_perform_parallel_in_finalization(self): """Ensure that the ThreadResources release does not prevent to start new threads. """ root = self.root event1 = threading.Event() event2 = threading.Event() event3 = threading.Event() comp = ComplexTask(name='comp') comp.parallel = {'activated': True, 'pool': 'test'} aux = CheckTask(name='signal', custom=lambda t, x: event1.set()) wait = CheckTask(name='test', custom=lambda t, x: event2.wait()) par = CheckTask(name='signal', custom=lambda t, x: event3.set()) # Test creating a new thread as by priority active_threads is released # later. par.parallel = {'activated': True, 'pool': 'test2'} comp.add_child_task(0, aux) comp.add_child_task(1, wait) comp.add_child_task(2, par) root.add_child_task(0, comp) root.check() t = threading.Thread(target=root.perform) t.start() event1.wait() assert root.resources['active_threads']['test'] assert not root.resources['active_threads']['test2'] event2.set() event3.wait() t.join() assert not root.should_pause.is_set() assert not root.should_stop.is_set() assert par.perform_called == 1 assert aux.perform_called == 1 assert wait.perform_called == 1 assert not root.resources['active_threads']['test']
def test_root_perform_parallel(self): """Test running a simple task in parallel. """ main = threading.current_thread().name def thread_checker(task, value): """Check that this is not running in the main thread. """ assert task.root.resources['threads']['test'] assert threading.current_thread().name != main root = self.root aux = CheckTask(name='test', custom=thread_checker) aux.parallel = {'activated': True, 'pool': 'test'} root.add_child_task(0, aux) root.add_child_task(1, CheckTask()) root.check() root.perform() assert not root.should_pause.is_set() assert not root.should_stop.is_set() assert aux.perform_called == 1
def test_root_perform_parallel(self): """Test running a simple task in parallel. """ main = threading.current_thread().name def thread_checker(task, value): """Check that this is not running in the main thread. """ assert threading.current_thread().name != main root = self.root aux = CheckTask(name='test', custom=thread_checker) aux.parallel = {'activated': True, 'pool': 'test'} root.add_child_task(0, aux) root.add_child_task(1, CheckTask()) root.check() root.perform() assert not root.should_pause.is_set() assert not root.should_stop.is_set() assert aux.perform_called == 1 assert root.resources['threads']['test']