Skip to content

Commit

Permalink
Twisted tests now pass under --debug-stacktraces
Browse files Browse the repository at this point in the history
Our tests make assertions that only hold when debugging is disabled.
This patch updates the code to ensure that debugging is in the state
we want.
  • Loading branch information
jml committed Jan 30, 2016
1 parent af8e294 commit 91c195c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
3 changes: 3 additions & 0 deletions NEWS
Expand Up @@ -12,6 +12,9 @@ Changes
* Python 2.6 and 3.2 are no longer supported. If you want to use either of
these versions of Python, use testtools 1.9.0. (Jonathan Lange)

* Make ``fixtures`` a real dependency, not just a test dependency.
(Jonathan Lange)


1.9.0
~~~~~
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
@@ -1,9 +1,9 @@
pbr>=0.11
extras
fixtures>=1.3.0
pyrsistent
# 'mimeparse' has not been uploaded by the maintainer with Python3 compat
# but someone kindly uploaded a fixed version as 'python-mimeparse'.
python-mimeparse
unittest2>=1.0.0
traceback2
fixtures>=1.3.0
21 changes: 21 additions & 0 deletions testtools/_deferreddebug.py
@@ -0,0 +1,21 @@
# Copyright (c) testtools developers. See LICENSE for details.
#
# TODO: Move this to testtools.twistedsupport. See testing-cabal/testtools#202.

from fixtures import Fixture, MonkeyPatch


class DebugTwisted(Fixture):
"""Set debug options for Twisted."""

def __init__(self, debug=True):
super(DebugTwisted, self).__init__()
self._debug_setting = debug

def _setUp(self):
self.useFixture(
MonkeyPatch('twisted.internet.defer.Deferred.debug',
self._debug_setting))
self.useFixture(
MonkeyPatch('twisted.internet.base.DelayedCall.debug',
self._debug_setting))
16 changes: 7 additions & 9 deletions testtools/_spinner.py
Expand Up @@ -16,12 +16,12 @@
'trap_unhandled_errors',
]

from fixtures import Fixture
import signal

from testtools.monkey import MonkeyPatcher
from testtools._deferreddebug import DebugTwisted

from twisted.internet import defer
from twisted.internet.base import DelayedCall
from twisted.internet.interfaces import IReactorThreads
from twisted.python.failure import Failure
from twisted.python.util import mergeFunctionMetadata
Expand Down Expand Up @@ -265,12 +265,12 @@ def run(self, timeout, function, *args, **kwargs):
:return: Whatever is at the end of the function's callback chain. If
it's an error, then raise that.
"""
debug = MonkeyPatcher()
if self._debug:
debug.add_patch(defer.Deferred, 'debug', True)
debug.add_patch(DelayedCall, 'debug', True)
debug.patch()
try:
debug_settings = DebugTwisted(True)
else:
debug_settings = Fixture()

with debug_settings:
junk = self.get_junk()
if junk:
raise StaleJunkError(junk)
Expand Down Expand Up @@ -300,5 +300,3 @@ def run_function():
return self._get_result()
finally:
self._clean()
finally:
debug.restore()
13 changes: 13 additions & 0 deletions testtools/tests/test_deferredruntest.py
Expand Up @@ -12,6 +12,7 @@
TestCase,
TestResult,
)
from testtools._deferreddebug import DebugTwisted
from testtools.matchers import (
AfterPreprocessing,
Contains,
Expand Down Expand Up @@ -388,6 +389,10 @@ def test_cruft(self):
def test_unhandled_error_from_deferred(self):
# If there's a Deferred with an unhandled error, the test fails. Each
# unhandled error is reported with a separate traceback.

# We're interested in the behavior when debugging is disabled. When
# debugging is enabled, we get more stack traces.
self.useFixture(DebugTwisted(False))
class SomeCase(TestCase):
def test_cruft(self):
# Note we aren't returning the Deferred so that the error will
Expand Down Expand Up @@ -415,6 +420,10 @@ def test_unhandled_error_from_deferred_combined_with_error(self):
# If there's a Deferred with an unhandled error, the test fails. Each
# unhandled error is reported with a separate traceback, and the error
# is still reported.

# We're interested in the behavior when debugging is disabled. When
# debugging is enabled, we get more stack traces.
self.useFixture(DebugTwisted(False))
class SomeCase(TestCase):
def test_cruft(self):
# Note we aren't returning the Deferred so that the error will
Expand Down Expand Up @@ -573,6 +582,10 @@ def test_something(self):
def test_only_addError_once(self):
# Even if the reactor is unclean and the test raises an error and the
# cleanups raise errors, we only called addError once per test.

# We're interested in the behavior when debugging is disabled. When
# debugging is enabled, we get more stack traces.
self.useFixture(DebugTwisted(False))
reactor = self.make_reactor()
class WhenItRains(TestCase):
def it_pours(self):
Expand Down

0 comments on commit 91c195c

Please sign in to comment.