From 1d0c862c54d2b4f4e188e546e8e3c46ca4a60514 Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone <exarkun@twistedmatrix.com> Date: Mon, 4 Mar 2019 16:22:06 -0500 Subject: [PATCH] convert some tests over to the new base class in support of better logging --- src/allmydata/test/common.py | 7 +++++++ src/allmydata/test/eliotutil.py | 11 ++++++----- src/allmydata/test/test_inotify.py | 19 +++++++++++-------- src/allmydata/test/test_magic_folder.py | 4 +--- src/allmydata/util/eliotutil.py | 3 ++- 5 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/allmydata/test/common.py b/src/allmydata/test/common.py index 5d44dd0e5..06a7161ff 100644 --- a/src/allmydata/test/common.py +++ b/src/allmydata/test/common.py @@ -2,6 +2,10 @@ __all__ = [ "SyncTestCase", "AsyncTestCase", "AsyncBrokenTestCase", + + "flush_logged_errors", + "skip", + "skipIf", ] import os, random, struct @@ -16,11 +20,14 @@ from zope.interface import implementer from testtools import ( TestCase, + skip, + skipIf, ) from testtools.twistedsupport import ( SynchronousDeferredRunTest, AsynchronousDeferredRunTest, AsynchronousDeferredRunTestForBrokenTwisted, + flush_logged_errors, ) from twisted.internet import defer diff --git a/src/allmydata/test/eliotutil.py b/src/allmydata/test/eliotutil.py index 90ff9179c..09d34f938 100644 --- a/src/allmydata/test/eliotutil.py +++ b/src/allmydata/test/eliotutil.py @@ -57,22 +57,23 @@ def eliot_logged_test(f): @wraps(f) def run_and_republish(self, *a, **kw): + # Unfortunately the only way to get at the global/default logger... + # This import is delayed here so that we get the *current* default + # logger at the time the decorated function is run. + from eliot._output import _DEFAULT_LOGGER as default_logger + def republish(): # This is called as a cleanup function after capture_logging has # restored the global/default logger to its original state. We # can now emit messages that go to whatever global destinations # are installed. - # Unfortunately the only way to get at the global/default - # logger... - from eliot._output import _DEFAULT_LOGGER as logger - # storage.logger.serialize() seems like it would make more sense # than storage.logger.messages here. However, serialize() # explodes, seemingly as a result of double-serializing the logged # messages. I don't understand this. for msg in storage.logger.messages: - logger.write(msg) + default_logger.write(msg) # And now that we've re-published all of the test's messages, we # can finish the test's action. diff --git a/src/allmydata/test/test_inotify.py b/src/allmydata/test/test_inotify.py index ea8c90315..77ea97fa9 100644 --- a/src/allmydata/test/test_inotify.py +++ b/src/allmydata/test/test_inotify.py @@ -12,21 +12,22 @@ import sys from twisted.internet import defer, reactor from twisted.python import filepath, runtime -from twisted.trial import unittest from allmydata.frontends.magic_folder import get_inotify_module - +from .common import ( + AsyncTestCase, + skip, + skipIf, +) inotify = get_inotify_module() -class INotifyTests(unittest.TestCase): +@skipIf(runtime.platformType == "win32", "inotify does not yet work on windows") +class INotifyTests(AsyncTestCase): """ Define all the tests for the basic functionality exposed by L{inotify.INotify}. """ - if runtime.platformType == "win32": - skip = "inotify does not yet work on windows" - def setUp(self): self.ignore_count = 0 self.dirname = filepath.FilePath(self.mktemp()) @@ -34,6 +35,7 @@ class INotifyTests(unittest.TestCase): self.inotify = inotify.INotify() self.inotify.startReading() self.addCleanup(self.inotify.stopReading) + return super(INotifyTests, self).setUp() def _notificationTest(self, mask, operation, expectedPath=None, ignore_count=0): @@ -215,6 +217,7 @@ class INotifyTests(unittest.TestCase): set(['close_write', 'access', 'open'])) + @skip("not relevant") def test_recursiveWatch(self): """ L{inotify.INotify.watch} with recursive==True will add all the @@ -230,9 +233,9 @@ class INotifyTests(unittest.TestCase): self.inotify.watch(self.dirname, recursive=True) for d in dirs: self.assertTrue(self.inotify._isWatched(d)) - test_recursiveWatch.skip = "not relevant" + @skip("Based on Twisted implementation details; not relevant") def test_connectionLostError(self): """ L{inotify.INotify.connectionLost} if there's a problem while closing @@ -243,7 +246,7 @@ class INotifyTests(unittest.TestCase): os.close(in_._fd) in_.loseConnection() self.flushLoggedErrors() - test_connectionLostError.skip = "Based on Twisted implementation details; not relevant" + def test_noAutoAddSubdirectory(self): """ diff --git a/src/allmydata/test/test_magic_folder.py b/src/allmydata/test/test_magic_folder.py index 4ed5ed9e7..d0e496a95 100644 --- a/src/allmydata/test/test_magic_folder.py +++ b/src/allmydata/test/test_magic_folder.py @@ -9,9 +9,6 @@ from twisted.internet import defer, task, reactor from twisted.python.runtime import platform from twisted.python.filepath import FilePath -from testtools import ( - skipIf, -) from testtools.matchers import ( Not, Is, @@ -41,6 +38,7 @@ from .common import ( ShouldFailMixin, SyncTestCase, AsyncTestCase, + skipIf, ) from .cli.test_magic_folder import MagicFolderCLITestMixin diff --git a/src/allmydata/util/eliotutil.py b/src/allmydata/util/eliotutil.py index 696a66afc..535f4913d 100644 --- a/src/allmydata/util/eliotutil.py +++ b/src/allmydata/util/eliotutil.py @@ -361,6 +361,7 @@ class _EliotLogging(Service): self.twisted_observer = _TwistedLoggerToEliotObserver() globalLogPublisher.addObserver(self.twisted_observer) add_destinations(*self.destinations) + return Service.startService(self) def stopService(self): @@ -368,7 +369,7 @@ class _EliotLogging(Service): remove_destination(dest) globalLogPublisher.removeObserver(self.twisted_observer) self.stdlib_cleanup() - + return Service.stopService(self) @implementer(ILogObserver)