1
+ import contextlib
1
2
from contextlib import nullcontext
2
3
from dataclasses import dataclass
3
4
from functools import partial
6
7
7
8
import pytest
8
9
import trio
10
+ from exceptiongroup import ExceptionGroup
9
11
from trio .testing import MockClock , wait_all_tasks_blocked
10
12
from trio_websocket import CloseReason , ConnectionClosed , ConnectionTimeout # type: ignore[import]
11
13
@@ -76,49 +78,64 @@ async def test_success(self, cdp_connection: CDPConnection):
76
78
async def test_failure (self , monkeypatch : pytest .MonkeyPatch ):
77
79
fake_connect_websocket_url = AsyncMock (side_effect = ConnectionTimeout )
78
80
monkeypatch .setattr ("streamlink.webbrowser.cdp.connection.connect_websocket_url" , fake_connect_websocket_url )
79
- with pytest .raises (ConnectionTimeout ) :
81
+ with pytest .raises (ExceptionGroup ) as excinfo :
80
82
async with CDPConnection .create ("ws://localhost:1234/fake" ):
81
83
pass # pragma: no cover
84
+ assert excinfo .group_contains (ConnectionTimeout )
82
85
83
86
@pytest .mark .trio ()
84
87
@pytest .mark .parametrize (("timeout" , "expected" ), [
85
88
pytest .param (None , 2 , id = "Default value of 2 seconds" ),
86
89
pytest .param (0 , 2 , id = "No timeout uses default value" ),
87
90
pytest .param (3 , 3 , id = "Custom timeout value" ),
88
91
])
89
- async def test_timeout (self , monkeypatch : pytest . MonkeyPatch , websocket_connection , timeout , expected ):
90
- async with CDPConnection .create ("ws://localhost:1234/fake" , timeout = timeout ) as cdp_connection :
92
+ async def test_timeout (self , websocket_connection : FakeWebsocketConnection , timeout : Optional [ int ] , expected : int ):
93
+ async with CDPConnection .create ("ws://localhost:1234/fake" , timeout = timeout ) as cdp_conn :
91
94
pass
92
- assert cdp_connection .cmd_timeout == expected
95
+ assert cdp_conn .cmd_timeout == expected
93
96
94
97
95
98
class TestReaderError :
96
99
@pytest .mark .trio ()
97
100
async def test_invalid_json (self , caplog : pytest .LogCaptureFixture , websocket_connection : FakeWebsocketConnection ):
98
- with pytest .raises (CDPError ) as cm : # noqa: PT012
101
+ with pytest .raises (ExceptionGroup ) as excinfo : # noqa: PT012
99
102
async with CDPConnection .create ("ws://localhost:1234/fake" ):
100
103
assert not websocket_connection .closed
101
104
await websocket_connection .sender .send ("INVALID JSON" )
102
105
await wait_all_tasks_blocked ()
103
106
104
- assert str (cm .value ) == "Received invalid CDP JSON data: Expecting value: line 1 column 1 (char 0)"
107
+ assert excinfo .group_contains (
108
+ CDPError ,
109
+ match = r"^Received invalid CDP JSON data: Expecting value: line 1 column 1 \(char 0\)$" ,
110
+ )
105
111
assert caplog .records == []
106
112
107
113
@pytest .mark .trio ()
108
114
async def test_unknown_session_id (self , caplog : pytest .LogCaptureFixture , websocket_connection : FakeWebsocketConnection ):
109
- with pytest .raises (CDPError ) as cm : # noqa: PT012
115
+ with pytest .raises (ExceptionGroup ) as excinfo : # noqa: PT012
110
116
async with CDPConnection .create ("ws://localhost:1234/fake" ):
111
117
assert not websocket_connection .closed
112
118
await websocket_connection .sender .send ("""{"sessionId":"unknown"}""" )
113
119
await wait_all_tasks_blocked ()
114
120
115
- assert str ( cm . value ) == " Unknown CDP session ID: SessionID('unknown')"
121
+ assert excinfo . group_contains ( CDPError , match = r"^ Unknown CDP session ID: SessionID\ ('unknown'\)$" )
116
122
assert [(record .name , record .levelname , record .message ) for record in caplog .records ] == [
117
123
("streamlink.webbrowser.cdp.connection" , "all" , """Received message: {"sessionId":"unknown"}""" ),
118
124
]
119
125
120
126
127
+ @contextlib .contextmanager
128
+ def raises_group (* group_contains ):
129
+ try :
130
+ with pytest .raises (ExceptionGroup ) as excinfo :
131
+ yield
132
+ finally :
133
+ for args , kwargs , expected in group_contains :
134
+ assert excinfo .group_contains (* args , ** kwargs ) is expected
135
+
136
+
121
137
class TestSend :
138
+ # noinspection PyUnusedLocal
122
139
@pytest .mark .trio ()
123
140
@pytest .mark .parametrize (("timeout" , "jump" , "raises" ), [
124
141
pytest .param (
@@ -130,7 +147,9 @@ class TestSend:
130
147
pytest .param (
131
148
None ,
132
149
2 ,
133
- pytest .raises (CDPError , match = "^Sending CDP message and receiving its response timed out$" ),
150
+ raises_group (
151
+ ((CDPError ,), {"match" : "^Sending CDP message and receiving its response timed out$" }, True ),
152
+ ),
134
153
id = "Default timeout, response not in time" ,
135
154
),
136
155
pytest .param (
@@ -142,7 +161,9 @@ class TestSend:
142
161
pytest .param (
143
162
3 ,
144
163
3 ,
145
- pytest .raises (CDPError , match = "^Sending CDP message and receiving its response timed out$" ),
164
+ raises_group (
165
+ ((CDPError ,), {"match" : "^Sending CDP message and receiving its response timed out$" }, True ),
166
+ ),
146
167
id = "Custom timeout, response not in time" ,
147
168
),
148
169
])
@@ -210,12 +231,12 @@ async def test_bad_command(
210
231
assert cdp_connection .cmd_buffers == {}
211
232
assert websocket_connection .sent == []
212
233
213
- with pytest .raises (CDPError ) as cm : # noqa: PT012
234
+ with pytest .raises (ExceptionGroup ) as excinfo : # noqa: PT012
214
235
async with trio .open_nursery () as nursery :
215
236
nursery .start_soon (cdp_connection .send , bad_command ())
216
237
nursery .start_soon (websocket_connection .sender .send , """{"id":0,"result":{}}""" )
217
238
218
- assert str ( cm . value ) == " Generator of CDP command ID 0 did not exit when expected!"
239
+ assert excinfo . group_contains ( CDPError , match = "^ Generator of CDP command ID 0 did not exit when expected!$" )
219
240
assert cdp_connection .cmd_buffers == {}
220
241
assert websocket_connection .sent == ["""{"id":0,"method":"Fake.badCommand","params":{}}""" ]
221
242
assert [(record .name , record .levelname , record .message ) for record in caplog .records ] == [
@@ -241,12 +262,12 @@ async def test_result_exception(
241
262
assert cdp_connection .cmd_buffers == {}
242
263
assert websocket_connection .sent == []
243
264
244
- with pytest .raises (CDPError ) as cm : # noqa: PT012
265
+ with pytest .raises (ExceptionGroup ) as excinfo : # noqa: PT012
245
266
async with trio .open_nursery () as nursery :
246
267
nursery .start_soon (cdp_connection .send , fake_command (FakeCommand ("foo" )))
247
268
nursery .start_soon (websocket_connection .sender .send , """{"id":0,"result":{}}""" )
248
269
249
- assert str ( cm . value ) == " Generator of CDP command ID 0 raised KeyError: 'value'"
270
+ assert excinfo . group_contains ( CDPError , match = "^ Generator of CDP command ID 0 raised KeyError: 'value'$" )
250
271
assert cdp_connection .cmd_buffers == {}
251
272
assert websocket_connection .sent == ["""{"id":0,"method":"Fake.fakeCommand","params":{"value":"foo"}}""" ]
252
273
assert [(record .name , record .levelname , record .message ) for record in caplog .records ] == [
@@ -364,12 +385,12 @@ async def test_response_error(
364
385
assert cdp_connection .cmd_buffers == {}
365
386
assert websocket_connection .sent == []
366
387
367
- with pytest .raises (CDPError ) as cm : # noqa: PT012
388
+ with pytest .raises (ExceptionGroup ) as excinfo : # noqa: PT012
368
389
async with trio .open_nursery () as nursery :
369
390
nursery .start_soon (cdp_connection .send , fake_command (FakeCommand ("foo" )))
370
391
nursery .start_soon (websocket_connection .sender .send , """{"id":0,"error":"Some error message"}""" )
371
392
372
- assert str ( cm . value ) == " Error in CDP command response 0: Some error message"
393
+ assert excinfo . group_contains ( CDPError , match = "^ Error in CDP command response 0: Some error message$" )
373
394
assert cdp_connection .cmd_buffers == {}
374
395
assert websocket_connection .sent == ["""{"id":0,"method":"Fake.fakeCommand","params":{"value":"foo"}}""" ]
375
396
assert [(record .name , record .levelname , record .message ) for record in caplog .records ] == [
@@ -395,12 +416,12 @@ async def test_response_no_result(
395
416
assert cdp_connection .cmd_buffers == {}
396
417
assert websocket_connection .sent == []
397
418
398
- with pytest .raises (CDPError ) as cm : # noqa: PT012
419
+ with pytest .raises (ExceptionGroup ) as excinfo : # noqa: PT012
399
420
async with trio .open_nursery () as nursery :
400
421
nursery .start_soon (cdp_connection .send , fake_command (FakeCommand ("foo" )))
401
422
nursery .start_soon (websocket_connection .sender .send , """{"id":0}""" )
402
423
403
- assert str ( cm . value ) == " No result in CDP command response 0"
424
+ assert excinfo . group_contains ( CDPError , match = "^ No result in CDP command response 0$" )
404
425
assert cdp_connection .cmd_buffers == {}
405
426
assert websocket_connection .sent == ["""{"id":0,"method":"Fake.fakeCommand","params":{"value":"foo"}}""" ]
406
427
assert [(record .name , record .levelname , record .message ) for record in caplog .records ] == [
0 commit comments