Skip to content

Commit cb976b5

Browse files
authored
opentelemetry-test-utils: add async base test class for asgi (#4106)
1 parent 19e0a09 commit cb976b5

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

tests/opentelemetry-test-utils/src/opentelemetry/test/asgitestutil.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import asyncio
16+
from unittest import IsolatedAsyncioTestCase
1617

1718
from 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

0 commit comments

Comments
 (0)