~danwent/nova/qmanager-new

« back to all changes in this revision

Viewing changes to nova/tests/test_rpc.py

  • Committer: Dan Wendlandt
  • Date: 2011-09-07 21:27:06 UTC
  • mfrom: (1476.2.54 nova)
  • Revision ID: dan@nicira.com-20110907212706-z0fm75250w0gqg5i
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
from nova import context
23
23
from nova import log as logging
24
24
from nova import rpc
25
 
from nova import test
 
25
from nova.tests import test_rpc_common
26
26
 
27
27
 
28
28
LOG = logging.getLogger('nova.tests.rpc')
29
29
 
30
30
 
31
 
class RpcTestCase(test.TestCase):
 
31
class RpcTestCase(test_rpc_common._BaseRpcTestCase):
32
32
    def setUp(self):
 
33
        self.rpc = rpc
33
34
        super(RpcTestCase, self).setUp()
34
 
        self.conn = rpc.create_connection(True)
35
 
        self.receiver = TestReceiver()
36
 
        self.consumer = rpc.create_consumer(self.conn,
37
 
                                            'test',
38
 
                                            self.receiver,
39
 
                                            False)
40
 
        self.consumer.attach_to_eventlet()
41
 
        self.context = context.get_admin_context()
42
 
 
43
 
    def test_call_succeed(self):
44
 
        value = 42
45
 
        result = rpc.call(self.context, 'test', {"method": "echo",
46
 
                                                 "args": {"value": value}})
47
 
        self.assertEqual(value, result)
48
 
 
49
 
    def test_call_succeed_despite_multiple_returns(self):
50
 
        value = 42
51
 
        result = rpc.call(self.context, 'test', {"method": "echo_three_times",
52
 
                                                 "args": {"value": value}})
53
 
        self.assertEqual(value + 2, result)
54
 
 
55
 
    def test_call_succeed_despite_multiple_returns_yield(self):
56
 
        value = 42
57
 
        result = rpc.call(self.context, 'test',
58
 
                          {"method": "echo_three_times_yield",
59
 
                           "args": {"value": value}})
60
 
        self.assertEqual(value + 2, result)
61
 
 
62
 
    def test_multicall_succeed_once(self):
63
 
        value = 42
64
 
        result = rpc.multicall(self.context,
65
 
                              'test',
66
 
                              {"method": "echo",
67
 
                               "args": {"value": value}})
68
 
        for i, x in enumerate(result):
69
 
            if i > 0:
70
 
                self.fail('should only receive one response')
71
 
            self.assertEqual(value + i, x)
72
 
 
73
 
    def test_multicall_succeed_three_times(self):
74
 
        value = 42
75
 
        result = rpc.multicall(self.context,
76
 
                              'test',
77
 
                              {"method": "echo_three_times",
78
 
                               "args": {"value": value}})
79
 
        for i, x in enumerate(result):
80
 
            self.assertEqual(value + i, x)
81
 
 
82
 
    def test_multicall_succeed_three_times_yield(self):
83
 
        value = 42
84
 
        result = rpc.multicall(self.context,
85
 
                              'test',
86
 
                              {"method": "echo_three_times_yield",
87
 
                               "args": {"value": value}})
88
 
        for i, x in enumerate(result):
89
 
            self.assertEqual(value + i, x)
90
 
 
91
 
    def test_context_passed(self):
92
 
        """Makes sure a context is passed through rpc call."""
93
 
        value = 42
94
 
        result = rpc.call(self.context,
95
 
                          'test', {"method": "context",
96
 
                                   "args": {"value": value}})
97
 
        self.assertEqual(self.context.to_dict(), result)
98
 
 
99
 
    def test_call_exception(self):
100
 
        """Test that exception gets passed back properly.
101
 
 
102
 
        rpc.call returns a RemoteError object.  The value of the
103
 
        exception is converted to a string, so we convert it back
104
 
        to an int in the test.
105
 
 
106
 
        """
107
 
        value = 42
108
 
        self.assertRaises(rpc.RemoteError,
109
 
                          rpc.call,
110
 
                          self.context,
111
 
                          'test',
112
 
                          {"method": "fail",
113
 
                           "args": {"value": value}})
114
 
        try:
115
 
            rpc.call(self.context,
116
 
                     'test',
117
 
                     {"method": "fail",
118
 
                      "args": {"value": value}})
119
 
            self.fail("should have thrown rpc.RemoteError")
120
 
        except rpc.RemoteError as exc:
121
 
            self.assertEqual(int(exc.value), value)
122
 
 
123
 
    def test_nested_calls(self):
124
 
        """Test that we can do an rpc.call inside another call."""
125
 
        class Nested(object):
126
 
            @staticmethod
127
 
            def echo(context, queue, value):
128
 
                """Calls echo in the passed queue"""
129
 
                LOG.debug(_("Nested received %(queue)s, %(value)s")
130
 
                        % locals())
131
 
                # TODO: so, it will replay the context and use the same REQID?
132
 
                # that's bizarre.
133
 
                ret = rpc.call(context,
134
 
                               queue,
135
 
                               {"method": "echo",
136
 
                                "args": {"value": value}})
137
 
                LOG.debug(_("Nested return %s"), ret)
138
 
                return value
139
 
 
140
 
        nested = Nested()
141
 
        conn = rpc.create_connection(True)
142
 
        consumer = rpc.create_consumer(conn,
143
 
                                       'nested',
144
 
                                       nested,
145
 
                                       False)
146
 
        consumer.attach_to_eventlet()
147
 
        value = 42
148
 
        result = rpc.call(self.context,
149
 
                          'nested', {"method": "echo",
150
 
                                     "args": {"queue": "test",
151
 
                                              "value": value}})
152
 
        self.assertEqual(value, result)
153
 
 
154
 
 
155
 
class TestReceiver(object):
156
 
    """Simple Proxy class so the consumer has methods to call.
157
 
 
158
 
    Uses static methods because we aren't actually storing any state.
159
 
 
160
 
    """
161
 
 
162
 
    @staticmethod
163
 
    def echo(context, value):
164
 
        """Simply returns whatever value is sent in."""
165
 
        LOG.debug(_("Received %s"), value)
166
 
        return value
167
 
 
168
 
    @staticmethod
169
 
    def context(context, value):
170
 
        """Returns dictionary version of context."""
171
 
        LOG.debug(_("Received %s"), context)
172
 
        return context.to_dict()
173
 
 
174
 
    @staticmethod
175
 
    def echo_three_times(context, value):
176
 
        context.reply(value)
177
 
        context.reply(value + 1)
178
 
        context.reply(value + 2)
179
 
 
180
 
    @staticmethod
181
 
    def echo_three_times_yield(context, value):
182
 
        yield value
183
 
        yield value + 1
184
 
        yield value + 2
185
 
 
186
 
    @staticmethod
187
 
    def fail(context, value):
188
 
        """Raises an exception with the value sent in."""
189
 
        raise Exception(value)
 
35
 
 
36
    def tearDown(self):
 
37
        super(RpcTestCase, self).tearDown()