def test_bad_options(self):
     with self.assertRaises(aschedule.api.AScheduleException):
         aschedule.every(self.sample_job,
                         timedelta=datetime.timedelta(seconds=0))
     with self.assertRaises(aschedule.api.AScheduleException):
         aschedule.every(self.sample_job,
                         timedelta=datetime.timedelta(minutes=-10))
    def test_cancel_by_sleep_including_jobs(self):
        self.schedule = aschedule.every(self.sample_job1, seconds=2)
        self.loop.run_until_complete(asyncio.sleep(15))

        # just to makes sure that the jobs are indeed cancelled.
        self.loop.run_until_complete(asyncio.sleep(20))
        self.assertEqual(1, self.count, "more than 1 job got executed")
    def test_cancel_just_schedule(self):
        self.cancel_running_jobs = False
        self.schedule = aschedule.every(self.sample_job2, seconds=2)
        try:
            self.loop.run_until_complete(self.schedule.future)
        except asyncio.CancelledError:
            pass

        self.loop.run_until_complete(asyncio.sleep(20))
        self.assertEqual(5, self.count)
    def test_cancel_including_jobs(self):
        self.cancel_running_jobs = True
        self.schedule = aschedule.every(self.sample_job1, seconds=2)
        try:
            self.loop.run_until_complete(self.schedule.future)
        except asyncio.CancelledError:
            pass

        # just to makes sure that the jobs are indeed cancelled.
        self.loop.run_until_complete(asyncio.sleep(20))
        self.assertEqual(1, self.count, "more than 1 job got executed")
Exemple #5
0
def main():
    async def job1():
        job1.counter += 1
        if job1.counter == 10:
            future1.cancel()
            return
        print('job1:', job1.counter, datetime.datetime.now())

    async def job2():
        job2.counter += 1
        if job2.counter == 10:
            future2.cancel()
            return
        print('job2:', job2.counter, datetime.datetime.now())

    job1.counter = 0
    job2.counter = -10
    loop = asyncio.get_event_loop()
    future1 = aschedule.every(job1, seconds=1)
    future2 = aschedule.every(job2, seconds=1)
    loop.run_until_complete(
        asyncio.gather(future1, future2, return_exceptions=True))
    def test_seconds(self):
        self.schedule = aschedule.every(self.sample_job,
                                        seconds=self.interval_in_seconds)
        start_time = self.loop.time()
        # error if: the future doesn't exit or produces other than CancelledError
        with self.assertRaises(asyncio.CancelledError):
            self.loop.run_until_complete(future=self.schedule.future)
        end_time = self.loop.time()

        self.assertAlmostEqual(start_time + self.interval_in_seconds *
                               (self.count_max - 1),
                               end_time,
                               places=0)
    def test_start_at_now(self):
        self.schedule = aschedule.every(self.sample_job,
                                        seconds=self.interval_in_seconds,
                                        start_at=datetime.datetime.now())
        start_time = self.loop.time()
        # error if: the future doesn't exit or produces other than CancelledError
        with self.assertRaises(asyncio.CancelledError):
            self.loop.run_until_complete(future=self.schedule.future)
        end_time = self.loop.time()

        # error if: given start_at, the job doesn't execute 5 times within 8 seconds.
        self.assertAlmostEqual(start_time + self.interval_in_seconds *
                               (self.count_max - 1),
                               end_time,
                               places=0)