1
# -*- test-case-name: twisted.internet.test.test_inlinecb -*-
2
# Copyright (c) 2009-2010 Twisted Matrix Laboratories.
3
# See LICENSE for details.
6
Tests for L{twisted.internet.defer.inlineCallbacks}.
8
These tests are defined in a non-C{test_*} module because they are
9
syntactically invalid on python < 2.5. test_inlinecb will conditionally import
10
these tests on python 2.5 and greater.
12
Some tests for inlineCallbacks are defined in L{twisted.test.test_defgen} as
13
well: see U{http://twistedmatrix.com/trac/ticket/4182}.
16
from twisted.trial.unittest import TestCase
17
from twisted.internet.defer import Deferred, returnValue, inlineCallbacks
19
class NonLocalExitTests(TestCase):
21
It's possible for L{returnValue} to be (accidentally) invoked at a stack
22
level below the L{inlineCallbacks}-decorated function which it is exiting.
23
If this happens, L{returnValue} should report useful errors.
25
If L{returnValue} is invoked from a function not decorated by
26
L{inlineCallbacks}, it will emit a warning if it causes an
27
L{inlineCallbacks} function further up the stack to exit.
30
def mistakenMethod(self):
32
This method mistakenly invokes L{returnValue}, despite the fact that it
33
is not decorated with L{inlineCallbacks}.
38
def assertMistakenMethodWarning(self, resultList):
40
Flush the current warnings and assert that we have been told that
41
C{mistakenMethod} was invoked, and that the result from the Deferred
42
that was fired (appended to the given list) is C{mistakenMethod}'s
43
result. The warning should indicate that an inlineCallbacks function
44
called 'inline' was made to exit.
46
self.assertEqual(resultList, [1])
47
warnings = self.flushWarnings(offendingFunctions=[self.mistakenMethod])
48
self.assertEqual(len(warnings), 1)
49
self.assertEquals(warnings[0]['category'], DeprecationWarning)
51
warnings[0]['message'],
52
"returnValue() in 'mistakenMethod' causing 'inline' to exit: "
53
"returnValue should only be invoked by functions decorated with "
57
def test_returnValueNonLocalWarning(self):
59
L{returnValue} will emit a non-local exit warning in the simplest case,
60
where the offending function is invoked immediately.
69
d.addCallback(results.append)
70
self.assertMistakenMethodWarning(results)
73
def test_returnValueNonLocalDeferred(self):
75
L{returnValue} will emit a non-local warning in the case where the
76
L{inlineCallbacks}-decorated function has already yielded a Deferred
77
and therefore moved its generator function along.
87
effect.addCallback(results.append)
88
self.assertEquals(results, [])
90
self.assertMistakenMethodWarning(results)