forked from Thriftpy/thriftpy2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_rpc.py
338 lines (243 loc) · 8.7 KB
/
test_rpc.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
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import multiprocessing
import os
import socket
import ssl
import sys
import time
import pytest
import thriftpy2
thriftpy2.install_import_hook()
from thriftpy2.rpc import client_context, make_server # noqa
from thriftpy2.thrift import TApplicationException # noqa
from thriftpy2.transport import TTransportException # noqa
if sys.platform == "win32":
pytest.skip("requires unix domain socket", allow_module_level=True)
addressbook = thriftpy2.load(os.path.join(os.path.dirname(__file__),
"addressbook.thrift"))
unix_sock = "/tmp/thriftpy_test.sock"
SSL_PORT = 50441
class Dispatcher(object):
def __init__(self):
self.ab = addressbook.AddressBook()
self.ab.people = {}
def ping(self):
return True
def hello(self, name):
return "hello " + name
def add(self, person):
self.ab.people[person.name] = person
return True
def remove(self, name):
try:
self.ab.people.pop(name)
return True
except KeyError:
raise addressbook.PersonNotExistsError(
"{} not exists".format(name))
def get(self, name):
try:
return self.ab.people[name]
except KeyError:
raise addressbook.PersonNotExistsError(
"{} not exists".format(name))
def book(self):
return self.ab
def get_phonenumbers(self, name, count):
p = [self.ab.people[name].phones[0]] if name in self.ab.people else []
return p * count
def get_phones(self, name):
phone_numbers = self.ab.people[name].phones
return dict((p.type, p.number) for p in phone_numbers)
def sleep(self, ms):
time.sleep(ms / 1000.0)
return True
def close(self, ms):
return
def raises(self, msg):
raise TApplicationException(message=msg)
@pytest.fixture(scope="module")
def server(request):
server = make_server(addressbook.AddressBookService, Dispatcher(),
unix_socket=unix_sock)
ps = multiprocessing.Process(target=server.serve)
ps.start()
time.sleep(0.1)
def fin():
if ps.is_alive():
ps.terminate()
try:
os.remove(unix_sock)
except IOError:
pass
request.addfinalizer(fin)
@pytest.fixture(scope="module")
def ipv6_server(request):
server = make_server(addressbook.AddressBookService, Dispatcher(),
unix_socket=unix_sock, socket_family=socket.AF_INET6)
ps = multiprocessing.Process(target=server.serve)
ps.start()
time.sleep(0.1)
def fin():
if ps.is_alive():
ps.terminate()
try:
os.remove(unix_sock)
except IOError:
pass
request.addfinalizer(fin)
@pytest.fixture(scope="module")
def ssl_server(request):
ssl_server = make_server(addressbook.AddressBookService, Dispatcher(),
host='localhost', port=SSL_PORT,
certfile="ssl/server.pem")
ps = multiprocessing.Process(target=ssl_server.serve)
ps.start()
time.sleep(0.1)
def fin():
if ps.is_alive():
ps.terminate()
request.addfinalizer(fin)
@pytest.fixture(scope="module")
def person():
phone1 = addressbook.PhoneNumber()
phone1.type = addressbook.PhoneType.MOBILE
phone1.number = '555-1212'
phone2 = addressbook.PhoneNumber()
phone2.type = addressbook.PhoneType.HOME
phone2.number = '555-1234'
# empty struct
phone3 = addressbook.PhoneNumber()
alice = addressbook.Person()
alice.name = "Alice"
alice.phones = [phone1, phone2, phone3]
alice.created_at = int(time.time())
return alice
def client(timeout=3000):
return client_context(addressbook.AddressBookService,
socket_timeout=timeout,
connect_timeout=timeout,
unix_socket=unix_sock)
def ipv6_client(timeout=3000):
return client_context(addressbook.AddressBookService,
socket_timeout=timeout,
connect_timeout=timeout,
unix_socket=unix_sock,
socket_family=socket.AF_INET6)
def ssl_client(timeout=3000):
return client_context(addressbook.AddressBookService,
host='localhost', port=SSL_PORT,
socket_timeout=timeout,
connect_timeout=timeout,
cafile="ssl/CA.pem", certfile="ssl/client.crt",
keyfile="ssl/client.key")
def ssl_client_with_url(timeout=3000):
return client_context(addressbook.AddressBookService,
url="thrift://localhost:{port}".format(
port=SSL_PORT),
socket_timeout=timeout,
connect_timeout=timeout,
cafile="ssl/CA.pem",
certfile="ssl/client.crt",
keyfile="ssl/client.key")
def test_clients(ssl_server):
with ssl_client() as c1, ssl_client_with_url() as c2:
assert c1.hello("world") == c2.hello("world")
def test_void_api(server):
with client() as c:
assert c.ping() is None
def test_ipv6_api(ipv6_server):
with ipv6_client() as c:
assert c.ping() is None
def test_void_api_with_ssl(ssl_server):
with ssl_client() as c:
assert c.ping() is None
def test_string_api(server):
with client() as c:
assert c.hello("world") == "hello world"
def test_required_argument(server):
with client() as c:
assert c.hello("") == "hello "
with pytest.raises(TApplicationException):
c.hello()
def test_string_api_with_ssl(ssl_server):
with ssl_client() as c:
assert c.hello("world") == "hello world"
def test_huge_res(server):
with client() as c:
big_str = "world" * 100000
assert c.hello(big_str) == "hello " + big_str
def test_huge_res_with_ssl(ssl_server):
with ssl_client() as c:
big_str = "world" * 100000
assert c.hello(big_str) == "hello " + big_str
def test_tstruct_req(person):
with client() as c:
assert c.add(person) is True
def test_tstruct_req_with_ssl(person):
with ssl_client() as c:
assert c.add(person) is True
def test_tstruct_res(person):
with client() as c:
assert person == c.get("Alice")
def test_tstruct_res_with_ssl(person):
with ssl_client() as c:
assert person == c.get("Alice")
def test_complex_tstruct():
with client() as c:
assert len(c.get_phonenumbers("Alice", 0)) == 0
assert len(c.get_phonenumbers("Alice", 1000)) == 1000
def test_complex_tstruct_with_ssl():
with ssl_client() as c:
assert len(c.get_phonenumbers("Alice", 0)) == 0
assert len(c.get_phonenumbers("Alice", 1000)) == 1000
def test_exception():
with pytest.raises(addressbook.PersonNotExistsError):
with client() as c:
c.remove("Bob")
def test_exception_with_ssl():
with pytest.raises(addressbook.PersonNotExistsError):
with ssl_client() as c:
c.remove("Bob")
def test_client_timeout():
with pytest.raises(socket.timeout):
with client(timeout=500) as c:
c.sleep(1000)
def test_client_socket_timeout():
with pytest.raises(socket.timeout):
with client_context(addressbook.AddressBookService,
unix_socket=unix_sock,
socket_timeout=500) as c:
c.sleep(1000)
def test_client_connect_timeout():
with pytest.raises(TTransportException):
with client_context(addressbook.AddressBookService,
unix_socket='/tmp/test.sock',
connect_timeout=1000) as c:
c.hello('test')
def test_ssl_client_timeout():
errors = (socket.timeout,)
# SSL socket timeout raises socket.timeout since Python 3.2.
# http://bugs.python.org/issue10272
# Newer versions of PyPy2 also implement this change, so
# always catch both errors
try:
errors += (getattr(ssl, 'SSLError'),)
except AttributeError:
pass
with pytest.raises(errors):
with ssl_client(timeout=500) as c:
c.sleep(1000)
def test_close_method():
with client() as c:
c.tclose(1)
def test_raises_method():
with client() as c:
try:
c.raises("foobarbaz")
except TApplicationException as e:
assert e.type == 0
assert e.message == "foobarbaz"
else:
raise ValueError("TApplicationException not raised")