-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
206 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from unittest import TestCase | ||
from warnings import warn | ||
|
||
from six import PY2 | ||
|
||
|
||
class GcAfterTestTests(TestCase): | ||
def tearDown(self): | ||
try: | ||
del self.cycle | ||
except AttributeError: | ||
pass | ||
|
||
def test_okay(self): | ||
pass | ||
|
||
def test_cycle_without_resource(self): | ||
self.cycle = _Cycle() | ||
|
||
def test_cycle_with_resource(self): | ||
self.cycle = _Cycle(resource=_Resource()) | ||
|
||
def test_test_holds_cycle(self): | ||
self.hold_cycle = _Cycle(resource=_Resource()) | ||
|
||
def test_failure(self): | ||
raise AssertionError("failure") | ||
|
||
def test_exception(self): | ||
1/0 | ||
|
||
|
||
class _Cycle(object): | ||
"""Auxiliary class creating a reference cycle.""" | ||
def __init__(self, **kw): | ||
self.self = self # create reference cycle | ||
self.__dict__.update(kw) | ||
|
||
|
||
class _Resource(object): | ||
"""Auxiliary class emulating a resource.""" | ||
closed = False | ||
|
||
def close(self): | ||
self.closed = True | ||
|
||
def __del__(self): | ||
if not self.closed: | ||
warn(ResourceWarning("not closed")) | ||
|
||
|
||
if PY2: | ||
class ResourceWarning(Warning): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
Debugging cyclic garbage and ResourceWarnings | ||
============================================= | ||
|
||
The --gc-after-test option can be used | ||
to detect the creation of cyclic garbage and diagnose ``ResourceWarning``s. | ||
Note: Python writes ``ResourceWarning`` messages to ``stderr`` | ||
which it not captured by ``doctest``. The sample output below | ||
therefore does not show the warnings (even though two are issued). | ||
|
||
>>> import os.path, sys | ||
>>> directory_with_tests = os.path.join(this_directory, 'testrunner-ex') | ||
>>> defaults = [ | ||
... '--path', directory_with_tests, | ||
... '--tests-pattern', 'gc-after-test', | ||
... ] | ||
|
||
>>> from zope import testrunner | ||
|
||
|
||
Verbosity level 1 | ||
>>> sys.argv = 'test --gc-after-test -v'.split() | ||
>>> _ = testrunner.run_internal(defaults) | ||
Running tests at level 1 | ||
Running zope.testrunner.layer.UnitTests tests: | ||
Set up zope.testrunner.layer.UnitTests in N.NNN seconds. | ||
Running: | ||
.!.!. | ||
<BLANKLINE> | ||
Error in test test_exception (gc-after-test.GcAfterTestTests) | ||
Traceback (most recent call last): | ||
... | ||
ZeroDivisionError: ... | ||
<BLANKLINE> | ||
. | ||
<BLANKLINE> | ||
Failure in test test_failure (gc-after-test.GcAfterTestTests) | ||
Traceback (most recent call last): | ||
... | ||
AssertionError: failure | ||
<BLANKLINE> | ||
..! | ||
Ran 6 tests with 1 failures, 1 errors and 0 skipped in N.NNN seconds. | ||
Tearing down left over layers: | ||
Tear down zope.testrunner.layer.UnitTests in N.NNN seconds. | ||
<BLANKLINE> | ||
Tests with errors: | ||
test_exception (gc-after-test.GcAfterTestTests) | ||
<BLANKLINE> | ||
Tests with failures: | ||
test_failure (gc-after-test.GcAfterTestTests) | ||
|
||
Verbosity level 2 (or higher) | ||
>>> sys.argv = 'test --gc-after-test -vv'.split() | ||
>>> _ = testrunner.run_internal(defaults) | ||
Running tests at level 1 | ||
Running zope.testrunner.layer.UnitTests tests: | ||
Set up zope.testrunner.layer.UnitTests in N.NNN seconds. | ||
Running: | ||
test_cycle_with_resource (gc-after-test.GcAfterTestTests) [3] | ||
<BLANKLINE> | ||
test_cycle_without_resource (gc-after-test.GcAfterTestTests) [2] | ||
<BLANKLINE> | ||
test_exception (gc-after-test.GcAfterTestTests) | ||
<BLANKLINE> | ||
Error in test test_exception (gc-after-test.GcAfterTestTests) | ||
Traceback (most recent call last): | ||
... | ||
ZeroDivisionError: ... | ||
<BLANKLINE> | ||
<BLANKLINE> | ||
test_failure (gc-after-test.GcAfterTestTests) | ||
<BLANKLINE> | ||
Failure in test test_failure (gc-after-test.GcAfterTestTests) | ||
Traceback (most recent call last): | ||
... | ||
AssertionError: failure | ||
<BLANKLINE> | ||
<BLANKLINE> | ||
test_okay (gc-after-test.GcAfterTestTests) | ||
test_test_holds_cycle (gc-after-test.GcAfterTestTests) [3] | ||
Ran 6 tests with 1 failures, 1 errors and 0 skipped in N.NNN seconds. | ||
Tearing down left over layers: | ||
Tear down zope.testrunner.layer.UnitTests in N.NNN seconds. | ||
<BLANKLINE> | ||
Tests with errors: | ||
test_exception (gc-after-test.GcAfterTestTests) | ||
<BLANKLINE> | ||
Tests with failures: | ||
test_failure (gc-after-test.GcAfterTestTests) | ||
|