Skip to content

Commit

Permalink
updated TestProtocol to use inlineCallbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
opalmer committed Sep 1, 2015
1 parent 497f21e commit e5dc607
Showing 1 changed file with 17 additions and 29 deletions.
46 changes: 17 additions & 29 deletions tests/test_jobtypes/test_core_process.py
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -166,22 +155,21 @@ 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):
self.assertIsInstance(protocol, ProcessProtocol)
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)
Expand Down

0 comments on commit e5dc607

Please sign in to comment.