Skip to content

Commit

Permalink
Fix AttributeError when @acomic decorator is used in Class Based Vi…
Browse files Browse the repository at this point in the history
…ews (#22)

* #21: @atomic not applicable for the Class Based Views

* CHANGES
  • Loading branch information
ebeseda authored and asvetlov committed Dec 20, 2017
1 parent 225538f commit 3c8e688
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/21.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix AttributeError when `@acomic` decorator is used in Class Based Views.
6 changes: 6 additions & 0 deletions aiojobs/aiohttp.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from functools import wraps

from aiohttp.web import View

from . import create_scheduler

__all__ = ('setup', 'spawn', 'get_scheduler', 'get_scheduler_from_app',
Expand All @@ -25,6 +27,10 @@ async def spawn(request, coro):
def atomic(coro):
@wraps(coro)
async def wrapper(request):
if isinstance(request, View):
# Class Based View decorated.
request = request.request

job = await spawn(request, coro(request))
return await job.wait()
return wrapper
Expand Down
21 changes: 21 additions & 0 deletions tests/test_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,24 @@ async def handler(request):

assert scheduler.active_count == 0
assert scheduler.pending_count == 0


async def test_atomic_from_view(test_client):
app = web.Application()

class MyView(web.View):
@atomic
async def get(self):
return web.Response()

app.router.add_route("*", "/", MyView)
aiojobs_setup(app)

client = await test_client(app)
resp = await client.get('/')
assert resp.status == 200

scheduler = get_scheduler_from_app(app)

assert scheduler.active_count == 0
assert scheduler.pending_count == 0

0 comments on commit 3c8e688

Please sign in to comment.