async def test_startup_schedule(self): """Test startup of _scheduler :assert: the number of running tasks """ await self.populate_test_data( ) # Populate data in foglamp.scheduled_processes scheduler = Scheduler(_address, _m_port) await scheduler.start() # Declare schedule startup, and execute startup_schedule = StartUpSchedule( ) # A scheduled process of the _scheduler startup_schedule.name = 'startup schedule' startup_schedule.process_name = 'sleep30' startup_schedule.repeat = datetime.timedelta( seconds=0) # set no repeat to startup startup_schedule.enabled = True await scheduler.save_schedule(startup_schedule) await asyncio.sleep(1) # Assert no tasks ar running tasks = await scheduler.get_running_tasks() assert len(tasks) == 0 await scheduler.get_schedule(startup_schedule.schedule_id ) # ID of the schedule startup await self.stop_scheduler(scheduler) scheduler = Scheduler() await scheduler.start() await asyncio.sleep(2) # Assert only 1 task is running tasks = await scheduler.get_running_tasks() assert len(tasks) == 1 scheduler.max_running_tasks = 0 # set that no tasks would run await scheduler.cancel_task(tasks[0].task_id) await asyncio.sleep(10) # Assert no tasks are running tasks = await scheduler.get_running_tasks() assert len(tasks) == 0 scheduler.max_running_tasks = 1 await asyncio.sleep(2) # Assert a single task is running tasks = await scheduler.get_running_tasks() assert len(tasks) == 1 await self.stop_scheduler(scheduler)
async def test_max_processes(self): """Test the maximum number of running processes :assert: the number of running processes """ await self.populate_test_data( ) # Populate data in foglamp.scheduled_processes scheduler = Scheduler(_address, _m_port) await scheduler.start() # 2 maximum tasks # 1 runs at 1 second # 2 runs at 2 seconds # 3 runs at 11 seconds # 4 runs at 12 seconds # 5 runs at 21 seconds # 6 runs at 22 seconds # 7 runs at 31 seconds # 8 runs at 32 seconds # Total: 6 scheduler.max_running_tasks = 2 # set the maximum number of running tasks in parallel # Set interval schedule configuration interval_schedule = IntervalSchedule() interval_schedule.repeat = datetime.timedelta(seconds=1) interval_schedule.name = 'max active' interval_schedule.exclusive = False interval_schedule.process_name = 'sleep10' interval_schedule.enabled = True await scheduler.save_schedule(interval_schedule) await asyncio.sleep(30.3) scheduler.max_running_tasks = 0 # set the maximum number of running tasks in parallel tasks = await scheduler.get_tasks(10) assert len(tasks) == 6 tasks = await scheduler.get_running_tasks() assert len(tasks) == 2 # They end... await asyncio.sleep(20) scheduler.max_running_tasks = 10 await asyncio.sleep(11) tasks = await scheduler.get_running_tasks() assert len(tasks) == 10 await self.stop_scheduler(scheduler)
async def test_purge_tasks(self): await self.populate_test_data( ) # Populate data in foglamp.scheduled_processes scheduler = Scheduler(_address, _m_port) await scheduler.start() interval_schedule = IntervalSchedule() interval_schedule.name = 'purge_task' interval_schedule.process_name = "sleep5" interval_schedule.repeat = datetime.timedelta(seconds=0) # interval_schedule.repeat = datetime.timedelta(seconds=30) await scheduler.save_schedule(interval_schedule) await asyncio.sleep(1) tasks = await scheduler.get_tasks(5) assert tasks scheduler.max_running_tasks = 0 await asyncio.sleep(7) scheduler.max_completed_task_age = datetime.timedelta(seconds=1) await scheduler.purge_tasks() tasks = await scheduler.get_tasks(5) assert not tasks await self.stop_scheduler(scheduler)