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

Remove pytest asyncio > anyio + fixx some unclosed ressources #3928

Draft
wants to merge 30 commits into
base: main
Choose a base branch
from

Conversation

euri10
Copy link
Contributor

@euri10 euri10 commented Jan 6, 2025

wanted to give it a try again with 2fb6f6b done

it fails on 4 tests left as is as they dont impact the rest, and on 3 that are impacting the rest so I commented them, that should be the focus

the 4 tests that have no impact on the rest so I left them failing are:

  • tests.unit.test_concurrency.test_get_set_asyncio_executor
  • tests.unit.test_concurrency.test_get_set_trio_capacity_limiter

I'm not too sure about those 2 are failing to be fair

  • tests.unit.test_cli.test_session_commands.test_delete_session
  • tests.unit.test_cli.test_session_commands.test_clear_sessions

those 2 are failing with RuntimeError('Already running asyncio in this thread') which seems normal to me and it looks like the test itself should probably needs to be rewritten,

The 3 commented which impacts the rest are:

  • test_create_ws_route_handlers
  • test_ws_route_handlers_receive_arbitrary_message
  • test_create_ws_route_handlers_arbitrary_channels_allowed

I'm not too sure in that case that we shouldnt handle the Plugin shutdown manually or soething, just a guess.

I'll leave some notes on the changes if someone wanna pick that or try fix the rest, that should be a good start,

gsakkis and others added 30 commits November 29, 2024 13:16
… on a diff param alternative test_cors_simple_response and test_openapi_swagger, all the time its a unclosed socket on redis port 6397
… tests, very weird, feels like redis-py has issues on closing stuff or we do it wrong
@github-actions github-actions bot added area/dependencies This PR involves changes to the dependencies area/docs This PR involves changes to the documentation size: large pr/external Triage Required 🏥 This requires triage labels Jan 6, 2025
@@ -102,7 +102,7 @@ async def on_startup(self) -> None:
pass

async def on_shutdown(self) -> None:
await self._pub_sub.reset()
await self._pub_sub.aclose()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reset is deprecated

def redis_store(redis_client: AsyncRedis) -> RedisStore:
return RedisStore(redis=redis_client)
def redis_store(redis_client: Redis) -> RedisStore:
return RedisStore(redis=redis_client, handle_client_shutdown=True)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to explicitely handle shutdown in test or we're left with dangling ressources

Comment on lines +333 to +336
async def redis_client(redis_service: None, docker_ip: str):
async with Redis(host=docker_ip, port=6397) as client:
yield client
await client.flushall()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall this feels more natural and the try / except of the original fixture is just a way to mask unclosed ressources

@@ -77,7 +77,6 @@ def test_plugin_dependency_signature_namespace(memory_backend: MemoryChannelsBac
assert app.signature_namespace["ChannelsPlugin"] is ChannelsPlugin


@pytest.mark.flaky(reruns=5)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed all the flaky instances : it will definitely play bad with unclosed ressources and makes it hard to track down things

Comment on lines +113 to +171
# @pytest.mark.parametrize("socket_send_mode", ["text", "binary"])
# @pytest.mark.parametrize("handler_base_path", [None, "/ws"])
# async def test_create_ws_route_handlers(
# channels_backend: ChannelsBackend, handler_base_path: str | None, socket_send_mode: WebSocketMode
# ) -> None:
# channels_plugin = ChannelsPlugin(
# backend=channels_backend,
# create_ws_route_handlers=True,
# channels=["something"],
# ws_handler_base_path=handler_base_path or "/",
# ws_send_mode=socket_send_mode,
# )
# app = Litestar(plugins=[channels_plugin])
#
# with TestClient(app) as client, client.websocket_connect(f"{handler_base_path or ''}/something") as ws:
# channels_plugin.publish(["foo"], "something")
# assert ws.receive_json(mode=socket_send_mode, timeout=2) == ["foo"]
#
#
# async def test_ws_route_handlers_receive_arbitrary_message(channels_backend: ChannelsBackend) -> None:
# """The websocket handlers await `WebSocket.receive()` to detect disconnection and stop the subscription.
#
# This test ensures that the subscription is only stopped in the case of receiving a `websocket.disconnect` message.
# """
# channels_plugin = ChannelsPlugin(
# backend=channels_backend,
# create_ws_route_handlers=True,
# channels=["something"],
# )
# app = Litestar(plugins=[channels_plugin])
#
# with TestClient(app) as client, client.websocket_connect("/something") as ws:
# channels_plugin.publish(["foo"], "something")
# # send some arbitrary message
# ws.send("bar")
# # the subscription should still be alive
# assert ws.receive_json(timeout=2) == ["foo"]


# async def test_create_ws_route_handlers_arbitrary_channels_allowed(channels_backend: ChannelsBackend) -> None:
# channels_plugin = ChannelsPlugin(
# backend=channels_backend,
# arbitrary_channels_allowed=True,
# create_ws_route_handlers=True,
# ws_handler_base_path="/ws",
# )
#
# app = Litestar(plugins=[channels_plugin])
#
# with TestClient(app) as client:
# with client.websocket_connect("/ws/foo") as ws:
# channels_plugin.publish("something", "foo")
# assert ws.receive_text(timeout=2) == "something"
#
# time.sleep(0.1)
#
# with client.websocket_connect("/ws/bar") as ws:
# channels_plugin.publish("something else", "bar")
# assert ws.receive_text(timeout=2) == "something else"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those 2 tests are the only one where it feels like we have an issue closing things, I didnt manage to understand the issue but uncommenting those 2 and you'll have dangling sockets in subsequent tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/dependencies This PR involves changes to the dependencies area/docs This PR involves changes to the documentation pr/external size: large Triage Required 🏥 This requires triage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants