Skip to content

Commit

Permalink
Let companion ASGI app handle lifespan events (Fixes #287)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Aug 2, 2022
1 parent f6df30b commit 1c9001c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/engineio/async_drivers/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async def __call__(self, scope, receive, send):
static_file = get_static_file(scope['path'], self.static_files) \
if scope['type'] == 'http' and self.static_files else None
if scope['type'] == 'lifespan':
await self.lifespan(receive, send)
await self.lifespan(scope, receive, send)
elif static_file and os.path.exists(static_file['filename']):
await self.serve_static_file(static_file, receive, send)
elif self.other_asgi_app is not None:
Expand All @@ -81,7 +81,13 @@ async def serve_static_file(self, static_file, receive,
await send({'type': 'http.response.body',
'body': payload})

async def lifespan(self, receive, send):
async def lifespan(self, scope, receive, send):
if self.other_asgi_app is not None and self.on_startup is None and \
self.on_shutdown is None:
# let the other ASGI app handle lifespan events
await self.other_asgi_app(scope, receive, send)
return

while True:
event = await receive()
if event['type'] == 'lifespan.startup':
Expand Down
7 changes: 7 additions & 0 deletions tests/asyncio/test_async_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ def test_other_app_routing(self):
_run(app(scope, 'receive', 'send'))
other_app.mock.assert_called_once_with(scope, 'receive', 'send')

def test_other_app_lifespan_routing(self):
other_app = AsyncMock()
app = async_asgi.ASGIApp('eio', other_app)
scope = {'type': 'lifespan'}
_run(app(scope, 'receive', 'send'))
other_app.mock.assert_called_once_with(scope, 'receive', 'send')

def test_static_file_routing(self):
root_dir = os.path.dirname(__file__)
app = async_asgi.ASGIApp(
Expand Down

0 comments on commit 1c9001c

Please sign in to comment.