Skip to content

Commit 387b1d7

Browse files
Address deprecations (Fixes #422)
1 parent 7fe1771 commit 387b1d7

File tree

9 files changed

+34
-16
lines changed

9 files changed

+34
-16
lines changed

examples/client/asyncio/latency_client.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import time
33
import engineio
44

5-
loop = asyncio.get_event_loop()
65
eio = engineio.AsyncClient()
76
start_timer = None
87

@@ -33,4 +32,4 @@ async def start_client():
3332

3433

3534
if __name__ == '__main__':
36-
loop.run_until_complete(start_client())
35+
asyncio.run(start_client())

examples/client/asyncio/simple_client.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import signal
33
import engineio
44

5-
loop = asyncio.get_event_loop()
65
eio = engineio.AsyncClient()
76
exit_event = asyncio.Event()
87
original_signal_handler = None
@@ -50,4 +49,4 @@ async def start_client():
5049

5150
if __name__ == '__main__':
5251
original_signal_handler = signal.signal(signal.SIGINT, signal_handler)
53-
loop.run_until_complete(start_client())
52+
asyncio.run(start_client())

examples/server/aiohttp/latency.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ async def message(sid, data):
2222

2323

2424
if __name__ == '__main__':
25-
web.run_app(app)
25+
web.run_app(app, port=5001)

src/engineio/async_client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
from http.cookies import SimpleCookie
3+
import inspect
34
import signal
45
import ssl
56
import threading
@@ -328,7 +329,8 @@ async def _connect_websocket(self, url, headers, engineio_path):
328329
del headers[header]
329330
break
330331

331-
extra_options = {'timeout': self.request_timeout}
332+
extra_options = {
333+
'timeout': aiohttp.ClientWSTimeout(ws_close=self.request_timeout)}
332334
if not self.ssl_verify:
333335
ssl_context = ssl.create_default_context()
334336
ssl_context.check_hostname = False
@@ -468,7 +470,7 @@ async def _trigger_event(self, event, *args, **kwargs):
468470
run_async = kwargs.pop('run_async', False)
469471
ret = None
470472
if event in self.handlers:
471-
if asyncio.iscoroutinefunction(self.handlers[event]) is True:
473+
if inspect.iscoroutinefunction(self.handlers[event]) is True:
472474
if run_async:
473475
task = self.start_background_task(self.handlers[event],
474476
*args)

src/engineio/async_drivers/aiohttp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import asyncio
1+
import inspect
22
import sys
33

44
from aiohttp.web import Response, WebSocketResponse
@@ -98,7 +98,7 @@ async def send(self, message):
9898
f = self._sock.send_bytes
9999
else:
100100
f = self._sock.send_str
101-
if asyncio.iscoroutinefunction(f):
101+
if inspect.iscoroutinefunction(f):
102102
await f(message)
103103
else:
104104
f(message)

src/engineio/async_drivers/asgi.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import inspect
12
import os
23
import sys
3-
import asyncio
44

55
from engineio.static_files import get_static_file
66

@@ -102,7 +102,7 @@ async def lifespan(self, scope, receive, send):
102102
if self.on_startup:
103103
try:
104104
await self.on_startup() \
105-
if asyncio.iscoroutinefunction(self.on_startup) \
105+
if inspect.iscoroutinefunction(self.on_startup) \
106106
else self.on_startup()
107107
except:
108108
await send({'type': 'lifespan.startup.failed'})
@@ -112,7 +112,7 @@ async def lifespan(self, scope, receive, send):
112112
if self.on_shutdown:
113113
try:
114114
await self.on_shutdown() \
115-
if asyncio.iscoroutinefunction(self.on_shutdown) \
115+
if inspect.iscoroutinefunction(self.on_shutdown) \
116116
else self.on_shutdown()
117117
except:
118118
await send({'type': 'lifespan.shutdown.failed'})

src/engineio/async_drivers/tornado.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import inspect
23
import sys
34
from urllib.parse import urlsplit
45
from .. import exceptions
@@ -24,7 +25,7 @@ def __init__(self, *args, **kwargs):
2425
async def get(self, *args, **kwargs):
2526
if self.request.headers.get('Upgrade', '').lower() == 'websocket':
2627
ret = super().get(*args, **kwargs)
27-
if asyncio.iscoroutine(ret):
28+
if inspect.iscoroutine(ret):
2829
await ret
2930
else:
3031
await engineio_server.handle_request(self)

src/engineio/async_server.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import inspect
23
import urllib
34

45
from . import base_server
@@ -212,7 +213,7 @@ async def handle_request(self, *args, **kwargs):
212213
Note: this method is a coroutine.
213214
"""
214215
translate_request = self._async['translate_request']
215-
if asyncio.iscoroutinefunction(translate_request):
216+
if inspect.iscoroutinefunction(translate_request):
216217
environ = await translate_request(*args, **kwargs)
217218
else:
218219
environ = translate_request(*args, **kwargs)
@@ -427,7 +428,7 @@ def create_event(self, *args, **kwargs):
427428
async def _make_response(self, response_dict, environ):
428429
cors_headers = self._cors_headers(environ)
429430
make_response = self._async['make_response']
430-
if asyncio.iscoroutinefunction(make_response):
431+
if inspect.iscoroutinefunction(make_response):
431432
response = await make_response(
432433
response_dict['status'],
433434
response_dict['headers'] + cors_headers,
@@ -502,7 +503,7 @@ async def _trigger_event(self, event, *args, **kwargs):
502503
run_async = kwargs.pop('run_async', False)
503504
ret = None
504505
if event in self.handlers:
505-
if asyncio.iscoroutinefunction(self.handlers[event]):
506+
if inspect.iscoroutinefunction(self.handlers[event]):
506507
async def run_async_handler():
507508
try:
508509
try:

tests/async/test_client.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ def mock_queue(self, client):
2424
client.queue.put = mock.AsyncMock()
2525
client.queue.join = mock.AsyncMock()
2626

27+
@staticmethod
28+
def mock_ws_timeout(ws_close):
29+
return ws_close
30+
2731
async def test_is_asyncio_based(self):
2832
c = async_client.AsyncClient()
2933
assert c.is_asyncio_based()
@@ -500,6 +504,8 @@ async def test_polling_connection_not_upgraded(self):
500504
assert c in base_client.connected_clients
501505

502506
@mock.patch('engineio.client.time.time', return_value=123.456)
507+
@mock.patch('engineio.async_client.aiohttp.ClientWSTimeout',
508+
new=mock_ws_timeout)
503509
async def test_websocket_connection_failed(self, _time):
504510
c = async_client.AsyncClient()
505511
c.http = mock.MagicMock(closed=False)
@@ -541,6 +547,8 @@ async def test_websocket_connection_extra(self, _time):
541547
)
542548

543549
@mock.patch('engineio.client.time.time', return_value=123.456)
550+
@mock.patch('engineio.async_client.aiohttp.ClientWSTimeout',
551+
new=mock_ws_timeout)
544552
async def test_websocket_upgrade_failed(self, _time):
545553
c = async_client.AsyncClient()
546554
c.http = mock.MagicMock(closed=False)
@@ -568,6 +576,8 @@ async def test_websocket_connection_no_open_packet(self):
568576
await c.connect('http://foo', transports=['websocket'])
569577

570578
@mock.patch('engineio.client.time.time', return_value=123.456)
579+
@mock.patch('engineio.async_client.aiohttp.ClientWSTimeout',
580+
new=mock_ws_timeout)
571581
async def test_websocket_connection_successful(self, _time):
572582
c = async_client.AsyncClient()
573583
c.http = mock.MagicMock(closed=False)
@@ -649,6 +659,8 @@ async def test_websocket_https_noverify_connection_successful(self, _time):
649659
assert kwargs['ssl'].verify_mode == ssl.CERT_NONE
650660

651661
@mock.patch('engineio.client.time.time', return_value=123.456)
662+
@mock.patch('engineio.async_client.aiohttp.ClientWSTimeout',
663+
new=mock_ws_timeout)
652664
async def test_websocket_connection_with_cookies(self, _time):
653665
c = async_client.AsyncClient()
654666
c.http = mock.MagicMock(closed=False)
@@ -682,6 +694,8 @@ async def test_websocket_connection_with_cookies(self, _time):
682694
)
683695

684696
@mock.patch('engineio.client.time.time', return_value=123.456)
697+
@mock.patch('engineio.async_client.aiohttp.ClientWSTimeout',
698+
new=mock_ws_timeout)
685699
async def test_websocket_connection_with_cookie_header(self, _time):
686700
c = async_client.AsyncClient()
687701
c.http = mock.MagicMock(closed=False)
@@ -718,6 +732,8 @@ async def test_websocket_connection_with_cookie_header(self, _time):
718732
)
719733

720734
@mock.patch('engineio.client.time.time', return_value=123.456)
735+
@mock.patch('engineio.async_client.aiohttp.ClientWSTimeout',
736+
new=mock_ws_timeout)
721737
async def test_websocket_connection_with_cookies_and_headers(self, _time):
722738
c = async_client.AsyncClient()
723739
c.http = mock.MagicMock(closed=False)

0 commit comments

Comments
 (0)