Skip to content

Commit

Permalink
Failing test that shows testLog resource leak.
Browse files Browse the repository at this point in the history
testLog becomes a private attribute on LocalWorker so that the tests
can check whether its closed.

FakeStream is unnecessary because LocalWorker creates actual files
that have a `closed` attribute.  Patching over them without closing
them first also results in further resource leaks.
  • Loading branch information
markrwilliams committed Dec 19, 2017
1 parent 7bd4639 commit 74c9221
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
27 changes: 7 additions & 20 deletions src/twisted/trial/_dist/test/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,21 +401,13 @@ def test_connectionLost(self):
L{LocalWorker.connectionLost} closes the log streams.
"""

class FakeStream(object):
callNumber = 0

def close(self):
self.callNumber += 1


transport = FakeTransport()
localWorker = LocalWorker(FakeAMProtocol(), '.', 'test.log')
localWorker.makeConnection(transport)
localWorker._outLog = FakeStream()
localWorker._errLog = FakeStream()
localWorker.connectionLost(None)
self.assertEqual(localWorker._outLog.callNumber, 1)
self.assertEqual(localWorker._errLog.callNumber, 1)
self.assertTrue(localWorker._outLog.closed)
self.assertTrue(localWorker._errLog.closed)
self.assertTrue(localWorker._testLog.closed)


def test_processEnded(self):
Expand All @@ -424,23 +416,18 @@ def test_processEnded(self):
the L{AMP} protocol.
"""

class FakeStream(object):
callNumber = 0

def close(self):
self.callNumber += 1


transport = FakeTransport()
protocol = FakeAMProtocol()
localWorker = LocalWorker(protocol, '.', 'test.log')
localWorker.makeConnection(transport)
localWorker._outLog = FakeStream()
localWorker.processEnded(Failure(CONNECTION_DONE))
self.assertEqual(localWorker._outLog.callNumber, 1)
self.assertTrue(localWorker._outLog.closed)
self.assertTrue(localWorker._errLog.closed)
self.assertTrue(localWorker._testLog.closed)
self.assertIdentical(None, protocol.transport)
return self.assertFailure(localWorker.endDeferred, ConnectionDone)


def test_addresses(self):
"""
L{LocalWorkerTransport.getPeer} and L{LocalWorkerTransport.getHost}
Expand Down
5 changes: 3 additions & 2 deletions src/twisted/trial/_dist/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,9 @@ def connectionMade(self):
os.makedirs(self._logDirectory)
self._outLog = open(os.path.join(self._logDirectory, 'out.log'), 'wb')
self._errLog = open(os.path.join(self._logDirectory, 'err.log'), 'wb')
testLog = open(os.path.join(self._logDirectory, self._logFile), 'wb')
self._ampProtocol.setTestStream(testLog)
self._testLog = open(
os.path.join(self._logDirectory, self._logFile), 'wb')
self._ampProtocol.setTestStream(self._testLog)
logDirectory = self._logDirectory
if isinstance(logDirectory, unicode):
logDirectory = logDirectory.encode("utf-8")
Expand Down

0 comments on commit 74c9221

Please sign in to comment.