File tree Expand file tree Collapse file tree 2 files changed +39
-2
lines changed Expand file tree Collapse file tree 2 files changed +39
-2
lines changed Original file line number Diff line number Diff line change 11import pytest
22
3- from aiohttp import WSServerHandshakeError
3+ from aiohttp import ClientConnectionError , WSServerHandshakeError
44
55from wsrpc_aiohttp import ClientException , WebSocketAsync
66from 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 )
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments