def testA(self): """ Check worker methods get called. We sleep occasionally to ensure these asynchronous calls have enough time to be called """ # Create a worker manager compDummy = Dummy() print('create manager') manager = WorkerThreadManager(compDummy) # Pause it print('pause workers') manager.pauseWorkers() # Add a worker, and check init method gets called print('add worker') manager.addWorker(DummyWorker1(), 1) time.sleep(3) self.assertEqual(WorkerThreadsTest._setupCalled, True) # Ensure the algo wasn't called whilst paused self.assertEqual(WorkerThreadsTest._algoCalled, False) print('resume workers') # Run the workers, pause, and check algo method gets called manager.resumeWorkers() time.sleep(3) manager.pauseWorkers() self.assertEqual(WorkerThreadsTest._algoCalled, True) print('terminate workers') # Terminate the workers, and check terminate method gets called manager.terminateWorkers() time.sleep(3) self.assertEqual(WorkerThreadsTest._terminateCalled, True)
def testB(self): """ Check we can terminate before pausing workers """ compDummy = Dummy() print('create manager') manager = WorkerThreadManager(compDummy) print('add worker') manager.addWorker(DummyWorker2(), 1) print('terminate worker') manager.terminateWorkers()
def testWorkerError(self): """If a worker raises an exception terminate the entire component""" compDummy = Dummy() print('create manager') manager = WorkerThreadManager(compDummy) # needed for handling errors - harness would generally set this myThread = threading.currentThread() myThread.workerThreadManager = manager print('add workers') manager.addWorker(DummyWorker1(), 0.1) manager.addWorker(ErrorWorker(), 0.1) print('run workers, one will throw an error') manager.resumeWorkers() # should do something smarter here # too short a time and threads havent exited yet time.sleep(6) # all threads should have ended after worker raised exception self.assertEqual(manager.activeThreadCount, 0)