def test_success(self): """Test a simple success case.""" self.setUpService() self.service.enqueue(FakeQueueItem('op 0')) self.service.enqueue(FakeQueueItem('op 1')) # Same element should not trigger new operations. self.service.enqueue(FakeQueueItem('op 1')) gevent.sleep(0.01) for notifier in self.notifiers: self.assertEqual(notifier.get_notifications(), 2)
def setUp(self): # Some items to play with. self.item_a = FakeQueueItem("a") self.item_b = FakeQueueItem("b") self.item_c = FakeQueueItem("c") self.item_d = FakeQueueItem("d") self.item_e = FakeQueueItem("e") # And an empty priority queue. self.queue = PriorityQueue()
def test_batch(self): """Test a batch executor.""" self.setUpService() batch_notifier = Notifier() self.service.add_executor(FakeBatchExecutor(batch_notifier)) self.service.enqueue(FakeQueueItem('op 0')) self.service.enqueue(FakeQueueItem('op 1')) gevent.sleep(0.01) for notifier in self.notifiers: self.assertEqual(notifier.get_notifications(), 2) # Just one call to the batch executor. self.assertEqual(batch_notifier.get_notifications(), 1)
def test_sweeper(self): """Test the sweeper. Test that operations added with _missing_operations are executed. """ self.setUpService(0.1) self.service.add_missing_operation(FakeQueueItem('op 0')) self.service.add_missing_operation(FakeQueueItem('op 1')) gevent.sleep(0.2) for notifier in self.notifiers: self.assertEqual(notifier.get_notifications(), 2)
def test_bad_executor(self): """Test that a slow executor does not block the others.""" self.setUpService() # Adding an executor that takes 0.1s to perform anything. slow_notifier = Notifier() self.service.add_executor(FakeSlowExecutor(slow_notifier, 0.1)) # First round of operations. self.service.enqueue(FakeQueueItem('op 0')) gevent.sleep(0.01) for notifier in self.notifiers: self.assertEqual(notifier.get_notifications(), 1) self.assertEqual(slow_notifier.get_notifications(), 0) # Second round. self.service.enqueue(FakeQueueItem('op 1')) gevent.sleep(0.01) for notifier in self.notifiers: self.assertEqual(notifier.get_notifications(), 2) self.assertEqual(slow_notifier.get_notifications(), 0) # After 0.1 * #operations, the slow one picked up. gevent.sleep(0.25) self.assertEqual(slow_notifier.get_notifications(), 2)