diff --git a/cloudbot/bot.py b/cloudbot/bot.py index 763cc786..e164ef05 100644 --- a/cloudbot/bot.py +++ b/cloudbot/bot.py @@ -23,7 +23,7 @@ from cloudbot.hook import Action from cloudbot.plugin import PluginManager from cloudbot.reloader import ConfigReloader, PluginReloader -from cloudbot.util import async_util, database, formatting +from cloudbot.util import database, formatting from cloudbot.util.mapping import KeyFoldDict logger = logging.getLogger("cloudbot") @@ -108,7 +108,7 @@ def __init__( self.running = True self.clients: Dict[str, Type[Client]] = {} # future which will be called when the bot stopsIf you - self.stopped_future = async_util.create_future(self.loop) + self.stopped_future = self.loop.create_future() # stores each bot server connection self.connections = KeyFoldDict() diff --git a/cloudbot/client.py b/cloudbot/client.py index 5dde041e..c71a6df9 100644 --- a/cloudbot/client.py +++ b/cloudbot/client.py @@ -5,7 +5,6 @@ from typing import Any, Dict from cloudbot.permissions import PermissionManager -from cloudbot.util import async_util logger = logging.getLogger("cloudbot") @@ -66,7 +65,7 @@ def __init__(self, bot, _type, name, nick, *, channels=None, config=None): self._active = False - self.cancelled_future = async_util.create_future(self.loop) + self.cancelled_future = self.loop.create_future() def describe_server(self): raise NotImplementedError diff --git a/cloudbot/clients/irc.py b/cloudbot/clients/irc.py index 6262dca0..e6b4503b 100644 --- a/cloudbot/clients/irc.py +++ b/cloudbot/clients/irc.py @@ -14,7 +14,7 @@ from cloudbot.client import Client, ClientConnectError, client from cloudbot.event import Event, EventType, IrcOutEvent -from cloudbot.util import async_util, colors +from cloudbot.util import colors logger = logging.getLogger("cloudbot") @@ -391,7 +391,7 @@ def _send(self, line, log=True): """ Sends a raw IRC line unchecked. Doesn't do connected check, and is *not* threadsafe """ - async_util.wrap_future( + asyncio.ensure_future( self._protocol.send(line, log=log), loop=self.loop ) @@ -423,7 +423,7 @@ def __init__(self, conn): self._transport = None # Future that waits until we are connected - self._connected_future = async_util.create_future(self.loop) + self._connected_future = self.loop.create_future() def connection_made(self, transport): self._transport = transport @@ -438,7 +438,7 @@ def connection_lost(self, exc): if exc: logger.error("[%s] Connection lost: %s", self.conn.name, exc) - async_util.wrap_future(self.conn.auto_reconnect(), loop=self.loop) + asyncio.ensure_future(self.conn.auto_reconnect(), loop=self.loop) def close(self): self._connecting = False @@ -522,7 +522,7 @@ def data_received(self, data): ) else: # handle the message, async - async_util.wrap_future(self.bot.process(event), loop=self.loop) + asyncio.ensure_future(self.bot.process(event), loop=self.loop) def parse_line(self, line: str) -> Event: message = Message.parse(line) diff --git a/cloudbot/plugin.py b/cloudbot/plugin.py index 2bbfa63d..4be51f27 100644 --- a/cloudbot/plugin.py +++ b/cloudbot/plugin.py @@ -42,7 +42,7 @@ SieveHook, hook_name_to_plugin, ) -from cloudbot.util import HOOK_ATTR, LOADED_ATTR, async_util, database +from cloudbot.util import HOOK_ATTR, LOADED_ATTR, database from cloudbot.util.func_utils import call_with_args logger = logging.getLogger("cloudbot") @@ -310,7 +310,7 @@ async def load_plugin(self, path): self._log_hook(on_cap_ack_hook) for periodic_hook in plugin.hooks["periodic"]: - task = async_util.wrap_future(self._start_periodic(periodic_hook)) + task = asyncio.ensure_future(self._start_periodic(periodic_hook)) plugin.tasks.append(task) self._log_hook(periodic_hook) @@ -566,7 +566,7 @@ async def internal_launch(self, hook, event): else: coro = self._execute_hook_sync(hook, event) - task = async_util.wrap_future(coro) + task = asyncio.ensure_future(coro) hook.plugin.tasks.append(task) try: out = await task @@ -621,7 +621,7 @@ async def _sieve(self, sieve, event, hook): coro = sieve.function(self.bot, event, hook) result, error = None, None - task = async_util.wrap_future(coro) + task = asyncio.ensure_future(coro) sieve.plugin.tasks.append(task) try: result = await task diff --git a/cloudbot/util/async_util.py b/cloudbot/util/async_util.py index 92970239..4c43308f 100644 --- a/cloudbot/util/async_util.py +++ b/cloudbot/util/async_util.py @@ -3,28 +3,10 @@ """ import asyncio -from asyncio import AbstractEventLoop -from asyncio.tasks import Task from functools import partial -from typing import List, Optional, cast from cloudbot.util.func_utils import call_with_args -try: - _asyncio_get_tasks = getattr(asyncio, "all_tasks") -except AttributeError: - _asyncio_get_tasks = getattr(Task, "all_tasks") - - -def wrap_future(fut, *, loop=None): - """ - Wraps asyncio.ensure_future() - :param fut: The awaitable, future, or coroutine to wrap - :param loop: The loop to run in - :return: The wrapped future - """ - return asyncio.ensure_future(fut, loop=loop) - async def run_func(loop, func, *args, **kwargs): part = partial(func, *args, **kwargs) @@ -56,14 +38,3 @@ def run_coroutine_threadsafe(coro, loop): raise TypeError("A coroutine object is required") asyncio.run_coroutine_threadsafe(coro, loop) - - -def create_future(loop): - return loop.create_future() - - -def get_all_tasks(loop: Optional[AbstractEventLoop] = None) -> List[Task]: - """ - Get a list of all tasks for the current loop - """ - return cast(List[Task], _asyncio_get_tasks(loop)) diff --git a/plugins/core/cap.py b/plugins/core/cap.py index 77ceec53..f9734683 100644 --- a/plugins/core/cap.py +++ b/plugins/core/cap.py @@ -35,7 +35,7 @@ async def handle_available_caps(conn, caplist, event, irc_paramlist, bot): ] results = await asyncio.gather(*tasks) if any(ok and (res or res is None) for ok, res in results): - cap_queue[name_cf] = async_util.create_future(conn.loop) + cap_queue[name_cf] = conn.loop.create_future() conn.cmd("CAP", "REQ", name) if irc_paramlist[2] != "*": diff --git a/plugins/core/sasl.py b/plugins/core/sasl.py index 12c9dc83..6cc0eab6 100644 --- a/plugins/core/sasl.py +++ b/plugins/core/sasl.py @@ -2,7 +2,6 @@ import logging from cloudbot import hook -from cloudbot.util import async_util logger = logging.getLogger("cloudbot") @@ -18,14 +17,14 @@ async def sasl_ack(conn): sasl_auth = conn.config.get("sasl") if sasl_auth and sasl_auth.get("enabled", True): sasl_mech = sasl_auth.get("mechanism", "PLAIN").upper() - auth_fut = async_util.create_future(conn.loop) + auth_fut = conn.loop.create_future() conn.memory["sasl_auth_future"] = auth_fut conn.cmd("AUTHENTICATE", sasl_mech) cmd, arg = await auth_fut if cmd == "908": logger.warning("[%s|sasl] SASL mechanism not supported", conn.name) elif cmd == "AUTHENTICATE" and arg[0] == "+": - num_fut = async_util.create_future(conn.loop) + num_fut = conn.loop.create_future() conn.memory["sasl_numeric_future"] = num_fut if sasl_mech == "PLAIN": auth_str = "{user}\0{user}\0{passwd}".format( diff --git a/requirements-dev.txt b/requirements-dev.txt index 97aa30e1..9e9d820b 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -4,7 +4,7 @@ mypy == 1.9.0 pre-commit == 3.3.3 pylint == 3.1.0 pytest == 8.1.1 -pytest-asyncio == 0.20.3 +pytest-asyncio == 0.23.5.post1 pytest-cov == 4.1.0 pytest-random-order == 1.1.1 responses == 0.25.0 diff --git a/tests/core_tests/irc_client_test.py b/tests/core_tests/irc_client_test.py index 7476dff7..8c5fdbb8 100644 --- a/tests/core_tests/irc_client_test.py +++ b/tests/core_tests/irc_client_test.py @@ -8,7 +8,6 @@ from cloudbot.client import ClientConnectError from cloudbot.clients import irc from cloudbot.event import Event, EventType -from cloudbot.util import async_util from tests.util.async_mock import AsyncMock if TYPE_CHECKING: @@ -53,7 +52,7 @@ def test_send_closed(event_loop): class TestLineParsing: @staticmethod def wait_tasks(conn, cancel=False): - tasks = async_util.get_all_tasks(conn.loop) + tasks = asyncio.all_tasks(conn.loop) if cancel: for task in tasks: task.cancel() @@ -440,8 +439,8 @@ def test_parse_pm_privmsg(self, caplog_bot, event_loop): class TestConnect: - async def make_client(self, event_loop) -> irc.IrcClient: - bot = MagicMock(loop=event_loop, config={}) + async def make_client(self) -> irc.IrcClient: + bot = MagicMock(loop=asyncio.get_running_loop(), config={}) conn_config = { "connection": { "server": "host.invalid", @@ -457,8 +456,8 @@ async def make_client(self, event_loop) -> irc.IrcClient: return client @pytest.mark.asyncio() - async def test_exc(self, caplog_bot, event_loop): - client = await self.make_client(event_loop) + async def test_exc(self, caplog_bot): + client = await self.make_client() runs = 0 # noinspection PyUnusedLocal @@ -520,8 +519,8 @@ async def connect(timeout): assert client.bot.mock_calls == [] @pytest.mark.asyncio() - async def test_timeout_exc(self, caplog_bot, event_loop): - client = await self.make_client(event_loop) + async def test_timeout_exc(self, caplog_bot): + client = await self.make_client() runs = 0 # noinspection PyUnusedLocal @@ -578,8 +577,8 @@ async def connect(timeout): assert client.bot.mock_calls == [] @pytest.mark.asyncio() - async def test_other_exc(self, caplog_bot, event_loop): - client = await self.make_client(event_loop) + async def test_other_exc(self, caplog_bot): + client = await self.make_client() client.connect = AsyncMock() # type: ignore client.connect.side_effect = Exception("foo") @@ -605,8 +604,8 @@ async def test_other_exc(self, caplog_bot, event_loop): assert client.bot.mock_calls == [] @pytest.mark.asyncio() - async def test_one_connect(self, caplog_bot, event_loop): - client = await self.make_client(event_loop) + async def test_one_connect(self, caplog_bot): + client = await self.make_client() async def _connect(timeout=5): await asyncio.sleep(timeout) @@ -636,8 +635,8 @@ async def _connect(timeout=5): assert client.bot.mock_calls == [] @pytest.mark.asyncio() - async def test_create_socket(self, caplog_bot, event_loop): - client = await self.make_client(event_loop) + async def test_create_socket(self, caplog_bot): + client = await self.make_client() client.loop.create_connection = mock = MagicMock() fut: "Future[Tuple[None, None]]" = asyncio.Future(loop=client.loop) fut.set_result((None, None)) @@ -668,14 +667,14 @@ async def test_create_socket(self, caplog_bot, event_loop): class TestSend: @pytest.mark.asyncio() - async def test_send_sieve_error(self, caplog_bot, event_loop): - conn = make_mock_conn(event_loop=event_loop) + async def test_send_sieve_error(self, caplog_bot): + conn = make_mock_conn(event_loop=asyncio.get_running_loop()) proto = irc._IrcProtocol(conn) proto.connection_made(MagicMock()) sieve = object() proto.bot.plugin_manager.out_sieves = [sieve] proto.bot.plugin_manager.internal_launch = launch = MagicMock() - fut = async_util.create_future(proto.loop) + fut = proto.loop.create_future() fut.set_result((False, None)) launch.return_value = fut diff --git a/tests/core_tests/reloader_test.py b/tests/core_tests/reloader_test.py index 2926579e..6341703a 100644 --- a/tests/core_tests/reloader_test.py +++ b/tests/core_tests/reloader_test.py @@ -9,10 +9,10 @@ class TestConfigReload: @pytest.mark.asyncio() - async def test_reload(self, mock_bot_factory, tmp_path, event_loop) -> None: + async def test_reload(self, mock_bot_factory, tmp_path) -> None: config_file = tmp_path / "config.json" config_file.touch() - bot = mock_bot_factory(loop=event_loop) + bot = mock_bot_factory() reloader = ConfigReloader(bot) bot.running = True with patch.object( @@ -25,12 +25,10 @@ async def test_reload(self, mock_bot_factory, tmp_path, event_loop) -> None: assert mocked.mock_calls == [call()] @pytest.mark.asyncio() - async def test_reload_not_running( - self, mock_bot_factory, tmp_path, event_loop - ): + async def test_reload_not_running(self, mock_bot_factory, tmp_path): config_file = tmp_path / "config.json" config_file.touch() - bot = mock_bot_factory(loop=event_loop) + bot = mock_bot_factory() reloader = ConfigReloader(bot) bot.running = False with patch.object( @@ -45,12 +43,12 @@ async def test_reload_not_running( class TestPluginReload: @pytest.mark.asyncio() - async def test_reload(self, mock_bot_factory, tmp_path, event_loop): + async def test_reload(self, mock_bot_factory, tmp_path): plugin_dir = tmp_path / "plugins" plugin_dir.mkdir() plugin_file = plugin_dir / "plugin.py" plugin_file.touch() - bot = mock_bot_factory(loop=event_loop) + bot = mock_bot_factory() reloader = PluginReloader(bot) with patch.object( reloader, "_reload", new_callable=AsyncMock @@ -62,11 +60,11 @@ async def test_reload(self, mock_bot_factory, tmp_path, event_loop): assert mocked.mock_calls == [call(Path(str(plugin_file)))] @pytest.mark.asyncio() - async def test_reload_no_path(self, mock_bot_factory, tmp_path, event_loop): + async def test_reload_no_path(self, mock_bot_factory, tmp_path): plugin_dir = tmp_path / "plugins" plugin_dir.mkdir() plugin_file = plugin_dir / "plugin.py" - bot = mock_bot_factory(loop=event_loop) + bot = mock_bot_factory() reloader = PluginReloader(bot) with patch.object( reloader, "_reload", new_callable=AsyncMock @@ -78,12 +76,12 @@ async def test_reload_no_path(self, mock_bot_factory, tmp_path, event_loop): assert mocked.mock_calls == [] @pytest.mark.asyncio() - async def test_unload(self, mock_bot_factory, tmp_path, event_loop): + async def test_unload(self, mock_bot_factory, tmp_path): plugin_dir = tmp_path / "plugins" plugin_dir.mkdir() plugin_file = plugin_dir / "plugin.py" plugin_file.touch() - bot = mock_bot_factory(loop=event_loop) + bot = mock_bot_factory() reloader = PluginReloader(bot) with patch.object( reloader, "_unload", new_callable=AsyncMock diff --git a/tests/core_tests/test_bot.py b/tests/core_tests/test_bot.py index 337d1173..9a2f67f9 100644 --- a/tests/core_tests/test_bot.py +++ b/tests/core_tests/test_bot.py @@ -16,9 +16,7 @@ @pytest.mark.asyncio() -async def test_migrate_db( - mock_db, mock_bot_factory, event_loop, mock_requests, tmp_path -): +async def test_migrate_db(mock_db, mock_bot_factory, mock_requests, tmp_path): old_db_url = "sqlite:///" + str(tmp_path / "database1.db") old_db = MockDB(old_db_url, True) table = Table( @@ -38,7 +36,6 @@ async def test_migrate_db( table.create(old_db.engine) other_table.create(old_db.engine) mock_bot = mock_bot_factory( - loop=event_loop, db=mock_db, config={"old_database": old_db_url, "migrate_db": True}, ) @@ -67,8 +64,8 @@ async def test_migrate_db( @pytest.mark.asyncio() -async def test_connect_clients(mock_bot_factory, event_loop): - bot = mock_bot_factory(loop=event_loop) +async def test_connect_clients(mock_bot_factory): + bot = mock_bot_factory() conn = MockConn() bot.connections = {"foo": conn} future = bot.loop.create_future() @@ -114,8 +111,8 @@ def __init__(self, nick=None): class TestProcessing: @pytest.mark.asyncio() - async def test_irc_catch_all(self, mock_bot_factory, event_loop) -> None: - bot = mock_bot_factory(loop=event_loop) + async def test_irc_catch_all(self, mock_bot_factory) -> None: + bot = mock_bot_factory() conn = MockConn(nick="bot") event = Event( irc_command="PRIVMSG", @@ -146,10 +143,8 @@ async def coro(hook): ) @pytest.mark.asyncio() - async def test_irc_catch_all_block( - self, mock_bot_factory, event_loop - ) -> None: - bot = mock_bot_factory(loop=event_loop) + async def test_irc_catch_all_block(self, mock_bot_factory) -> None: + bot = mock_bot_factory() conn = MockConn(nick="bot") event = Event( irc_command="PRIVMSG", @@ -186,8 +181,8 @@ async def coro1(hook): # pragma: no cover ) @pytest.mark.asyncio() - async def test_command(self, mock_bot_factory, event_loop) -> None: - bot = mock_bot_factory(loop=event_loop) + async def test_command(self, mock_bot_factory) -> None: + bot = mock_bot_factory() conn = MockConn(nick="bot") event = Event( irc_command="PRIVMSG", @@ -220,8 +215,8 @@ async def coro(hook): ) @pytest.mark.asyncio() - async def test_command_partial(self, mock_bot_factory, event_loop) -> None: - bot = mock_bot_factory(loop=event_loop) + async def test_command_partial(self, mock_bot_factory) -> None: + bot = mock_bot_factory() conn = MockConn(nick="bot") event = Event( irc_command="PRIVMSG", @@ -256,8 +251,8 @@ async def coro(hook): # pragma: no cover ] @pytest.mark.asyncio() - async def test_event(self, mock_bot_factory, event_loop) -> None: - bot = mock_bot_factory(loop=event_loop) + async def test_event(self, mock_bot_factory) -> None: + bot = mock_bot_factory() conn = MockConn(nick="bot") event = Event( irc_command="PRIVMSG", @@ -291,8 +286,8 @@ async def coro(hook): ) @pytest.mark.asyncio() - async def test_event_block(self, mock_bot_factory, event_loop) -> None: - bot = mock_bot_factory(loop=event_loop) + async def test_event_block(self, mock_bot_factory) -> None: + bot = mock_bot_factory() conn = MockConn(nick="bot") event = Event( irc_command="PRIVMSG", @@ -338,8 +333,8 @@ async def coro1(hook): # pragma: no cover ) @pytest.mark.asyncio() - async def test_irc_raw(self, mock_bot_factory, event_loop) -> None: - bot = mock_bot_factory(loop=event_loop) + async def test_irc_raw(self, mock_bot_factory) -> None: + bot = mock_bot_factory() conn = MockConn(nick="bot") event = Event( irc_command="PRIVMSG", @@ -369,8 +364,8 @@ async def coro(hook): ) @pytest.mark.asyncio() - async def test_irc_raw_block(self, mock_bot_factory, event_loop): - bot = mock_bot_factory(loop=event_loop) + async def test_irc_raw_block(self, mock_bot_factory): + bot = mock_bot_factory() conn = MockConn(nick="bot") event = Event( irc_command="PRIVMSG", @@ -408,8 +403,8 @@ async def coro1(hook): # pragma: no cover @pytest.mark.asyncio() -async def test_reload_config(mock_bot_factory, event_loop): - bot = mock_bot_factory(loop=event_loop) +async def test_reload_config(mock_bot_factory): + bot = mock_bot_factory() conn = MockConn() bot.connections = {"foo": conn} bot.config.load_config = MagicMock() diff --git a/tests/core_tests/test_client.py b/tests/core_tests/test_client.py index 892a8c29..111b8f04 100644 --- a/tests/core_tests/test_client.py +++ b/tests/core_tests/test_client.py @@ -5,12 +5,6 @@ from cloudbot.client import Client -class Bot(MagicMock): - def __init__(self, loop, *args, **kw): - super().__init__(*args, **kw) - self.loop = loop - - class MockClient(Client): # pylint: disable=abstract-method _connected = False @@ -37,21 +31,21 @@ async def connect(self, timeout=None): raise ValueError("This is a test") -def test_reload(event_loop): - client = MockClient(Bot(event_loop), "foo", "foobot", channels=["#foo"]) +def test_reload(mock_bot): + client = MockClient(mock_bot, "foo", "foobot", channels=["#foo"]) client.permissions = MagicMock() client.reload() assert client.permissions.mock_calls == [call.reload()] -def test_client_no_config(event_loop): - client = MockClient(Bot(event_loop), "foo", "foobot", channels=["#foo"]) +def test_client_no_config(mock_bot): + client = MockClient(mock_bot, "foo", "foobot", channels=["#foo"]) assert client.config.get("a") is None -def test_client(event_loop): +def test_client(mock_bot): client = MockClient( - Bot(event_loop), + mock_bot, "foo", "foobot", channels=["#foo"], @@ -70,10 +64,10 @@ def test_client(event_loop): client.loop.run_until_complete(client.try_connect()) -def test_client_connect_exc(event_loop): +def test_client_connect_exc(mock_bot): with patch("random.randrange", return_value=1): client = FailingMockClient( - Bot(event_loop), + mock_bot, "foo", "foobot", channels=["#foo"], @@ -84,9 +78,9 @@ def test_client_connect_exc(event_loop): @pytest.mark.asyncio() -async def test_try_connect(event_loop): +async def test_try_connect(mock_bot): client = MockClient( - Bot(event_loop), + mock_bot, "foo", "foobot", channels=["#foo"], @@ -97,9 +91,9 @@ async def test_try_connect(event_loop): await client.try_connect() -def test_auto_reconnect(event_loop): +def test_auto_reconnect(mock_bot): client = MockClient( - Bot(event_loop), + mock_bot, "foo", "foobot", channels=["#foo"], diff --git a/tests/core_tests/test_plugin_manager.py b/tests/core_tests/test_plugin_manager.py index 7f48de01..b3b6e71a 100644 --- a/tests/core_tests/test_plugin_manager.py +++ b/tests/core_tests/test_plugin_manager.py @@ -14,12 +14,13 @@ from cloudbot.util import database from tests.util.mock_module import MockModule + @pytest.fixture() -def mock_bot(mock_bot_factory, event_loop, tmp_path): +def mock_bot(mock_bot_factory, tmp_path): tmp_base = tmp_path / "tmp" tmp_base.mkdir(exist_ok=True) - yield mock_bot_factory(base_dir=tmp_base, loop=event_loop) + yield mock_bot_factory(base_dir=tmp_base) @pytest.fixture() @@ -765,11 +766,9 @@ async def post_hook(db): @pytest.mark.asyncio -async def test_create_tables( - mock_bot_factory, caplog_bot, tmp_path, event_loop, mock_db -): +async def test_create_tables(mock_bot_factory, caplog_bot, tmp_path, mock_db): db = mock_db - bot = mock_bot_factory(db=db, loop=event_loop) + bot = mock_bot_factory(db=db) table = Table( "test", database.metadata, diff --git a/tests/plugin_tests/cap_test.py b/tests/plugin_tests/cap_test.py index 1351a2f4..de75db9e 100644 --- a/tests/plugin_tests/cap_test.py +++ b/tests/plugin_tests/cap_test.py @@ -1,3 +1,4 @@ +import asyncio from pathlib import Path from unittest.mock import MagicMock, patch @@ -7,19 +8,12 @@ from cloudbot import hook from cloudbot.event import Event from cloudbot.plugin import PluginManager -from cloudbot.util import async_util from plugins.core import cap from tests.util.mock_module import MockModule -@pytest.fixture() -def patch_import_module(): - with patch("importlib.import_module") as mocked: - yield mocked - - @pytest.mark.asyncio() -async def test_cap_req(patch_import_module, event_loop): +async def test_cap_req(patch_import_module): caps = [ "some-cap", "another-cap", @@ -35,7 +29,7 @@ async def test_cap_req(patch_import_module, event_loop): bot=MagicMock(), conn=MagicMock(), ) - event.conn.loop = event.bot.loop = event_loop + event.conn.loop = event.bot.loop = asyncio.get_running_loop() event.bot.config = {} event.conn.type = "irc" event.bot.base_dir = Path(".").resolve() @@ -71,7 +65,7 @@ def cmd(cmd, subcmd, *args): bot=event.bot, conn=event.conn, ) - async_util.wrap_future(cap.on_cap(p, cmd_event), loop=event.loop) + asyncio.ensure_future(cap.on_cap(p, cmd_event), loop=event.loop) with patch.object(event.conn, "cmd", new=cmd): res = await cap.on_cap(params, event) diff --git a/tests/plugin_tests/core_sieve_test.py b/tests/plugin_tests/core_sieve_test.py index e478d8b1..d86e2fb7 100644 --- a/tests/plugin_tests/core_sieve_test.py +++ b/tests/plugin_tests/core_sieve_test.py @@ -154,7 +154,7 @@ def test_check_acls_no_chan() -> None: @pytest.mark.asyncio() -async def test_permissions(event_loop) -> None: +async def test_permissions() -> None: event = make_command_event() event.hook.permissions = ["admin"] @@ -168,7 +168,7 @@ async def test_permissions(event_loop) -> None: @pytest.mark.asyncio() -async def test_permissions_no_perms(event_loop) -> None: +async def test_permissions_no_perms() -> None: event = make_command_event() event.hook.permissions = [] diff --git a/tests/plugin_tests/test_admin_bot.py b/tests/plugin_tests/test_admin_bot.py index 421b0920..f192f852 100644 --- a/tests/plugin_tests/test_admin_bot.py +++ b/tests/plugin_tests/test_admin_bot.py @@ -1,3 +1,4 @@ +import asyncio from unittest.mock import MagicMock, call import pytest @@ -31,8 +32,9 @@ def test_parse_targets(text, chan, out): @pytest.mark.asyncio -async def test_reload_config(event_loop): +async def test_reload_config(): bot = MagicMock() + event_loop = asyncio.get_running_loop() future = event_loop.create_future() bot.reload_config.return_value = future future.set_result(True) @@ -51,7 +53,7 @@ async def test_reload_config(event_loop): ("channel", "#channel", None), ], ) -async def test_join(input_text, chan, key, event_loop): +async def test_join(input_text, chan, key): conn = MagicMock() conn.config = {} conn.bot = None @@ -67,7 +69,9 @@ async def test_join(input_text, chan, key, event_loop): nick="foobaruser", ) - await async_util.run_func_with_args(event_loop, admin_bot.join, event) + await async_util.run_func_with_args( + asyncio.get_running_loop(), admin_bot.join, event + ) event.conn.join.assert_called_with(chan, key) diff --git a/tests/plugin_tests/test_chan_log.py b/tests/plugin_tests/test_chan_log.py index afa352f9..8ce21dab 100644 --- a/tests/plugin_tests/test_chan_log.py +++ b/tests/plugin_tests/test_chan_log.py @@ -6,7 +6,7 @@ def test_format_exception_chain(): def _get_data(exc): yield repr(exc) - if hasattr(exc, 'add_note'): + if hasattr(exc, "add_note"): yield f" add_note = {exc.add_note!r}" yield f" args = {exc.args!r}" diff --git a/tests/plugin_tests/test_core_connect.py b/tests/plugin_tests/test_core_connect.py index 76845d7c..9f256718 100644 --- a/tests/plugin_tests/test_core_connect.py +++ b/tests/plugin_tests/test_core_connect.py @@ -14,8 +14,8 @@ async def connect(self, timeout=None): pass -def test_ssl_client(event_loop, mock_bot_factory): - bot = mock_bot_factory(loop=event_loop) +def test_ssl_client(mock_bot_factory): + bot = mock_bot_factory() client = MockClient( bot, "mock", @@ -38,8 +38,8 @@ def test_ssl_client(event_loop, mock_bot_factory): assert client.ssl_context.verify_mode is ssl.CERT_REQUIRED -def test_ssl_client_no_verify(event_loop, mock_bot_factory): - bot = mock_bot_factory(loop=event_loop) +def test_ssl_client_no_verify(mock_bot_factory): + bot = mock_bot_factory() client = MockClient( bot, "mock", @@ -64,8 +64,8 @@ def test_ssl_client_no_verify(event_loop, mock_bot_factory): @pytest.mark.asyncio() -async def test_core_connects(event_loop, mock_bot_factory): - bot = mock_bot_factory(loop=event_loop) +async def test_core_connects(mock_bot_factory): + bot = mock_bot_factory() client = MockClient( bot, "mock", diff --git a/tests/plugin_tests/test_core_misc.py b/tests/plugin_tests/test_core_misc.py index 96105c50..bdefe1d3 100644 --- a/tests/plugin_tests/test_core_misc.py +++ b/tests/plugin_tests/test_core_misc.py @@ -15,9 +15,9 @@ def __init__(self, bot, *args, **kwargs): @pytest.mark.asyncio -async def test_do_joins(mock_bot_factory, event_loop): +async def test_do_joins(mock_bot_factory): client = MockClient( - mock_bot_factory(loop=event_loop), + mock_bot_factory(), "foo", "foobot", channels=[ diff --git a/tests/plugin_tests/test_ignore.py b/tests/plugin_tests/test_ignore.py index a5953fc2..36354208 100644 --- a/tests/plugin_tests/test_ignore.py +++ b/tests/plugin_tests/test_ignore.py @@ -93,7 +93,7 @@ def test_ignore_case(mock_db): @pytest.mark.asyncio -async def test_ignore_sieve(mock_db, event_loop): +async def test_ignore_sieve(mock_db): setup_db(mock_db) sess = mock_db.session() diff --git a/tests/plugin_tests/test_mylife.py b/tests/plugin_tests/test_mylife.py index b98ccc4b..111893be 100644 --- a/tests/plugin_tests/test_mylife.py +++ b/tests/plugin_tests/test_mylife.py @@ -1,3 +1,4 @@ +import asyncio from unittest.mock import MagicMock import pytest @@ -6,7 +7,7 @@ @pytest.mark.asyncio() -async def test_mylife(mock_requests, event_loop): +async def test_mylife(mock_requests): mock_requests.add( "GET", "http://www.fmylife.com/random", @@ -83,6 +84,6 @@ async def test_mylife(mock_requests, event_loop): """, ) reply = MagicMock() - res = await mylife.fml(reply, event_loop) + res = await mylife.fml(reply, loop=asyncio.get_running_loop()) assert res is None reply.assert_called_with("(#132473) Today, bar fml") diff --git a/tests/plugin_tests/test_remind.py b/tests/plugin_tests/test_remind.py index afb619de..c3415392 100644 --- a/tests/plugin_tests/test_remind.py +++ b/tests/plugin_tests/test_remind.py @@ -208,10 +208,9 @@ async def test_no_conn( mock_db, setup_db, freeze_time, - event_loop, ): await remind.load_cache(async_call, mock_db.session()) - bot = mock_bot_factory(loop=event_loop) + bot = mock_bot_factory() bot.connections = {} mock_conn = MagicMock() mock_conn.name = "test" @@ -239,10 +238,9 @@ async def test_conn_not_ready( mock_db, setup_db, freeze_time, - event_loop, ): await remind.load_cache(async_call, mock_db.session()) - bot = mock_bot_factory(loop=event_loop) + bot = mock_bot_factory() mock_conn = MagicMock() mock_conn.name = "test" mock_conn.ready = False @@ -270,10 +268,9 @@ async def test_late( mock_db, setup_db, freeze_time, - event_loop, ): await remind.load_cache(async_call, mock_db.session()) - bot = mock_bot_factory(loop=event_loop) + bot = mock_bot_factory() mock_conn = MagicMock() mock_conn.name = "test" mock_conn.ready = True @@ -303,10 +300,9 @@ async def test_normal( mock_db, setup_db, freeze_time, - event_loop, ): await remind.load_cache(async_call, mock_db.session()) - bot = mock_bot_factory(loop=event_loop) + bot = mock_bot_factory() mock_conn = MagicMock() mock_conn.name = "test" mock_conn.ready = True diff --git a/tests/util/mock_bot.py b/tests/util/mock_bot.py index 0ea6d6b8..ab0b0e8d 100644 --- a/tests/util/mock_bot.py +++ b/tests/util/mock_bot.py @@ -6,7 +6,6 @@ from cloudbot.bot import CloudBot from cloudbot.client import Client from cloudbot.plugin import PluginManager -from cloudbot.util.async_util import create_future from tests.util.mock_config import MockConfig from tests.util.mock_db import MockDB @@ -28,7 +27,7 @@ def __init__( self.data_dir = str(self.data_path) self.plugin_dir = self.base_dir / "plugins" if self.loop: - self.stopped_future: Awaitable[bool] = create_future(self.loop) + self.stopped_future: Awaitable[bool] = self.loop.create_future() else: self.stopped_future = None