Skip to content

opentelemetry-test-utils: add async base test class for asgi #4106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import asyncio
from unittest import IsolatedAsyncioTestCase

from asgiref.testing import ApplicationCommunicator

Expand Down Expand Up @@ -74,3 +75,39 @@ def get_all_output(self):
except asyncio.TimeoutError:
break
return outputs


class AsyncAsgiTestBase(TestBase, IsolatedAsyncioTestCase):
def setUp(self):
super().setUp()

self.scope = {}
setup_testing_defaults(self.scope)
self.communicator = None

def tearDown(self):
if self.communicator:
asyncio.get_event_loop().run_until_complete(
self.communicator.wait()
)

def seed_app(self, app):
self.communicator = ApplicationCommunicator(app, self.scope)

async def send_input(self, message):
await self.communicator.send_input(message)

async def send_default_request(self):
await self.send_input({"type": "http.request", "body": b""})

async def get_output(self, timeout=0.01):
return await self.communicator.receive_output(timeout)

async def get_all_output(self, timeout=0.01):
outputs = []
while True:
try:
outputs.append(await self.communicator.receive_output(timeout))
except asyncio.TimeoutError:
break
return outputs