示例#1
0
 def test__reset_cancels_all_hooks(self):
     canceller = Mock()
     dhooks = DeferredHooks()
     d1, d2 = Deferred(canceller), Deferred(canceller)
     dhooks.add(d1)
     dhooks.add(d2)
     dhooks.reset()
     self.assertThat(dhooks.hooks, HasLength(0))
     self.assertThat(canceller, MockCallsMatch(call(d1), call(d2)))
示例#2
0
    def test__reset_suppresses_CancelledError(self):
        logger = self.useFixture(TwistedLoggerFixture())

        dhooks = DeferredHooks()
        d = Deferred()
        dhooks.add(d)
        dhooks.reset()
        self.assertThat(dhooks.hooks, HasLength(0))
        self.assertThat(extract_result(d), Is(None))
        self.assertEqual("", logger.output)
示例#3
0
    def test__reset_cancels_in_reactor(self):
        def validate_in_reactor(_):
            self.assertTrue(isInIOThread())

        dhooks = DeferredHooks()
        d = Deferred()
        d.addBoth(validate_in_reactor)
        dhooks.add(d)
        dhooks.reset()
        self.assertThat(dhooks.hooks, HasLength(0))
        self.assertThat(d, IsFiredDeferred())
示例#4
0
    def test__logs_failures_from_cancelled_hooks(self):
        logger = self.useFixture(TwistedLoggerFixture())

        error = factory.make_exception()
        dhooks = DeferredHooks()
        d = Deferred()
        d.addBoth(lambda _: Failure(error))
        dhooks.add(d)
        dhooks.reset()
        self.assertThat(dhooks.hooks, HasLength(0))
        self.assertThat(d, IsFiredDeferred())
        self.assertDocTestMatches(
            dedent("""\
            Failure when cancelling hook.
            Traceback (most recent call last):
            ...
            maastesting.factory.TestException#...
            """), logger.output)
示例#5
0
    def test__logs_failures_from_cancellers_when_hook_already_fired(self):
        logger = self.useFixture(TwistedLoggerFixture())

        def canceller(d):
            d.callback(None)
            raise factory.make_exception()

        dhooks = DeferredHooks()
        d = Deferred(canceller)
        dhooks.add(d)
        dhooks.reset()
        self.assertThat(dhooks.hooks, HasLength(0))
        self.assertThat(d, IsFiredDeferred())
        self.assertDocTestMatches(
            dedent("""\
            Failure when cancelling hook.
            Traceback (most recent call last):
            ...
            maastesting.factory.TestException#...
            """), logger.output)
示例#6
0
    def test__logs_failures_from_cancellers(self):
        logger = self.useFixture(TwistedLoggerFixture())

        canceller = Mock()
        canceller.side_effect = factory.make_exception()

        dhooks = DeferredHooks()
        d = Deferred(canceller)
        dhooks.add(d)
        dhooks.reset()
        self.assertThat(dhooks.hooks, HasLength(0))
        # The hook has not been fired, but because the user-supplied canceller
        # has failed we're not in a position to know what to do. This reflects
        # a programming error and not a run-time error that we ought to be
        # prepared for, so it is left as-is.
        self.assertThat(d, IsUnfiredDeferred())
        self.assertDocTestMatches(
            dedent("""\
            Failure when cancelling hook.
            Traceback (most recent call last):
            ...
            maastesting.factory.TestException#...
            """), logger.output)