def test_should_return_task_control(self): class LongTask(Task): def __init__(self, sleepDurationSec): self._sleepDurationSec = sleepDurationSec self._done = False @override def perform(self): while not self._done: time.sleep(self._sleepDurationSec) def terminate(self): self._done = True task = LongTask(0.01) taskCtrl = self.executor.execute(task) self.assertFalse(taskCtrl.isDone()) Poller(1, pollingDelaySec=0.01).check( ExecutionProbe(lambda: self.assertTrue(taskCtrl.isRunning()))) task.terminate() Poller(2, pollingDelaySec=0.01).check( ExecutionProbe(lambda: self.assertTrue(taskCtrl.isDone()))) task = LongTask(0.5) taskCtrl = self.executor.execute(task) task.terminate() taskCtrl.waitForCompletion() self.assertTrue(taskCtrl.isDone())
def test_pessimistic_task(self): task_1 = FailingTask() task_2 = self._createDummyTask() self.executor.execute(task_1) self.executor.execute(task_2) Poller(5).check(TaskCompletionProbe(task_1)) Poller(5).check(TaskCompletionProbe(task_2))
def _testProcessesActuallyStarted(self): controllerLogFile = os.path.join( self.LOG_DIR, '%s.log' % Constants.SERVER_1_CONFIG_SECTION) Poller(5).check( MessageInFileProbe(Runner.RUNNING_MESSAGE, controllerLogFile)) controller2LogFile = os.path.join( self.LOG_DIR, '%s.log' % Constants.SERVER_2_CONFIG_SECTION) Poller(5).check( MessageInFileProbe(Runner.RUNNING_MESSAGE, controller2LogFile))
def test_should_ignore_failing_tasks(self): failingTask = FailingTask("broken") self.executor.execute(failingTask) Poller(4).check(TaskCompletionProbe(failingTask)) dummyTask = SlowTask(0, "slow task") Poller(1).check( ExecutionProbe(lambda: self.executor.execute(dummyTask))) Poller(4).check(TaskCompletionProbe(dummyTask))
def test_should_discard_task_when_executor_is_busy(self): class SlowTaskBusyProbe(Probe): def __init__(self, task_1): Probe.__init__(self) self._task= task_1 @override def probe(self): pass @override def isSatisfied(self): return self._task.isBusy() @override def errorMessage(self): return "Task is not busy" class ExecutorIdleProbe(Probe): def __init__(self, executor): Probe.__init__(self) self.executor= executor @override def probe(self): pass @override def isSatisfied(self): return self.executor.isIdle() @override def errorMessage(self): return "Executor is busy" SLEEP_DURATION_SEC= 1.0 task_1= SlowTask(sleepDurationSec=SLEEP_DURATION_SEC, name="one") self.executor.execute(task_1) Poller(4).check(SlowTaskBusyProbe(task_1)) time.sleep(SLEEP_DURATION_SEC / 5.0) task_2= DummyTask("two") try: self.executor.execute(task_2) except BusyExecutorException as e: self.assertEqual("one", e.nameOfCurrentTask) Poller(4).check(ExecutorIdleProbe(self.executor)) self.executor.execute(task_2)
def test_FIFO_like_behaviour(self): task_1 = self._createDummyTask() task_2 = self._createDummyTask() task_3 = self._createDummyTask() self.executor.execute(task_1) self.executor.execute(task_2) self.executor.execute(task_3) Poller(5).check(TaskCompletionProbe(task_1)) Poller(5).check(TaskCompletionProbe(task_2)) Poller(5).check(TaskCompletionProbe(task_3)) self.assertTrue(task_1.runningNumber() < task_2.runningNumber()) self.assertTrue(task_2.runningNumber() < task_3.runningNumber())
def _testMoveTo(self): actuatorPosition = np.arange(10) self.instrumentClient1.moveTo(actuatorPosition) Poller(3).check( ExecutionProbe(lambda: self.assertTrue( np.allclose(actuatorPosition, self.instrumentClient1.getPosition()))))
def test_ignores_step_failures(self): self.convergeable.requestPanic() self.closeLoopAndWaitForStep() Poller(1).check( ExecutionProbe(lambda: self.assertTrue( self.operatorLog.getLoopFailureLogCount() >= 1))) self.loop.open()
def _testGetStatus(self): status = self.instrumentClient1.getStatus() cmdCounter = status.commandCounter() self.instrumentClient1.moveTo(np.arange(4)) Poller(3).check( ExecutionProbe(lambda: self.assertEqual( cmdCounter + 1, self.instrumentClient1.getStatus().commandCounter())))
def _spawnProcess(self, commandToSpawn, logFilePath, runningMessage): processLog = io.open(os.path.join(logFilePath), "w") cmd = "%s" % commandToSpawn() print(cmd) process = subprocess.Popen(shlex.split(cmd), stdout=processLog, stderr=processLog) Poller(5).check(MessageInFileProbe(runningMessage, logFilePath)) return process
def test_awakes_from_long_running_sleeps(self): self.interStepTimeSec = 10 cnt = self.convergeable.getMeasureConvergenceCount() self.buildInitializedLoop() t0Sec = time.time() Poller(2).check( ExecutionProbe(lambda: self.assertTrue( self.convergeable.getMeasureConvergenceCount() > cnt))) self.loop.deinitialize() t1Sec = time.time() self.assertTrue(t1Sec - t0Sec < self.interStepTimeSec / 10)
def _startProcesses(self): psh = ProcessStartUpHelper() serverLog = open(os.path.join(self.LOG_DIR, "server.out"), "wb") self.server = subprocess.Popen([ sys.executable, psh.processProcessMonitorStartUpScriptPath(), self.CONF_FILE, self.CONF_SECTION ], stdout=serverLog, stderr=serverLog) Poller(5).check( MessageInFileProbe(ProcessMonitorRunner.RUNNING_MESSAGE, self.SERVER_LOG_PATH))
def test_deinitialize_and_reinitialize_restarts_in_open_loop(self): self.closeLoopAndWaitForStep() self.loop.deinitialize() closedLoopCnt = \ self.convergeable.getConvergenceStepCount() openLoopCnt = \ self.convergeable.getMeasureConvergenceCount() self.loop.initialize() self.assertFalse(self.loop.isClosed()) Poller(4).check( ExecutionProbe(lambda: self.assertEqual( closedLoopCnt, self.convergeable.getConvergenceStepCount()))) Poller(4).check( ExecutionProbe(lambda: self.assertTrue( self.convergeable.getMeasureConvergenceCount() > openLoopCnt))) self.loop.close() Poller(4).check( ExecutionProbe(lambda: self.assertTrue( closedLoopCnt < self.convergeable.getConvergenceStepCount())))
def waitForAtLeastConvergenceSteps(self, expectedSteps, timeoutSec=2): Poller(timeoutSec).check( ExecutionProbe(lambda: self.assertTrue( self.convergeable.getConvergenceStepCount() >= expectedSteps)))
def test_creation(self): self.assertFalse(self.loop.isClosed()) self.assertEqual(0, self.loop.getConvergenceStepCount()) Poller(2).check( ExecutionProbe(lambda: self.assertTrue( self.convergeable.getMeasureConvergenceCount() > 1)))
def closeLoopAndWaitForStep(self): self.loop.close() Poller(2).check( ExecutionProbe(lambda: self.assertTrue( self.convergeable.getConvergenceStepCount() > 0)))
def test_optimistic(self): t = self._createDummyTask() self.executor.execute(t) Poller(5).check(TaskCompletionProbe(t)) self.assertTrue(t.hasBeenPerformed())