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 1313# limitations under the License.
1414
1515import asyncio
16+ from unittest import IsolatedAsyncioTestCase
1617
1718from asgiref .testing import ApplicationCommunicator
1819
@@ -74,3 +75,39 @@ def get_all_output(self):
7475 except asyncio .TimeoutError :
7576 break
7677 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