1
1
import asyncio
2
2
import socket as sock
3
- from functools import partial
4
3
5
4
import pytest
5
+ import pytest_asyncio
6
6
7
7
from asgiref .server import StatelessServer
8
8
@@ -74,8 +74,8 @@ def close(self):
74
74
self ._sock .close ()
75
75
76
76
77
- @pytest .fixture (scope = "function" )
78
- def server ():
77
+ @pytest_asyncio .fixture (scope = "function" )
78
+ async def server ():
79
79
async def app (scope , receive , send ):
80
80
while True :
81
81
msg = await receive ()
@@ -92,25 +92,12 @@ async def check_client_msg(client, expected_address, expected_msg):
92
92
assert server_addr == expected_address
93
93
94
94
95
- async def server_auto_close (fut , timeout ):
96
- """Server run based on run_until_complete. It will block forever with handle
97
- function because it is a while True loop without break. Use this method to close
98
- server automatically."""
99
- loop = asyncio .get_running_loop ()
100
- task = asyncio .ensure_future (fut , loop = loop )
101
- await asyncio .sleep (timeout )
102
- task .cancel ()
103
-
104
-
105
- def test_stateless_server (server ):
95
+ @pytest .mark .asyncio
96
+ async def test_stateless_server (server ):
106
97
"""StatelessServer can be instantiated with an ASGI 3 application."""
107
98
"""Create a UDP Server can register instance based on name from message of client.
108
99
Clients can communicate to other client by name through server"""
109
100
110
- loop = asyncio .new_event_loop ()
111
- asyncio .set_event_loop (loop )
112
- server .handle = partial (server_auto_close , fut = server .handle (), timeout = 1.0 )
113
-
114
101
client1 = Client (name = "client1" )
115
102
client2 = Client (name = "client2" )
116
103
@@ -124,30 +111,35 @@ async def check_client2_behavior():
124
111
await check_client_msg (client2 , server .address , b"Welcome" )
125
112
await check_client_msg (client2 , server .address , b"Hello" )
126
113
127
- task1 = loop . create_task ( check_client1_behavior ())
128
- task2 = loop . create_task ( check_client2_behavior ())
114
+ class Done ( Exception ):
115
+ pass
129
116
130
- server .run ()
117
+ async def do_test ():
118
+ await asyncio .gather (check_client1_behavior (), check_client2_behavior ())
119
+ raise Done
131
120
132
- assert task1 .done ()
133
- assert task2 .done ()
121
+ try :
122
+ await asyncio .gather (server .arun (), do_test ())
123
+ except Done :
124
+ pass
134
125
135
126
136
- def test_server_delete_instance (server ):
127
+ @pytest .mark .asyncio
128
+ async def test_server_delete_instance (server ):
137
129
"""The max_applications of Server is 10. After 20 times register, application number should be 10."""
138
- loop = asyncio .new_event_loop ()
139
- asyncio .set_event_loop (loop )
140
- server .handle = partial (server_auto_close , fut = server .handle (), timeout = 1.0 )
141
-
142
130
client1 = Client (name = "client1" )
143
131
132
+ class Done (Exception ):
133
+ pass
134
+
144
135
async def client1_multiple_register ():
145
136
for i in range (20 ):
146
137
await client1 .register (server .address , name = f"client{ i } " )
147
138
print (f"client{ i } " )
148
139
await check_client_msg (client1 , server .address , b"Welcome" )
140
+ raise Done
149
141
150
- task = loop . create_task ( client1_multiple_register ())
151
- server .run ( )
152
-
153
- assert task . done ()
142
+ try :
143
+ await asyncio . gather ( client1_multiple_register (), server .arun () )
144
+ except Done :
145
+ pass
0 commit comments