This repository was archived by the owner on Jan 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathtest_tchannel.py
616 lines (459 loc) · 16.3 KB
/
test_tchannel.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# Copyright (c) 2016 Uber Technologies, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import json
import os
import socket
import subprocess
import textwrap
import psutil
import pytest
import tornado
from mock import MagicMock, patch, ANY
from tornado import gen
from tornado.netutil import bind_sockets
from tornado.tcpserver import TCPServer
from tchannel.tornado.stream import InMemStream
from tchannel import TChannel, Request, Response, schemes, errors, thrift
from tchannel.errors import AlreadyListeningError, TimeoutError
from tchannel.event import EventHook
from tchannel.response import TransportHeaders
from tchannel.tornado import connection
# TODO - need integration tests for timeout and retries, use testing.vcr
@pytest.fixture
def thrift_module(tmpdir, request):
thrift_file = tmpdir.join('service.thrift')
thrift_file.write('''
service Service {
bool healthy()
}
''')
return thrift.load(str(thrift_file), request.node.name)
@pytest.mark.call
def test_should_have_default_schemes():
tchannel = TChannel(name='test')
for f in schemes.DEFAULT_SCHEMES:
scheme = getattr(tchannel, f.NAME)
assert scheme, "default scheme not found"
assert isinstance(scheme, f)
@pytest.mark.gen_test
@pytest.mark.call
def test_call_should_get_response():
# Given this test server:
server = TChannel(name='server')
@server.register(scheme=schemes.RAW)
def endpoint(request):
assert isinstance(request, Request)
assert request.headers == b'req headers'
assert request.body == b'req body'
return Response(b'resp body', b'resp headers')
server.listen()
# Make a call:
tchannel = TChannel(name='client')
resp = yield tchannel.call(
scheme=schemes.RAW,
service='server',
arg1='endpoint',
arg2='req headers',
arg3='req body',
hostport=server.hostport,
)
# verify response
assert isinstance(resp, Response)
assert resp.headers == b'resp headers'
assert resp.body == b'resp body'
# verify response transport headers
assert isinstance(resp.transport, TransportHeaders)
assert resp.transport.scheme == schemes.RAW
assert resp.transport.failure_domain is None
@pytest.mark.gen_test
@pytest.mark.call
def test_timeout_should_raise_timeout_error():
# Given this test server:
server = TChannel(name='server')
@server.register(scheme=schemes.RAW)
@gen.coroutine
def endpoint(request):
yield gen.sleep(0.05)
raise gen.Return('hello')
server.listen()
# Make a call:
tchannel = TChannel(name='client')
# timeout is less than server, should timeout
with pytest.raises(TimeoutError):
yield tchannel.call(
scheme=schemes.RAW,
service='server',
arg1='endpoint',
hostport=server.hostport,
timeout=0.02,
)
# timeout is more than server, should not timeout
yield tchannel.raw(
service='server',
endpoint='endpoint',
hostport=server.hostport,
timeout=0.1,
)
def test_uninitialized_tchannel_is_fork_safe():
"""TChannel('foo') should not schedule any work on the io loop."""
process = psutil.Popen(
[
'python',
'-c',
textwrap.dedent(
"""
import os
from tchannel import TChannel
t = TChannel("app")
os.fork()
t.listen()
"""
),
],
stderr=subprocess.PIPE,
)
try:
stderr = process.stderr.read()
ret = process.wait()
assert ret == 0 and not stderr, stderr
finally:
if process.is_running():
process.kill()
@pytest.mark.gen_test
@pytest.mark.call
def test_headers_and_body_should_be_optional():
# Given this test server:
server = TChannel(name='server')
@server.register(scheme=schemes.RAW)
def endpoint(request):
# assert request.headers is None # TODO uncomment
# assert request.body is None # TODO uncomment
pass
server.listen()
# Make a call:
tchannel = TChannel(name='client')
resp = yield tchannel.call(
scheme=schemes.RAW,
service='server',
arg1='endpoint',
hostport=server.hostport,
)
# verify response
assert isinstance(resp, Response)
assert resp.headers == b'' # TODO should be None to match server
assert resp.body == b'' # TODO should be None to match server
@pytest.mark.gen_test
@pytest.mark.call
def test_endpoint_can_return_just_body():
# Given this test server:
server = TChannel(name='server')
@server.register(scheme=schemes.RAW)
def endpoint(request):
return 'resp body'
server.listen()
# Make a call:
tchannel = TChannel(name='client')
resp = yield tchannel.call(
scheme=schemes.RAW,
service='server',
arg1='endpoint',
hostport=server.hostport,
)
# verify response
assert isinstance(resp, Response)
assert resp.headers == b'' # TODO should be is None to match server
assert resp.body == b'resp body'
# TODO - verify register programmatic use cases
@pytest.mark.gen_test
@pytest.mark.call
def test_endpoint_can_be_called_as_a_pure_func():
# Given this test server:
server = TChannel(name='server')
@server.register(scheme=schemes.RAW)
def endpoint(request):
assert isinstance(request, Request)
assert request.body == b'req body'
assert request.headers == b'req headers'
return Response(b'resp body', headers=b'resp headers')
server.listen()
# Able to call over TChannel
tchannel = TChannel(name='client')
resp = yield tchannel.call(
scheme=schemes.RAW,
service='server',
arg1='endpoint',
arg2='req headers',
arg3='req body',
hostport=server.hostport,
)
assert isinstance(resp, Response)
assert resp.headers == b'resp headers'
assert resp.body == b'resp body'
# Able to call as function
resp = endpoint(Request(b'req body', headers=b'req headers'))
assert isinstance(resp, Response)
assert resp.headers == b'resp headers'
assert resp.body == b'resp body'
@pytest.mark.gen_test
@pytest.mark.call
def test_endpoint_not_found_with_raw_request():
server = TChannel(name='server')
server.listen()
tchannel = TChannel(name='client')
with pytest.raises(errors.BadRequestError) as e:
yield tchannel.raw(
service='server',
hostport=server.hostport,
endpoint='foo',
)
assert "Endpoint 'foo' is not defined" in e.value.args[0]
@pytest.mark.gen_test
@pytest.mark.call
def test_endpoint_not_found_with_json_request():
server = TChannel(name='server')
server.listen()
tchannel = TChannel(name='client')
with pytest.raises(errors.BadRequestError) as e:
yield tchannel.json(
service='server',
hostport=server.hostport,
endpoint='foo',
)
assert "Endpoint 'foo' is not defined" in e.value.args[0]
def test_event_hook_register():
server = TChannel(name='server')
mock_hook = MagicMock(spec=EventHook)
with (
patch(
'tchannel.event.EventRegistrar.register',
autospec=True,
)
) as mock_register:
server.hooks.register(mock_hook)
mock_register.called
@pytest.fixture
def router_file():
return os.path.join(os.path.dirname(os.path.realpath(__file__)),
'data/hosts.json')
@pytest.mark.gen_test
def test_advertise_should_take_a_router_file(router_file):
from tchannel.tornado.response import Response as TornadoResponse
tchannel = TChannel(name='client')
with open(router_file, 'r') as json_data:
routers = json.load(json_data)
with (
patch(
'tchannel.tornado.TChannel.advertise',
autospec=True,
)
) as mock_advertise:
f = gen.Future()
mock_advertise.return_value = f
f.set_result(TornadoResponse())
tchannel.advertise(router_file=router_file)
mock_advertise.assert_called_once_with(ANY, routers=routers,
name=ANY, timeout=ANY)
@pytest.mark.gen_test
def test_advertise_should_raise_on_invalid_router_file():
tchannel = TChannel(name='client')
with pytest.raises(IOError):
yield tchannel.advertise(router_file='?~~lala')
with pytest.raises(ValueError):
yield tchannel.advertise(routers='lala', router_file='?~~lala')
@pytest.mark.gen_test
def test_advertise_is_idempotent(router_file):
from tchannel.tornado.response import Response as TornadoResponse
def new_advertise(*args, **kwargs):
f = gen.Future()
f.set_result(TornadoResponse(
argstreams=[closed_stream(b'{}') for i in range(3)],
))
return f
tchannel = TChannel(name='client')
with patch(
'tchannel.tornado.TChannel.advertise', autospec=True
) as mock_advertise:
mock_advertise.side_effect = new_advertise
yield tchannel.advertise(router_file=router_file)
yield tchannel.advertise(router_file=router_file)
yield tchannel.advertise(router_file=router_file)
assert mock_advertise.call_count == 1
@pytest.mark.gen_test
def test_advertise_is_retryable(router_file):
from tchannel.tornado.response import Response as TornadoResponse
def new_advertise(*args, **kwargs):
f = gen.Future()
f.set_result(TornadoResponse(
argstreams=[closed_stream(b'{}') for i in range(3)],
))
return f
tchannel = TChannel(name='client')
with patch(
'tchannel.tornado.TChannel.advertise', autospec=True
) as mock_advertise:
f = gen.Future()
f.set_exception(Exception('great sadness'))
mock_advertise.return_value = f
with pytest.raises(Exception) as e:
yield tchannel.advertise(router_file=router_file)
assert 'great sadness' in str(e)
assert mock_advertise.call_count == 1
mock_advertise.side_effect = new_advertise
yield tchannel.advertise(router_file=router_file)
yield tchannel.advertise(router_file=router_file)
yield tchannel.advertise(router_file=router_file)
yield tchannel.advertise(router_file=router_file)
assert mock_advertise.call_count == 2
def closed_stream(body):
stream = InMemStream(body)
stream.close()
return stream
def test_listen_different_ports():
server = TChannel(name='test_server')
server.listen()
with pytest.raises(AlreadyListeningError):
server.listen(server.port + 1)
def test_listen_duplicate_ports():
server = TChannel(name='test_server')
server.listen()
server.listen()
server.listen(server.port)
server.listen()
@pytest.mark.skipif(
tuple(tornado.version.split('.')) < ('4', '3'),
reason='reuse_port is not supported in tornado < 4.3',
)
def test_reuse_port():
# start a tchannel w SO_REUSEPORT on
one = TChannel('holler', reuse_port=True)
one.listen()
# another one at the same address can reuse port
two = TChannel('back', hostport=one.hostport, reuse_port=True)
two.listen()
# if another tchannel w SO_REUSEPORT off listens, it blows up
with pytest.raises(socket.error):
three = TChannel('yall', hostport=one.hostport, reuse_port=False)
three.listen()
def test_close_stops_listening():
server = TChannel(name='server')
server.listen()
host = server.host
port = server.port
# Can connect
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
sock.close()
server.close()
# Can't connect
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
with pytest.raises(socket.error):
sock.connect((host, port))
def test_hostport_gets_set():
tchannel = TChannel(name='holler')
tchannel.listen()
host, port = tchannel.hostport.split(':')
assert tchannel.host == host
assert tchannel.port == int(port)
@pytest.mark.gen_test
def test_response_status_is_copied():
server = TChannel(name='server')
server.listen()
@server.register(TChannel.FALLBACK)
def handler(request):
return Response(
status=1,
headers=b'\x00\x00',
body=b'\x00',
)
client = TChannel(name='client', known_peers=[server.hostport])
response = yield client.call(
scheme='thrift',
service='server',
arg1='hello',
arg2=b'\x00\x00',
arg3=b'\x00',
)
assert 1 == response.status
@pytest.mark.gen_test
def test_per_request_caller_name_raw():
server = TChannel('server')
server.listen()
@server.raw.register('foo')
def handler(request):
assert request.transport.caller_name == 'bar'
return b'success'
client = TChannel('client', known_peers=[server.hostport])
res = yield client.raw('service', 'foo', b'', caller_name='bar')
assert res.body == b'success'
@pytest.mark.gen_test
def test_per_request_caller_name_json():
server = TChannel('server')
server.listen()
@server.json.register('foo')
def handler(request):
assert request.transport.caller_name == 'bar'
return {'success': True}
client = TChannel('client', known_peers=[server.hostport])
res = yield client.json('service', 'foo', {}, caller_name='bar')
assert res.body == {'success': True}
@pytest.mark.gen_test
def test_per_request_caller_name_thrift(thrift_module):
server = TChannel('server')
server.listen()
@server.thrift.register(thrift_module.Service)
def healthy(request):
assert request.transport.caller_name == 'bar'
return True
client = TChannel('client', known_peers=[server.hostport])
res = yield client.thrift(
thrift_module.Service.healthy(), caller_name='bar',
)
assert res.body is True
@pytest.mark.parametrize("name", [None, ""])
def test_service_name_is_required(name):
with pytest.raises(errors.ServiceNameIsRequiredError) as exc_info:
TChannel(name)
assert 'service name cannot be empty or None' in str(exc_info)
@pytest.mark.gen_test
def test_timeout_during_handshake_is_retried(timeout_server):
tchannel = TChannel(name='client', known_peers=[timeout_server])
# We want the client to look for other peers if an INIT times out rather
# than raising a timeout error so we expect a NoAvailablePeerError here.
with patch.object(connection, 'DEFAULT_INIT_TIMEOUT_SECS', 0.1):
with pytest.raises(errors.NoAvailablePeerError):
yield tchannel.raw(service='server', endpoint='endpoint')
@pytest.yield_fixture
def timeout_server():
class HandshakeTimeoutServer(TCPServer):
def handle_stream(self, stream, address):
return gen.sleep(10)
sockets = bind_sockets(port=0, family=socket.AF_INET)
server = HandshakeTimeoutServer()
server.add_sockets(sockets)
port = sockets[0].getsockname()[1]
try:
yield ('127.0.0.1:%d' % port)
finally:
for s in sockets:
s.close()