From e5dc60769172fb6d94ea77e5e099cdbca7c9d849 Mon Sep 17 00:00:00 2001 From: Oliver Palmer Date: Mon, 31 Aug 2015 22:27:24 -0400 Subject: [PATCH] updated TestProtocol to use inlineCallbacks --- tests/test_jobtypes/test_core_process.py | 46 +++++++++--------------- 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/tests/test_jobtypes/test_core_process.py b/tests/test_jobtypes/test_core_process.py index 36660eec..1b867750 100644 --- a/tests/test_jobtypes/test_core_process.py +++ b/tests/test_jobtypes/test_core_process.py @@ -25,7 +25,7 @@ import psutil from twisted.internet import reactor -from twisted.internet.defer import Deferred, DeferredList +from twisted.internet.defer import Deferred, DeferredList, inlineCallbacks from twisted.internet.error import ProcessTerminated from twisted.internet.protocol import ProcessProtocol as _ProcessProtocol @@ -82,15 +82,12 @@ def test_process(self): self.assertIs(protocol.process, protocol.transport) return fake_jobtype.stopped + @inlineCallbacks def test_psutil_process_after_exit(self): fake_jobtype = FakeJobType() protocol = self._launch_python(fake_jobtype) - - def exited(*_): - self.assertIsNone(protocol.psutil_process) - - fake_jobtype.stopped.addCallback(exited) - return fake_jobtype.stopped + yield fake_jobtype.stopped + self.assertIsNone(protocol.psutil_process) def test_psutil_process_running(self): fake_jobtype = FakeJobType() @@ -106,29 +103,21 @@ def test_running(self): self.assertTrue(protocol.running()) return fake_jobtype.stopped + @inlineCallbacks def test_connectionMade(self): fake_jobtype = FakeJobType() - - def stopped(*_): - self.assertTrue(fake_jobtype.started.called) - - fake_jobtype.started.addCallback( - lambda value: self.assertIsInstance(value, ProcessProtocol)) - fake_jobtype.stopped.addCallback(stopped) self._launch_python(fake_jobtype) - return fake_jobtype.stopped + started = yield fake_jobtype.started + self.assertIsInstance(started, ProcessProtocol) + yield fake_jobtype.stopped + @inlineCallbacks def test_processEnded(self): fake_jobtype = FakeJobType() - - def stopped(*_): - self.assertTrue(fake_jobtype.stopped.called) - - fake_jobtype.stopped.addCallback( - lambda data: self.assertIsInstance(data[0], ProcessProtocol)) - fake_jobtype.stopped.addCallback(stopped) self._launch_python(fake_jobtype) - return fake_jobtype.stopped + yield fake_jobtype.started + stopped = yield fake_jobtype.stopped + self.assertIsInstance(stopped[0], ProcessProtocol) def test_processEnded_error(self): jobtype = ProcessProtocol(None) @@ -140,20 +129,20 @@ def test_processEnded_error(self): "Exception caught while running jobtype._process_stopped", logger.error.call_args[0][0]) + @inlineCallbacks def test_outReceived(self): - finished = Deferred() rand_str = os.urandom(24).encode("hex") def check_stdout(protocol, data): self.assertIsInstance(protocol, ProcessProtocol) self.assertEqual(data.strip(), rand_str) - finished.callback(None) fake_jobtype = FakeJobType(stdout=check_stdout) self._launch_python( fake_jobtype, "import sys; print >> sys.stdout, %r" % rand_str) - return DeferredList([finished, fake_jobtype.stopped]) + + yield fake_jobtype.stopped def test_outReceived_error(self): jobtype = ProcessProtocol(None) @@ -166,8 +155,8 @@ def test_outReceived_error(self): "jobtype._process_output", logger.error.call_args[0][0]) + @inlineCallbacks def test_errReceived(self): - finished = Deferred() rand_str = os.urandom(24).encode("hex") def check_stdout(protocol, data): @@ -175,13 +164,12 @@ def check_stdout(protocol, data): data = data.strip() if data: # we may not get it in the first line of output self.assertEqual(data.strip(), rand_str) - finished.callback(None) fake_jobtype = FakeJobType(stderr=check_stdout) self._launch_python( fake_jobtype, "import sys; print >> sys.stderr, %r" % rand_str) - return DeferredList([finished, fake_jobtype.stopped]) + yield fake_jobtype.stopped def test_errReceived_error(self): jobtype = ProcessProtocol(None)