Skip to content

Commit

Permalink
Merge pull request #3 from alternativehood/master
Browse files Browse the repository at this point in the history
Removed hardcoded timeout. Minor QoA additions.
  • Loading branch information
mosquito committed Sep 27, 2017
2 parents 5ed5221 + 65bf22f commit c4c2aa2
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 13 deletions.
6 changes: 6 additions & 0 deletions Makefile
@@ -1,3 +1,5 @@
VENV = env

release: upload_doc
python3 setup.py sdist bdist_wheel upload

Expand All @@ -8,3 +10,7 @@ upload_doc: build_doc
rsync -zav --delete docs/build/html/ root@wsrpc.info:/home/site-wsrpc/wsrpc-aiohttp-doc/

doc: upload_doc

develop:
virtualenv $(VENV)
$(VENV)/bin/pip install -Ue ".[develop]"
2 changes: 1 addition & 1 deletion docs/source/index.rst
Expand Up @@ -164,7 +164,7 @@ Clone the project:

.. code-block:: shell
git clone https://github.com/mosquito/wsrpc-aiohttp.git
git clone https://github.com/wsrpc/wsrpc-aiohttp.git
cd wsrpc-aiohttp
Expand Down
34 changes: 28 additions & 6 deletions tests/test_rpc.py
@@ -1,9 +1,15 @@
import concurrent
import uuid

import time

from wsrpc_aiohttp.testing import BaseTestCase, async_timeout
from wsrpc_aiohttp import WebSocketRoute


DATA_TO_RETURN = 1000


class ReverseRoute(WebSocketRoute):
def init(self, data):
self.data = data
Expand All @@ -16,6 +22,7 @@ def get_data(self):


class TestServerRPC(BaseTestCase):

@async_timeout
async def test_call(self):
self.WebSocketHandler.add_route('reverse', ReverseRoute)
Expand All @@ -34,26 +41,41 @@ async def test_call(self):
@async_timeout
async def test_call_func(self):
def get_data(_):
return 1000
return DATA_TO_RETURN

self.WebSocketHandler.add_route('get_data', get_data)

client = await self.get_ws_client()

response = await client.proxy.get_data()
self.assertEqual(response, 1000)
self.assertEqual(response, DATA_TO_RETURN)

@async_timeout
async def test_call_method(self):
class DataStore:
DATA = 1000

def get_data(self, _):
return 1000
return DATA_TO_RETURN

self.WebSocketHandler.add_route('get_data', DataStore().get_data)

client = await self.get_ws_client()

response = await client.proxy.get_data()
self.assertEqual(response, 1000)
self.assertEqual(response, DATA_TO_RETURN)

@async_timeout
async def test_call_timeout(self):

def will_sleep_for(_, seconds):
time.sleep(seconds)
return DATA_TO_RETURN

self.WebSocketHandler.add_route('will_sleep_for', will_sleep_for)

client = await self.get_ws_client(timeout=5)

response = await client.proxy.will_sleep_for(seconds=3)
self.assertEqual(response, DATA_TO_RETURN)

with self.assertRaises(concurrent.futures._base.CancelledError):
await client.proxy.will_sleep_for(seconds=7)
4 changes: 2 additions & 2 deletions wsrpc_aiohttp/testing.py
Expand Up @@ -82,8 +82,8 @@ async def get_application(self):
app.router.add_route('*', self.path, self.WebSocketHandler)
return app

async def get_ws_client(self) -> WSRPCClient:
ws_client = WSRPCClient(endpoint=self.url)
async def get_ws_client(self, timeout=None) -> WSRPCClient:
ws_client = WSRPCClient(endpoint=self.url, timeout=timeout)
await ws_client.connect()
self.addCleanup(ws_client.close)
return ws_client
Expand Down
2 changes: 1 addition & 1 deletion wsrpc_aiohttp/version.py
Expand Up @@ -9,7 +9,7 @@

team_email = 'me@mosquito.su'

version_info = (0, 5, 2)
version_info = (0, 6, 0)


__author__ = ", ".join("{} <{}>".format(*info) for info in author_info)
Expand Down
3 changes: 2 additions & 1 deletion wsrpc_aiohttp/websocket/client.py
Expand Up @@ -15,10 +15,11 @@

class WSRPCClient(WSRPCBase):

def __init__(self, endpoint: Union[URL, str], loop=None, **kwargs):
def __init__(self, endpoint: Union[URL, str], loop=None, timeout=None, **kwargs):
WSRPCBase.__init__(self, loop=loop)
self._url = URL(str(endpoint))
self._session = aiohttp.ClientSession(loop=self._loop, **kwargs)
self._timeout = timeout
self.socket = None
self.closed = False

Expand Down
5 changes: 3 additions & 2 deletions wsrpc_aiohttp/websocket/common.py
Expand Up @@ -57,13 +57,14 @@ class WSRPCBase:
_CLIENTS = defaultdict(dict)
_CLEAN_LOCK_TIMEOUT = 2

__slots__ = ('_handlers', '_loop', '_pending_tasks', '_locks', '_futures', '_serial')
__slots__ = ('_handlers', '_loop', '_pending_tasks', '_locks', '_futures', '_serial', '_timeout')

def __init__(self, loop=None):
self._loop = loop or asyncio.get_event_loop()
self._handlers = {}
self._pending_tasks = set()
self._serial = 0
self._timeout = None
self._locks = defaultdict(partial(asyncio.Lock, loop=self._loop))
self._futures = defaultdict(self._loop.create_future)

Expand Down Expand Up @@ -246,7 +247,7 @@ def call(self, func, **kwargs):

log.info("Sending %r request #%d \"%s(%r)\" to the client.", req_type, serial, func, kwargs)

future = asyncio.ensure_future(asyncio.wait_for(future, 3, loop=self._loop), loop=self._loop)
future = asyncio.ensure_future(asyncio.wait_for(future, self._timeout, loop=self._loop), loop=self._loop)

def propagate_exception(f):
if f.exception():
Expand Down

0 comments on commit c4c2aa2

Please sign in to comment.