Esempio n. 1
0
    async def test_update(self):
        """Test update of a running task
        :assert:
            the number of tasks running
            information regarding the process running
        """
        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 = 'sleep10'
        interval_schedule.process_name = "sleep10"
        interval_schedule.repeat = datetime.timedelta(seconds=0)
        interval_schedule.enabled = True

        await scheduler.save_schedule(interval_schedule
                                      )  # Save update on _scheduler

        await asyncio.sleep(1)
        # Assert only 1 task is running
        tasks = await scheduler.get_running_tasks()
        assert len(tasks) == 1

        # Update 'updated' schedule interval
        interval_schedule.name = 'updated'
        interval_schedule.process_name = "sleep1"
        interval_schedule.repeat = datetime.timedelta(
            seconds=5)  # Set time interval to 5 sec
        interval_schedule.enabled = True

        await scheduler.save_schedule(interval_schedule
                                      )  # Save update on _scheduler
        await asyncio.sleep(6)

        # Assert: only 1 task is running
        tasks = await scheduler.get_running_tasks(
        )  # list of current running tasks
        assert len(tasks) == 1

        interval_schedule.exclusive = False
        await scheduler.save_schedule(interval_schedule)

        # Check able to get same schedule after restart
        # Check fields have been modified
        await self.stop_scheduler(scheduler)
        scheduler = Scheduler(_address, _m_port)
        await scheduler.start()

        schedule = await scheduler.get_schedule(interval_schedule.schedule_id)

        # Make sure that the values used by schedule are as expected
        assert schedule.process_name == 'sleep1'
        assert schedule.name == 'updated'
        assert schedule.repeat.seconds == 5
        assert not schedule.exclusive

        await self.stop_scheduler(scheduler)
Esempio n. 2
0
    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)
Esempio n. 3
0
    async def test_stop(self):
        """Test that stop_scheduler actually works"""
        await self.populate_test_data(
        )  # Populate data in foglamp.scheduled_processes

        scheduler = Scheduler(_address, _m_port)
        await scheduler.start()

        # Set schedule interval
        interval_schedule = IntervalSchedule()
        interval_schedule.exclusive = False
        interval_schedule.name = 'sleep1'
        interval_schedule.process_name = "sleep1"
        interval_schedule.repeat = datetime.timedelta(
            seconds=1)  # Set frequency of

        await scheduler.save_schedule(interval_schedule
                                      )  # Save schedule updates
        await asyncio.sleep(10)

        await self.stop_scheduler(scheduler)
Esempio n. 4
0
    async def test_get_tasks(self):
        """Get list of tasks
        :assert:
            Number of running tasks
            The state of tasks
            the start time of a given task
        """
        await self.populate_test_data(
        )  # Populate data in foglamp.scheduled_processes

        scheduler = Scheduler(_address, _m_port)
        await scheduler.start()

        # declare _scheduler task
        interval_schedule = IntervalSchedule()
        interval_schedule.name = 'get_tasks'
        interval_schedule.process_name = "sleep5"
        interval_schedule.repeat = datetime.timedelta(seconds=1)
        interval_schedule.exclusive = False

        await scheduler.save_schedule(interval_schedule)

        await asyncio.sleep(15)

        # Assert running tasks
        tasks = await scheduler.get_tasks(
            where=["state", "=", int(Task.State.INTERRUPTED)])
        assert not tasks

        tasks = await scheduler.get_tasks(where=["end_time", "=", 'NULL'])
        assert tasks

        tasks = await scheduler.get_tasks(limit=50)
        states = [int(task.state) for task in tasks]

        assert len(tasks) > 1
        assert int(Task.State.RUNNING) in states
        assert int(Task.State.COMPLETE) in states

        tasks = await scheduler.get_tasks(1)
        assert len(tasks) == 1

        tasks = await scheduler.get_tasks(
            where=["state", "=", int(Task.State.RUNNING)],
            sort=[["state", "desc"]],
            offset=50)
        assert not tasks

        tasks = await scheduler.get_tasks(
            where=["state", "=", int(Task.State.RUNNING)],
            sort=[["state", "desc"], ["start_time", "asc"]])
        assert tasks

        tasks = await scheduler.get_tasks(or_where_list=[["state", "=", int(Task.State.RUNNING)], \
                                                         ["state", "=", int(Task.State.RUNNING)]])
        assert tasks

        tasks = await scheduler.get_tasks(and_where_list=[["state", "=", int(Task.State.RUNNING)], \
                                                          ["state", "=", int(Task.State.RUNNING)]])
        assert tasks

        await self.stop_scheduler(scheduler)