|
| 1 | +"""Unit tests for the uvloop event-loop policy installer. |
| 2 | +
|
| 3 | +Covers the structural fix for issue #831 / #877: the asyncpg engine-dispose |
| 4 | +race ("IndexError: pop from an empty deque") is avoided by running the Postgres |
| 5 | +backend on uvloop, whose C scheduler has no self._ready.popleft() codepath. |
| 6 | +
|
| 7 | +These tests verify the gating logic of ``maybe_install_uvloop`` without touching |
| 8 | +a real database. The global event-loop policy is saved and restored around each |
| 9 | +test so installing uvloop here cannot leak into the rest of the suite. |
| 10 | +""" |
| 11 | + |
| 12 | +import asyncio |
| 13 | +import sys |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +from basic_memory.config import BasicMemoryConfig, DatabaseBackend |
| 18 | +from basic_memory.db import maybe_install_uvloop |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def restore_event_loop_policy(): |
| 23 | + """Save/restore the global event-loop policy around a test.""" |
| 24 | + original = asyncio.get_event_loop_policy() |
| 25 | + try: |
| 26 | + yield |
| 27 | + finally: |
| 28 | + asyncio.set_event_loop_policy(original) |
| 29 | + |
| 30 | + |
| 31 | +def _postgres_config() -> BasicMemoryConfig: |
| 32 | + return BasicMemoryConfig( |
| 33 | + env="test", |
| 34 | + database_backend=DatabaseBackend.POSTGRES, |
| 35 | + database_url="postgresql+asyncpg://user:pass@localhost/db", |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +def _sqlite_config() -> BasicMemoryConfig: |
| 40 | + return BasicMemoryConfig(env="test", database_backend=DatabaseBackend.SQLITE) |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.skipif(sys.platform == "win32", reason="uvloop is not available on Windows") |
| 44 | +def test_installs_uvloop_for_postgres_backend(restore_event_loop_policy): |
| 45 | + """uvloop policy is installed when backend is Postgres and uvloop is available.""" |
| 46 | + import uvloop |
| 47 | + |
| 48 | + installed = maybe_install_uvloop(_postgres_config()) |
| 49 | + |
| 50 | + assert installed is True |
| 51 | + assert isinstance(asyncio.get_event_loop_policy(), uvloop.EventLoopPolicy) |
| 52 | + |
| 53 | + |
| 54 | +def test_no_uvloop_for_sqlite_backend(restore_event_loop_policy): |
| 55 | + """SQLite users keep the default loop - the helper is a no-op.""" |
| 56 | + before = asyncio.get_event_loop_policy() |
| 57 | + |
| 58 | + installed = maybe_install_uvloop(_sqlite_config()) |
| 59 | + |
| 60 | + assert installed is False |
| 61 | + # Policy must be unchanged for the default (SQLite) path. |
| 62 | + assert asyncio.get_event_loop_policy() is before |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.skipif(sys.platform == "win32", reason="uvloop is not available on Windows") |
| 66 | +def test_uvloop_unavailable_is_a_safe_noop(restore_event_loop_policy, monkeypatch): |
| 67 | + """When uvloop cannot be imported the helper returns False without raising.""" |
| 68 | + import builtins |
| 69 | + |
| 70 | + real_import = builtins.__import__ |
| 71 | + |
| 72 | + def _fail_uvloop_import(name, *args, **kwargs): |
| 73 | + if name == "uvloop": |
| 74 | + raise ImportError("simulated missing uvloop") |
| 75 | + return real_import(name, *args, **kwargs) |
| 76 | + |
| 77 | + monkeypatch.setattr(builtins, "__import__", _fail_uvloop_import) |
| 78 | + |
| 79 | + before = asyncio.get_event_loop_policy() |
| 80 | + installed = maybe_install_uvloop(_postgres_config()) |
| 81 | + |
| 82 | + assert installed is False |
| 83 | + assert asyncio.get_event_loop_policy() is before |
0 commit comments