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

Commit

Permalink
refactored transport
Browse files Browse the repository at this point in the history
  • Loading branch information
maralla committed Mar 5, 2015
1 parent ce512b3 commit 61ac867
Show file tree
Hide file tree
Showing 20 changed files with 953 additions and 499 deletions.
25 changes: 14 additions & 11 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

# cython detection
try:
from Cython.Distutils import build_ext
from Cython.Build import cythonize
CYTHON = True
except ImportError:
CYTHON = False
Expand All @@ -46,16 +46,19 @@
# only build ext in CPython
if not PYPY:
if CYTHON:
ext_modules.append(Extension("thriftpy.transport.cytransport",
["thriftpy/transport/cytransport.pyx"]))
ext_modules.append(Extension("thriftpy.protocol.cybin",
["thriftpy/protocol/cybin/cybin.pyx"]))
cmdclass["build_ext"] = build_ext
else:
ext_modules.append(Extension("thriftpy.transport.cytransport",
["thriftpy/transport/cytransport.c"]))
ext_modules.append(Extension("thriftpy.protocol.cybin",
["thriftpy/protocol/cybin/cybin.c"]))
cythonize("thriftpy/transport/**/*.pyx")
cythonize("thriftpy/protocol/cybin/cybin.pyx")

ext_modules.append(Extension("thriftpy.transport.cybase",
["thriftpy/transport/cybase.c"]))
ext_modules.append(Extension("thriftpy.transport.buffered.cybuffered",
["thriftpy/transport/buffered/cybuffered.c"]))
ext_modules.append(Extension("thriftpy.transport.memory.cymemory",
["thriftpy/transport/memory/cymemory.c"]))
ext_modules.append(Extension("thriftpy.transport.framed.cyframed",
["thriftpy/transport/framed/cyframed.c"]))
ext_modules.append(Extension("thriftpy.protocol.cybin",
["thriftpy/protocol/cybin/cybin.c"]))

setup(name="thriftpy",
version=version,
Expand Down
99 changes: 99 additions & 0 deletions tests/test_buffered_transport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# -*- coding: utf-8 -*-

from __future__ import absolute_import

import logging
import multiprocessing
import time

from os import path
from unittest import TestCase

import thriftpy
from thriftpy.rpc import client_context, make_server
from thriftpy.transport.buffered import TBufferedTransportFactory
from thriftpy.protocol.binary import TBinaryProtocolFactory

from thriftpy._compat import CYTHON
logging.basicConfig(level=logging.INFO)

addressbook = thriftpy.load(path.join(path.dirname(__file__),
"addressbook.thrift"))


class Dispatcher(object):
def __init__(self):
self.registry = {}

def add(self, person):
"""
bool add(1: Person person);
"""
if person.name in self.registry:
return False
self.registry[person.name] = person
return True

def get(self, name):
"""
Person get(1: string name)
"""
if name not in self.registry:
raise addressbook.PersonNotExistsError()
return self.registry[name]


class BufferedTransportTestCase(TestCase):
TRANSPORT_FACTORY = TBufferedTransportFactory()
PROTOCOL_FACTORY = TBinaryProtocolFactory()

PORT = 50001

def mk_server(self):
server = make_server(addressbook.AddressBookService, Dispatcher(),
host="localhost", port=self.PORT,
proto_factory=self.PROTOCOL_FACTORY,
trans_factory=self.TRANSPORT_FACTORY)
p = multiprocessing.Process(target=server.serve)
return p

def client(self):
return client_context(addressbook.AddressBookService,
host="localhost", port=self.PORT,
proto_factory=self.PROTOCOL_FACTORY,
trans_factory=self.TRANSPORT_FACTORY)

def setUp(self):
self.server = self.mk_server()
self.server.start()
time.sleep(0.3)

def tearDown(self):
if self.server.is_alive():
self.server.terminate()

def test_able_to_communicate(self):
dennis = addressbook.Person(name='Dennis Ritchie')
with self.client() as c:
success = c.add(dennis)
assert success

success = c.add(dennis)
assert not success

def test_zero_length_string(self):
dennis = addressbook.Person(name='')
with self.client() as c:
success = c.add(dennis)
assert success
success = c.get(name='')
assert success


if CYTHON:
from thriftpy.transport.buffered import TCyBufferedTransportFactory
from thriftpy.protocol.cybin import TCyBinaryProtocolFactory

class TCyBufferedTransportTestCase(BufferedTransportTestCase):
TRANSPORT_FACTORY = TCyBufferedTransportFactory()
PROTOCOL_FACTORY = TCyBinaryProtocolFactory()
6 changes: 3 additions & 3 deletions tests/test_framed_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import thriftpy
from thriftpy.tornado import make_server
from thriftpy.rpc import make_client
from thriftpy.transport.framed import TFramedTransportFactory
from thriftpy.protocol.binary import TBinaryProtocolFactory
from thriftpy.transport.transport import TFramedTransportFactory

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


class FramedTransportTestCase(TestCase):
PROTOCOL_FACTORY = TBinaryProtocolFactory()
TRANSPORT_FACTORY = TFramedTransportFactory()
PROTOCOL_FACTORY = TBinaryProtocolFactory()

def mk_server(self):
self.io_loop = ioloop.IOLoop()
Expand Down Expand Up @@ -99,8 +99,8 @@ def test_zero_length_string(self):


if CYTHON:
from thriftpy.transport.framed import TCyFramedTransportFactory
from thriftpy.protocol.cybin import TCyBinaryProtocolFactory
from thriftpy.transport.cytransport import TCyFramedTransportFactory

class CyFramedTransportTestCase(FramedTransportTestCase):
PROTOCOL_FACTORY = TCyBinaryProtocolFactory()
Expand Down
65 changes: 65 additions & 0 deletions tests/test_memory_transport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from unittest import TestCase

from thriftpy.transport.memory import TMemoryBuffer
from thriftpy._compat import CYTHON


class MemoryTransport(TestCase):
@staticmethod
def trans(data=b'', *args, **kwargs):
return TMemoryBuffer(data)

def test_write(self):
m = self.trans()
m.write(b"hello world")

assert b"hello world" == m.getvalue()

def test_read(self):
m = self.trans(b"hello world")
b = m.read(5)

assert b"hello" == b


if CYTHON:
from thriftpy.transport.memory import TCyMemoryBuffer

class CyMemoryTransport(MemoryTransport):
@staticmethod
def trans(*args, **kwargs):
return TCyMemoryBuffer(*args, **kwargs)

def test_write_move(self):
m = self.trans(buf_size=10)
m.write(b"helloworld")

m.read(6)
assert b"orld" == m.getvalue()

m.write(b"he")
assert b"orldhe" == m.getvalue()

def test_write_grow(self):
m = self.trans(buf_size=10)
m.write(b"hello world")
assert b"hello world" == m.getvalue()

m.read(5)
m.write(b"hello ")
assert b" worldhello " == m.getvalue()

def test_write_move_grow(self):
m = self.trans(buf_size=10)
m.write(b"helloworld")

m.read(6)
m.write(b"hellowaaa")
assert b"orldhellowaaa" == m.getvalue()

def test_read(self):
m = self.trans(b"hello world")
b = m.read(5)

assert b"hello" == b
assert b" world" == m.getvalue()
Loading

0 comments on commit 61ac867

Please sign in to comment.