def test_queue_timeout(self): taskrunner = TaskRunner("guest", "guest", "localhost", "/", "ots", 5672, "test_taskrunner", 1, 1, 1, 1) _init_queue(self.channel, "test_taskrunner", taskrunner._services_exchange, "test_taskrunner") taskrunner.add_task("dummycommand") self.assertRaises(OtsQueueTimeoutError, taskrunner.run)
class TestTaskRunner(unittest.TestCase): def setUp(self): self.taskrunner = TaskRunner("guest", "guest", "localhost", "/", "ots", 5672, "test_taskrunner", 1, 60, 1, 1) self.connection = amqp.Connection(host="localhost", userid="guest", password="******", virtual_host="/", insist=False) self.channel = self.connection.channel() _init_queue(self.channel, "test_taskrunner", self.taskrunner._services_exchange, "test_taskrunner") def tearDown(self): self.channel.queue_delete(queue="test_taskrunner", nowait=True) def test_on_message_status(self): task_1 = Task([1, 2], 10) task_2 = Task([1, 2], 10) start_msg = StateChangeMessage(task_1.task_id, TaskCondition.START) message = AMQPMessageStub() message.body = dumps(start_msg) self.taskrunner._tasks = [task_1, task_2] self.taskrunner._on_message(message) self.assertEquals(2, len(self.taskrunner._tasks)) end_msg = StateChangeMessage(task_1.task_id, TaskCondition.FINISH) message = AMQPMessageStub() message.body = dumps(end_msg) self.taskrunner._on_message(message) self.assertEquals(1, len(self.taskrunner._tasks)) self.assertEquals(task_2, self.taskrunner._tasks[0]) def test_on_message_relay(self): message = AMQPMessageStub() message.body = dumps(Foo()) def test_handler(signal, dto, **kwargs): self.assertTrue(isinstance(dto, Foo)) self.assertEquals(1, dto.bar) DTO_SIGNAL.connect(test_handler) self.taskrunner._on_message(message) def test_dispatch_tasks(self): task_1 = Task(["1", "2"], 10) task_2 = Task(["1", "2"], 10) self.taskrunner._tasks = [task_1, task_2] self.taskrunner._dispatch_tasks() def test_cb(message): self.channel.basic_ack(delivery_tag=message.delivery_tag) self.assertEquals("1 2", loads(message.body).command) self.channel.basic_qos(0, 1, False) self.channel.basic_consume(queue="test_taskrunner", callback=test_cb) self.channel.wait() self.channel.wait() def test_wait_for_all_tasks(self): class ChannelStub: count = 5 def __init__(self, taskrunner=None): self.taskrunner = taskrunner def wait(self): if self.taskrunner: assert (range(self.count) == self.taskrunner._tasks) self.taskrunner._tasks.pop() self.count -= 1 self.taskrunner._channel = ChannelStub() self.taskrunner._wait_for_all_tasks() self.taskrunner._tasks = [0, 1, 2, 3, 4] self.taskrunner._channel = ChannelStub(self.taskrunner) self.taskrunner._wait_for_all_tasks() def test_add_task(self): self.taskrunner.add_task([1, 2, 3]) self.assertEquals(1, len(self.taskrunner._tasks)) task = self.taskrunner._tasks[0] self.assertEquals([1, 2, 3], task.command) self.taskrunner._is_run = True self.assertRaises(TaskRunnerException, self.taskrunner.add_task, [1]) def test_cannot_run_multiple_times(self): self.taskrunner._is_run = True self.assertRaises(TaskRunnerException, self.taskrunner.run)
class TestTaskRunner(unittest.TestCase): def setUp(self): self.taskrunner = TaskRunner("guest", "guest", "localhost", "/", "ots", 5672, "test_taskrunner", 1, 60, 1, 1) self.connection = amqp.Connection(host = "localhost", userid = "guest", password = "******", virtual_host = "/", insist = False) self.channel = self.connection.channel() _init_queue(self.channel, "test_taskrunner", self.taskrunner._services_exchange, "test_taskrunner") def tearDown(self): self.channel.queue_delete(queue = "test_taskrunner", nowait=True) def test_on_message_status(self): task_1 = Task([1, 2], 10) task_2 = Task([1, 2], 10) start_msg = StateChangeMessage(task_1.task_id, TaskCondition.START) message = AMQPMessageStub() message.body = dumps(start_msg) self.taskrunner._tasks = [task_1, task_2] self.taskrunner._on_message(message) self.assertEquals(2, len(self.taskrunner._tasks)) end_msg = StateChangeMessage(task_1.task_id, TaskCondition.FINISH) message = AMQPMessageStub() message.body = dumps(end_msg) self.taskrunner._on_message(message) self.assertEquals(1, len(self.taskrunner._tasks)) self.assertEquals(task_2, self.taskrunner._tasks[0]) def test_on_message_relay(self): message = AMQPMessageStub() message.body = dumps(Foo()) def test_handler(signal, dto, **kwargs): self.assertTrue(isinstance(dto, Foo)) self.assertEquals(1, dto.bar) DTO_SIGNAL.connect(test_handler) self.taskrunner._on_message(message) def test_dispatch_tasks(self): task_1 = Task(["1", "2"], 10) task_2 = Task(["1", "2"], 10) self.taskrunner._tasks = [task_1, task_2] self.taskrunner._dispatch_tasks() def test_cb(message): self.channel.basic_ack(delivery_tag = message.delivery_tag) self.assertEquals("1 2", loads(message.body).command) self.channel.basic_qos(0, 1, False) self.channel.basic_consume(queue = "test_taskrunner", callback = test_cb) self.channel.wait() self.channel.wait() def test_wait_for_all_tasks(self): class ChannelStub: count = 5 def __init__(self, taskrunner = None): self.taskrunner = taskrunner def wait(self): if self.taskrunner: assert(range(self.count) == self.taskrunner._tasks) self.taskrunner._tasks.pop() self.count -= 1 self.taskrunner._channel = ChannelStub() self.taskrunner._wait_for_all_tasks() self.taskrunner._tasks = [0,1,2,3,4] self.taskrunner._channel = ChannelStub(self.taskrunner) self.taskrunner._wait_for_all_tasks() def test_add_task(self): self.taskrunner.add_task([1,2,3]) self.assertEquals(1, len(self.taskrunner._tasks)) task = self.taskrunner._tasks[0] self.assertEquals([1,2,3], task.command) self.taskrunner._is_run = True self.assertRaises(TaskRunnerException, self.taskrunner.add_task, [1]) def test_cannot_run_multiple_times(self): self.taskrunner._is_run = True self.assertRaises(TaskRunnerException, self.taskrunner.run)