2019-05-12 14:56:09 +00:00
|
|
|
import time
|
|
|
|
import unittest
|
|
|
|
import threading
|
2019-05-16 21:02:32 +00:00
|
|
|
import random
|
2019-05-12 14:56:09 +00:00
|
|
|
from opendm.remote import LocalRemoteExecutor, Task, NodeTaskLimitReachedException
|
2019-05-13 11:18:03 +00:00
|
|
|
from pyodm import Node, exceptions
|
2019-05-12 17:41:39 +00:00
|
|
|
from pyodm.types import TaskStatus
|
2019-05-12 14:56:09 +00:00
|
|
|
|
|
|
|
class TestRemote(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2019-05-21 14:33:48 +00:00
|
|
|
self.lre = LocalRemoteExecutor('http://localhost:9001')
|
2019-05-16 21:02:32 +00:00
|
|
|
|
|
|
|
projects = []
|
|
|
|
for i in range(9):
|
|
|
|
projects.append('/submodels/submodel_00' + str(i).rjust(2, '0'))
|
|
|
|
self.lre.set_projects(projects)
|
2019-05-12 14:56:09 +00:00
|
|
|
|
|
|
|
def test_lre_init(self):
|
|
|
|
self.assertFalse(self.lre.node_online)
|
|
|
|
|
|
|
|
def test_processing_logic(self):
|
|
|
|
# Fake online status
|
|
|
|
self.lre.node_online = True
|
|
|
|
|
|
|
|
MAX_QUEUE = 2
|
|
|
|
class nonloc:
|
|
|
|
local_task_check = False
|
2019-05-12 17:41:39 +00:00
|
|
|
remote_queue = 1
|
2019-05-13 11:18:03 +00:00
|
|
|
should_fail = False
|
2019-05-16 21:02:32 +00:00
|
|
|
task_limit_reached = False
|
2019-05-12 17:41:39 +00:00
|
|
|
|
|
|
|
class OdmTaskMock:
|
|
|
|
def __init__(self, running, queue_num):
|
|
|
|
self.running = running
|
|
|
|
self.queue_num = queue_num
|
|
|
|
self.uuid = 'xxxxx-xxxxx-xxxxx-xxxxx-xxxx' + str(queue_num)
|
|
|
|
|
2019-12-05 20:28:06 +00:00
|
|
|
def info(self, with_output=None):
|
2019-05-12 17:41:39 +00:00
|
|
|
class StatusMock:
|
|
|
|
status = TaskStatus.RUNNING if self.running else TaskStatus.QUEUED
|
2019-05-13 11:18:03 +00:00
|
|
|
processing_time = 1
|
2019-12-05 20:28:06 +00:00
|
|
|
output = "test output"
|
2019-05-12 17:41:39 +00:00
|
|
|
return StatusMock()
|
|
|
|
|
|
|
|
def remove(self):
|
|
|
|
return True
|
2019-05-12 14:56:09 +00:00
|
|
|
|
|
|
|
class TaskMock(Task):
|
|
|
|
def process_local(self):
|
2019-05-12 17:41:39 +00:00
|
|
|
# First task should be 0000 or 0001
|
|
|
|
if not nonloc.local_task_check: nonloc.local_task_check = self.project_path.endswith("0000") or self.project_path.endswith("0001")
|
2019-05-21 14:33:48 +00:00
|
|
|
|
|
|
|
if nonloc.should_fail:
|
|
|
|
if self.project_path.endswith("0006"):
|
|
|
|
raise exceptions.TaskFailedError("FAIL #6")
|
|
|
|
|
2019-05-12 17:41:39 +00:00
|
|
|
time.sleep(1)
|
2019-05-12 14:56:09 +00:00
|
|
|
|
|
|
|
def process_remote(self, done):
|
2019-05-12 17:41:39 +00:00
|
|
|
time.sleep(0.05) # file upload
|
|
|
|
|
|
|
|
self.remote_task = OdmTaskMock(nonloc.remote_queue <= MAX_QUEUE, nonloc.remote_queue)
|
|
|
|
self.params['tasks'].append(self.remote_task)
|
2019-05-13 11:18:03 +00:00
|
|
|
|
|
|
|
if nonloc.should_fail:
|
|
|
|
if self.project_path.endswith("0006"):
|
|
|
|
raise exceptions.TaskFailedError("FAIL #6")
|
2019-05-16 21:02:32 +00:00
|
|
|
|
2019-05-12 17:41:39 +00:00
|
|
|
nonloc.remote_queue += 1
|
2019-05-12 14:56:09 +00:00
|
|
|
|
|
|
|
# Upload successful
|
|
|
|
done(error=None, partial=True)
|
|
|
|
|
|
|
|
# Async processing
|
|
|
|
def monitor():
|
|
|
|
try:
|
2019-05-16 21:02:32 +00:00
|
|
|
if nonloc.task_limit_reached and random.randint(0, 4) == 0:
|
2019-05-12 17:41:39 +00:00
|
|
|
nonloc.remote_queue -= 1
|
2019-05-16 21:02:32 +00:00
|
|
|
raise NodeTaskLimitReachedException("Random fail!")
|
|
|
|
|
|
|
|
if not nonloc.task_limit_reached and self.remote_task.queue_num > MAX_QUEUE:
|
|
|
|
nonloc.remote_queue -= 1
|
|
|
|
nonloc.task_limit_reached = True
|
2019-05-12 14:56:09 +00:00
|
|
|
raise NodeTaskLimitReachedException("Delayed task limit reached")
|
2019-05-12 17:41:39 +00:00
|
|
|
time.sleep(0.5)
|
|
|
|
nonloc.remote_queue -= 1
|
2019-05-12 14:56:09 +00:00
|
|
|
done()
|
|
|
|
except Exception as e:
|
|
|
|
done(e)
|
|
|
|
|
|
|
|
t = threading.Thread(target=monitor)
|
|
|
|
self.params['threads'].append(t)
|
|
|
|
t.start()
|
|
|
|
|
|
|
|
self.lre.run(TaskMock)
|
|
|
|
self.assertTrue(nonloc.local_task_check)
|
|
|
|
|
2019-05-13 11:18:03 +00:00
|
|
|
nonloc.should_fail = True
|
2019-05-16 21:02:32 +00:00
|
|
|
nonloc.remote_queue = 1
|
|
|
|
nonloc.task_limit_reached = False
|
|
|
|
|
2019-05-13 11:18:03 +00:00
|
|
|
with self.assertRaises(exceptions.TaskFailedError):
|
|
|
|
self.lre.run(TaskMock)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|