This repository has been archived by the owner on Dec 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
953 additions
and
499 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Oops, something went wrong.