def test_task_sleep(self): sleep = get_task('sleep')() loop = asyncio.get_event_loop() loop.run_until_complete(sleep.execute('1')) self.assertTrue(sleep.duration > 1) sleep2 = sleep = get_task('sleep')(max_time=1) with self.assertRaises(TimeoutError): loop.run_until_complete(sleep2.execute('2')) self.assertTrue(sleep.duration < 1.1)
async def add_subtask(self, multitask, task_no, total_tasks, task_name, task_args=(), task_kwargs={}, task_priority=NORMAL_PRIORITY, authenticated_user=None, task_context=None): assert(not task_context is None) task = get_task(task_name)(user=authenticated_user) task_id = uuid.uuid4().hex await self._q.put((task_priority, time.time(), TaskInfo(task_id, task, task_args, task_kwargs, task_context, multitask, task_no, total_tasks)))
def test_multi(self): loop = asyncio.get_event_loop() run = loop.run_until_complete m = get_task('multi')() run(m.start(['date', 'sleep', 'date'], [(['%d-%m-%Y %H:%M %Z'], {'utc':True}), ([1], {}), (['%d-%m-%Y %H:%M %Z'], {'utc':False}),])) cb = Mock() t = run(m.next_task()) self.assertEqual(t.task_name, 'date') self.assertEqual(t.task_no, 0) run(m.update_task_result(0, result=0, on_all_finished=cb)) cb.assert_not_called() t = run(m.next_task()) self.assertEqual(t.task_name, 'sleep') self.assertEqual(t.task_no, 1) run(m.update_task_result(1, result=1, on_all_finished=cb)) cb.assert_not_called() t = run(m.next_task()) self.assertEqual(t.task_name, 'date') self.assertEqual(t.task_no, 2) self.assertEqual(t.task_kwargs, {'utc':False}) run(m.update_task_result(2, result=2, on_all_finished=cb)) self.assertEqual(cb.call_count, 1) self.assertEqual(cb.call_args[0][0]['results'], [0,1,2]) self.assertTrue(cb.call_args[0][0]['duration']>0 ) t = run(m.next_task()) self.assertTrue(t is None)
def test_task_date(self): date = get_task('date')() self.assertTrue(date) loop = asyncio.get_event_loop() args = loop.run_until_complete( date.validate_args('%d-%m-%Y %H:%M %Z', utc=True)) self.assertEqual(args[1], '+%d-%m-%Y %H:%M %Z') res = loop.run_until_complete(date.execute(*args)) res = loop.run_until_complete(date.parse_result(res)) self.assertTrue(res.endswith('UTC')) self.assertTrue(date.duration > 0) res = loop.run_until_complete(date.run('%d-%m-%Y %H:%M %Z', utc=True)) self.assertTrue(res.endswith('UTC'))
def add_task(self, task_name, task_args=(), task_kwargs={}, task_priority=NORMAL_PRIORITY, authenticated_user=None, task_context=None): assert(not task_context is None) task = get_task(task_name)(user=authenticated_user) task_id = uuid.uuid4().hex loop = asyncio.get_event_loop() #async def _add(): # we need to assure that task_id from run_task is sent before first update # this is a hack - need to find a better way # await asyncio.sleep(0.1) # for security reason consider sync version of put and throw error when q is full # await self._q.put((task_priority, time.time(), TaskInfo(task_id, task, task_args, task_kwargs, task_context, # None, None, None))) self._q.put_nowait((task_priority, time.time(), TaskInfo(task_id, task, task_args, task_kwargs, task_context, None, None, None))) return task_id
def test_multi(self): loop = asyncio.get_event_loop() run = loop.run_until_complete m = get_task('multi')() run( m.start(['date', 'sleep', 'date'], [ (['%d-%m-%Y %H:%M %Z'], { 'utc': True }), ([1], {}), (['%d-%m-%Y %H:%M %Z'], { 'utc': False }), ])) cb = Mock() t = run(m.next_task()) self.assertEqual(t.task_name, 'date') self.assertEqual(t.task_no, 0) run(m.update_task_result(0, result=0, on_all_finished=cb)) cb.assert_not_called() t = run(m.next_task()) self.assertEqual(t.task_name, 'sleep') self.assertEqual(t.task_no, 1) run(m.update_task_result(1, result=1, on_all_finished=cb)) cb.assert_not_called() t = run(m.next_task()) self.assertEqual(t.task_name, 'date') self.assertEqual(t.task_no, 2) self.assertEqual(t.task_kwargs, {'utc': False}) run(m.update_task_result(2, result=2, on_all_finished=cb)) self.assertEqual(cb.call_count, 1) self.assertEqual(cb.call_args[0][0]['results'], [0, 1, 2]) self.assertTrue(cb.call_args[0][0]['duration'] > 0) t = run(m.next_task()) self.assertTrue(t is None)