Skip to content

Commit

Permalink
Monkeypatch startService and stopService to assert on running
Browse files Browse the repository at this point in the history
This adds a new monkeypatch module which will hopefully be useful in
adding more monkey patches later.  It should make tracking down
service-management bugs in tests *much* easier.
  • Loading branch information
Dustin J. Mitchell committed Mar 12, 2010
1 parent 9ce34be commit db22729
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 1 deletion.
4 changes: 3 additions & 1 deletion buildbot/broken_test/runs/test_web.py
Expand Up @@ -6,7 +6,7 @@
from twisted.python import components

from twisted.trial import unittest
from buildbot.broken_test.runutils import RunMixin
from buildbot.broken_test.runutils import RunMixin, monkeypatch

from twisted.internet import reactor, defer, protocol
from twisted.internet.interfaces import IReactorUNIX
Expand All @@ -22,6 +22,8 @@
from buildbot.process.buildstep import BuildStep
from buildbot.broken_test.runutils import setupBuildStepStatus

monkeypatch()

class SimpleMaster(master.BuildMaster):
"""This BuildMaster variant uses a local sqlite DB, and gives more
control over the loading of the config file."""
Expand Down
2 changes: 2 additions & 0 deletions buildbot/broken_test/runutils.py
Expand Up @@ -614,3 +614,5 @@ def _get_buildstatus(build_set_status):
d.addCallback(_get_buildstatus)
return d

# convenient access to *new* test utilities
from buildbot.test.util.monkeypatches import monkeypatch
3 changes: 3 additions & 0 deletions buildbot/test/unit/test_util_loop.py
Expand Up @@ -41,9 +41,12 @@
from twisted.python import log

from buildbot.test.fake.state import State
from buildbot.test.util.monkeypatches import monkeypatch

from buildbot.util import loop, eventual

monkeypatch()

class TestLoopMixin(object):
def setUpTestLoop(self, skipConstructor=False):
if not skipConstructor:
Expand Down
Empty file added buildbot/test/util/__init__.py
Empty file.
18 changes: 18 additions & 0 deletions buildbot/test/util/monkeypatches.py
@@ -0,0 +1,18 @@
# Import this module and call the monkeypatch() method to add a bunch of useful
# Twisted monkeypatches that help to catch stupid errors.

def monkeypatch_startService():
from twisted.application.service import Service
old_startService = Service.startService
old_stopService = Service.stopService
def startService(self):
assert not self.running
return old_startService(self)
def stopService(self):
assert self.running
return old_stopService(self)
Service.startService = startService
Service.stopService = stopService

def monkeypatch():
monkeypatch_startService()
15 changes: 15 additions & 0 deletions docs/developer.texinfo
Expand Up @@ -417,6 +417,7 @@ stopped.

@menu
* Keeping State in Tests::
* Better Debugging through Monkeypatching::
@end menu

@node Keeping State in Tests
Expand Down Expand Up @@ -457,6 +458,20 @@ def test_localVariable(self):

This is almost as readable as the first example, but it actually works.

@node Better Debugging through Monkeypatching
@subsection Better Debugging through Monkeypatching
@dvindex buildbot.test.util.monkeypatches

The module @code{buildbot.test.util.monkeypatches} contains a few
monkey-patches to Twisted that detect errors a bit better. These patches
shouldn't affect correct behavior, so it's worthwhile including something like
this in the header of every test file:

@example
from buildbot.test.util.monkeypatches import monkeypatch
monkeypatch()
@end example

@node Developer Class Index
@section Developer Class Index
@printindex dv

0 comments on commit db22729

Please sign in to comment.