Tests turn things into inlineCallbacks.

This is to duplicate the old behavior of BaseTestCase and to generally
prevent the bad situation in that tests appear to be passing when in
fact they haven't run because @defer.inlineCallbacks was forgotten.
This commit is contained in:
andy
2010-09-08 22:43:54 +02:00
parent 670eaf26b0
commit b00f5d8ca7
4 changed files with 49 additions and 8 deletions

View File

@@ -250,7 +250,6 @@ def call(topic, msg):
msg_id = uuid.uuid4().hex msg_id = uuid.uuid4().hex
msg.update({'_msg_id': msg_id}) msg.update({'_msg_id': msg_id})
LOG.debug("MSG_ID is %s" % (msg_id)) LOG.debug("MSG_ID is %s" % (msg_id))
conn = Connection.instance() conn = Connection.instance()
d = defer.Deferred() d = defer.Deferred()
consumer = DirectConsumer(connection=conn, msg_id=msg_id) consumer = DirectConsumer(connection=conn, msg_id=msg_id)

View File

@@ -33,6 +33,7 @@ from twisted.trial import unittest
from nova import fakerabbit from nova import fakerabbit
from nova import flags from nova import flags
from nova import rpc
FLAGS = flags.FLAGS FLAGS = flags.FLAGS
@@ -63,22 +64,28 @@ class TrialTestCase(unittest.TestCase):
self.stubs = stubout.StubOutForTesting() self.stubs = stubout.StubOutForTesting()
self.flag_overrides = {} self.flag_overrides = {}
self.injected = [] self.injected = []
self._monkeyPatchAttach()
def tearDown(self): # pylint: disable-msg=C0103 def tearDown(self): # pylint: disable-msg=C0103
"""Runs after each test method to finalize/tear down test environment""" """Runs after each test method to finalize/tear down test environment"""
super(TrialTestCase, self).tearDown()
self.reset_flags() self.reset_flags()
self.mox.UnsetStubs() self.mox.UnsetStubs()
self.stubs.UnsetAll() self.stubs.UnsetAll()
self.stubs.SmartUnsetAll() self.stubs.SmartUnsetAll()
self.mox.VerifyAll() self.mox.VerifyAll()
rpc.Consumer.attach_to_twisted = self.originalAttach
for x in self.injected: for x in self.injected:
x.stop() try:
x.stop()
except AssertionError:
pass
if FLAGS.fake_rabbit: if FLAGS.fake_rabbit:
fakerabbit.reset_all() fakerabbit.reset_all()
super(TrialTestCase, self).tearDown()
def flags(self, **kw): def flags(self, **kw):
"""Override flag variables for a test""" """Override flag variables for a test"""
for k, v in kw.iteritems(): for k, v in kw.iteritems():
@@ -94,10 +101,46 @@ class TrialTestCase(unittest.TestCase):
for k, v in self.flag_overrides.iteritems(): for k, v in self.flag_overrides.iteritems():
setattr(FLAGS, k, v) setattr(FLAGS, k, v)
def run(self, result=None):
test_method = getattr(self, self._testMethodName)
setattr(self,
self._testMethodName,
self._maybeInlineCallbacks(test_method, result))
rv = super(TrialTestCase, self).run(result)
setattr(self, self._testMethodName, test_method)
return rv
def _maybeInlineCallbacks(self, func, result):
def _wrapped():
g = func()
if isinstance(g, defer.Deferred):
return g
if not hasattr(g, 'send'):
return defer.succeed(g)
inlined = defer.inlineCallbacks(func)
d = inlined()
return d
_wrapped.func_name = func.func_name
return _wrapped
def _monkeyPatchAttach(self):
self.originalAttach = rpc.Consumer.attach_to_twisted
def _wrapped(innerSelf):
rv = self.originalAttach(innerSelf)
self.injected.append(rv)
return rv
_wrapped.func_name = self.originalAttach.func_name
rpc.Consumer.attach_to_twisted = _wrapped
class BaseTestCase(TrialTestCase): class BaseTestCase(TrialTestCase):
# TODO(jaypipes): Can this be moved into the TrialTestCase class? # TODO(jaypipes): Can this be moved into the TrialTestCase class?
"""Base test case class for all unit tests.""" """Base test case class for all unit tests.
DEPRECATED: This is being removed once Tornado is gone, use TrialTestCase.
"""
def setUp(self): # pylint: disable-msg=C0103 def setUp(self): # pylint: disable-msg=C0103
"""Run before each test method to initialize test environment""" """Run before each test method to initialize test environment"""
super(BaseTestCase, self).setUp() super(BaseTestCase, self).setUp()

View File

@@ -52,7 +52,7 @@ class CloudTestCase(test.TrialTestCase):
self.compute_consumer = rpc.AdapterConsumer(connection=self.conn, self.compute_consumer = rpc.AdapterConsumer(connection=self.conn,
topic=FLAGS.compute_topic, topic=FLAGS.compute_topic,
proxy=self.compute) proxy=self.compute)
self.injected.append(self.compute_consumer.attach_to_twisted()) self.compute_consumer.attach_to_twisted()
try: try:
manager.AuthManager().create_user('admin', 'admin', 'admin') manager.AuthManager().create_user('admin', 'admin', 'admin')

View File

@@ -39,8 +39,7 @@ class RpcTestCase(test.TrialTestCase):
self.consumer = rpc.AdapterConsumer(connection=self.conn, self.consumer = rpc.AdapterConsumer(connection=self.conn,
topic='test', topic='test',
proxy=self.receiver) proxy=self.receiver)
self.consumer.attach_to_twisted()
self.injected.append(self.consumer.attach_to_twisted())
def test_call_succeed(self): def test_call_succeed(self):
"""Get a value through rpc call""" """Get a value through rpc call"""