/
test_serialization.py
443 lines (320 loc) · 12.1 KB
/
test_serialization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import json
import uuid
import pytest
import yaml
from kombu import Exchange, Queue
from mock import Mock, call
from nameko.constants import (
ACCEPT_CONFIG_KEY, SERIALIZER_CONFIG_KEY, SERIALIZERS_CONFIG_KEY
)
from nameko.events import EventDispatcher, event_handler
from nameko.exceptions import ConfigurationError, RemoteError
from nameko.messaging import consume
from nameko.rpc import RpcProxy, rpc
from nameko.standalone.rpc import ServiceRpcProxy
from nameko.testing.services import entrypoint_hook, entrypoint_waiter
entrypoint_called = Mock()
test_data = {
"hello": ("world",),
123: 456,
'abc': [7, 8, 9],
'foobar': 1.5,
}
serialized_info = {
'json': {
'content_type': 'application/json',
'content_encoding': 'utf-8',
'data': json.loads(json.dumps(test_data))
},
'pickle': {
'content_type': 'application/x-python-serialize',
'content_encoding': 'binary',
'data': test_data
}
}
unserializable = object()
class Service(object):
name = "service"
@rpc
def echo(self, arg):
entrypoint_called(arg)
return arg
@rpc
def broken(self):
return unserializable
@pytest.fixture
def sniffer_queue_factory(rabbit_config, rabbit_manager):
""" Return a function that creates message queues to 'sniff' messages
published to exchanges.
"""
vhost = rabbit_config['vhost']
def make(exchange, routing_key="*"):
""" Create a uniquely named queue and bind it to an exchange so that
it collects messages published to that exchange.
:Parameters:
exchange : str
Name of the exchange to bind to
routing_key : str
Routing key to bind with
:Returns:
A function that returns all the messages received by the queue
"""
queue_name = "sniffer_{}".format(uuid.uuid4())
rabbit_manager.create_queue(vhost, queue_name, auto_delete=True)
rabbit_manager.create_queue_binding(
vhost, exchange, queue_name, routing_key=routing_key)
def get_messages():
""" Return all messages received by the sniffer queue and remove
them from the queue.
"""
return rabbit_manager.get_messages(vhost, queue_name)
return get_messages
return make
@pytest.mark.parametrize("serializer", ['json', 'pickle'])
def test_rpc_serialization(container_factory, rabbit_config,
sniffer_queue_factory, serializer):
config = rabbit_config
config[SERIALIZER_CONFIG_KEY] = serializer
container = container_factory(Service, config)
container.start()
get_messages = sniffer_queue_factory('nameko-rpc')
serialized = serialized_info[serializer]
with ServiceRpcProxy('service', rabbit_config) as proxy:
assert proxy.echo(test_data) == serialized['data']
assert entrypoint_called.call_args == call(serialized['data'])
msg = get_messages()[0]
assert msg['properties']['content_type'] == serialized['content_type']
def test_rpc_result_serialization_error(container_factory, rabbit_config):
container = container_factory(Service, rabbit_config)
container.start()
with ServiceRpcProxy('service', rabbit_config) as proxy:
with pytest.raises(RemoteError) as exc:
proxy.broken()
assert exc.value.exc_type == "UnserializableValueError"
assert proxy.echo('foo') == "foo" # subsequent calls ok
def test_rpc_arg_serialization_error(container_factory, rabbit_config):
container = container_factory(Service, rabbit_config)
container.start()
with ServiceRpcProxy('service', rabbit_config) as proxy:
with pytest.raises(Exception):
proxy.echo(unserializable)
assert proxy.echo('foo') == "foo" # subsequent calls ok
@pytest.mark.parametrize("serializer", ['json', 'pickle'])
def test_event_serialization(
container_factory, rabbit_config, sniffer_queue_factory, serializer
):
handler_called = Mock()
class Service(object):
name = "srcservice"
dispatch = EventDispatcher()
@rpc
def dispatch_event(self, payload):
self.dispatch("eventtype", payload)
@event_handler('srcservice', 'eventtype')
def handle_event(self, event_data):
handler_called(event_data)
rabbit_config[SERIALIZER_CONFIG_KEY] = serializer
container = container_factory(Service, rabbit_config)
container.start()
get_messages = sniffer_queue_factory(
"srcservice.events", routing_key="eventtype")
serialized = serialized_info[serializer]
# dispatch an event with a tuple payload
with entrypoint_waiter(container, "handle_event"):
with entrypoint_hook(container, "dispatch_event") as dispatch_event:
dispatch_event(test_data)
# verify data serialized to expected value
assert handler_called.call_args == call(serialized['data'])
# verify sniffed messages serialized as expected
msg = get_messages()[0]
assert msg['properties']['content_type'] == serialized['content_type']
def upperjson_encode(value):
value = json.dumps(value)
return value.upper()
def upperjson_decode(value):
value = value.lower()
return json.loads(value)
def test_custom_serializer(container_factory, rabbit_config,
sniffer_queue_factory):
class Service(object):
name = "service"
@rpc
def echo(self, arg):
return arg
rabbit_config[SERIALIZER_CONFIG_KEY] = "upperjson"
rabbit_config[SERIALIZERS_CONFIG_KEY] = {
'upperjson': {
'encoder': 'test.test_serialization.upperjson_encode',
'decoder': 'test.test_serialization.upperjson_decode',
'content_type': 'application/x-upper-json'
}
}
container = container_factory(Service, rabbit_config)
container.start()
get_messages = sniffer_queue_factory('nameko-rpc')
# verify RPC works end-to-end
with ServiceRpcProxy('service', rabbit_config) as proxy:
assert proxy.echo("hello") == "hello"
# verify sniffed messages serialized as expected
msg = get_messages()[0]
assert '"RESULT": "HELLO"' in msg['payload']
assert msg['properties']['content_type'] == "application/x-upper-json"
@pytest.mark.parametrize(
'config',
(
{SERIALIZER_CONFIG_KEY: 'unknown'},
{ACCEPT_CONFIG_KEY: ['json', 'unknown']},
{
SERIALIZER_CONFIG_KEY: 'json',
ACCEPT_CONFIG_KEY: ['json', 'unknown'],
},
)
)
def test_missing_serializers(container_factory, rabbit_config, config):
rabbit_config.update(config)
with pytest.raises(ConfigurationError) as exc:
container_factory(Service, rabbit_config)
assert (
str(exc.value) ==
'Please register a serializer for "unknown" format')
@pytest.mark.parametrize(
'content_type, encode',
(
('application/json', json.dumps),
('application/x-yaml', yaml.dump),
)
)
def test_consumer_accepts_multiple_serialization_formats(
container_factory, rabbit_config, rabbit_manager, content_type, encode
):
class Service(object):
name = 'service'
@consume(queue=Queue(exchange=Exchange('spam'), name='some-queue'))
def consume(self, payload):
assert payload == {'spam': 'ham'}
rabbit_config[ACCEPT_CONFIG_KEY] = ['json', 'yaml']
container = container_factory(Service, rabbit_config)
container.start()
payload = {'spam': 'ham'}
with entrypoint_waiter(container, 'consume'):
rabbit_manager.publish(
rabbit_config['vhost'], 'spam', '', encode(payload),
properties={'content_type': content_type})
@pytest.mark.parametrize(
'serializer, content_type, encode',
(
('json', 'application/json', json.dumps),
('yaml', 'application/x-yaml', yaml.dump),
)
)
def test_standalone_rpc_accepts_multiple_serialization_formats(
container_factory, rabbit_config, rabbit_manager,
sniffer_queue_factory, serializer, content_type, encode
):
called = Mock()
class EchoingService(object):
name = 'echoer'
@rpc
def echo(self, payload):
called(payload)
return payload
echoer_config = rabbit_config.copy()
echoer_config[SERIALIZER_CONFIG_KEY] = 'json'
echoer_config[ACCEPT_CONFIG_KEY] = ['json', 'yaml']
echoer = container_factory(EchoingService, echoer_config)
echoer.start()
payload = {'spam': 'ham'}
proxy_config = rabbit_config.copy()
proxy_config[SERIALIZER_CONFIG_KEY] = serializer
get_messages = sniffer_queue_factory('nameko-rpc')
with ServiceRpcProxy('echoer', proxy_config) as proxy:
assert proxy.echo(payload) == payload
msg = get_messages().pop()
assert encode(payload) in msg['payload']
assert msg['properties']['content_type'] == content_type
assert called.mock_calls == [call(payload)]
@pytest.mark.parametrize(
'serializer, content_type, encode',
(
('json', 'application/json', json.dumps),
('yaml', 'application/x-yaml', yaml.dump),
)
)
def test_rpc_accepts_multiple_serialization_formats(
container_factory, rabbit_config, rabbit_manager,
sniffer_queue_factory, serializer, content_type, encode
):
called = Mock()
class ForwardingService(object):
name = 'forwarder'
echoer = RpcProxy('echoer')
@rpc
def forward(self, payload):
return self.echoer.echo(payload)
class EchoingService(object):
name = 'echoer'
@rpc
def echo(self, payload):
called(payload)
return payload
echoer_config = rabbit_config.copy()
echoer_config[SERIALIZER_CONFIG_KEY] = 'json'
echoer_config[ACCEPT_CONFIG_KEY] = ['json', 'yaml']
echoer = container_factory(EchoingService, echoer_config)
echoer.start()
payload = {'spam': 'ham'}
forwarder_config = rabbit_config.copy()
forwarder_config[SERIALIZER_CONFIG_KEY] = serializer
forwarder = container_factory(ForwardingService, forwarder_config)
forwarder.start()
get_messages = sniffer_queue_factory('nameko-rpc')
with entrypoint_hook(forwarder, 'forward') as echo:
assert echo(payload) == payload
msg = get_messages().pop()
assert encode(payload) in msg['payload']
assert msg['properties']['content_type'] == content_type
assert called.mock_calls == [call(payload)]
@pytest.mark.parametrize(
'serializer, content_type, encode',
(
('json', 'application/json', json.dumps),
('yaml', 'application/x-yaml', yaml.dump),
)
)
def test_events_accepts_multiple_serialization_formats(
container_factory, rabbit_config, rabbit_manager,
sniffer_queue_factory, serializer, content_type, encode
):
called = Mock()
class PublishingService(object):
name = 'publisher'
dispatch = EventDispatcher()
@rpc
def dispatch_event(self, payload):
self.dispatch('spam', payload)
class ConsumingService(object):
name = 'consumer'
@event_handler('publisher', 'spam')
def handle_event(self, event_data):
called(event_data)
def publish(consumer, publisher):
with entrypoint_waiter(consumer, "handle_event"):
with entrypoint_hook(publisher, "dispatch_event") as dispatch:
dispatch(payload)
consumer_config = rabbit_config.copy()
consumer_config[SERIALIZER_CONFIG_KEY] = 'json'
consumer_config[ACCEPT_CONFIG_KEY] = ['json', 'yaml']
consumer = container_factory(ConsumingService, consumer_config)
consumer.start()
get_messages = sniffer_queue_factory(
'publisher.events', routing_key='spam')
payload = {'spam': 'ham'}
publisher_config = rabbit_config.copy()
publisher_config[SERIALIZER_CONFIG_KEY] = serializer
publisher = container_factory(PublishingService, publisher_config)
publisher.start()
publish(consumer, publisher)
msg = get_messages().pop()
assert encode(payload) in msg['payload']
assert msg['properties']['content_type'] == content_type
assert called.mock_calls == [call(payload)]