Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /tests /test_asyncio_fix.py
| """ | |
| Tests für Python 3.13 Windows AsyncIO Fix | |
| Issue #10 | |
| """ | |
| import asyncio | |
| import platform | |
| import sys | |
| import pytest | |
| from core.asyncio_fix import ( | |
| AsyncIOContext, | |
| SafeAsyncioRunner, | |
| is_python_313_or_higher, | |
| is_windows, | |
| safe_asyncio_run, | |
| ) | |
| class TestAsyncIOFixUtils: | |
| """Test utility functions""" | |
| def test_is_windows(self): | |
| """Test Windows detection""" | |
| result = is_windows() | |
| expected = platform.system() == "Windows" | |
| assert result == expected | |
| def test_is_python_313_or_higher(self): | |
| """Test Python version detection""" | |
| result = is_python_313_or_higher() | |
| expected = sys.version_info >= (3, 13) | |
| assert result == expected | |
| class TestSafeAsyncioRunner: | |
| """Test SafeAsyncioRunner""" | |
| async def test_simple_coroutine(self): | |
| """Test running simple coroutine""" | |
| async def simple(): | |
| await asyncio.sleep(0.01) | |
| return "success" | |
| runner = SafeAsyncioRunner() | |
| result = runner.run(simple()) | |
| assert result == "success" | |
| async def test_coroutine_with_result(self): | |
| """Test coroutine returning data""" | |
| async def compute(): | |
| await asyncio.sleep(0.01) | |
| return {"data": [1, 2, 3], "status": "ok"} | |
| runner = SafeAsyncioRunner() | |
| result = runner.run(compute()) | |
| assert result["status"] == "ok" | |
| assert result["data"] == [1, 2, 3] | |
| async def test_coroutine_with_exception(self): | |
| """Test coroutine raising exception""" | |
| async def failing(): | |
| await asyncio.sleep(0.01) | |
| raise ValueError("Test error") | |
| runner = SafeAsyncioRunner() | |
| with pytest.raises(ValueError, match="Test error"): | |
| runner.run(failing()) | |
| async def test_timeout(self): | |
| """Test timeout functionality""" | |
| async def slow(): | |
| await asyncio.sleep(10) | |
| return "done" | |
| runner = SafeAsyncioRunner(timeout=0.1) | |
| with pytest.raises(asyncio.TimeoutError): | |
| runner.run(slow()) | |
| async def test_concurrent_tasks(self): | |
| """Test running multiple concurrent tasks""" | |
| async def task(name, delay): | |
| await asyncio.sleep(delay) | |
| return f"task_{name}" | |
| runner = SafeAsyncioRunner() | |
| # Run tasks | |
| results = [] | |
| for i in range(3): | |
| result = runner.run(task(i, 0.01)) | |
| results.append(result) | |
| assert len(results) == 3 | |
| assert all(r.startswith("task_") for r in results) | |
| class TestSafeAsyncioRun: | |
| """Test safe_asyncio_run function""" | |
| def test_basic_usage(self): | |
| """Test basic safe_asyncio_run usage""" | |
| async def coro(): | |
| return "hello" | |
| result = safe_asyncio_run(coro()) | |
| assert result == "hello" | |
| def test_with_timeout(self): | |
| """Test safe_asyncio_run with timeout""" | |
| async def slow_coro(): | |
| await asyncio.sleep(10) | |
| return "done" | |
| with pytest.raises(asyncio.TimeoutError): | |
| safe_asyncio_run(slow_coro(), timeout=0.1) | |
| class TestAsyncIOContext: | |
| """Test AsyncIOContext""" | |
| async def test_context_manager(self): | |
| """Test async context manager""" | |
| async with AsyncIOContext() as ctx: | |
| async def task(): | |
| return "context_result" | |
| result = await ctx.run_task(task()) | |
| assert result == "context_result" | |
| async def test_context_with_timeout(self): | |
| """Test context with timeout""" | |
| async with AsyncIOContext(timeout=0.1) as ctx: | |
| async def slow_task(): | |
| await asyncio.sleep(10) | |
| return "done" | |
| with pytest.raises(asyncio.TimeoutError): | |
| await ctx.run_task(slow_task()) | |
| class TestWindowsSpecific: | |
| """Windows-specific tests""" | |
| def test_windows_detection(self): | |
| """Verify Windows is detected correctly""" | |
| assert platform.system() == "Windows" | |
| def test_event_loop_policy(self): | |
| """Test that event loop policy is set on Windows""" | |
| # This test verifies the fix is applied | |
| from core.asyncio_fix import apply_windows_asyncio_fix | |
| # Apply fix | |
| apply_windows_asyncio_fix() | |
| # On Python 3.13+, should have WindowsSelectorEventLoopPolicy | |
| if is_python_313_or_higher(): | |
| policy = asyncio.get_event_loop_policy() | |
| assert isinstance(policy, asyncio.WindowsSelectorEventLoopPolicy) | |
| class TestIntegration: | |
| """Integration tests""" | |
| def test_multiple_sequential_runs(self): | |
| """Test multiple sequential asyncio runs""" | |
| async def counter(): | |
| await asyncio.sleep(0.001) | |
| return 1 | |
| results = [] | |
| for _ in range(5): | |
| result = safe_asyncio_run(counter()) | |
| results.append(result) | |
| assert results == [1, 1, 1, 1, 1] | |
| def test_nested_async_operations(self): | |
| """Test nested async operations""" | |
| async def inner(): | |
| await asyncio.sleep(0.001) | |
| return "inner" | |
| async def outer(): | |
| result = await inner() | |
| return f"outer_{result}" | |
| result = safe_asyncio_run(outer()) | |
| assert result == "outer_inner" | |
| if __name__ == "__main__": | |
| pytest.main([__file__, "-v"]) | |
Xet Storage Details
- Size:
- 5.78 kB
- Xet hash:
- a4df45f0d87110253150a8d2a53db1357c30176884c5d33e7eecceff9ccb22ef
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.