Esempio n. 1
0
 def test_emptyList(self):
     """
     When asked to run an empty list of callables, runSequentially returns a
     successful Deferred that fires an empty list.
     """
     d = util._runSequentially([])
     self.assertDeferredResult(d, self.assertEqual, [])
Esempio n. 2
0
 def test_singleAsynchronousSuccess(self):
     """
     When given a callable that returns a successful Deferred, include the
     result of the Deferred in the results list, tagged with a SUCCESS flag.
     """
     d = util._runSequentially([lambda: defer.succeed(None)])
     self.assertDeferredResult(d, self.assertEqual, [(defer.SUCCESS, None)])
Esempio n. 3
0
 def test_emptyList(self):
     """
     When asked to run an empty list of callables, runSequentially returns a
     successful Deferred that fires an empty list.
     """
     d = util._runSequentially([])
     self.assertDeferredResult(d, self.assertEqual, [])
Esempio n. 4
0
 def test_singleAsynchronousSuccess(self):
     """
     When given a callable that returns a successful Deferred, include the
     result of the Deferred in the results list, tagged with a SUCCESS flag.
     """
     d = util._runSequentially([lambda: defer.succeed(None)])
     self.assertDeferredResult(d, self.assertEqual, [(defer.SUCCESS, None)])
Esempio n. 5
0
    def test_callablesCalledInOrder(self):
        """
        Check that the callables are called in the given order, one after the
        other.
        """
        log = []
        deferreds = []

        def append(value):
            d = defer.Deferred()
            log.append(value)
            deferreds.append(d)
            return d

        d = util._runSequentially([lambda: append('foo'),
                                   lambda: append('bar')])

        # runSequentially should wait until the Deferred has fired before
        # running the second callable.
        self.assertEqual(log, ['foo'])
        deferreds[-1].callback(None)
        self.assertEqual(log, ['foo', 'bar'])

        # Because returning created Deferreds makes jml happy.
        deferreds[-1].callback(None)
        return d
    def test_callablesCalledInOrder(self):
        """
        Check that the callables are called in the given order, one after the
        other.
        """
        log = []
        deferreds = []

        def append(value):
            d = defer.Deferred()
            log.append(value)
            deferreds.append(d)
            return d

        d = util._runSequentially(
            [lambda: append('foo'), lambda: append('bar')])

        # runSequentially should wait until the Deferred has fired before
        # running the second callable.
        self.assertEqual(log, ['foo'])
        deferreds[-1].callback(None)
        self.assertEqual(log, ['foo', 'bar'])

        # Because returning created Deferreds makes jml happy.
        deferreds[-1].callback(None)
        return d
Esempio n. 7
0
 def test_singleSynchronousSuccess(self):
     """
     When given a callable that succeeds without returning a Deferred,
     include the return value in the results list, tagged with a SUCCESS
     flag.
     """
     d = util._runSequentially([lambda: None])
     self.assertDeferredResult(d, self.assertEqual, [(defer.SUCCESS, None)])
Esempio n. 8
0
 def test_singleSynchronousSuccess(self):
     """
     When given a callable that succeeds without returning a Deferred,
     include the return value in the results list, tagged with a SUCCESS
     flag.
     """
     d = util._runSequentially([lambda: None])
     self.assertDeferredResult(d, self.assertEqual, [(defer.SUCCESS, None)])
Esempio n. 9
0
 def test_singleAsynchronousFailure(self):
     """
     When given a callable that returns a failing Deferred, include the
     failure the results list, tagged with a FAILURE flag.
     """
     d = util._runSequentially([lambda: defer.fail(ValueError('foo'))])
     def check(results):
         [(flag, fail)] = results
         fail.trap(ValueError)
         self.assertEqual(fail.getErrorMessage(), 'foo')
         self.assertEqual(flag, defer.FAILURE)
     self.assertDeferredResult(d, check)
Esempio n. 10
0
 def test_singleSynchronousFailure(self):
     """
     When given a callable that raises an exception, include a Failure for
     that exception in the results list, tagged with a FAILURE flag.
     """
     d = util._runSequentially([lambda: self.fail('foo')])
     def check(results):
         [(flag, fail)] = results
         fail.trap(self.failureException)
         self.assertEqual(fail.getErrorMessage(), 'foo')
         self.assertEqual(flag, defer.FAILURE)
     self.assertDeferredResult(d, check)
Esempio n. 11
0
 def test_singleSynchronousFailure(self):
     """
     When given a callable that raises an exception, include a Failure for
     that exception in the results list, tagged with a FAILURE flag.
     """
     d = util._runSequentially([lambda: self.fail('foo')])
     def check(results):
         [(flag, fail)] = results
         fail.trap(self.failureException)
         self.assertEqual(fail.getErrorMessage(), 'foo')
         self.assertEqual(flag, defer.FAILURE)
     return d.addCallback(check)
Esempio n. 12
0
    def test_callablesCalledInOrder(self):
        """
        Check that the callables are called in the given order, one after the
        other.
        """
        log = []
        deferreds = []

        def append(value):
            d = defer.Deferred()
            log.append(value)
            deferreds.append(d)
            return d

        util._runSequentially([lambda: append("foo"), lambda: append("bar")])

        # runSequentially should wait until the Deferred has fired before
        # running the second callable.
        self.assertEqual(log, ["foo"])
        deferreds[-1].callback(None)
        self.assertEqual(log, ["foo", "bar"])
