googleapis / googleapis/google-cloud-python

Properly configure async tests

Đang mở
#17,311 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
priority: p2 type: process
Ngôn ngữ chính
Python
Star
5.4k
Fork
1.8k
Merge trung bình
3 ngày 4 giờ
Pull request đã merge (30 ngày)
122

Mô tả

A recent release of `pytest-asyncio` ([v1.4.0](https://pypi.org/project/pytest-asyncio/1.4.0/)) caused several tests to fail with `RuntimeError: There is no current event loop in thread 'MainThread'` (see [comment here](https://github.com/googleapis/google-cloud-python/pull/17275#issuecomment-4558294736)).

As a quick workaround, [PR #17260](https://github.com/googleapis/google-cloud-python/pull/17260) introduced a temporary autouse fixture to manually manage the event loop:

```
@pytest.fixture(autouse=True)
def set_event_loop():
try:
asyncio.get_running_loop()
yield
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
yield
finally:
loop.close()
asyncio.set_event_loop(None)
```

**The Correct Long-Term Fix**

This workaround is a stopgap. The root cause is that tests containing asynchronous code are missing the proper `async def` declaration and the `@pytest.mark.asyncio` marker.

The correct long-term solution is to properly decorate and define these tests. For example, [PR #17308](https://github.com/googleapis/google-cloud-python/pull/17308) resolved this error ([CI run logs](https://github.com/googleapis/google-cloud-python/actions/runs/26642937390/job/78520665070)) by properly updating the test definitions (see [commit diff](https://github.com/googleapis/google-cloud-python/pull/17308/changes/4a9db423f8426ce19aeec016be80e0fd99791cd4)).

**Next Steps**

Please keep this issue open until the temporary `set_event_loop` fixture is entirely removed from the codebase and all affected tests are properly refactored with `async def` and `@pytest.mark.asyncio`.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.