Skip to content

Commit

Permalink
discard async cleanups when running them
Browse files Browse the repository at this point in the history
  • Loading branch information
exarkun committed Mar 27, 2022
1 parent f28b36d commit 34e657a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/twisted/trial/_asynctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ def deferRunCleanups(self, ignored, result):
object.
"""
failures = []
for func, args, kwargs in self._cleanups[::-1]:
while len(self._cleanups) > 0:
func, args, kwargs = self._cleanups.pop()
try:
yield func(*args, **kwargs)
except Exception:
Expand Down
2 changes: 2 additions & 0 deletions src/twisted/trial/newsfragments/10320.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

twisted.trial.unittest.TestCase now discards cleanup functions after running them. Notably, this prevents them from being run an ever growing number of times with `trial -u ...`.
15 changes: 15 additions & 0 deletions src/twisted/trial/test/test_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,21 @@ def test_multipleErrorsReported(self):
self.assertEqual(error1.getErrorMessage(), "bar")
self.assertEqual(error2.getErrorMessage(), "foo")

def test_cleanupRunsOnce(self):
"""
A function registered as a cleanup is run once.
"""
cleanups = []
self.test.addCleanup(lambda: cleanups.append(stage))
# It should get run this time.
stage = "first"
self.test.run(self.result)
# It should not get run the next time since it has not been
# re-registered.
stage = "second"
self.test.run(self.result)
self.assertEqual(cleanups, ["first"])


class SynchronousAddCleanupTests(AddCleanupMixin, unittest.SynchronousTestCase):
"""
Expand Down

0 comments on commit 34e657a

Please sign in to comment.