Skip to content

Commit

Permalink
[fix] no warnings for coroutine
Browse files Browse the repository at this point in the history
  • Loading branch information
mosquito committed Nov 7, 2019
1 parent 0592bcd commit 5d07287
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 48 deletions.
5 changes: 2 additions & 3 deletions tests/test_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ def get_data(self, _):
response = await client.proxy.get_data()
self.assertEqual(response, DATA_TO_RETURN)

@async_timeout
async def test_call_timeout(self):

@async_timeout
def will_sleep_for(_, seconds):
time.sleep(seconds)
return DATA_TO_RETURN
Expand All @@ -77,5 +76,5 @@ def will_sleep_for(_, seconds):
response = await client.proxy.will_sleep_for(seconds=1)
self.assertEqual(response, DATA_TO_RETURN)

with self.assertRaises(asyncio.CancelledError):
with self.assertRaises(asyncio.TimeoutError):
await client.proxy.will_sleep_for(seconds=7)
33 changes: 5 additions & 28 deletions wsrpc_aiohttp/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,35 +46,12 @@ async def test_custom_timeout(self):
if func is None:
return partial(async_timeout, seconds=seconds)

# convert function to coroutine anyway
coro_func = awaitable(func)

@wraps(func)
async def wrap(self: TestCase, *args, **kwargs):
task = self.loop.create_task(
coro_func(self, *args, **kwargs)
) # type: asyncio.Task

cancelled = False

def on_timeout(task: asyncio.Task):
nonlocal cancelled

if task.done():
return

task.cancel()
cancelled = True

handle = self.loop.call_later(seconds, on_timeout, task)
task.add_done_callback(lambda x: handle.cancel())

try:
return await task
except asyncio.CancelledError as e:
if cancelled:
raise TimeoutError from e
raise
async def wrap(*args, **kwargs):
return await asyncio.wait_for(
awaitable(func)(*args, **kwargs),
timeout=seconds
)

return wrap

Expand Down
6 changes: 3 additions & 3 deletions wsrpc_aiohttp/websocket/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def __init__(self, loop: asyncio.AbstractEventLoop = None, timeout=None):
self._pending_tasks = set()
self._serial = 0
self._timeout = timeout
self._locks = defaultdict(partial(asyncio.Lock, loop=self._loop))
self._locks = defaultdict(asyncio.Lock)
self._futures = defaultdict(self._loop.create_future)
self._event_listeners = set()

Expand All @@ -102,7 +102,7 @@ def _create_task(self, coro):

def _call_later(self, timer, callback, *args, **kwargs):
def handler():
self._create_task(asyncio.coroutine(callback)(*args, **kwargs))
self._create_task(awaitable(callback)(*args, **kwargs))

self._pending_tasks.add(self._loop.call_later(timer, handler))

Expand Down Expand Up @@ -349,7 +349,7 @@ async def make_something(self, foo, bar):
)

await self._send(**payload)
return await asyncio.wait_for(future, self._timeout, loop=self._loop)
return await asyncio.wait_for(future, self._timeout)

async def emit(self, event):
await self._send(**event)
Expand Down
20 changes: 6 additions & 14 deletions wsrpc_aiohttp/websocket/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ def __init__(self, request):
self.id = uuid.uuid4()
self.protocol_version = None
self.serial = 0
self.semaphore = asyncio.Semaphore(
self.MAX_CONCURRENT_REQUESTS, loop=self._loop
)
self.semaphore = asyncio.Semaphore(self.MAX_CONCURRENT_REQUESTS)

@classmethod
def configure(cls, keepalive_timeout=KEEPALIVE_PING_TIMEOUT,
Expand All @@ -55,12 +53,8 @@ def configure(cls, keepalive_timeout=KEEPALIVE_PING_TIMEOUT,
cls.CLIENT_TIMEOUT = client_timeout
cls.MAX_CONCURRENT_REQUESTS = max_concurrent_requests

@asyncio.coroutine
def __iter__(self):
return (yield from self.__handle_request())

def __await__(self):
return (yield from self.__iter__())
return self.__handle_request().__await__()

async def authorize(self) -> bool:
""" Special method for authorize client.
Expand All @@ -76,7 +70,6 @@ async def authorize(self) -> bool:
You can validate some headers (self.request.headers) or
check cookies (self.reauest.cookies).
"""

return True

async def __handle_request(self):
Expand Down Expand Up @@ -122,8 +115,7 @@ def broadcast(cls, func, callback=WebSocketRoute.placebo, **kwargs):
client.call, func, callback, **kwargs
))

return asyncio.wait(tasks, loop=loop,
return_when=asyncio.ALL_COMPLETED)
return asyncio.wait(tasks, return_when=asyncio.ALL_COMPLETED)

async def _send(self, **kwargs):
try:
Expand Down Expand Up @@ -155,7 +147,7 @@ async def close(self):
self.clients.pop(self.id)

for name, obj in self._handlers.items():
self._loop.create_task(asyncio.coroutine(obj._onclose)())
self._loop.create_task(awaitable(obj._onclose)())

def _log_client_list(self):
log.debug('CLIENTS: %s', Lazy(lambda: ''.join([
Expand All @@ -168,7 +160,7 @@ async def _start_ping(self):
return

future = asyncio.ensure_future(
self.call('ping', seq=self._loop.time()), loop=self._loop
self.call('ping', seq=self._loop.time())
)

def on_timeout():
Expand Down Expand Up @@ -214,7 +206,7 @@ def on_timeout():
self._loop.create_task(self.close())
break

await asyncio.sleep(self.KEEPALIVE_PING_TIMEOUT, loop=self._loop)
await asyncio.sleep(self.KEEPALIVE_PING_TIMEOUT)


class WebSocketAsync(WebSocketBase):
Expand Down

0 comments on commit 5d07287

Please sign in to comment.