def test_kill(self): self.thread.start() wait_until(lambda: self.executor.status == Status.RUNNING, 3) self.assertEqual(self.executor.status, Status.RUNNING) self.executor.make_request(Request.KILL) wait_until(lambda: Status.is_final(self.executor.status), 5) self.assertEqual(self.executor.status, Status.KILLED)
def test_job(self): schedule = Schedule.create(self.job, self.queue, 1) instance = Instance.objects.create(task=self.job, schedule=schedule) # instance.log = log.Log(os.devnull) self.thread = Thread(target=instance.start) self.thread.start() wait_until(lambda: self.queue.count() == 2, 2) self.assertEqual(set([i.item.node for i in self.queue.items.all()]), set([self.nodes[0], self.nodes[1]])) for i in self.queue_items(): self._start_instance(i) self.assertEqual(set([i.item.node for i in self.queue.items.all()]), set([self.nodes[2], self.nodes[4]])) for i in self.queue_items(): self._start_instance(i) self.assertEqual(set([i.item.node for i in self.queue.items.all()]), set([self.nodes[3]])) for i in self.queue_items(): self._start_instance(i) self.assertEqual(set([i.item.node for i in self.queue.items.all()]), set([self.nodes[5]])) for i in self.queue_items(): self._start_instance(i) self.thread.join(2) self.assertFalse(self.thread.isAlive())
def test_cron_make_up(self): task = make_task() queue = make_queue() now = datetime.utcnow() s = CronSchedule(encoding='o*d*w*h*m*s%s' % ((now.second - 1) % 60), task=task, queue=queue, repetitions=0, remaining=0, make_up=False) s.base = now - timedelta(seconds=2) s.save() self._scheduler.flag.set() wait_until(lambda: s.instances.count() == 1, 3) now = datetime.utcnow() s = CronSchedule(encoding='o*d*w*h*m*s*', task=task, queue=queue, repetitions=0, remaining=0, make_up=True) s.base = now - timedelta(seconds=5) s.save() self._scheduler.flag.set() wait_until(lambda: s.instances.count() == 6, 1)
def test_bad_schedule(self): task = make_task() queue = make_queue() s = CronSchedule.create(task, queue, "o*d*w*h*m*s*") s.encoding = "gibberish" s.save() self._scheduler.flag.set() wait_until(lambda: CronSchedule.objects.get(pk=s.pk).deleted, 2)
def test_start_stop(self): self.assertEqual(self.executor.status, Status.CREATED) self.thread.start() wait_until(lambda: self.executor.status == Status.RUNNING, 3) self.assertEqual(self.executor.status, Status.RUNNING) self.executor.make_request(Request.STOP) wait_until(lambda: Status.is_final(self.executor.status), 5) self.assertEqual(self.executor.status, Status.ENDED)
def test_push_pop(self): self.queue.push(self.item) self.i = None def get_item(): self.i = self.queue.pop() return self.i != None wait_until(get_item) self.assertEqual(self.item, self.i)
def test_make_up(self): task = make_task() queue = make_queue() s = Schedule.create(task, queue, 1, 10, -10, True) self._scheduler.flag.set() wait_until(lambda: s.instances.count() == 10, 5) s = Schedule.create(task, queue, 60, 10, -10, False) self._scheduler.flag.set() wait_until(lambda: s.instances.count() == 1, 5)
def test_update_schedule(self): task = make_task() queue = make_queue() s = CronSchedule.create(task, queue, 'o*d*w*h*m*s*', 10) self._scheduler.flag.set() wait_until(lambda: queue.count() == 2, 5) s.encoding = 'o*d*w*h*m*s4' s.save() self.assertRaises( Exception, lambda: wait_until(lambda: s.instances.count() > 3, 3))
def test_update_schedule(self): task = make_task() queue = make_queue() s = CronSchedule.create(task, queue, 'o*d*w*h*m*s*', 10) self._scheduler.flag.set() wait_until(lambda: queue.count() == 2, 5) s.encoding = 'o*d*w*h*m*s4' s.save() self.assertRaises(Exception, lambda: wait_until(lambda: s.instances.count() > 3, 3))
def test_duplicate(self): task = make_task() queue = make_queue() s = Schedule.create(task, queue, 1, 2, start=2) self._scheduler.flag.set() wait_until(lambda: self.scheduler.schedules.count() == 1, 2) s = Schedule.objects.get(pk=s.pk) s.scheduler = None s.save() self._scheduler.flag.set() wait_until(lambda: s.instances.count() == 2, 5)
def test_cron(self): task = make_task() queue = make_queue() s = CronSchedule.create(task, queue, 'o*d*w*h*m*s*', 3) self._scheduler.flag.set() wait_until(lambda: queue.count() == 3, 8) enqueued = map(lambda i: i.enqueued, s.instances) def fold(acc, e): self.assertEqual(e - acc, timedelta(seconds=1)) return e reduce(fold, enqueued)
def test_reload(self): task = make_task() queue = make_queue() now = datetime.utcnow() s = CronSchedule.create(task, queue, 'o*d*w*h*m*s%s' % ((now.second - 1) % 60), 1) self._scheduler.flag.set() wait_until(lambda: self.scheduler.cronschedules.count() == 1, 5) CronSchedule.objects.get(pk=s.pk).reschedule('o*d*w*h*m*s*') self.scheduler.make_request(Request.RELOAD) self._scheduler.flag.set() wait_until(lambda: s.instances.count() == 1, 10)
def test_pause_resume(self): self.thread.start() wait_until(lambda: self.executor.status == Status.RUNNING, 3) self.assertEqual(self.executor.status, Status.RUNNING) self.executor.make_request(Request.PAUSE) wait_until(lambda: self.executor.status == Status.PAUSED, 5) self.assertEqual(self.executor.status, Status.PAUSED) self.executor.make_request(Request.RESUME) wait_until(lambda: self.executor.status == Status.RUNNING, 5) self.assertEqual(self.executor.status, Status.RUNNING)
def setUp(self): self._scheduler = Scheduler.objects.create() self._scheduler.log = log.Log(os.devnull) self.thread = Thread(target=self._scheduler.start) self.thread.start() wait_until(lambda: self.scheduler.is_alive(), 3)
def test_stop(self): self.scheduler.make_request(Request.STOP) self._scheduler.flag.set() wait_until(lambda: not self.scheduler.is_alive(), 3)
def test_schedule(self): task = make_task() queue = make_queue() s = Schedule.create(task, queue, 0, 5) self._scheduler.flag.set() wait_until(lambda: s.instances.count() == 5, 5)
def test_push_pop(self): self.queue.push(self.queue) wait_until(self.queue.peek, 10) q = self.queue.pop() self.assertEqual(self.queue, q)