def test__cannot_add_task_when_queue_is_full(self): service = DatabaseTasksService(0) service.startService() try: event = threading.Event() service.addTask(event.wait) try: self.assertRaises(QueueOverflow, service.addTask, noop) finally: event.set() finally: service.stopService()
def test__failure_in_task_is_logged(self): logger = self.useFixture(TwistedLoggerFixture()) service = DatabaseTasksService() service.startService() try: service.addTask(lambda: 0 / 0) finally: service.stopService() self.assertDocTestMatches( """\ ...Unhandled failure in database task. Traceback (most recent call last): ... builtins.ZeroDivisionError: ... """, logger.output)
def test__tasks_are_all_run_before_shutdown_completes(self): service = DatabaseTasksService() service.startService() try: queue = service.queue event = threading.Event() count = random.randint(20, 40) for _ in range(count): service.addTask(event.wait) # The queue has `count` tasks (or `count - 1` tasks; the first may # have already been pulled off the queue) still pending. self.assertThat(queue.pending, MatchesAny(HasLength(count), HasLength(count - 1))) finally: event.set() service.stopService() # The queue is empty and nothing is waiting. self.assertThat(queue, MatchesStructure.byEquality(waiting=[], pending=[]))
def test_failure_in_added_task_does_not_crash_service(self): things = [] # This will be populated by tasks. exception_type = factory.make_exception_type() def be_bad(): raise exception_type("I'm bad, so bad.") service = DatabaseTasksService() service.startService() try: service.addTask(things.append, 1) service.addTask(be_bad) service.addTask(things.append, 2) finally: service.stopService() self.assertThat(things, Equals([1, 2]))