Skip to content
This repository was archived by the owner on Dec 10, 2018. It is now read-only.

Commit 50fa59f

Browse files
committed
Merge pull request #93 from maralla/cytransport
refactored transport
2 parents ce512b3 + 61ac867 commit 50fa59f

20 files changed

+953
-499
lines changed

setup.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
# cython detection
3434
try:
35-
from Cython.Distutils import build_ext
35+
from Cython.Build import cythonize
3636
CYTHON = True
3737
except ImportError:
3838
CYTHON = False
@@ -46,16 +46,19 @@
4646
# only build ext in CPython
4747
if not PYPY:
4848
if CYTHON:
49-
ext_modules.append(Extension("thriftpy.transport.cytransport",
50-
["thriftpy/transport/cytransport.pyx"]))
51-
ext_modules.append(Extension("thriftpy.protocol.cybin",
52-
["thriftpy/protocol/cybin/cybin.pyx"]))
53-
cmdclass["build_ext"] = build_ext
54-
else:
55-
ext_modules.append(Extension("thriftpy.transport.cytransport",
56-
["thriftpy/transport/cytransport.c"]))
57-
ext_modules.append(Extension("thriftpy.protocol.cybin",
58-
["thriftpy/protocol/cybin/cybin.c"]))
49+
cythonize("thriftpy/transport/**/*.pyx")
50+
cythonize("thriftpy/protocol/cybin/cybin.pyx")
51+
52+
ext_modules.append(Extension("thriftpy.transport.cybase",
53+
["thriftpy/transport/cybase.c"]))
54+
ext_modules.append(Extension("thriftpy.transport.buffered.cybuffered",
55+
["thriftpy/transport/buffered/cybuffered.c"]))
56+
ext_modules.append(Extension("thriftpy.transport.memory.cymemory",
57+
["thriftpy/transport/memory/cymemory.c"]))
58+
ext_modules.append(Extension("thriftpy.transport.framed.cyframed",
59+
["thriftpy/transport/framed/cyframed.c"]))
60+
ext_modules.append(Extension("thriftpy.protocol.cybin",
61+
["thriftpy/protocol/cybin/cybin.c"]))
5962

