Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛(backend) LRSHTTP methods return an error when run inside an asynchroneous function. #400

Closed
Leobouloc opened this issue Jul 26, 2023 · 0 comments · Fixed by #402
Closed
Assignees
Labels

Comments

@Leobouloc
Copy link
Contributor

Leobouloc commented Jul 26, 2023

Bug Report

Expected behavior/code
LRSHTTP.status() and all other methods should work when called inside an async function.

Actual Behavior
As reported openfun/warren#44 (comment) , the LRSHTTP methods returns an error when called within an events loop. The problem is caused by nesting of asyncio.get_event_loop().run_until_complete(...), which is not allowed.

Steps to Reproduce
Add and run test:

import asyncio
from unittest.mock import AsyncMock

import pytest

from ralph.backends.http.async_lrs import AsyncLRSHTTP, HTTPBackendStatus
from ralph.backends.http.lrs import LRSHTTP

@pytest.mark.anyio
async def test_backend_http_lrs_in_async_setting(monkeypatch):
    """Test that backend works when run in async function."""

    async def async_function():
        lrs = LRSHTTP()
        status_mock_response = HTTPBackendStatus.OK
        status_mock = AsyncMock(return_value=status_mock_response)
        monkeypatch.setattr(AsyncLRSHTTP, "status", status_mock)
        assert lrs.status() == status_mock_response
    
    await async_function()

Results in:

=================================================================== FAILURES ===================================================================
____________________________________________________ test_backend_http_lrs_in_async_setting ____________________________________________________

monkeypatch = <_pytest.monkeypatch.MonkeyPatch object at 0x7f2a4181dfd0>

    @pytest.mark.anyio
    async def test_backend_http_lrs_in_async_setting(monkeypatch):
        """Test that backend works when run in async function."""
    
        async def async_function():
            lrs = LRSHTTP()
            status_mock_response = HTTPBackendStatus.OK
            status_mock = AsyncMock(return_value=status_mock_response)
            monkeypatch.setattr(AsyncLRSHTTP, "status", status_mock)
            assert lrs.status() == status_mock_response
    
>       await async_function()

tests/backends/http/test_lrs.py:27: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
tests/backends/http/test_lrs.py:25: in async_function
    assert lrs.status() == status_mock_response
src/ralph/backends/http/lrs.py:19: in status
    return asyncio.get_event_loop().run_until_complete(
/usr/local/lib/python3.11/asyncio/base_events.py:629: in run_until_complete
    self._check_running()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <_UnixSelectorEventLoop running=False closed=True debug=False>

    def _check_running(self):
        if self.is_running():
>           raise RuntimeError('This event loop is already running')
E           RuntimeError: This event loop is already running

/usr/local/lib/python3.11/asyncio/base_events.py:588: RuntimeError


FAILED tests/backends/http/test_lrs.py::test_backend_http_lrs_in_async_setting - RuntimeError: This event loop is already running

Environment

  • Ralph version: 3.9

Possible Solution
The first answer of this post suggest to use nest-asyncio.

@Leobouloc Leobouloc changed the title (backend) LRSHTTP is not working properly when run inside an Async function. (backend) LRSHTTP methods return an error when run inside an Async function. Jul 26, 2023
@Leobouloc Leobouloc changed the title (backend) LRSHTTP methods return an error when run inside an Async function. (backend) LRSHTTP methods return an error when run inside an asynchroneous function. Jul 26, 2023
@Leobouloc Leobouloc changed the title (backend) LRSHTTP methods return an error when run inside an asynchroneous function. 🐛(backend) LRSHTTP methods return an error when run inside an asynchroneous function. Jul 26, 2023
@Leobouloc Leobouloc added the bug label Jul 26, 2023
@Leobouloc Leobouloc self-assigned this Jul 26, 2023
@wilbrdt wilbrdt linked a pull request Aug 7, 2023 that will close this issue
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant