def test_enqueue_dequeue(self, mock_may_be_start_runner): workq = WorkQueue(self.worker, start_runner=None, max_qsize=1) entry = WorkQueueEntry() # basic enqueue test self.assertEqual(True, workq.enqueue(entry)) self.assertEqual(1, workq.num_enqueues()) self.assertEqual(0, workq.drops()) self.assertEqual(1, workq.size()) self.assertEqual(1, workq.may_be_start_runner.call_count) # work queue is not bounded # verify that we could enqueue more than max_qsize self.assertFalse(workq.bounded()) self.assertEqual(True, workq.enqueue(entry)) self.assertEqual(2, workq.num_enqueues()) self.assertEqual(0, workq.drops()) self.assertEqual(2, workq.size()) self.assertEqual(2, workq.may_be_start_runner.call_count) # work queue is bounded # verify that enqueue fails if qsize > max_qsize workq.set_bounded(True) self.assertTrue(workq.bounded()) self.assertEqual(False, workq.enqueue(entry)) self.assertEqual(2, workq.num_enqueues()) self.assertEqual(1, workq.drops()) self.assertEqual(2, workq.size()) self.assertEqual(2, workq.may_be_start_runner.call_count) # basic dequeue test self.assertIs(entry, workq.dequeue()) self.assertEqual(1, workq.num_dequeues()) self.assertEqual(1, workq.size()) self.assertEqual(entry, workq.dequeue()) self.assertEqual(2, workq.num_dequeues()) self.assertEqual(0, workq.size()) # call dequeue with no item in the queue self.assertEqual(None, workq.dequeue()) self.assertEqual(2, workq.num_dequeues()) self.assertEqual(0, workq.size())
def test_runner(self): # Don't set start_runner # verify runner is executed and worker() is invoked workq = WorkQueue(self.worker) self.assertFalse(workq.runner().running()) self.enqueue_entries(workq, 5) self.assertEqual(5, workq.size()) self.assertEqual(5, workq.num_enqueues()) self.assertTrue(workq.runner().running()) gevent.sleep() self.assertEqual(0, workq.size()) self.assertEqual(5, workq.num_dequeues()) self.assertEqual(5, self.worker_count) # set start_runner to return False # verify that runner doesn't start workq._start_runner = self.start_runner_never self.assertFalse(workq.runner().running()) self.enqueue_entries(workq, 3) self.assertFalse(workq.runner().running()) self.assertEqual(3, workq.size()) self.assertEqual(8, workq.num_enqueues()) gevent.sleep() self.assertEqual(3, workq.size()) self.assertEqual(5, workq.num_dequeues()) self.assertEqual(5, self.worker_count) # set start_runner to return True # verify that runner gets executed workq._start_runner = self.start_runner_always workq.may_be_start_runner() self.assertTrue(workq.runner().running()) gevent.sleep() self.assertEqual(0, workq.size()) self.assertEqual(8, workq.num_dequeues()) self.assertEqual(8, self.worker_count)
def test_max_workload(self): max_workload = 2 workq = WorkQueue(self.worker, max_work_load=max_workload) self.assertFalse(workq.runner().running()) num_iterations = 3 total_work_items = max_workload * num_iterations self.enqueue_entries(workq, total_work_items) self.assertEqual(total_work_items, workq.size()) self.assertEqual(total_work_items, workq.num_enqueues()) # verify that only max_work_load is executed per cycle num_dequeues = 0 num_work_items = total_work_items for i in range(num_iterations): self.assertTrue(workq.runner().running()) gevent.sleep() num_work_items -= max_workload num_dequeues += max_workload self.assertEqual(num_work_items, workq.size()) self.assertEqual(num_dequeues, workq.num_dequeues()) self.assertFalse(workq.runner().running()) self.assertEqual(0, workq.size()) self.assertEqual(total_work_items, workq.num_dequeues())
def test_max_workload(self): max_workload = 2 workq = WorkQueue(self.worker, max_work_load=max_workload) self.assertFalse(workq.runner().running()) num_iterations = 3 total_work_items = max_workload*num_iterations self.enqueue_entries(workq, total_work_items) self.assertEqual(total_work_items, workq.size()) self.assertEqual(total_work_items, workq.num_enqueues()) # verify that only max_work_load is executed per cycle num_dequeues = 0 num_work_items = total_work_items for i in range(num_iterations): self.assertTrue(workq.runner().running()) gevent.sleep() num_work_items -= max_workload num_dequeues += max_workload self.assertEqual(num_work_items, workq.size()) self.assertEqual(num_dequeues, workq.num_dequeues()) self.assertFalse(workq.runner().running()) self.assertEqual(0, workq.size()) self.assertEqual(total_work_items, workq.num_dequeues())