6063
setup(name="thriftpy",
6164
version=version,

tests/test_buffered_transport.py

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from __future__ import absolute_import
4+
5+
import logging
6+
import multiprocessing
7+
import time
8+
9+
from os import path
10+
from unittest import TestCase
11+
12+
import thriftpy
13+
from thriftpy.rpc import client_context, make_server
14+
from thriftpy.transport.buffered import TBufferedTransportFactory
15+
from thriftpy.protocol.binary import TBinaryProtocolFactory
16+
17+
from thriftpy._compat import CYTHON
18+
logging.basicConfig(level=logging.INFO)
19+
20+
addressbook = thriftpy.load(path.join(path.dirname(__file__),
21+
"addressbook.thrift"))
22+
23+
24+
class Dispatcher(object):
25+
def __init__(self):
26+
self.registry = {}
27+
28+
def add(self, person):
29+
"""
30+
bool add(1: Person person);
31+
"""
32+
if person.name in self.registry:
33+
return False
34+
self.registry[person.name] = person
35+
return True
36+
37+
def get(self, name):
38+
"""
39+
Person get(1: string name)
40+
"""
41+
if name not in self.registry:
42+
raise addressbook.PersonNotExistsError()
43+
return self.registry[name]
44+
45+
46+
class BufferedTransportTestCase(TestCase):
47+
TRANSPORT_FACTORY = TBufferedTransportFactory()
48+
PROTOCOL_FACTORY = TBinaryProtocolFactory()
49+
50+
PORT = 50001
51+
52+
def mk_server(self):
53+
server = make_server(addressbook.AddressBookService, Dispatcher(),
54+
host="localhost", port=self.PORT,
55+
proto_factory=self.PROTOCOL_FACTORY,
56+
trans_factory=self.TRANSPORT_FACTORY)
57+
p = multiprocessing.Process(target=server.serve)
58+
return p
59+
60+
def client(self):
61+
return client_context(addressbook.AddressBookService,
62+
host="localhost", port=self.PORT,
63+
proto_factory=self.PROTOCOL_FACTORY,
64+
trans_factory=self.TRANSPORT_FACTORY)
65+
66+
def setUp(self):
67+
self.server = self.mk_server()
68+
self.server.start()
69+
time.sleep(0.3)
70+
71+
def tearDown(self):
72+
if self.server.is_alive():
73+
self.server.terminate()
74+
75+
def test_able_to_communicate(self):
76+
dennis = addressbook.Person(name='Dennis Ritchie')
77+
with self.client() as c:
78+
success = c.add(dennis)
79+
assert success
80+
81+
success = c.add(dennis)
82+
assert not success
83+
84+
def test_zero_length_string(self):
85+
dennis = addressbook.Person(name='')
86+
with self.client() as c:
87+
success = c.add(dennis)
88+
assert success
89+
success = c.get(name='')
90+
assert success
91+
92+
93+
if CYTHON:
94+
from thriftpy.transport.buffered import TCyBufferedTransportFactory
95+
from thriftpy.protocol.cybin import TCyBinaryProtocolFactory
96+
97+
class TCyBufferedTransportTestCase(BufferedTransportTestCase):
98+
TRANSPORT_FACTORY = TCyBufferedTransportFactory()
99+
PROTOCOL_FACTORY = TCyBinaryProtocolFactory()

tests/test_framed_transport.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
import thriftpy
1616
from thriftpy.tornado import make_server
1717
from thriftpy.rpc import make_client
18+
from thriftpy.transport.framed import TFramedTransportFactory
1819
from thriftpy.protocol.binary import TBinaryProtocolFactory
19-
from thriftpy.transport.transport import TFramedTransportFactory
2020

2121
from thriftpy._compat import CYTHON
2222
logging.basicConfig(level=logging.INFO)
@@ -49,8 +49,8 @@ def get(self, name):
4949

5050

5151
class FramedTransportTestCase(TestCase):
52-
PROTOCOL_FACTORY = TBinaryProtocolFactory()
5352
TRANSPORT_FACTORY = TFramedTransportFactory()
53+
PROTOCOL_FACTORY = TBinaryProtocolFactory()
5454

5555
def mk_server(self):
5656
self.io_loop = ioloop.IOLoop()
@@ -99,8 +99,8 @@ def test_zero_length_string(self):
9999

100100

101101
if CYTHON:
102+
from thriftpy.transport.framed import TCyFramedTransportFactory
102103
from thriftpy.protocol.cybin import TCyBinaryProtocolFactory
103-
from thriftpy.transport.cytransport import TCyFramedTransportFactory
104104

105105
class CyFramedTransportTestCase(FramedTransportTestCase):
106106
PROTOCOL_FACTORY = TCyBinaryProtocolFactory()

tests/test_memory_transport.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from unittest import TestCase
2+
3+
from thriftpy.transport.memory import TMemoryBuffer
4+
from thriftpy._compat import CYTHON
5+
6+
7+
class MemoryTransport(TestCase):
8+
@staticmethod
9+
def trans(data=b'', *args, **kwargs):
10+
return TMemoryBuffer(data)
11+
12+
def test_write(self):
13+
m = self.trans()
14+
m.write(b"hello world")
15+
16+
assert b"hello world" == m.getvalue()
17+
18+
def test_read(self):
19+
m = self.trans(b"hello world")
20+
b = m.read(5)
21+
22+
assert b"hello" == b
23+
24+
25+
if CYTHON:
26+
from thriftpy.transport.memory import TCyMemoryBuffer
27+
28+
class CyMemoryTransport(MemoryTransport):
29+
@staticmethod
30+
def trans(*args, **kwargs):
31+
return TCyMemoryBuffer(*args, **kwargs)
32+
33+
def test_write_move(self):
34+
m = self.trans(buf_size=10)
35+
m.write(b"helloworld")
36+
37+
m.read(6)
38+
assert b"orld" == m.getvalue()
39+
40+
m.write(b"he")
41+
assert b"orldhe" == m.getvalue()
42+
43+
def test_write_grow(self):
44+
m = self.trans(buf_size=10)
45+
m.write(b"hello world")
46+
assert b"hello world" == m.getvalue()
47+
48+
m.read(5)
49+
m.write(b"hello ")
50+
assert b" worldhello " == m.getvalue()
51+
52+
def test_write_move_grow(self):
53+
m = self.trans(buf_size=10)
54+
m.write(b"helloworld")
55+
56+
m.read(6)
57+
m.write(b"hellowaaa")
58+
assert b"orldhellowaaa" == m.getvalue()
59+
60+
def test_read(self):
61+
m = self.trans(b"hello world")
62+
b = m.read(5)
63+
64+
assert b"hello" == b
65+
assert b" world" == m.getvalue()

0 commit comments

Comments
 (0)