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

Fully Asyncio Support #308

Open
wants to merge 20 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ pip-log.txt
.pydevproject
*.sublime-workspace
*.sw[op]
env/
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ python:
- 3.3
- 3.4
- 3.5
- 3.6
- pypy

matrix:
# include test for flake8
include:
- python: 3.5
script: tox -e flake8


install:
- pip install cython tox

Expand Down
19 changes: 19 additions & 0 deletions examples/asyncio_echo/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
import thriftpy
import asyncio
from thriftpy.rpc import make_aio_client


echo_thrift = thriftpy.load("echo.thrift", module_name="echo_thrift")


async def main():
client = await make_aio_client(
echo_thrift.EchoService, '127.0.0.1', 6000)
print(await client.echo('hello, world'))
client.close()


if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
7 changes: 7 additions & 0 deletions examples/asyncio_echo/echo.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# ping service demo
service EchoService {
/*
* Sexy c style comment
*/
string echo(1: string param),
}
24 changes: 24 additions & 0 deletions examples/asyncio_echo/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
import asyncio
import thriftpy

from thriftpy.rpc import make_aio_server

echo_thrift = thriftpy.load("echo.thrift", module_name="echo_thrift")


class Dispatcher(object):
async def echo(self, param):
print(param)
await asyncio.sleep(0.1)
return param


def main():
server = make_aio_server(
echo_thrift.EchoService, Dispatcher(), '127.0.0.1', 6000)
server.serve()


if __name__ == '__main__':
main()
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import sys

collect_ignore = ["setup.py"]
if sys.version_info < (3, 5):
collect_ignore.append("test_aio.py")
280 changes: 280 additions & 0 deletions tests/test_aio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
# -*- coding: utf-8 -*-
import os
import asyncio
# import uvloop
import threading

# asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

import time

import pytest

import thriftpy

thriftpy.install_import_hook()

from thriftpy.rpc import make_aio_server, make_aio_client # noqa
from thriftpy.transport import TTransportException # noqa

addressbook = thriftpy.load(os.path.join(os.path.dirname(__file__),
"addressbook.thrift"))
unix_sock = "/tmp/aio_thriftpy_test.sock"
SSL_PORT = 50442


class Dispatcher:
def __init__(self):
self.ab = addressbook.AddressBook()
self.ab.people = {}

@asyncio.coroutine
def ping(self):
return True

@asyncio.coroutine
def hello(self, name):
return "hello " + name

@asyncio.coroutine
def add(self, person):
self.ab.people[person.name] = person
return True

@asyncio.coroutine
def remove(self, name):
try:
self.ab.people.pop(name)
return True
except KeyError:
raise addressbook.PersonNotExistsError(
"{0} not exists".format(name))

@asyncio.coroutine
def get(self, name):
try:
return self.ab.people[name]
except KeyError:
raise addressbook.PersonNotExistsError(
"{0} not exists".format(name))

@asyncio.coroutine
def book(self):
return self.ab

@asyncio.coroutine
def get_phonenumbers(self, name, count):
p = [self.ab.people[name].phones[0]] if name in self.ab.people else []
return p * count

@asyncio.coroutine
def get_phones(self, name):
phone_numbers = self.ab.people[name].phones
return dict((p.type, p.number) for p in phone_numbers)

@asyncio.coroutine
def sleep(self, ms):
yield from asyncio.sleep(ms / 1000.0)
return True


@pytest.fixture(scope="module")
def aio_server(request):
loop = asyncio.new_event_loop()
server = make_aio_server(
addressbook.AddressBookService,
Dispatcher(),
unix_socket=unix_sock,
loop=loop
)
st = threading.Thread(target=server.serve)
st.daemon = True
st.start()
time.sleep(0.1)


@pytest.fixture(scope="module")
def aio_ssl_server(request):
loop = asyncio.new_event_loop()
ssl_server = make_aio_server(
addressbook.AddressBookService, Dispatcher(),
host='localhost', port=SSL_PORT,
certfile="ssl/server.pem", keyfile="ssl/server.key", loop=loop
)
st = threading.Thread(target=ssl_server.serve)
st.daemon = True
st.start()
time.sleep(0.1)


@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


async def client(timeout=3000):
return await make_aio_client(
addressbook.AddressBookService,
unix_socket=unix_sock, socket_timeout=timeout
)


