File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
tests/opentelemetry-test-utils/src/opentelemetry/test Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 13
13
# limitations under the License.
14
14
15
15
import asyncio
16
+ from unittest import IsolatedAsyncioTestCase
16
17
17
18
from asgiref .testing import ApplicationCommunicator
18
19
@@ -74,3 +75,39 @@ def get_all_output(self):
74
75
except asyncio .TimeoutError :
75
76
break
76
77
return outputs
78
+
79
+
80
+ class AsyncAsgiTestBase (TestBase , IsolatedAsyncioTestCase ):
81
+ def setUp (self ):
82
+ super ().setUp ()
83
+
84
+ self .scope = {}
85
+ setup_testing_defaults (self .scope )
86
+ self .communicator = None
87
+
88
+ def tearDown (self ):
89
+ if self .communicator :
90
+ asyncio .get_event_loop ().run_until_complete (
91
+ self .communicator .wait ()
92
+ )
93
+
94
+ def seed_app (self , app ):
95
+ self .communicator = ApplicationCommunicator (app , self .scope )
96
+
97
+ async def send_input (self , message ):
98
+ await self .communicator .send_input (message )
99
+
100
+ async def send_default_request (self ):
101
+ await self .send_input ({"type" : "http.request" , "body" : b"" })
102
+
103
+ async def get_output (self , timeout = 0.01 ):
104
+ return await self .communicator .receive_output (timeout )
105
+
106
+ async def get_all_output (self , timeout = 0.01 ):
107
+ outputs = []
108
+ while True :
109
+ try :
110
+ outputs .append (await self .communicator .receive_output (timeout ))
111
+ except asyncio .TimeoutError :
112
+ break
113
+ return outputs
You can’t perform that action at this time.
0 commit comments