sanic-org / sanic-testing

Test clients for Sanic

Home Page:https://sanic.dev/en/plugins/sanic-testing/getting-started.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Getting AttributeError: __aenter__ on async with self.queue_lock:

Vedant-R opened this issue · comments

Hi,

I wanted to enquire if there are examples of unit tests for this. I have been continuously struggling with unit tests for sanic async post method with the following error:

async with self.queue_lock:
AttributeError: __aenter__

I am getting this error for:

async with self.queue_lock:
        if len(self.queue) >= MAX_QUEUE_SIZE:

For more details, I am using a code from here: https://github.com/deep-learning-with-pytorch/dlwpt-code/blob/master/p3ch15/request_batching_server.py#L58

The test I am trying:

import json
import pytest
from ..app import app

@pytest.mark.asyncio
async def test_basic_post():
    _, response = await app.asgi_client.post("/predict", data=json.dumps(INPUT), headers=HEADERS)
    print(response)

Please do let me know if anyone has come across this and has a solution. Thank you.

CC: @ahopkins

I am trying similar thing which works for GET but does not work for POST in my endpoints.
I get error as above when I try for POST, I think it is something to do with asyncio lock and loop.

self.queue_lock = asyncio.Lock(loop=self.app.loop)

I am not sure that this is related to sanic-testing at all. Am I mistaken? It seems lore likely to me that the issue is in the route handler as whether or not the lock is properly setup does not have anything to do with this lib.

I guess one thought I have is this:

self.queue_lock = asyncio.Lock(loop=self.app.loop)

You may be running into problems by manually setting the loop. You do not need to do this on Lock, and in fact you should not do it at all:

Deprecated since version 3.8, removed in version 3.10: The loop parameter. This class has been implicitly getting the current running loop since 3.7.

Source

Thank you for your response @ahopkins.

I understand what you mentioned here, this means I do not need a loop here in lock at all. I will try that and get back. Thanks.

Hey @ahopkins

My apologies, this issue was more from pytest-sanic side of things and I have it resolved now.

You can close this. Thank you for your time and efforts.

The only thing I was not doing right was while testing I did not add a task to the app in the fixture where I yield the app. I got it resolved by adding the task before yielding the app.

That was the reason the lock was not getting initialised at the first place and hence the above error because they was no lock to acquire by the loop.

Thanks again.