Beispiel #1
0
    def test_sync_task_can_be_cancelled_when_enqueued(self):
        things = []  # This will NOT be populated by tasks.

        service = DatabaseTasksService()
        yield service.startService()
        try:
            event = threading.Event()
            service.deferTask(event.wait)
            service.syncTask().cancel()
        finally:
            event.set()
            yield service.stopService()

        self.assertThat(things, Equals([]))
Beispiel #2
0
    def test_failure_in_deferred_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 being very naughty.")

        service = DatabaseTasksService()
        service.startService()
        try:
            service.deferTask(things.append, 1).wait(30)
            self.assertRaises(exception_type,
                              service.deferTask(be_bad).wait, 30)
            service.deferTask(things.append, 2).wait(30)
        finally:
            service.stopService()

        self.assertThat(things, Equals([1, 2]))
Beispiel #3
0
 def test__task_is_executed_in_other_thread(self):
     get_thread_ident = lambda: threading.currentThread().ident
     service = DatabaseTasksService()
     service.startService()
     try:
         ident_from_task = service.deferTask(get_thread_ident).wait(30)
         ident_from_here = get_thread_ident()
         self.expectThat(ident_from_task, IsInstance(int, int))
         self.expectThat(ident_from_task, Not(Equals(ident_from_here)))
     finally:
         service.stopService()
Beispiel #4
0
    def test_task_can_access_database_from_other_thread(self):
        @transactional
        def database_task():
            # Merely being here means we've accessed the database.
            return sentinel.beenhere

        service = DatabaseTasksService()
        service.startService()
        try:
            result = service.deferTask(database_task).wait(30)
            self.assertThat(result, Is(sentinel.beenhere))
        finally:
            service.stopService()
Beispiel #5
0
 def test__cannot_defer_task_when_queue_is_full(self):
     service = DatabaseTasksService(0)
     service.startService()
     try:
         event = threading.Event()
         service.addTask(event.wait)
         try:
             self.assertRaises(QueueOverflow,
                               lambda: service.deferTask(noop).wait(30))
         finally:
             event.set()
     finally:
         service.stopService()
Beispiel #6
0
 def test_deferred_task_cannot_be_cancelled_when_running(self):
     # DatabaseTaskAlreadyRunning is raised when attempting to cancel a
     # database task that's already running.
     service = DatabaseTasksService()
     yield service.startService()
     try:
         ready = Deferred()
         d = service.deferTask(reactor.callFromThread, ready.callback, None)
         # Wait for the task to begin running.
         yield ready
         # We have the reactor thread. Even if the task completes its
         # status will not be updated until the reactor's next iteration.
         self.assertRaises(DatabaseTaskAlreadyRunning, d.cancel)
     finally:
         yield service.stopService()
Beispiel #7
0
    def test_arguments_are_passed_through_to_task(self):
        def return_args(*args, **kwargs):
            return sentinel.here, args, kwargs

        service = DatabaseTasksService()
        service.startService()
        try:
            result = service.deferTask(
                return_args, sentinel.arg, kw=sentinel.kw
            ).wait(30)
            self.assertThat(
                result,
                Equals((sentinel.here, (sentinel.arg,), {"kw": sentinel.kw})),
            )
        finally:
            service.stopService()