Beispiel #1
0
 async def test_retry_failed_failure_cannot_hook_metric(self):
     ''' retry_failed should fail if we cannot enable a tm. It should generate a retry task '''
     tm = transfer_methods.transfermethod(f=noop,
                                          schedule=OnUpdateSchedule(
                                              Datasource('uri')))
     tm._decorate_method(tm._f)
     tmi = TransferMethodsIndex()
     self.assertTrue(tmi.add_tm(tm))
     self.assertTrue(tm.mid in tmi._disabled_methods)
     self.assertFalse(await tmi.enable_tm(tm.mid))
     self.assertFalse(tm.mid in tmi._enabled_methods)
     self.assertIsNone(tmi._disabled_methods[tm.mid]['first'])
     current_task = asyncio.Task.current_task()
     tasks = asyncio.Task.all_tasks()
     self.assertEqual(len(tasks), 2)  #this task and retry task
     tm.schedule = OnUpdateSchedule()
     self.assertTrue(await tmi._retry_failed(sleep=1))
     tasks = asyncio.Task.all_tasks()
     self.assertEqual(len(tasks), 2)  # no new task
     [
         task.cancel() for task in asyncio.Task.all_tasks()
         if task != current_task
     ]
    async def test_bind_transfermethod_success_with_CronSchedule(self):
        ''' calling bind should succeed with a tm with CronSchedule '''
        try:

            async def enable_tm(mid):
                return True

            enable_tm_bck = tmIndex.enable_tm
            tmIndex.enable_tm = enable_tm

            def func(param):
                pass

            tm = transfer_methods.transfermethod(
                f=func,
                f_params={'param': 'param'},
                schedule=schedules.CronSchedule())
            self.assertTrue(isinstance(tm.mid, uuid.UUID))
            self.assertEqual(tm._f, func)
            self.assertNotEqual(tm.schedule, None)
            self.assertTrue(isinstance(tm.schedule, schedules.CronSchedule))
            self.assertEqual(tm.f_params, {'param': 'param'})
            await tm.bind()
            self.assertEqual(tm._func_params.keys(), {'param': 'param'}.keys())
            self.assertNotEqual(tm.schedule, None)
            self.assertTrue(isinstance(tm.schedule, schedules.CronSchedule))
            self.assertEqual(tm.schedule.activation_metrics, [])
            self.assertEqual(tm.schedule.exec_on_load, False)
            self.assertIsNotNone(getattr(tm, 'f', None))
            self.assertTrue(asyncio.iscoroutinefunction(tm.f))
            tm_info = tmIndex.get_tm_info(tm.mid)
            self.assertEqual(tm_info['enabled'], False)
            self.assertEqual(tm_info['tm'], tm)
        except:
            raise
        finally:
            tmIndex.enable_tm = enable_tm_bck
    def test_transfermethod_success_registering_decorated_transfermethod_CronSchedule(
            self):
        '''transfermethod object should be able to register a decorated transfer_method with CronSchedule '''
        def func(param):
            pass

        tm = transfer_methods.transfermethod(f_params={'param': 'param'},
                                             schedule=schedules.CronSchedule())
        self.assertTrue(isinstance(tm.mid, uuid.UUID))
        self.assertNotEqual(tm.schedule, None)
        self.assertTrue(isinstance(tm.schedule, schedules.CronSchedule))
        self.assertEqual(tm.f_params, {'param': 'param'})
        f = tm(func)
        self.assertEqual(f, func)
        self.assertEqual(tm._func_params.keys(), {'param': 'param'}.keys())
        self.assertNotEqual(tm.schedule, None)
        self.assertTrue(isinstance(tm.schedule, schedules.CronSchedule))
        self.assertEqual(tm.schedule.activation_metrics, [])
        self.assertEqual(tm.schedule.exec_on_load, False)
        self.assertIsNotNone(getattr(tm, 'f', None))
        self.assertTrue(asyncio.iscoroutinefunction(tm.f))
        tm_info = tmIndex.get_tm_info(tm.mid)
        self.assertEqual(tm_info['enabled'], False)
        self.assertEqual(tm_info['tm'], tm)
 def test_transfer_method_CronSchedule_success(self):
     ''' creation of a transfermethod object should succeed if schedule is CronSchedule'''
     schedule = schedules.CronSchedule()
     tm = transfer_methods.transfermethod(schedule=schedule)
     self.assertEqual(tm.schedule, schedule)
 def test_transfermethod_failure_invalid_schedule(self):
     ''' creation of a transfermethod object should fail if shedule parameter is invalid '''
     schedule = 'once upon a time'
     with self.assertRaises(exceptions.BadParametersException) as cm:
         tm = transfer_methods.transfermethod(schedule=schedule)
     self.assertEqual(cm.exception.msg, 'Invalid "schedule" attribute')