Skip to content

Commit 910d2ec

Browse files
author
IURII Shikanov
committed
Add ON_CONN_FAIL signal
1 parent 7dc7d31 commit 910d2ec

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

tests/test_signal.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from aiohttp import WSServerHandshakeError
3+
from aiohttp import ClientConnectionError, WSServerHandshakeError
44

55
from wsrpc_aiohttp import ClientException, WebSocketAsync
66
from wsrpc_aiohttp.signal import Signal
@@ -165,3 +165,30 @@ async def on_call_fail(**kwargs):
165165
assert call_fail
166166
assert call_fail_args["method"] == "proc_fail"
167167
assert isinstance(call_fail_args["err"], RuntimeError)
168+
169+
170+
async def test_on_conn_fail_signal(client, handler, monkeypatch):
171+
conn_fail = False
172+
conn_err = None
173+
174+
async def on_conn_fail(socket, request, err):
175+
nonlocal conn_fail
176+
nonlocal conn_err
177+
conn_fail = True
178+
conn_err = err
179+
180+
handler.ON_CONN_FAIL.connect(on_conn_fail)
181+
182+
async def prepare_mock(*args, **kwargs):
183+
raise RuntimeError("Prepare failed")
184+
185+
monkeypatch.setattr(
186+
"wsrpc_aiohttp.websocket.handler.web.WebSocketResponse.prepare",
187+
prepare_mock,
188+
)
189+
with pytest.raises(Exception):
190+
async with client:
191+
pass
192+
193+
assert conn_fail
194+
assert isinstance(conn_err, RuntimeError)

wsrpc_aiohttp/websocket/handler.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class WebSocketBase(WSRPCBase, AbstractView):
4242
ON_AUTH_FAIL = Signal()
4343
ON_CONN_OPEN = Signal()
4444
ON_CONN_CLOSE = Signal()
45+
ON_CONN_FAIL = Signal()
4546

4647
def __init__(self, request):
4748
AbstractView.__init__(self, request)
@@ -80,6 +81,7 @@ def freeze(cls):
8081
cls.ON_AUTH_FAIL,
8182
cls.ON_CONN_OPEN,
8283
cls.ON_CONN_CLOSE,
84+
cls.ON_CONN_FAIL,
8385
cls.ON_CALL_START,
8486
cls.ON_CALL_SUCCESS,
8587
cls.ON_CALL_FAIL,
@@ -123,7 +125,15 @@ async def __handle_request(self):
123125
request=self.request,
124126
)
125127

126-
await self.socket.prepare(self.request)
128+
try:
129+
await self.socket.prepare(self.request)
130+
except Exception as err:
131+
await self.ON_CONN_FAIL.call(
132+
socket=self.socket,
133+
request=self.request,
134+
err=err,
135+
)
136+
raise
127137

128138
try:
129139
self.clients[self.id] = self

0 commit comments

Comments
 (0)