1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
import asyncio
from collections.abc import Awaitable
from typing import Callable
import pytest
from aiohttp import ClientSession, web
# isort: off
from aiojobs.aiohttp import (
atomic,
get_scheduler,
get_scheduler_from_app,
get_scheduler_from_request,
setup as aiojobs_setup,
shield,
spawn,
)
# isort: on
_Client = Callable[[web.Application], Awaitable[ClientSession]]
async def test_plugin(aiohttp_client: _Client) -> None:
job = None
async def shielded() -> str:
await asyncio.sleep(0)
return "TEST"
async def coro() -> None:
await asyncio.sleep(10)
async def handler(request: web.Request) -> web.Response:
nonlocal job
job = await spawn(request, coro())
assert not job.closed
res = await shield(request, shielded())
return web.Response(text=res)
app = web.Application()
app.router.add_get("/", handler)
aiojobs_setup(app)
client = await aiohttp_client(app)
resp = await client.get("/")
assert resp.status == 200
assert await resp.text() == "TEST"
assert job is not None
assert job.active
await client.close()
assert job.closed
async def test_no_setup(aiohttp_client: _Client) -> None:
async def handler(request: web.Request) -> web.Response:
with pytest.raises(RuntimeError):
get_scheduler(request)
return web.Response()
app = web.Application()
app.router.add_get("/", handler)
client = await aiohttp_client(app)
resp = await client.get("/")
assert resp.status == 200
async def test_atomic(aiohttp_client: _Client) -> None:
@atomic
async def handler(request: web.Request) -> web.Response:
await asyncio.sleep(0)
return web.Response()
app = web.Application()
app.router.add_get("/", handler)
aiojobs_setup(app)
client = await aiohttp_client(app)
resp = await client.get("/")
assert resp.status == 200
scheduler = get_scheduler_from_app(app)
assert scheduler is not None
assert scheduler.active_count == 0
assert scheduler.pending_count == 0
async def test_atomic_from_view(aiohttp_client: _Client) -> None:
app = web.Application()
class MyView(web.View):
@atomic
async def get(self) -> web.Response:
return web.Response(text=self.request.method)
app.router.add_route("*", "/", MyView)
aiojobs_setup(app)
client = await aiohttp_client(app)
resp = await client.get("/")
assert resp.status == 200
assert await resp.text() == "GET"
scheduler = get_scheduler_from_app(app)
assert scheduler is not None
assert scheduler.active_count == 0
assert scheduler.pending_count == 0
async def test_nested_application(aiohttp_client: _Client) -> None:
app = web.Application()
aiojobs_setup(app)
app2 = web.Application()
class MyView(web.View):
async def get(self) -> web.Response:
assert get_scheduler_from_request(self.request) == get_scheduler_from_app(
app
)
return web.Response()
app2.router.add_route("*", "/", MyView)
app.add_subapp("/sub/", app2)
client = await aiohttp_client(app)
resp = await client.get("/sub/")
assert resp.status == 200
async def test_nested_application_separate_scheduler(aiohttp_client: _Client) -> None:
app = web.Application()
aiojobs_setup(app)
app2 = web.Application()
aiojobs_setup(app2)
class MyView(web.View):
async def get(self) -> web.Response:
assert get_scheduler_from_request(self.request) != get_scheduler_from_app(
app
)
assert get_scheduler_from_request(self.request) == get_scheduler_from_app(
app2
)
return web.Response()
app2.router.add_route("*", "/", MyView)
app.add_subapp("/sub/", app2)
client = await aiohttp_client(app)
resp = await client.get("/sub/")
assert resp.status == 200
async def test_nested_application_not_set(aiohttp_client: _Client) -> None:
app = web.Application()
app2 = web.Application()
class MyView(web.View):
async def get(self) -> web.Response:
assert get_scheduler_from_request(self.request) is None
return web.Response()
app2.router.add_route("*", "/", MyView)
app.add_subapp("/sub/", app2)
client = await aiohttp_client(app)
resp = await client.get("/sub/")
assert resp.status == 200
|