2016-01-04 01:56:22 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2017-05-14 12:18:29 +02:00
|
|
|
# Copyright (C) 2015-2017
|
2016-01-05 14:12:03 +01:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
2016-01-04 01:56:22 +01:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
|
|
|
"""
|
2016-10-17 00:22:40 +02:00
|
|
|
This module contains an object that represents Tests for JobQueue
|
2016-01-04 01:56:22 +01:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
import sys
|
2016-08-20 22:01:07 +02:00
|
|
|
import unittest
|
2016-11-08 23:39:25 +01:00
|
|
|
import datetime
|
|
|
|
import time
|
|
|
|
from math import ceil
|
2016-01-04 01:56:22 +01:00
|
|
|
from time import sleep
|
|
|
|
|
2016-09-06 15:38:07 +02:00
|
|
|
from tests.test_updater import MockBot
|
|
|
|
|
2016-01-04 01:56:22 +01:00
|
|
|
sys.path.append('.')
|
|
|
|
|
2016-05-25 22:51:13 +02:00
|
|
|
from telegram.ext import JobQueue, Job, Updater
|
2016-01-04 01:56:22 +01:00
|
|
|
from tests.base import BaseTest
|
|
|
|
|
|
|
|
# Enable logging
|
|
|
|
root = logging.getLogger()
|
|
|
|
root.setLevel(logging.INFO)
|
|
|
|
|
|
|
|
ch = logging.StreamHandler(sys.stdout)
|
|
|
|
ch.setLevel(logging.WARN)
|
2016-05-15 02:52:35 +02:00
|
|
|
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
2016-01-04 01:56:22 +01:00
|
|
|
ch.setFormatter(formatter)
|
|
|
|
root.addHandler(ch)
|
|
|
|
|
|
|
|
|
2016-01-04 11:08:43 +01:00
|
|
|
class JobQueueTest(BaseTest, unittest.TestCase):
|
2016-01-04 01:56:22 +01:00
|
|
|
"""
|
|
|
|
This object represents Tests for Updater, Dispatcher, WebhookServer and
|
|
|
|
WebhookHandler
|
|
|
|
"""
|
|
|
|
|
|
|
|
def setUp(self):
|
2016-09-06 15:38:07 +02:00
|
|
|
self.jq = JobQueue(MockBot('jobqueue_test'))
|
2016-09-27 09:21:35 +02:00
|
|
|
self.jq.start()
|
2016-01-04 01:56:22 +01:00
|
|
|
self.result = 0
|
2016-11-08 23:39:25 +01:00
|
|
|
self.job_time = 0
|
2016-01-04 01:56:22 +01:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
if self.jq is not None:
|
|
|
|
self.jq.stop()
|
|
|
|
|
2016-05-25 22:51:13 +02:00
|
|
|
def job1(self, bot, job):
|
2016-01-04 01:56:22 +01:00
|
|
|
self.result += 1
|
|
|
|
|
2016-05-25 22:51:13 +02:00
|
|
|
def job2(self, bot, job):
|
2016-01-04 02:36:15 +01:00
|
|
|
raise Exception("Test Error")
|
|
|
|
|
2016-05-25 22:51:13 +02:00
|
|
|
def job3(self, bot, job):
|
|
|
|
self.result += 1
|
|
|
|
job.schedule_removal()
|
|
|
|
|
2016-05-26 14:07:44 +02:00
|
|
|
def job4(self, bot, job):
|
|
|
|
self.result += job.context
|
|
|
|
|
2016-11-08 23:39:25 +01:00
|
|
|
def job5(self, bot, job):
|
2016-12-13 23:38:13 +01:00
|
|
|
self.job_time = time.time()
|
2016-11-08 23:39:25 +01:00
|
|
|
|
2016-01-04 01:56:22 +01:00
|
|
|
def test_basic(self):
|
2016-05-25 22:51:13 +02:00
|
|
|
self.jq.put(Job(self.job1, 0.1))
|
2016-01-04 02:11:42 +01:00
|
|
|
sleep(1.5)
|
|
|
|
self.assertGreaterEqual(self.result, 10)
|
2016-01-04 01:56:22 +01:00
|
|
|
|
2016-05-26 14:07:44 +02:00
|
|
|
def test_job_with_context(self):
|
|
|
|
self.jq.put(Job(self.job4, 0.1, context=5))
|
|
|
|
sleep(1.5)
|
|
|
|
self.assertGreaterEqual(self.result, 50)
|
|
|
|
|
2016-01-04 01:56:22 +01:00
|
|
|
def test_noRepeat(self):
|
2016-05-25 22:51:13 +02:00
|
|
|
self.jq.put(Job(self.job1, 0.1, repeat=False))
|
2016-01-04 01:56:22 +01:00
|
|
|
sleep(0.5)
|
|
|
|
self.assertEqual(1, self.result)
|
|
|
|
|
|
|
|
def test_nextT(self):
|
2016-05-25 22:51:13 +02:00
|
|
|
self.jq.put(Job(self.job1, 0.1), next_t=0.5)
|
2016-01-04 01:56:22 +01:00
|
|
|
sleep(0.45)
|
|
|
|
self.assertEqual(0, self.result)
|
|
|
|
sleep(0.1)
|
|
|
|
self.assertEqual(1, self.result)
|
|
|
|
|
2016-01-04 02:36:15 +01:00
|
|
|
def test_multiple(self):
|
2016-05-25 22:51:13 +02:00
|
|
|
self.jq.put(Job(self.job1, 0.1, repeat=False))
|
|
|
|
self.jq.put(Job(self.job1, 0.2, repeat=False))
|
|
|
|
self.jq.put(Job(self.job1, 0.4))
|
2016-01-04 02:36:15 +01:00
|
|
|
sleep(1)
|
|
|
|
self.assertEqual(4, self.result)
|
|
|
|
|
2016-05-25 22:51:13 +02:00
|
|
|
def test_disabled(self):
|
|
|
|
j0 = Job(self.job1, 0.1)
|
|
|
|
j1 = Job(self.job1, 0.2)
|
|
|
|
|
|
|
|
self.jq.put(j0)
|
|
|
|
self.jq.put(Job(self.job1, 0.4))
|
|
|
|
self.jq.put(j1)
|
|
|
|
|
|
|
|
j0.enabled = False
|
|
|
|
j1.enabled = False
|
|
|
|
|
|
|
|
sleep(1)
|
|
|
|
self.assertEqual(2, self.result)
|
|
|
|
|
|
|
|
def test_schedule_removal(self):
|
|
|
|
j0 = Job(self.job1, 0.1)
|
|
|
|
j1 = Job(self.job1, 0.2)
|
|
|
|
|
|
|
|
self.jq.put(j0)
|
|
|
|
self.jq.put(Job(self.job1, 0.4))
|
|
|
|
self.jq.put(j1)
|
|
|
|
|
|
|
|
j0.schedule_removal()
|
|
|
|
j1.schedule_removal()
|
|
|
|
|
|
|
|
sleep(1)
|
|
|
|
self.assertEqual(2, self.result)
|
|
|
|
|
|
|
|
def test_schedule_removal_from_within(self):
|
|
|
|
self.jq.put(Job(self.job1, 0.4))
|
|
|
|
self.jq.put(Job(self.job3, 0.2))
|
|
|
|
|
|
|
|
sleep(1)
|
|
|
|
self.assertEqual(3, self.result)
|
|
|
|
|
|
|
|
def test_longer_first(self):
|
|
|
|
self.jq.put(Job(self.job1, 0.2, repeat=False))
|
|
|
|
self.jq.put(Job(self.job1, 0.1, repeat=False))
|
|
|
|
sleep(0.15)
|
|
|
|
self.assertEqual(1, self.result)
|
|
|
|
|
2016-01-04 02:36:15 +01:00
|
|
|
def test_error(self):
|
2016-05-25 22:51:13 +02:00
|
|
|
self.jq.put(Job(self.job2, 0.1))
|
|
|
|
self.jq.put(Job(self.job1, 0.2))
|
|
|
|
sleep(0.5)
|
|
|
|
self.assertEqual(2, self.result)
|
|
|
|
|
|
|
|
def test_jobs_tuple(self):
|
2016-06-22 00:46:29 +02:00
|
|
|
self.jq.stop()
|
2016-05-25 22:51:13 +02:00
|
|
|
jobs = tuple(Job(self.job1, t) for t in range(5, 25))
|
|
|
|
|
|
|
|
for job in jobs:
|
|
|
|
self.jq.put(job)
|
|
|
|
|
|
|
|
self.assertTupleEqual(jobs, self.jq.jobs())
|
2016-01-05 13:32:19 +01:00
|
|
|
|
|
|
|
def test_inUpdater(self):
|
2016-05-25 22:51:13 +02:00
|
|
|
u = Updater(bot="MockBot")
|
2016-09-27 09:21:35 +02:00
|
|
|
u.job_queue.start()
|
2016-09-06 15:38:07 +02:00
|
|
|
try:
|
|
|
|
u.job_queue.put(Job(self.job1, 0.5))
|
|
|
|
sleep(0.75)
|
|
|
|
self.assertEqual(1, self.result)
|
|
|
|
u.stop()
|
|
|
|
sleep(2)
|
|
|
|
self.assertEqual(1, self.result)
|
|
|
|
finally:
|
|
|
|
u.stop()
|
2016-01-04 11:08:43 +01:00
|
|
|
|
2016-11-08 23:39:25 +01:00
|
|
|
def test_time_unit_int(self):
|
|
|
|
# Testing seconds in int
|
2016-12-14 16:27:45 +01:00
|
|
|
delta = 2
|
|
|
|
expected_time = time.time() + delta
|
2016-11-08 23:39:25 +01:00
|
|
|
|
2016-12-14 16:27:45 +01:00
|
|
|
self.jq.put(Job(self.job5, delta, repeat=False))
|
2016-12-13 23:38:13 +01:00
|
|
|
sleep(2.5)
|
|
|
|
self.assertAlmostEqual(self.job_time, expected_time, delta=0.1)
|
2016-11-08 23:39:25 +01:00
|
|
|
|
2016-12-13 23:38:13 +01:00
|
|
|
def test_time_unit_dt_timedelta(self):
|
2016-11-08 23:39:25 +01:00
|
|
|
# Testing seconds, minutes and hours as datetime.timedelta object
|
|
|
|
# This is sufficient to test that it actually works.
|
2016-12-13 23:38:13 +01:00
|
|
|
interval = datetime.timedelta(seconds=2)
|
|
|
|
expected_time = time.time() + interval.total_seconds()
|
2016-11-08 23:39:25 +01:00
|
|
|
|
|
|
|
self.jq.put(Job(self.job5, interval, repeat=False))
|
2016-12-13 23:38:13 +01:00
|
|
|
sleep(2.5)
|
|
|
|
self.assertAlmostEqual(self.job_time, expected_time, delta=0.1)
|
|
|
|
|
|
|
|
def test_time_unit_dt_datetime(self):
|
|
|
|
# Testing running at a specific datetime
|
|
|
|
delta = datetime.timedelta(seconds=2)
|
|
|
|
next_t = datetime.datetime.now() + delta
|
|
|
|
expected_time = time.time() + delta.total_seconds()
|
|
|
|
|
|
|
|
self.jq.put(Job(self.job5, repeat=False), next_t=next_t)
|
|
|
|
sleep(2.5)
|
|
|
|
self.assertAlmostEqual(self.job_time, expected_time, delta=0.1)
|
|
|
|
|
|
|
|
def test_time_unit_dt_time_today(self):
|
|
|
|
# Testing running at a specific time today
|
|
|
|
delta = 2
|
2017-01-07 14:04:58 +01:00
|
|
|
next_t = (datetime.datetime.now() + datetime.timedelta(seconds=delta)).time()
|
2016-12-13 23:38:13 +01:00
|
|
|
expected_time = time.time() + delta
|
|
|
|
|
|
|
|
self.jq.put(Job(self.job5, repeat=False), next_t=next_t)
|
|
|
|
sleep(2.5)
|
|
|
|
self.assertAlmostEqual(self.job_time, expected_time, delta=0.1)
|
|
|
|
|
|
|
|
def test_time_unit_dt_time_tomorrow(self):
|
|
|
|
# Testing running at a specific time that has passed today. Since we can't wait a day, we
|
|
|
|
# test if the jobs next_t has been calculated correctly
|
|
|
|
delta = -2
|
2017-01-07 14:04:58 +01:00
|
|
|
next_t = (datetime.datetime.now() + datetime.timedelta(seconds=delta)).time()
|
2016-12-13 23:38:13 +01:00
|
|
|
expected_time = time.time() + delta + 60 * 60 * 24
|
|
|
|
|
|
|
|
self.jq.put(Job(self.job5, repeat=False), next_t=next_t)
|
|
|
|
self.assertAlmostEqual(self.jq.queue.get(False)[0], expected_time, delta=0.1)
|
2016-11-08 23:39:25 +01:00
|
|
|
|
2016-12-14 18:01:44 +01:00
|
|
|
def test_run_once(self):
|
2016-12-14 16:27:45 +01:00
|
|
|
delta = 2
|
|
|
|
expected_time = time.time() + delta
|
|
|
|
|
2016-12-14 18:01:44 +01:00
|
|
|
self.jq.run_once(self.job5, delta)
|
2016-12-14 16:27:45 +01:00
|
|
|
sleep(2.5)
|
|
|
|
self.assertAlmostEqual(self.job_time, expected_time, delta=0.1)
|
|
|
|
|
2016-12-14 18:01:44 +01:00
|
|
|
def test_run_repeating(self):
|
2016-12-14 16:27:45 +01:00
|
|
|
interval = 0.1
|
|
|
|
first = 1.5
|
|
|
|
|
2016-12-14 18:01:44 +01:00
|
|
|
self.jq.run_repeating(self.job1, interval, first=first)
|
2016-12-14 16:27:45 +01:00
|
|
|
sleep(2.505)
|
|
|
|
self.assertAlmostEqual(self.result, 10, delta=1)
|
|
|
|
|
2016-12-14 18:01:44 +01:00
|
|
|
def test_run_daily(self):
|
2016-12-14 16:27:45 +01:00
|
|
|
delta = 1
|
2017-01-07 14:04:58 +01:00
|
|
|
time_of_day = (datetime.datetime.now() + datetime.timedelta(seconds=delta)).time()
|
2016-12-14 16:27:45 +01:00
|
|
|
expected_time = time.time() + 60 * 60 * 24 + delta
|
|
|
|
|
2016-12-14 18:01:44 +01:00
|
|
|
self.jq.run_daily(self.job1, time_of_day)
|
2016-12-14 16:27:45 +01:00
|
|
|
sleep(2 * delta)
|
|
|
|
self.assertEqual(self.result, 1)
|
|
|
|
self.assertAlmostEqual(self.jq.queue.get(False)[0], expected_time, delta=0.1)
|
|
|
|
|
2016-04-24 04:11:25 +02:00
|
|
|
|
2016-01-04 01:56:22 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|