|
| 1 | +"""Test uvloop compatibility with nest_asyncio.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import sys |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | + |
| 9 | +class TestUvloopCompatibility: |
| 10 | + """Test that ragas works with uvloop event loops.""" |
| 11 | + |
| 12 | + @pytest.mark.skipif(sys.version_info < (3, 8), reason="uvloop requires Python 3.8+") |
| 13 | + def test_apply_nest_asyncio_with_uvloop_returns_false(self): |
| 14 | + """Test that apply_nest_asyncio returns False with uvloop.""" |
| 15 | + uvloop = pytest.importorskip("uvloop") |
| 16 | + |
| 17 | + from ragas.async_utils import apply_nest_asyncio |
| 18 | + |
| 19 | + async def test_func(): |
| 20 | + result = apply_nest_asyncio() |
| 21 | + return result |
| 22 | + |
| 23 | + uvloop.install() |
| 24 | + try: |
| 25 | + result = asyncio.run(test_func()) |
| 26 | + assert result is False |
| 27 | + finally: |
| 28 | + asyncio.set_event_loop_policy(None) |
| 29 | + |
| 30 | + @pytest.mark.skipif(sys.version_info < (3, 8), reason="uvloop requires Python 3.8+") |
| 31 | + def test_run_with_uvloop_and_running_loop(self): |
| 32 | + """Test that run() raises clear error with uvloop in running event loop (Jupyter scenario).""" |
| 33 | + uvloop = pytest.importorskip("uvloop") |
| 34 | + |
| 35 | + from ragas.async_utils import run |
| 36 | + |
| 37 | + async def inner_task(): |
| 38 | + return "success" |
| 39 | + |
| 40 | + async def outer_task(): |
| 41 | + with pytest.raises(RuntimeError, match="Cannot execute nested async code"): |
| 42 | + run(inner_task) |
| 43 | + |
| 44 | + uvloop.install() |
| 45 | + try: |
| 46 | + asyncio.run(outer_task()) |
| 47 | + finally: |
| 48 | + asyncio.set_event_loop_policy(None) |
| 49 | + |
| 50 | + @pytest.mark.skipif(sys.version_info < (3, 8), reason="uvloop requires Python 3.8+") |
| 51 | + def test_run_async_tasks_with_uvloop(self): |
| 52 | + """Test that run_async_tasks works with uvloop.""" |
| 53 | + uvloop = pytest.importorskip("uvloop") |
| 54 | + |
| 55 | + from ragas.async_utils import run_async_tasks |
| 56 | + |
| 57 | + async def task(n): |
| 58 | + return n * 2 |
| 59 | + |
| 60 | + tasks = [task(i) for i in range(5)] |
| 61 | + |
| 62 | + uvloop.install() |
| 63 | + try: |
| 64 | + results = run_async_tasks(tasks, show_progress=False) |
| 65 | + assert sorted(results) == [0, 2, 4, 6, 8] |
| 66 | + finally: |
| 67 | + asyncio.set_event_loop_policy(None) |
| 68 | + |
| 69 | + def test_apply_nest_asyncio_without_uvloop_returns_true(self): |
| 70 | + """Test that apply_nest_asyncio returns True with standard asyncio.""" |
| 71 | + from ragas.async_utils import apply_nest_asyncio |
| 72 | + |
| 73 | + async def test_func(): |
| 74 | + result = apply_nest_asyncio() |
| 75 | + return result |
| 76 | + |
| 77 | + result = asyncio.run(test_func()) |
| 78 | + assert result is True |
| 79 | + |
| 80 | + def test_run_with_standard_asyncio_and_running_loop(self): |
| 81 | + """Test that run() works with standard asyncio in a running loop.""" |
| 82 | + from ragas.async_utils import run |
| 83 | + |
| 84 | + async def inner_task(): |
| 85 | + return "nested_success" |
| 86 | + |
| 87 | + async def outer_task(): |
| 88 | + result = run(inner_task) |
| 89 | + return result |
| 90 | + |
| 91 | + result = asyncio.run(outer_task()) |
| 92 | + assert result == "nested_success" |
0 commit comments