| 57 | |
| 58 | |
| 59 | def test_lifespan_auto(): |
| 60 | startup_complete = False |
| 61 | shutdown_complete = False |
| 62 | |
| 63 | async def app(scope, receive, send): |
| 64 | nonlocal startup_complete, shutdown_complete |
| 65 | message = await receive() |
| 66 | assert message[class="st">"type"] == class="st">"lifespan.startup" |
| 67 | startup_complete = True |
| 68 | await send({class="st">"type": class="st">"lifespan.startup.complete"}) |
| 69 | message = await receive() |
| 70 | assert message[class="st">"type"] == class="st">"lifespan.shutdown" |
| 71 | shutdown_complete = True |
| 72 | await send({class="st">"type": class="st">"lifespan.shutdown.complete"}) |
| 73 | |
| 74 | async def test(): |
| 75 | config = Config(app=app, lifespan=class="st">"auto") |
| 76 | lifespan = LifespanOn(config) |
| 77 | |
| 78 | assert not startup_complete |
| 79 | assert not shutdown_complete |
| 80 | await lifespan.startup() |
| 81 | assert startup_complete |
| 82 | assert not shutdown_complete |
| 83 | await lifespan.shutdown() |
| 84 | assert startup_complete |
| 85 | assert shutdown_complete |
| 86 | |
| 87 | loop = asyncio.new_event_loop() |
| 88 | loop.run_until_complete(test()) |
| 89 | loop.close() |
| 90 | |
| 91 | |
| 92 | def test_lifespan_auto_with_error(): |