2022-01-20 02:05:57 +08:00
|
|
|
from contextlib import asynccontextmanager
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from utils import make_fake_event
|
|
|
|
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
@pytest.mark.anyio
|
2022-01-20 02:05:57 +08:00
|
|
|
async def test_matcher_mutex():
|
2022-01-20 03:16:04 +08:00
|
|
|
from nonebot.plugins.single_session import matcher_mutex, _running_matcher
|
2022-01-20 02:05:57 +08:00
|
|
|
|
|
|
|
am = asynccontextmanager(matcher_mutex)
|
|
|
|
event = make_fake_event()()
|
|
|
|
event_1 = make_fake_event()()
|
|
|
|
event_2 = make_fake_event(_session_id="test1")()
|
2022-01-20 03:16:04 +08:00
|
|
|
event_3 = make_fake_event(_session_id=None)()
|
2022-01-20 02:05:57 +08:00
|
|
|
|
|
|
|
async with am(event) as ctx:
|
2023-06-24 14:47:35 +08:00
|
|
|
assert ctx is False
|
2022-01-20 03:16:04 +08:00
|
|
|
assert not _running_matcher
|
2022-01-20 02:05:57 +08:00
|
|
|
|
|
|
|
async with am(event) as ctx:
|
|
|
|
async with am(event_1) as ctx_1:
|
2023-06-24 14:47:35 +08:00
|
|
|
assert ctx is False
|
|
|
|
assert ctx_1 is True
|
2022-01-20 03:16:04 +08:00
|
|
|
assert not _running_matcher
|
2022-01-20 02:05:57 +08:00
|
|
|
|
|
|
|
async with am(event) as ctx:
|
|
|
|
async with am(event_2) as ctx_2:
|
2023-06-24 14:47:35 +08:00
|
|
|
assert ctx is False
|
|
|
|
assert ctx_2 is False
|
2022-01-20 03:16:04 +08:00
|
|
|
assert not _running_matcher
|
|
|
|
|
|
|
|
async with am(event_3) as ctx_3:
|
2023-06-24 14:47:35 +08:00
|
|
|
assert ctx_3 is False
|
2022-01-20 03:16:04 +08:00
|
|
|
assert not _running_matcher
|