async def ssl_client(timeout=3000):
return await make_aio_client(
addressbook.AddressBookService,
host='localhost', port=SSL_PORT,
socket_timeout=timeout,
cafile="ssl/CA.pem", certfile="ssl/client.crt",
keyfile="ssl/client.key")


@pytest.mark.asyncio
async def test_void_api(aio_server):
c = await client()
assert await c.ping() is None
c.close()


@pytest.mark.asyncio
async def test_void_api_with_ssl(aio_ssl_server):
c = await ssl_client()
assert await c.ping() is None
c.close()


@pytest.mark.asyncio
async def test_string_api(aio_server):
c = await client()
assert await c.hello("world") == "hello world"
c.close()


@pytest.mark.asyncio
async def test_string_api_with_ssl(aio_ssl_server):
c = await client()
assert await c.hello("world") == "hello world"
c.close()


@pytest.mark.asyncio
async def test_huge_res(aio_server):
c = await client()
big_str = "world" * 100000
assert await c.hello(big_str) == "hello " + big_str
c.close()


@pytest.mark.asyncio
async def test_huge_res_with_ssl(aio_ssl_server):
c = await ssl_client()
big_str = "world" * 100000
assert await c.hello(big_str) == "hello " + big_str
c.close()


@pytest.mark.asyncio
async def test_tstruct_req(person):
c = await client()
assert await c.add(person) is True
c.close()


@pytest.mark.asyncio
async def test_tstruct_req_with_ssl(person):
c = await ssl_client()
assert await c.add(person) is True
c.close()


@pytest.mark.asyncio
async def test_tstruct_res(person):
c = await client()
assert person == await c.get("Alice")
c.close()


@pytest.mark.asyncio
async def test_tstruct_res_with_ssl(person):
c = await ssl_client()
assert person == await c.get("Alice")
c.close()


@pytest.mark.asyncio
async def test_complex_tstruct():
c = await client()
assert len(await c.get_phonenumbers("Alice", 0)) == 0
assert len(await c.get_phonenumbers("Alice", 1000)) == 1000
c.close()


@pytest.mark.asyncio
async def test_complex_tstruct_with_ssl():
c = await ssl_client()
assert len(await c.get_phonenumbers("Alice", 0)) == 0
assert len(await c.get_phonenumbers("Alice", 1000)) == 1000
c.close()


@pytest.mark.asyncio
async def test_exception():
with pytest.raises(addressbook.PersonNotExistsError):
c = await client()
await c.remove("Bob")


@pytest.mark.asyncio
async def test_exception_iwth_ssl():
with pytest.raises(addressbook.PersonNotExistsError):
c = await ssl_client()
await c.remove("Bob")


@pytest.mark.asyncio
async def test_client_socket_timeout():
with pytest.raises(asyncio.TimeoutError):
try:
c = await ssl_client(timeout=500)
await c.sleep(1000)
except:
c.close()
raise


@pytest.mark.asyncio
async def test_ssl_socket_timeout():
# SSL socket timeout raises socket.timeout since Python 3.2.
# http://bugs.python.org/issue10272
with pytest.raises(asyncio.TimeoutError):
try:
c = await ssl_client(timeout=500)
await c.sleep(1000)
except:
c.close()
raise


@pytest.mark.asyncio
async def test_client_connect_timeout():
with pytest.raises(TTransportException):
c = await make_aio_client(
addressbook.AddressBookService,
unix_socket='/tmp/test.sock',
connect_timeout=1000
)
await c.hello('test')
4 changes: 4 additions & 0 deletions tests/test_framed_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import absolute_import

import sys
import logging
import socket
import threading
Expand All @@ -10,6 +11,7 @@
from os import path
from unittest import TestCase

import pytest
from tornado import ioloop

import thriftpy
Expand Down Expand Up @@ -83,13 +85,15 @@ def setUp(self):
time.sleep(0.1)
self.client = self.mk_client()

@pytest.mark.skipif(sys.version_info[:2] == (2, 6), reason="not support")
def test_able_to_communicate(self):
dennis = addressbook.Person(name='Dennis Ritchie')
success = self.client.add(dennis)
assert success
success = self.client.add(dennis)
assert not success

@pytest.mark.skipif(sys.version_info[:2] == (2, 6), reason="not support")
def test_zero_length_string(self):
dennis = addressbook.Person(name='')
success = self.client.add(dennis)
Expand Down
Loading