Esempio n. 13
0
 def test_singleAsynchronousFailure(self):
     """
     When given a callable that returns a failing Deferred, include the
     failure the results list, tagged with a FAILURE flag.
     """
     d = util._runSequentially([lambda: defer.fail(ValueError('foo'))])
     def check(results):
         [(flag, fail)] = results
         fail.trap(ValueError)
         self.assertEqual(fail.getErrorMessage(), 'foo')
         self.assertEqual(flag, defer.FAILURE)
     return d.addCallback(check)
Esempio n. 14
0
    def _runCleanups(self):
        """
        Run the cleanups added with L{addCleanup} in order.

        @return: A C{Deferred} that fires when all cleanups are run.
        """
        def _makeFunction(f, args, kwargs):
            return lambda: f(*args, **kwargs)
        callables = []
        while len(self._cleanups) > 0:
            f, args, kwargs = self._cleanups.pop()
            callables.append(_makeFunction(f, args, kwargs))
        return util._runSequentially(callables)
Esempio n. 15
0
    def _runCleanups(self):
        """
        Run the cleanups added with L{addCleanup} in order.

        @return: A C{Deferred} that fires when all cleanups are run.
        """
        def _makeFunction(f, args, kwargs):
            return lambda: f(*args, **kwargs)
        callables = []
        while len(self._cleanups) > 0:
            f, args, kwargs = self._cleanups.pop()
            callables.append(_makeFunction(f, args, kwargs))
        return util._runSequentially(callables)
Esempio n. 16
0
 def test_stopOnFirstError(self):
     """
     If the C{stopOnFirstError} option is passed to C{runSequentially}, then
     no further callables are called after the first exception is raised.
     """
     d = util._runSequentially([lambda: self.fail('foo'), lambda: 'bar'],
                               stopOnFirstError=True)
     def check(results):
         [(flag1, fail)] = results
         fail.trap(self.failureException)
         self.assertEqual(flag1, defer.FAILURE)
         self.assertEqual(fail.getErrorMessage(), 'foo')
     self.assertDeferredResult(d, check)
Esempio n. 17
0
 def test_stopOnFirstError(self):
     """
     If the C{stopOnFirstError} option is passed to C{runSequentially}, then
     no further callables are called after the first exception is raised.
     """
     d = util._runSequentially([lambda: self.fail('foo'), lambda: 'bar'],
                               stopOnFirstError=True)
     def check(results):
         [(flag1, fail)] = results
         fail.trap(self.failureException)
         self.assertEqual(flag1, defer.FAILURE)
         self.assertEqual(fail.getErrorMessage(), 'foo')
     return d.addCallback(check)
Esempio n. 18
0
 def test_continuesAfterError(self):
     """
     If one of the callables raises an error, then runSequentially continues
     to run the remaining callables.
     """
     d = util._runSequentially([lambda: self.fail('foo'), lambda: 'bar'])
     def check(results):
         [(flag1, fail), (flag2, result)] = results
         fail.trap(self.failureException)
         self.assertEqual(flag1, defer.FAILURE)
         self.assertEqual(fail.getErrorMessage(), 'foo')
         self.assertEqual(flag2, defer.SUCCESS)
         self.assertEqual(result, 'bar')
     self.assertDeferredResult(d, check)
Esempio n. 19
0
 def test_continuesAfterError(self):
     """
     If one of the callables raises an error, then runSequentially continues
     to run the remaining callables.
     """
     d = util._runSequentially([lambda: self.fail('foo'), lambda: 'bar'])
     def check(results):
         [(flag1, fail), (flag2, result)] = results
         fail.trap(self.failureException)
         self.assertEqual(flag1, defer.FAILURE)
         self.assertEqual(fail.getErrorMessage(), 'foo')
         self.assertEqual(flag2, defer.SUCCESS)
         self.assertEqual(result, 'bar')
     return d.addCallback(check)
Esempio n. 20
0
 def test_stripFlags(self):
     """
     If the C{stripFlags} option is passed to C{runSequentially} then the
     SUCCESS / FAILURE flags are stripped from the output. Instead, the
     Deferred fires a flat list of results containing only the results and
     failures.
     """
     d = util._runSequentially([lambda: self.fail('foo'), lambda: 'bar'],
                               stripFlags=True)
     def check(results):
         [fail, result] = results
         fail.trap(self.failureException)
         self.assertEqual(fail.getErrorMessage(), 'foo')
         self.assertEqual(result, 'bar')
     return d.addCallback(check)
Esempio n. 21
0
    def test_continuesAfterError(self):
        """
        If one of the callables raises an error, then runSequentially continues
        to run the remaining callables.
        """
        d = util._runSequentially([lambda: self.fail("foo"), lambda: "bar"])

        def check(results):
            [(flag1, fail), (flag2, result)] = results
            fail.trap(self.failureException)
            self.assertEqual(flag1, defer.FAILURE)
            self.assertEqual(fail.getErrorMessage(), "foo")
            self.assertEqual(flag2, defer.SUCCESS)
            self.assertEqual(result, "bar")

        self.assertDeferredResult(d, check)
Esempio n. 22
0
    def test_continuesAfterError(self):
        """
        If one of the callables raises an error, then runSequentially continues
        to run the remaining callables.
        """
        d = util._runSequentially([lambda: self.fail("foo"), lambda: "bar"])

        def check(results):
            [(flag1, fail), (flag2, result)] = results
            fail.trap(self.failureException)
            self.assertEqual(flag1, defer.FAILURE)
            self.assertEqual(fail.getErrorMessage(), "foo")
            self.assertEqual(flag2, defer.SUCCESS)
            self.assertEqual(result, "bar")

        return d.addCallback(check)