1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2010 United States Government as represented by the
4
# Administrator of the National Aeronautics and Space Administration.
7
# Licensed under the Apache License, Version 2.0 (the "License"); you may
8
# not use this file except in compliance with the License. You may obtain
9
# a copy of the License at
11
# http://www.apache.org/licenses/LICENSE-2.0
13
# Unless required by applicable law or agreed to in writing, software
14
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16
# License for the specific language governing permissions and limitations
19
Unit Tests for remote procedure calls shared between all implementations
22
from nova import context
23
from nova import log as logging
24
from nova.rpc.common import RemoteError
28
LOG = logging.getLogger('nova.tests.rpc')
31
class _BaseRpcTestCase(test.TestCase):
33
super(_BaseRpcTestCase, self).setUp()
34
self.conn = self.rpc.create_connection(True)
35
self.receiver = TestReceiver()
36
self.conn.create_consumer('test', self.receiver, False)
37
self.conn.consume_in_thread()
38
self.context = context.get_admin_context()
42
super(_BaseRpcTestCase, self).tearDown()
44
def test_call_succeed(self):
46
result = self.rpc.call(self.context, 'test', {"method": "echo",
47
"args": {"value": value}})
48
self.assertEqual(value, result)
50
def test_call_succeed_despite_multiple_returns(self):
52
result = self.rpc.call(self.context, 'test',
53
{"method": "echo_three_times",
54
"args": {"value": value}})
55
self.assertEqual(value + 2, result)
57
def test_call_succeed_despite_multiple_returns_yield(self):
59
result = self.rpc.call(self.context, 'test',
60
{"method": "echo_three_times_yield",
61
"args": {"value": value}})
62
self.assertEqual(value + 2, result)
64
def test_multicall_succeed_once(self):
66
result = self.rpc.multicall(self.context,
69
"args": {"value": value}})
70
for i, x in enumerate(result):
72
self.fail('should only receive one response')
73
self.assertEqual(value + i, x)
75
def test_multicall_succeed_three_times(self):
77
result = self.rpc.multicall(self.context,
79
{"method": "echo_three_times",
80
"args": {"value": value}})
81
for i, x in enumerate(result):
82
self.assertEqual(value + i, x)
84
def test_multicall_succeed_three_times_yield(self):
86
result = self.rpc.multicall(self.context,
88
{"method": "echo_three_times_yield",
89
"args": {"value": value}})
90
for i, x in enumerate(result):
91
self.assertEqual(value + i, x)
93
def test_context_passed(self):
94
"""Makes sure a context is passed through rpc call."""
96
result = self.rpc.call(self.context,
97
'test', {"method": "context",
98
"args": {"value": value}})
99
self.assertEqual(self.context.to_dict(), result)
101
def test_call_exception(self):
102
"""Test that exception gets passed back properly.
104
rpc.call returns a RemoteError object. The value of the
105
exception is converted to a string, so we convert it back
106
to an int in the test.
110
self.assertRaises(RemoteError,
115
"args": {"value": value}})
117
self.rpc.call(self.context,
120
"args": {"value": value}})
121
self.fail("should have thrown RemoteError")
122
except RemoteError as exc:
123
self.assertEqual(int(exc.value), value)
125
def test_nested_calls(self):
126
"""Test that we can do an rpc.call inside another call."""
127
class Nested(object):
129
def echo(context, queue, value):
130
"""Calls echo in the passed queue"""
131
LOG.debug(_("Nested received %(queue)s, %(value)s")
133
# TODO: so, it will replay the context and use the same REQID?
135
ret = self.rpc.call(context,
138
"args": {"value": value}})
139
LOG.debug(_("Nested return %s"), ret)
143
conn = self.rpc.create_connection(True)
144
conn.create_consumer('nested', nested, False)
145
conn.consume_in_thread()
147
result = self.rpc.call(self.context,
148
'nested', {"method": "echo",
149
"args": {"queue": "test",
152
self.assertEqual(value, result)
155
class TestReceiver(object):
156
"""Simple Proxy class so the consumer has methods to call.
158
Uses static methods because we aren't actually storing any state.
163
def echo(context, value):
164
"""Simply returns whatever value is sent in."""
165
LOG.debug(_("Received %s"), value)
169
def context(context, value):
170
"""Returns dictionary version of context."""
171
LOG.debug(_("Received %s"), context)
172
return context.to_dict()
175
def echo_three_times(context, value):
177
context.reply(value + 1)
178
context.reply(value + 2)
181
def echo_three_times_yield(context, value):
187
def fail(context, value):
188
"""Raises an exception with the value sent in."""
189
raise Exception(value)