def test_create_stop(self): l = EventTracker() with MONITOR.listen(l): self.assertFalse(l.created_called) self.assertFalse(l.stopped_called) self.assertFalse(l.failed_called) DummyProcess.run() self.assertTrue(l.created_called) self.assertTrue(l.stopped_called) self.assertFalse(l.failed_called)
def test_spec(self): """ Check that the references to specs are doing the right thing... """ dp = DummyProcess.new() self.assertIsNot(DummyProcess.spec(), Process.spec()) self.assertIs(dp.spec(), DummyProcess.spec()) class Proc(DummyProcess): pass self.assertIsNot(Proc.spec(), Process.spec()) self.assertIsNot(Proc.spec(), DummyProcess.spec()) p = Proc.new() self.assertIs(p.spec(), Proc.spec())
def test_interrupt_not_waiting(self): """ If you interrupt when it's not waiting then nothing happens. """ p = DummyProcess.new() waiton = WaitOnProcessState(p, ProcessState.STOPPED) waiton.interrupt()
def test_wait(self): p = DummyProcess.new() self.assertTrue(p.wait(timeout=2.), "Not running process didn't return from wait") self.executor.play(p) self.assertTrue(p.wait(timeout=2.), "Process failed to return from wait when done")
def test_interrupt(self): p = DummyProcess.new() w = WaitOnState(p, ProcessState.STOPPED) self.assertFalse(w.wait(0.2)) w.interrupt() self.manager.start(p) self.assertFalse(w.wait(0.2))
def test_play(self): p = DummyProcess.new() self.assertFalse(p.has_finished()) fut = self.executor.play(p) self.assertTrue(fut.wait(timeout=1.)) self.assertTrue(p.has_terminated()) self.assertTrue(p.has_finished())
def test_normal_process(self): saver = _EventSaver() self.emitter.start_listening(saver.event_ocurred, "process.*") p = DummyProcess.new() preamble = "process.{}.".format(p.pid) p.play() self.assertEqual(saver.events, [preamble + 'finished', preamble + 'stopped'])
def test_pause_play(self): """ Pausing a process that is not playing should have no effect and next time it is played it should continue normally. """ p = DummyProcess.new() self.assertTrue(p.pause(timeout=1.)) p.play() self.assertEqual(p.state, ProcessState.STOPPED)
def test_abort(self): # Abort a process before it gets started, this will get ignored and the # process will run normally proc = DummyProcess.new() proc.abort() proc.play() self.assertFalse(proc.has_aborted()) self.assertEqual(proc.state, ProcessState.STOPPED)
def test_simple_queue(self): with SchedulingExecutor(max_threads=2) as executor: procs = [] for i in range(10): proc = DummyProcess() procs.append(proc) executor.play(proc) self.assertTrue(wait_until(procs, ProcessState.STOPPED, timeout=2.))
def test_get_description(self): # Not all that much we can test for, but check if it's a string at # least for ProcClass in TEST_PROCESSES: desc = ProcClass.get_description() self.assertIsInstance(desc, str) # Dummy process should at least use the docstring as part of the # description and so it shouldn't be empty desc = DummyProcess.get_description() self.assertNotEqual(desc, "")
def test_state_messages(self): tp = PythonThreadPoolExecutor(max_workers=1) for state in (ProcessState.RUNNING, ProcessState.STOPPED): p = DummyProcess.new() waiton = WaitOnProcessState(p, state) future = tp.submit(waiton.wait) while not future.running(): pass p.play() self.assertTrue(future.result(timeout=2.))
def test_finished_stopped(self): for event in ("finished", "stopped"): tp = ThreadPoolExecutor(max_workers=1) p = DummyProcess.new() w = WaitOnProcessEvent(self.emitter, p.pid, event) future = tp.submit(w.wait) while not future.running(): pass p.play() self.assertTrue(future.result(timeout=2.))
def test_interrupt(self): tp = PythonThreadPoolExecutor(max_workers=1) p = DummyProcess.new() waiton = WaitOnProcessState(p, ProcessState.STOPPED) future = tp.submit(waiton.wait) while not future.running(): pass with self.assertRaises(Interrupted): waiton.interrupt() future.result(timeout=2.)
def test_direct_instantiate(self): with self.assertRaises(AssertionError): DummyProcess(inputs={}, pid=None)
def test_play_terminated(self): p = DummyProcess() p.play() self.assertTrue(p.has_terminated()) p.play()
def test_future_pid(self): p = DummyProcess.new() future = self.executor.play(p) self.assertEqual(future.pid, p.pid)
def test_abort(self): proc = DummyProcess.new() proc.abort() self.assertTrue(proc.has_aborted()) self.assertEqual(proc.state, ProcessState.STOPPED)
def test_start(self): p = DummyProcess.new() self.assertFalse(p.has_finished()) self.manager.start(p) wait_until_stopped(p, 1) self.assertTrue(p.has_finished())
def test_future_pid(self): p = DummyProcess.new() future = self.manager.start(p) self.assertEqual(future.pid, p.pid)
def test_already_in_state(self): p = DummyProcess.new() self.assertTrue(WaitOnProcessState(p, ProcessState.CREATED).wait(timeout=2.))