def test_dispatcher(self): endpoints = [] for e in self.endpoints: target = messaging.Target(**e) if e else None endpoints.append(_FakeEndpoint(target)) serializer = None dispatcher = messaging.RPCDispatcher(endpoints, serializer) if self.dispatch_to is not None: endpoint = endpoints[self.dispatch_to['endpoint']] method = self.dispatch_to['method'] self.mox.StubOutWithMock(endpoint, method) method = getattr(endpoint, method) method(self.ctxt, **self.msg.get('args', {})) self.mox.ReplayAll() try: dispatcher(self.ctxt, self.msg) except Exception as ex: self.assertFalse(self.success, ex) self.assertIsNotNone(self.ex, ex) self.assertIsInstance(ex, self.ex, ex) if isinstance(ex, messaging.NoSuchMethod): self.assertEqual(ex.method, self.msg.get('method')) elif isinstance(ex, messaging.UnsupportedVersion): self.assertEqual(ex.version, self.msg.get('version', '1.0')) else: self.assertTrue(self.success)
def test_serializer(self): endpoint = _FakeEndpoint() serializer = msg_serializer.NoOpSerializer() target = messaging.Target() dispatcher = messaging.RPCDispatcher(target, [endpoint], serializer) self.mox.StubOutWithMock(endpoint, 'foo') args = dict([(k, 'd' + v) for k, v in self.args.items()]) endpoint.foo(self.dctxt, **args).AndReturn(self.retval) self.mox.StubOutWithMock(serializer, 'serialize_entity') self.mox.StubOutWithMock(serializer, 'deserialize_entity') self.mox.StubOutWithMock(serializer, 'deserialize_context') serializer.deserialize_context(self.ctxt).AndReturn(self.dctxt) for arg in self.args: serializer.deserialize_entity(self.dctxt, arg).AndReturn('d' + arg) serializer.serialize_entity(self.dctxt, self.retval).\ AndReturn('s' + self.retval if self.retval else None) self.mox.ReplayAll() retval = dispatcher._dispatch(self.ctxt, dict(method='foo', args=self.args)) if self.retval is not None: self.assertEqual('s' + self.retval, retval)
def test_dispatcher(self): endpoints = [ mock.Mock(spec=_FakeEndpoint, target=messaging.Target(**e)) for e in self.endpoints ] serializer = None target = messaging.Target() dispatcher = messaging.RPCDispatcher(target, endpoints, serializer) def check_reply(reply=None, failure=None, log_failure=True): if self.ex and failure is not None: ex = failure[1] self.assertFalse(self.success, ex) self.assertIsNotNone(self.ex, ex) self.assertIsInstance(ex, self.ex, ex) if isinstance(ex, messaging.NoSuchMethod): self.assertEqual(self.msg.get('method'), ex.method) elif isinstance(ex, messaging.UnsupportedVersion): self.assertEqual(self.msg.get('version', '1.0'), ex.version) if ex.method: self.assertEqual(self.msg.get('method'), ex.method) else: self.assertTrue(self.success, failure) self.assertIsNone(failure) incoming = mock.Mock(ctxt=self.ctxt, message=self.msg) incoming.reply.side_effect = check_reply with dispatcher(incoming) as callback: callback() for n, endpoint in enumerate(endpoints): for method_name in ['foo', 'bar']: method = getattr(endpoint, method_name) if self.dispatch_to and n == self.dispatch_to['endpoint'] and \ method_name == self.dispatch_to['method']: method.assert_called_once_with(self.ctxt, **self.msg.get('args', {})) else: self.assertEqual(0, method.call_count) self.assertEqual(1, incoming.reply.call_count)