summaryrefslogtreecommitdiff
path: root/upstream-layers/meta-openembedded/meta-python/recipes-devtools/python/python3-simple-websocket/0001-Async-test-improvements-for-Python-3.14-48.patch
blob: 2be661c29e200d8be5a80dd630076058f80436fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
From 03832db418f11a818051eefcd774495798b203af Mon Sep 17 00:00:00 2001
From: Miguel Grinberg <miguel.grinberg@gmail.com>
Date: Sun, 12 Apr 2026 10:38:44 +0100
Subject: [PATCH] Async test improvements for Python 3.14 (#48)

Python 3.14 no longer creates an event loop in asyncio.get_event_loop(),
so the make_sync() test helper fails with "There is no current event
loop in thread 'MainThread'". Run the async tests with pytest-asyncio.

Upstream-Status: Backport [https://github.com/miguelgrinberg/simple-websocket/commit/2005dbb96b5b42a4ebf309d10ac1d33daaddb99f]
Signed-off-by: Khem Raj <raj.khem@gmail.com>
---
 tests/helpers.py        | 25 -------------------------
 tests/test_aioclient.py | 39 ++++++++++++++-------------------------
 tests/test_aioserver.py | 39 +++++++++++++--------------------------
 tests/test_server.py    |  2 +-
 4 files changed, 28 insertions(+), 77 deletions(-)
 delete mode 100644 tests/helpers.py

diff --git a/tests/helpers.py b/tests/helpers.py
deleted file mode 100644
index a7c1a46..0000000
--- a/tests/helpers.py
+++ /dev/null
@@ -1,25 +0,0 @@
-import asyncio
-from unittest import mock
-
-
-def AsyncMock(*args, **kwargs):
-    """Return a mock asynchronous function."""
-    m = mock.MagicMock(*args, **kwargs)
-
-    async def mock_coro(*args, **kwargs):
-        return m(*args, **kwargs)
-
-    mock_coro.mock = m
-    return mock_coro
-
-
-def _run(coro):
-    """Run the given coroutine."""
-    return asyncio.get_event_loop().run_until_complete(coro)
-
-
-def make_sync(coro):
-    """Wrap a coroutine so that it can be executed by pytest."""
-    def wrapper(*args, **kwargs):
-        return _run(coro(*args, **kwargs))
-    return wrapper
diff --git a/tests/test_aioclient.py b/tests/test_aioclient.py
index 01a772a..5ec12ff 100644
--- a/tests/test_aioclient.py
+++ b/tests/test_aioclient.py
@@ -1,15 +1,14 @@
 import asyncio
-import unittest
 from unittest import mock
 import pytest  # noqa: F401
 
 from wsproto.events import AcceptConnection, CloseConnection, TextMessage, \
     BytesMessage, Ping
 import simple_websocket
-from .helpers import make_sync, AsyncMock
 
 
-class AioSimpleWebSocketClientTestCase(unittest.TestCase):
+@pytest.mark.asyncio
+class TestAioSimpleWebSocketClient:
     async def get_client(self, mock_wsconn, url, events=[], subprotocols=None,
                          headers=None):
         mock_wsconn().events.side_effect = \
@@ -19,11 +18,10 @@ class AioSimpleWebSocketClientTestCase(unittest.TestCase):
         return await simple_websocket.AioClient.connect(
             url, subprotocols=subprotocols, headers=headers)
 
-    @make_sync
     @mock.patch('simple_websocket.aiows.asyncio.open_connection')
     @mock.patch('simple_websocket.aiows.WSConnection')
     async def test_make_client(self, mock_wsconn, mock_open_connection):
-        rsock = mock.MagicMock(read=AsyncMock(return_value=b'x'))
+        rsock = mock.MagicMock(read=mock.AsyncMock(return_value=b'x'))
         wsock = mock.MagicMock()
         mock_open_connection.return_value = (rsock, wsock)
         client = await self.get_client(mock_wsconn, 'ws://example.com/ws?a=1')
@@ -40,12 +38,11 @@ class AioSimpleWebSocketClientTestCase(unittest.TestCase):
         assert client.port == 80
         assert client.path == '/ws?a=1'
 
-    @make_sync
     @mock.patch('simple_websocket.aiows.asyncio.open_connection')
     @mock.patch('simple_websocket.aiows.WSConnection')
     async def test_make_client_subprotocol(self, mock_wsconn,
                                            mock_open_connection):
-        rsock = mock.MagicMock(read=AsyncMock(return_value=b'x'))
+        rsock = mock.MagicMock(read=mock.AsyncMock(return_value=b'x'))
         wsock = mock.MagicMock()
         mock_open_connection.return_value = (rsock, wsock)
         client = await self.get_client(mock_wsconn, 'ws://example.com/ws?a=1',
@@ -55,12 +52,11 @@ class AioSimpleWebSocketClientTestCase(unittest.TestCase):
             b"Request(host='example.com', target='/ws?a=1', extensions=[], "
             b"extra_headers=[], subprotocols=['foo'])")
 
-    @make_sync
     @mock.patch('simple_websocket.aiows.asyncio.open_connection')
     @mock.patch('simple_websocket.aiows.WSConnection')
     async def test_make_client_subprotocols(self, mock_wsconn,
                                             mock_open_connection):
-        rsock = mock.MagicMock(read=AsyncMock(return_value=b'x'))
+        rsock = mock.MagicMock(read=mock.AsyncMock(return_value=b'x'))
         wsock = mock.MagicMock()
         mock_open_connection.return_value = (rsock, wsock)
         client = await self.get_client(mock_wsconn, 'ws://example.com/ws?a=1',
@@ -70,12 +66,11 @@ class AioSimpleWebSocketClientTestCase(unittest.TestCase):
             b"Request(host='example.com', target='/ws?a=1', extensions=[], "
             b"extra_headers=[], subprotocols=['foo', 'bar'])")
 
-    @make_sync
     @mock.patch('simple_websocket.aiows.asyncio.open_connection')
     @mock.patch('simple_websocket.aiows.WSConnection')
     async def test_make_client_headers(self, mock_wsconn,
                                        mock_open_connection):
-        rsock = mock.MagicMock(read=AsyncMock(return_value=b'x'))
+        rsock = mock.MagicMock(read=mock.AsyncMock(return_value=b'x'))
         wsock = mock.MagicMock()
         mock_open_connection.return_value = (rsock, wsock)
         client = await self.get_client(mock_wsconn, 'ws://example.com/ws?a=1',
@@ -84,12 +79,11 @@ class AioSimpleWebSocketClientTestCase(unittest.TestCase):
             b"Request(host='example.com', target='/ws?a=1', extensions=[], "
             b"extra_headers=[('Foo', 'Bar')], subprotocols=[])")
 
-    @make_sync
     @mock.patch('simple_websocket.aiows.asyncio.open_connection')
     @mock.patch('simple_websocket.aiows.WSConnection')
     async def test_make_client_headers2(self, mock_wsconn,
                                         mock_open_connection):
-        rsock = mock.MagicMock(read=AsyncMock(return_value=b'x'))
+        rsock = mock.MagicMock(read=mock.AsyncMock(return_value=b'x'))
         wsock = mock.MagicMock()
         mock_open_connection.return_value = (rsock, wsock)
         client = await self.get_client(
@@ -100,11 +94,10 @@ class AioSimpleWebSocketClientTestCase(unittest.TestCase):
             b"extra_headers=[('Foo', 'Bar'), ('Foo', 'Baz')], "
             b"subprotocols=[])")
 
-    @make_sync
     @mock.patch('simple_websocket.aiows.asyncio.open_connection')
     @mock.patch('simple_websocket.aiows.WSConnection')
     async def test_send(self, mock_wsconn, mock_open_connection):
-        rsock = mock.MagicMock(read=AsyncMock(return_value=b'x'))
+        rsock = mock.MagicMock(read=mock.AsyncMock(return_value=b'x'))
         wsock = mock.MagicMock()
         mock_open_connection.return_value = (rsock, wsock)
         client = await self.get_client(mock_wsconn, 'ws://example.com/ws')
@@ -123,11 +116,10 @@ class AioSimpleWebSocketClientTestCase(unittest.TestCase):
             b"Message(data=b'hello', frame_finished=True, "
             b"message_finished=True)")
 
-    @make_sync
     @mock.patch('simple_websocket.aiows.asyncio.open_connection')
     @mock.patch('simple_websocket.aiows.WSConnection')
     async def test_receive(self, mock_wsconn, mock_open_connection):
-        rsock = mock.MagicMock(read=AsyncMock(return_value=b'x'))
+        rsock = mock.MagicMock(read=mock.AsyncMock(return_value=b'x'))
         wsock = mock.MagicMock()
         mock_open_connection.return_value = (rsock, wsock)
         client = await self.get_client(
@@ -142,12 +134,11 @@ class AioSimpleWebSocketClientTestCase(unittest.TestCase):
         assert await client.receive() == b'hello'
         assert await client.receive(timeout=0) is None
 
-    @make_sync
     @mock.patch('simple_websocket.aiows.asyncio.open_connection')
     @mock.patch('simple_websocket.aiows.WSConnection')
     async def test_receive_after_close(self, mock_wsconn,
                                        mock_open_connection):
-        rsock = mock.MagicMock(read=AsyncMock(return_value=b'x'))
+        rsock = mock.MagicMock(read=mock.AsyncMock(return_value=b'x'))
         wsock = mock.MagicMock()
         mock_open_connection.return_value = (rsock, wsock)
         client = await self.get_client(
@@ -160,11 +151,10 @@ class AioSimpleWebSocketClientTestCase(unittest.TestCase):
         with pytest.raises(simple_websocket.ConnectionClosed):
             await client.receive()
 
-    @make_sync
     @mock.patch('simple_websocket.aiows.asyncio.open_connection')
     @mock.patch('simple_websocket.aiows.WSConnection')
     async def test_receive_ping(self, mock_wsconn, mock_open_connection):
-        rsock = mock.MagicMock(read=AsyncMock(return_value=b'x'))
+        rsock = mock.MagicMock(read=mock.AsyncMock(return_value=b'x'))
 
         wsock = mock.MagicMock()
         mock_open_connection.return_value = (rsock, wsock)
@@ -176,11 +166,11 @@ class AioSimpleWebSocketClientTestCase(unittest.TestCase):
             await asyncio.sleep(0.01)
         wsock.write.assert_any_call(b"Pong(payload=b'hello')")
 
-    @make_sync
     @mock.patch('simple_websocket.aiows.asyncio.open_connection')
     @mock.patch('simple_websocket.aiows.WSConnection')
     async def test_receive_empty(self, mock_wsconn, mock_open_connection):
-        rsock = mock.MagicMock(read=AsyncMock(side_effect=[b'x', b'x', b'']))
+        rsock = mock.MagicMock(read=mock.AsyncMock(
+            side_effect=[b'x', b'x', b'']))
         wsock = mock.MagicMock()
         mock_open_connection.return_value = (rsock, wsock)
         client = await self.get_client(
@@ -193,11 +183,10 @@ class AioSimpleWebSocketClientTestCase(unittest.TestCase):
         assert await client.receive() == 'hello'
         assert await client.receive(timeout=0) is None
 
-    @make_sync
     @mock.patch('simple_websocket.aiows.asyncio.open_connection')
     @mock.patch('simple_websocket.aiows.WSConnection')
     async def test_close(self, mock_wsconn, mock_open_connection):
-        rsock = mock.MagicMock(read=AsyncMock(return_value=b'x'))
+        rsock = mock.MagicMock(read=mock.AsyncMock(return_value=b'x'))
         wsock = mock.MagicMock()
         mock_open_connection.return_value = (rsock, wsock)
         client = await self.get_client(
diff --git a/tests/test_aioserver.py b/tests/test_aioserver.py
index a274019..071e035 100644
--- a/tests/test_aioserver.py
+++ b/tests/test_aioserver.py
@@ -1,15 +1,14 @@
 import asyncio
-import unittest
 from unittest import mock
 import pytest  # noqa: F401
 
 from wsproto.events import Request, CloseConnection, TextMessage, \
     BytesMessage, Ping, Pong
 import simple_websocket
-from .helpers import make_sync, AsyncMock
 
 
-class AioSimpleWebSocketServerTestCase(unittest.TestCase):
+@pytest.mark.asyncio
+class TestAioSimpleWebSocketServer:
     async def get_server(self, mock_wsconn, request, events=[],
                          client_subprotocols=None, server_subprotocols=None,
                          **kwargs):
@@ -29,12 +28,11 @@ class AioSimpleWebSocketServerTestCase(unittest.TestCase):
         return await simple_websocket.AioServer.accept(
             aiohttp=request, subprotocols=server_subprotocols, **kwargs)
 
-    @make_sync
     @mock.patch('simple_websocket.aiows.asyncio.open_connection')
     @mock.patch('simple_websocket.aiows.WSConnection')
     async def test_aiohttp(self, mock_wsconn, mock_open_connection):
         mock_request = mock.MagicMock(headers={})
-        rsock = mock.MagicMock(read=AsyncMock(return_value=b'x'))
+        rsock = mock.MagicMock(read=mock.AsyncMock(return_value=b'x'))
         wsock = mock.MagicMock()
         mock_open_connection.return_value = (rsock, wsock)
         server = await self.get_server(mock_wsconn, mock_request)
@@ -53,19 +51,17 @@ class AioSimpleWebSocketServerTestCase(unittest.TestCase):
             b'Sec-Websocket-Version: 13\r\n\r\n')
         assert server.is_server
 
-    @make_sync
     async def test_invalid_request(self):
         with pytest.raises(ValueError):
             await simple_websocket.AioServer.accept(aiohttp='foo', asgi='bar')
         with pytest.raises(ValueError):
             await simple_websocket.AioServer.accept(asgi='bar', sock='baz')
 
-    @make_sync
     @mock.patch('simple_websocket.aiows.asyncio.open_connection')
     @mock.patch('simple_websocket.aiows.WSConnection')
     async def test_send(self, mock_wsconn, mock_open_connection):
         mock_request = mock.MagicMock(headers={})
-        rsock = mock.MagicMock(read=AsyncMock(return_value=b'x'))
+        rsock = mock.MagicMock(read=mock.AsyncMock(return_value=b'x'))
         wsock = mock.MagicMock()
         mock_open_connection.return_value = (rsock, wsock)
         server = await self.get_server(mock_wsconn, mock_request)
@@ -84,12 +80,11 @@ class AioSimpleWebSocketServerTestCase(unittest.TestCase):
             b"Message(data=b'hello', frame_finished=True, "
             b"message_finished=True)")
 
-    @make_sync
     @mock.patch('simple_websocket.aiows.asyncio.open_connection')
     @mock.patch('simple_websocket.aiows.WSConnection')
     async def test_receive(self, mock_wsconn, mock_open_connection):
         mock_request = mock.MagicMock(headers={})
-        rsock = mock.MagicMock(read=AsyncMock(return_value=b'x'))
+        rsock = mock.MagicMock(read=mock.AsyncMock(return_value=b'x'))
         wsock = mock.MagicMock()
         mock_open_connection.return_value = (rsock, wsock)
         server = await self.get_server(mock_wsconn, mock_request, events=[
@@ -103,13 +98,12 @@ class AioSimpleWebSocketServerTestCase(unittest.TestCase):
         assert await server.receive() == b'hello'
         assert await server.receive(timeout=0) is None
 
-    @make_sync
     @mock.patch('simple_websocket.aiows.asyncio.open_connection')
     @mock.patch('simple_websocket.aiows.WSConnection')
     async def test_receive_after_close(self, mock_wsconn,
                                        mock_open_connection):
         mock_request = mock.MagicMock(headers={})
-        rsock = mock.MagicMock(read=AsyncMock(return_value=b'x'))
+        rsock = mock.MagicMock(read=mock.AsyncMock(return_value=b'x'))
         wsock = mock.MagicMock()
         mock_open_connection.return_value = (rsock, wsock)
         server = await self.get_server(mock_wsconn, mock_request, events=[
@@ -121,13 +115,12 @@ class AioSimpleWebSocketServerTestCase(unittest.TestCase):
         with pytest.raises(simple_websocket.ConnectionClosed):
             await server.receive()
 
-    @make_sync
     @mock.patch('simple_websocket.aiows.asyncio.open_connection')
     @mock.patch('simple_websocket.aiows.WSConnection')
     async def test_receive_split_messages(self, mock_wsconn,
                                           mock_open_connection):
         mock_request = mock.MagicMock(headers={})
-        rsock = mock.MagicMock(read=AsyncMock(return_value=b'x'))
+        rsock = mock.MagicMock(read=mock.AsyncMock(return_value=b'x'))
         wsock = mock.MagicMock()
         mock_open_connection.return_value = (rsock, wsock)
         server = await self.get_server(mock_wsconn, mock_request, events=[
@@ -151,12 +144,11 @@ class AioSimpleWebSocketServerTestCase(unittest.TestCase):
         assert await server.receive() == b'hello'
         assert await server.receive(timeout=0) is None
 
-    @make_sync
     @mock.patch('simple_websocket.aiows.asyncio.open_connection')
     @mock.patch('simple_websocket.aiows.WSConnection')
     async def test_receive_ping(self, mock_wsconn, mock_open_connection):
         mock_request = mock.MagicMock(headers={})
-        rsock = mock.MagicMock(read=AsyncMock(return_value=b'x'))
+        rsock = mock.MagicMock(read=mock.AsyncMock(return_value=b'x'))
         wsock = mock.MagicMock()
         mock_open_connection.return_value = (rsock, wsock)
         server = await self.get_server(mock_wsconn, mock_request, events=[
@@ -166,12 +158,11 @@ class AioSimpleWebSocketServerTestCase(unittest.TestCase):
             await asyncio.sleep(0.01)
         wsock.write.assert_any_call(b"Pong(payload=b'hello')")
 
-    @make_sync
     @mock.patch('simple_websocket.aiows.asyncio.open_connection')
     @mock.patch('simple_websocket.aiows.WSConnection')
     async def test_receive_empty(self, mock_wsconn, mock_open_connection):
         mock_request = mock.MagicMock(headers={})
-        rsock = mock.MagicMock(read=AsyncMock(return_value=b'x'))
+        rsock = mock.MagicMock(read=mock.AsyncMock(return_value=b'x'))
         wsock = mock.MagicMock()
         mock_open_connection.return_value = (rsock, wsock)
         server = await self.get_server(mock_wsconn, mock_request, events=[
@@ -183,12 +174,11 @@ class AioSimpleWebSocketServerTestCase(unittest.TestCase):
         assert await server.receive() == 'hello'
         assert await server.receive(timeout=0) is None
 
-    @make_sync
     @mock.patch('simple_websocket.aiows.asyncio.open_connection')
     @mock.patch('simple_websocket.aiows.WSConnection')
     async def test_receive_large(self, mock_wsconn, mock_open_connection):
         mock_request = mock.MagicMock(headers={})
-        rsock = mock.MagicMock(read=AsyncMock(return_value=b'x'))
+        rsock = mock.MagicMock(read=mock.AsyncMock(return_value=b'x'))
         wsock = mock.MagicMock()
         mock_open_connection.return_value = (rsock, wsock)
         server = await self.get_server(mock_wsconn, mock_request, events=[
@@ -201,12 +191,11 @@ class AioSimpleWebSocketServerTestCase(unittest.TestCase):
         assert await server.receive() == 'hello'
         assert await server.receive(timeout=0) is None
 
-    @make_sync
     @mock.patch('simple_websocket.aiows.asyncio.open_connection')
     @mock.patch('simple_websocket.aiows.WSConnection')
     async def test_close(self, mock_wsconn, mock_open_connection):
         mock_request = mock.MagicMock(headers={})
-        rsock = mock.MagicMock(read=AsyncMock(return_value=b'x'))
+        rsock = mock.MagicMock(read=mock.AsyncMock(return_value=b'x'))
         wsock = mock.MagicMock()
         mock_open_connection.return_value = (rsock, wsock)
         server = await self.get_server(mock_wsconn, mock_request)
@@ -222,7 +211,6 @@ class AioSimpleWebSocketServerTestCase(unittest.TestCase):
             b'CloseConnection(code=<CloseReason.NORMAL_CLOSURE: 1000>, '
             b'reason=None)')
 
-    @make_sync
     @mock.patch('simple_websocket.aiows.asyncio.open_connection')
     @mock.patch('simple_websocket.aiows.WSConnection')
     @mock.patch('simple_websocket.aiows.time')
@@ -230,7 +218,7 @@ class AioSimpleWebSocketServerTestCase(unittest.TestCase):
     async def test_ping_pong(self, mock_wait_for, mock_time, mock_wsconn,
                              mock_open_connection):
         mock_request = mock.MagicMock(headers={})
-        rsock = mock.MagicMock(read=AsyncMock())
+        rsock = mock.MagicMock(read=mock.AsyncMock())
         wsock = mock.MagicMock()
         mock_open_connection.return_value = (rsock, wsock)
         server = await self.get_server(mock_wsconn, mock_request, events=[
@@ -246,12 +234,11 @@ class AioSimpleWebSocketServerTestCase(unittest.TestCase):
         assert wsock.write.call_args_list[2][0][0].startswith(b'Ping')
         assert wsock.write.call_args_list[3][0][0].startswith(b'Close')
 
-    @make_sync
     @mock.patch('simple_websocket.aiows.asyncio.open_connection')
     @mock.patch('simple_websocket.aiows.WSConnection')
     async def test_subprotocols(self, mock_wsconn, mock_open_connection):
         mock_request = mock.MagicMock(headers={})
-        rsock = mock.MagicMock(read=AsyncMock(return_value=b'x'))
+        rsock = mock.MagicMock(read=mock.AsyncMock(return_value=b'x'))
         wsock = mock.MagicMock()
         mock_open_connection.return_value = (rsock, wsock)
 
diff --git a/tests/test_server.py b/tests/test_server.py
index 7be5d15..ca5fe99 100644
--- a/tests/test_server.py
+++ b/tests/test_server.py
@@ -92,7 +92,7 @@ class SimpleWebSocketServerTestCase(unittest.TestCase):
             b"message_finished=True)")
         server.connected = True
         server.send(b'hello')
-        mock_socket.send.assert_called_with(
+        mock_socket.send.assert_any_call(
             b"Message(data=b'hello', frame_finished=True, "
             b"message_finished=True)")