aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/buildbot-0.8.8-py2.7.egg/buildbot/test/unit/test_schedulers_timed_Periodic.py
blob: 5139a96a5b3e85a6cac93948079f9e0d6d836df8 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# 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 twisted.internet import task, defer
from buildbot.schedulers import timed
from buildbot import config

class Periodic(unittest.TestCase):

    def makeScheduler(self, firstBuildDuration=0, exp_branch=None, **kwargs):
        self.sched = sched = timed.Periodic(**kwargs)

        # add a Clock to help checking timing issues
        self.clock = sched._reactor = task.Clock()

        # keep track of builds in self.events
        self.events = []
        def addBuildsetForLatest(reason=None, branch=None):
            self.assertIn('Periodic scheduler named', reason)
            self.assertEqual(branch, exp_branch)
            isFirst = (self.events == [])
            self.events.append('B@%d' % self.clock.seconds())
            if isFirst and firstBuildDuration:
                d = defer.Deferred()
                self.clock.callLater(firstBuildDuration, d.callback, None)
                return d
            else:
                return defer.succeed(None)
        sched.addBuildsetForLatest = addBuildsetForLatest

        # handle state locally
        self.state = {}

        def getState(k, default):
            return defer.succeed(self.state.get(k, default))
        sched.getState = getState

        def setState(k, v):
            self.state[k] = v
            return defer.succeed(None)
        sched.setState = setState

        return sched

    # tests

    def test_constructor_invalid(self):
        self.assertRaises(config.ConfigErrors,
                lambda : timed.Periodic(name='test', builderNames=[ 'test' ],
                                        periodicBuildTimer=-2))

    def test_iterations_simple(self):
        sched = self.makeScheduler(name='test', builderNames=[ 'test' ],
                        periodicBuildTimer=13)

        sched.startService()
        self.clock.advance(0) # let it trigger the first build
        while self.clock.seconds() < 30:
            self.clock.advance(1)
        self.assertEqual(self.events, [ 'B@0', 'B@13', 'B@26' ])
        self.assertEqual(self.state.get('last_build'), 26)

        d = sched.stopService()
        return d

    def test_iterations_simple_branch(self):
        sched = self.makeScheduler(exp_branch='newfeature',
                name='test', builderNames=[ 'test' ],
                periodicBuildTimer=13, branch='newfeature')

        sched.startService()
        self.clock.advance(0) # let it trigger the first build
        while self.clock.seconds() < 30:
            self.clock.advance(1)
        self.assertEqual(self.events, [ 'B@0', 'B@13', 'B@26' ])
        self.assertEqual(self.state.get('last_build'), 26)

        d = sched.stopService()
        return d

    def test_iterations_long(self):
        sched = self.makeScheduler(name='test', builderNames=[ 'test' ],
                        periodicBuildTimer=10,
                        firstBuildDuration=15) # takes a while to start a build

        sched.startService()
        self.clock.advance(0) # let it trigger the first (longer) build
        while self.clock.seconds() < 40:
            self.clock.advance(1)
        self.assertEqual(self.events, [ 'B@0', 'B@15', 'B@25', 'B@35' ])
        self.assertEqual(self.state.get('last_build'), 35)

        d = sched.stopService()
        return d

    def test_iterations_stop_while_starting_build(self):
        sched = self.makeScheduler(name='test', builderNames=[ 'test' ],
                        periodicBuildTimer=13,
                        firstBuildDuration=6) # takes a while to start a build

        sched.startService()
        self.clock.advance(0) # let it trigger the first (longer) build
        self.clock.advance(3) # get partway into that build

        d = sched.stopService() # begin stopping the service
        d.addCallback(lambda _ : self.events.append('STOP@%d' % self.clock.seconds()))

        # run the clock out
        while self.clock.seconds() < 40:
            self.clock.advance(1)

        # note that the stopService completes after the first build completes, and no
        # subsequent builds occur
        self.assertEqual(self.events, [ 'B@0', 'STOP@6' ])
        self.assertEqual(self.state.get('last_build'), 0)

        return d

    def test_iterations_with_initial_state(self):
        sched = self.makeScheduler(name='test', builderNames=[ 'test' ],
                        periodicBuildTimer=13)
        self.state['last_build'] = self.clock.seconds() - 7 # so next build should start in 6s

        sched.startService()
        self.clock.advance(0) # let it trigger the first build
        while self.clock.seconds() < 30:
            self.clock.advance(1)
        self.assertEqual(self.events, [ 'B@6', 'B@19' ])
        self.assertEqual(self.state.get('last_build'), 19)

        d = sched.stopService()
        return d

    def test_getNextBuildTime_None(self):
        sched = self.makeScheduler(name='test', builderNames=[ 'test' ],
                        periodicBuildTimer=13)
        # given None, build right away
        d = sched.getNextBuildTime(None)
        d.addCallback(lambda t : self.assertEqual(t, 0))
        return d

    def test_getNextBuildTime_given(self):
        sched = self.makeScheduler(name='test', builderNames=[ 'test' ],
                        periodicBuildTimer=13)
        # given a time, add the periodicBuildTimer to it
        d = sched.getNextBuildTime(20)
        d.addCallback(lambda t : self.assertEqual(t, 33))
        return d

    def test_getPendingBuildTimes(self):
        sched = self.makeScheduler(name='test', builderNames=[ 'test' ],
                        periodicBuildTimer=13)
        self.state['last_build'] = self.clock.seconds() - 10 # so next build should start in 3s

        sched.startService()
        self.clock.advance(0) # let it schedule the first build
        self.assertEqual(sched.getPendingBuildTimes(), [ 3.0 ])

        d = sched.stopService()
        return d