diff options
author | Dan Smith <danms@us.ibm.com> | 2013-05-19 14:13:59 -0700 |
---|---|---|
committer | Dan Smith <danms@us.ibm.com> | 2013-05-21 06:40:51 -0700 |
commit | 93ee6e3bfb280bfe5b5f6617c5d89c65cc54028d (patch) | |
tree | 7dd9d7485b729bf1fa613c3b877912a2be729750 /tests | |
parent | df7ea8308363235afba503f8562341bd2e3dce4f (diff) | |
download | oslo-93ee6e3bfb280bfe5b5f6617c5d89c65cc54028d.tar.gz oslo-93ee6e3bfb280bfe5b5f6617c5d89c65cc54028d.tar.xz oslo-93ee6e3bfb280bfe5b5f6617c5d89c65cc54028d.zip |
Add generic serialization support
This adds a generic serialization model that the RpcProxy and
RpcDispatcher will use to serialize and deserialize arguments
and return values. The base definition is provided, as well
as a default NoOpSerializer which performs no special
action (retaining the existing behavior).
Related to blueprint rpc-object-serialization
Change-Id: I0a33baddee3e28dfc47100eb3216b679558b0bdd
Diffstat (limited to 'tests')
-rw-r--r-- | tests/unit/rpc/test_dispatcher.py | 27 | ||||
-rw-r--r-- | tests/unit/rpc/test_proxy.py | 35 |
2 files changed, 62 insertions, 0 deletions
diff --git a/tests/unit/rpc/test_dispatcher.py b/tests/unit/rpc/test_dispatcher.py index 8998a90..a09d358 100644 --- a/tests/unit/rpc/test_dispatcher.py +++ b/tests/unit/rpc/test_dispatcher.py @@ -18,9 +18,12 @@ Unit Tests for rpc.dispatcher """ +import mox + from openstack.common import context from openstack.common.rpc import common as rpc_common from openstack.common.rpc import dispatcher +from openstack.common.rpc import serializer as rpc_serializer from tests import utils @@ -35,6 +38,7 @@ class RpcDispatcherTestCase(utils.BaseTestCase): def test_method(self, ctxt, arg1): self.test_method_ctxt = ctxt self.test_method_arg1 = arg1 + return 'fake-result' class API2(object): RPC_API_VERSION = '2.1' @@ -73,6 +77,11 @@ class RpcDispatcherTestCase(utils.BaseTestCase): def setUp(self): super(RpcDispatcherTestCase, self).setUp() self.ctxt = context.RequestContext('fake_user', 'fake_project') + self.mox = mox.Mox() + + def cleanUp(self): + super(RpcDispatcherTestCase, self).setUp() + self.mox.VerifyAll() def _test_dispatch(self, version, expectations): v2 = self.API2() @@ -158,3 +167,21 @@ class RpcDispatcherTestCase(utils.BaseTestCase): self.assertEqual(v1.test_method_arg1, None) self.assertEqual(v4.test_method_ctxt, self.ctxt) self.assertEqual(v4.test_method_arg1, 1) + + def test_serializer(self): + api = self.API1() + serializer = rpc_serializer.NoOpSerializer() + + self.mox.StubOutWithMock(serializer, 'serialize_entity') + self.mox.StubOutWithMock(serializer, 'deserialize_entity') + + serializer.deserialize_entity(self.ctxt, 1).AndReturn(1) + serializer.serialize_entity(self.ctxt, 'fake-result').AndReturn( + 'worked!') + + self.mox.ReplayAll() + + disp = dispatcher.RpcDispatcher([api], serializer) + result = disp.dispatch(self.ctxt, '1.0', 'test_method', + None, arg1=1) + self.assertEqual(result, 'worked!') diff --git a/tests/unit/rpc/test_proxy.py b/tests/unit/rpc/test_proxy.py index f05964b..9427cbe 100644 --- a/tests/unit/rpc/test_proxy.py +++ b/tests/unit/rpc/test_proxy.py @@ -19,6 +19,7 @@ Unit Tests for rpc.proxy """ import copy +import mox import six @@ -27,11 +28,20 @@ from openstack.common import lockutils from openstack.common import rpc from openstack.common.rpc import common as rpc_common from openstack.common.rpc import proxy +from openstack.common.rpc import serializer as rpc_serializer from tests import utils class RpcProxyTestCase(utils.BaseTestCase): + def setUp(self): + super(RpcProxyTestCase, self).setUp() + self.mox = mox.Mox() + + def cleanUp(self): + super(RpcProxyTestCase, self).cleanUp() + self.mox.VerifyAll() + def _test_rpc_method(self, rpc_method, has_timeout=False, has_retval=False, server_params=None, supports_topic_override=True): topic = 'fake_topic' @@ -174,3 +184,28 @@ class RpcProxyTestCase(utils.BaseTestCase): expected = {'method': 'test_method', 'namespace': 'meow', 'args': {'a': 1, 'b': 2}} self.assertEqual(msg, expected) + + def test_serializer(self): + ctxt = context.RequestContext('fake', 'fake') + serializer = rpc_serializer.NoOpSerializer() + + self.mox.StubOutWithMock(serializer, 'serialize_entity') + self.mox.StubOutWithMock(serializer, 'deserialize_entity') + self.mox.StubOutWithMock(rpc, 'call') + + serializer.serialize_entity(ctxt, 1).AndReturn(1) + serializer.serialize_entity(ctxt, 2).AndReturn(2) + rpc.call(ctxt, 'fake', + {'args': {'a': 1, 'b': 2}, + 'namespace': None, + 'method': 'foo', + 'version': '1.0'}, + None).AndReturn('foo') + serializer.deserialize_entity(ctxt, 'foo').AndReturn('worked!') + + self.mox.ReplayAll() + + rpc_proxy = proxy.RpcProxy('fake', '1.0', serializer=serializer) + msg = rpc_proxy.make_msg('foo', a=1, b=2) + result = rpc_proxy.call(ctxt, msg) + self.assertEqual(result, 'worked!') |