Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / internet / test / inlinecb_tests.py
1 # -*- test-case-name: twisted.internet.test.test_inlinecb -*-
2 # Copyright (c) Twisted Matrix Laboratories.
3 # See LICENSE for details.
4
5 """
6 Tests for L{twisted.internet.defer.inlineCallbacks}.
7
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.
11
12 Some tests for inlineCallbacks are defined in L{twisted.test.test_defgen} as
13 well: see U{http://twistedmatrix.com/trac/ticket/4182}.
14 """
15
16 from twisted.trial.unittest import TestCase
17 from twisted.internet.defer import Deferred, returnValue, inlineCallbacks
18
19 class NonLocalExitTests(TestCase):
20     """
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.
24
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.
28     """
29
30     def mistakenMethod(self):
31         """
32         This method mistakenly invokes L{returnValue}, despite the fact that it
33         is not decorated with L{inlineCallbacks}.
34         """
35         returnValue(1)
36
37
38     def assertMistakenMethodWarning(self, resultList):
39         """
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.
45         """
46         self.assertEqual(resultList, [1])
47         warnings = self.flushWarnings(offendingFunctions=[self.mistakenMethod])
48         self.assertEqual(len(warnings), 1)
49         self.assertEqual(warnings[0]['category'], DeprecationWarning)
50         self.assertEqual(
51             warnings[0]['message'],
52             "returnValue() in 'mistakenMethod' causing 'inline' to exit: "
53             "returnValue should only be invoked by functions decorated with "
54             "inlineCallbacks")
55
56
57     def test_returnValueNonLocalWarning(self):
58         """
59         L{returnValue} will emit a non-local exit warning in the simplest case,
60         where the offending function is invoked immediately.
61         """
62         @inlineCallbacks
63         def inline():
64             self.mistakenMethod()
65             returnValue(2)
66             yield 0
67         d = inline()
68         results = []
69         d.addCallback(results.append)
70         self.assertMistakenMethodWarning(results)
71
72
73     def test_returnValueNonLocalDeferred(self):
74         """
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.
78         """
79         cause = Deferred()
80         @inlineCallbacks
81         def inline():
82             yield cause
83             self.mistakenMethod()
84             returnValue(2)
85         effect = inline()
86         results = []
87         effect.addCallback(results.append)
88         self.assertEqual(results, [])
89         cause.callback(1)
90         self.assertMistakenMethodWarning(results)
91
92