Feature: 迁移至结构化并发框架 AnyIO (#3053)

This commit is contained in:
Ju4tCode
2024-10-26 15:36:01 +08:00
committed by GitHub
parent bd9befbb55
commit ff21ceb946
39 changed files with 5422 additions and 4080 deletions

View File

@ -17,7 +17,7 @@ from nonebot.drivers import (
)
@pytest.mark.asyncio
@pytest.mark.anyio
async def test_adapter_connect(app: App, driver: Driver):
last_connect_bot: Optional[Bot] = None
last_disconnect_bot: Optional[Bot] = None
@ -45,7 +45,6 @@ async def test_adapter_connect(app: App, driver: Driver):
assert bot.self_id not in adapter.bots
@pytest.mark.asyncio
@pytest.mark.parametrize(
"driver",
[
@ -75,7 +74,7 @@ async def test_adapter_connect(app: App, driver: Driver):
],
indirect=True,
)
async def test_adapter_server(driver: Driver):
def test_adapter_server(driver: Driver):
last_http_setup: Optional[HTTPServerSetup] = None
last_ws_setup: Optional[WebSocketServerSetup] = None
@ -112,7 +111,7 @@ async def test_adapter_server(driver: Driver):
assert last_ws_setup is setup
@pytest.mark.asyncio
@pytest.mark.anyio
@pytest.mark.parametrize(
"driver",
[
@ -159,7 +158,7 @@ async def test_adapter_http_client(driver: Driver):
assert last_request is request
@pytest.mark.asyncio
@pytest.mark.anyio
@pytest.mark.parametrize(
"driver",
[

View File

@ -1,5 +1,6 @@
from typing import Any, Optional
import anyio
import pytest
from nonebug import App
@ -7,7 +8,7 @@ from nonebot.adapters import Bot
from nonebot.exception import MockApiException
@pytest.mark.asyncio
@pytest.mark.anyio
async def test_bot_call_api(app: App):
async with app.test_api() as ctx:
bot = ctx.create_bot()
@ -23,7 +24,7 @@ async def test_bot_call_api(app: App):
await bot.call_api("test")
@pytest.mark.asyncio
@pytest.mark.anyio
async def test_bot_calling_api_hook_simple(app: App):
runned: bool = False
@ -49,7 +50,7 @@ async def test_bot_calling_api_hook_simple(app: App):
assert result is True
@pytest.mark.asyncio
@pytest.mark.anyio
async def test_bot_calling_api_hook_mock(app: App):
runned: bool = False
@ -76,7 +77,47 @@ async def test_bot_calling_api_hook_mock(app: App):
assert result is False
@pytest.mark.asyncio
@pytest.mark.anyio
async def test_bot_calling_api_hook_multi_mock(app: App):
runned1: bool = False
runned2: bool = False
event = anyio.Event()
async def calling_api_hook1(bot: Bot, api: str, data: dict[str, Any]):
nonlocal runned1
runned1 = True
event.set()
raise MockApiException(1)
async def calling_api_hook2(bot: Bot, api: str, data: dict[str, Any]):
nonlocal runned2
runned2 = True
with anyio.fail_after(1):
await event.wait()
raise MockApiException(2)
hooks = set()
with pytest.MonkeyPatch.context() as m:
m.setattr(Bot, "_calling_api_hook", hooks)
Bot.on_calling_api(calling_api_hook1)
Bot.on_calling_api(calling_api_hook2)
assert hooks == {calling_api_hook1, calling_api_hook2}
async with app.test_api() as ctx:
bot = ctx.create_bot()
result = await bot.call_api("test")
assert runned1 is True
assert runned2 is True
assert result == 1
@pytest.mark.anyio
async def test_bot_called_api_hook_simple(app: App):
runned: bool = False
@ -108,7 +149,7 @@ async def test_bot_called_api_hook_simple(app: App):
assert result is True
@pytest.mark.asyncio
@pytest.mark.anyio
async def test_bot_called_api_hook_mock(app: App):
runned: bool = False
@ -150,3 +191,56 @@ async def test_bot_called_api_hook_mock(app: App):
assert runned is True
assert result is False
@pytest.mark.anyio
async def test_bot_called_api_hook_multi_mock(app: App):
runned1: bool = False
runned2: bool = False
event = anyio.Event()
async def called_api_hook1(
bot: Bot,
exception: Optional[Exception],
api: str,
data: dict[str, Any],
result: Any,
):
nonlocal runned1
runned1 = True
event.set()
raise MockApiException(1)
async def called_api_hook2(
bot: Bot,
exception: Optional[Exception],
api: str,
data: dict[str, Any],
result: Any,
):
nonlocal runned2
runned2 = True
with anyio.fail_after(1):
await event.wait()
raise MockApiException(2)
hooks = set()
with pytest.MonkeyPatch.context() as m:
m.setattr(Bot, "_called_api_hook", hooks)
Bot.on_called_api(called_api_hook1)
Bot.on_called_api(called_api_hook2)
assert hooks == {called_api_hook1, called_api_hook2}
async with app.test_api() as ctx:
bot = ctx.create_bot()
ctx.should_call_api("test", {}, True)
result = await bot.call_api("test")
assert runned1 is True
assert runned2 is True
assert result == 1