File tree Expand file tree Collapse file tree 3 files changed +26
-58
lines changed
Expand file tree Collapse file tree 3 files changed +26
-58
lines changed Original file line number Diff line number Diff line change @@ -76,6 +76,12 @@ async def close(self) -> None:
7676 self ._client = None
7777 await client .close ()
7878
79+ if self ._rx_task :
80+ task = self ._rx_task
81+ self ._rx_task = None
82+ task .cancel ()
83+ await task
84+
7985 @handle_error
8086 async def send (self , message : SendMessages ) -> None :
8187 """Send a message."""
@@ -110,34 +116,23 @@ async def _receive_messages(self) -> None:
110116 if TYPE_CHECKING :
111117 assert self ._client
112118
113- try :
114- while self .connected :
115- msg = await self ._client .receive ()
116- match msg .type :
117- case (
118- WSMsgType .CLOSE
119- | WSMsgType .CLOSED
120- | WSMsgType .CLOSING
121- | WSMsgType .PING
122- | WSMsgType .PONG
123- ):
124- break
125- case WSMsgType .ERROR :
126- _LOGGER .error ("Error received: %s" , msg .data )
127- case WSMsgType .TEXT :
128- self ._process_text_message (msg .data )
129- case _:
130- _LOGGER .warning ("Received unknown message: %s" , msg )
131- except Exception :
132- _LOGGER .exception ("Unexpected error while receiving message" )
133- raise
134- finally :
135- _LOGGER .debug (
136- "Websocket client connection from %s closed" , self ._server_url
137- )
138-
139- if self .connected :
140- await self .close ()
119+ while self .connected :
120+ msg = await self ._client .receive ()
121+ match msg .type :
122+ case (
123+ WSMsgType .CLOSE
124+ | WSMsgType .CLOSED
125+ | WSMsgType .CLOSING
126+ | WSMsgType .PING
127+ | WSMsgType .PONG
128+ ):
129+ break
130+ case WSMsgType .ERROR :
131+ _LOGGER .error ("Error received: %s" , msg .data )
132+ case WSMsgType .TEXT :
133+ self ._process_text_message (msg .data )
134+ case _:
135+ _LOGGER .warning ("Received unknown message: %s" , msg )
141136
142137 def subscribe (
143138 self , callback : Callable [[ReceiveMessages ], None ]
Original file line number Diff line number Diff line change @@ -56,7 +56,7 @@ include = [
5656
5757[tool .coverage .report ]
5858show_missing = true
59- fail_under = 50
59+ fail_under = 97
6060
6161[tool .coverage .run ]
6262plugins = [" covdefaults" ]
Original file line number Diff line number Diff line change 3434class TestServer :
3535 """Test server."""
3636
37+ __test__ = False
38+
3739 def __init__ (self ) -> None :
3840 """Initialize the test server."""
3941 self .server : AioHttpTestServer
@@ -361,32 +363,3 @@ async def receive() -> WSMessage:
361363 await asyncio .sleep (0.1 )
362364
363365 assert caplog .record_tuples == [record ]
364-
365-
366- async def test_receive_raised (
367- caplog : pytest .LogCaptureFixture ,
368- ws_client : Go2RtcWsClient ,
369- ) -> None :
370- """Test getting message raised an exception."""
371- client = AsyncMock ()
372- client .return_value .closed = False
373- ws_client ._session .ws_connect = client # type: ignore[method-assign] # pylint: disable=protected-access
374-
375- async def receive () -> WSMessage :
376- nonlocal client
377- client .return_value .closed = True
378-
379- raise ValueError
380-
381- client .return_value .receive .side_effect = receive
382-
383- await ws_client .connect ()
384- await asyncio .sleep (0.1 )
385-
386- assert caplog .record_tuples == [
387- (
388- "go2rtc_client.ws.client" ,
389- logging .ERROR ,
390- "Unexpected error while receiving message" ,
391- )
392- ]
You can’t perform that action at this time.
0 commit comments