aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/buildbot-0.8.8-py2.7.egg/buildbot/test/unit/test_util_misc.py
blob: 78f38144e39e7b4f07785efb5b4e5b35a9e68da1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# This file is part of Buildbot.  Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members

from twisted.trial import unittest
from buildbot.util import misc
from buildbot import util
from twisted.python import failure
from twisted.internet import defer, reactor
from buildbot.test.util import compat
from buildbot.util.eventual import eventually

class deferredLocked(unittest.TestCase):
    def test_name(self):
        self.assertEqual(util.deferredLocked, misc.deferredLocked)

    def test_fn(self):
        l = defer.DeferredLock()
        @util.deferredLocked(l)
        def check_locked(arg1, arg2):
            self.assertEqual([l.locked, arg1, arg2], [True, 1, 2])
            return defer.succeed(None)
        d = check_locked(1, 2)
        def check_unlocked(_):
            self.assertFalse(l.locked)
        d.addCallback(check_unlocked)
        return d

    def test_fn_fails(self):
        l = defer.DeferredLock()
        @util.deferredLocked(l)
        def do_fail():
            return defer.fail(RuntimeError("oh noes"))
        d = do_fail()
        def check_unlocked(_):
            self.assertFalse(l.locked)
        d.addCallbacks(lambda _ : self.fail("didn't errback"),
                       lambda _ : self.assertFalse(l.locked))
        return d

    def test_fn_exception(self):
        l = defer.DeferredLock()
        @util.deferredLocked(l)
        def do_fail():
            raise RuntimeError("oh noes")
        d = do_fail()
        def check_unlocked(_):
            self.assertFalse(l.locked)
        d.addCallbacks(lambda _ : self.fail("didn't errback"),
                       lambda _ : self.assertFalse(l.locked))
        return d

    def test_method(self):
        testcase = self
        class C:
            @util.deferredLocked('aLock')
            def check_locked(self, arg1, arg2):
                testcase.assertEqual([self.aLock.locked, arg1, arg2], [True, 1, 2])
                return defer.succeed(None)
        obj = C()
        obj.aLock = defer.DeferredLock()
        d = obj.check_locked(1, 2)
        def check_unlocked(_):
            self.assertFalse(obj.aLock.locked)
        d.addCallback(check_unlocked)
        return d

class SerializedInvocation(unittest.TestCase):

    def waitForQuiet(self, si):
        d = defer.Deferred()
        si._quiet = lambda : d.callback(None)
        return d

    # tests

    def test_name(self):
        self.assertEqual(util.SerializedInvocation, misc.SerializedInvocation)

    def testCallFolding(self):
        events = []
        def testfn():
            d = defer.Deferred()
            def done():
                events.append('TM')
                d.callback(None)
            eventually(done)
            return d
        si = misc.SerializedInvocation(testfn)

        # run three times - the first starts testfn, the second
        # requires a second run, and the third is folded.
        d1 = si()
        d2 = si()
        d3 = si()

        dq = self.waitForQuiet(si)
        d = defer.gatherResults([d1, d2, d3, dq])
        def check(_):
            self.assertEqual(events, [ 'TM', 'TM' ])
        d.addCallback(check)
        return d

    @compat.usesFlushLoggedErrors
    def testException(self):
        def testfn():
            d = defer.Deferred()
            reactor.callLater(0, d.errback,
                              failure.Failure(RuntimeError("oh noes")))
            return d
        si = misc.SerializedInvocation(testfn)

        d = si()

        def check(_):
            self.assertEqual(len(self.flushLoggedErrors(RuntimeError)), 1)
        d.addCallback(check)
        return d