Commit 29ebfe30 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Update anyio support for tests

* Move the module to avoid name conflicts when test framework adds test
  directory to sys.paths
* Add typing
* Add >=3.8 mocks support
parent 5dd46726
Loading
Loading
Loading
Loading
+19 −8
Original line number Diff line number Diff line
#  Copyright 2019  Dom Sekotill <dom.sekotill@kodo.org.uk>
#  Copyright 2019-2021  Dom Sekotill <dom.sekotill@kodo.org.uk>
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
@@ -16,24 +16,33 @@
Anyio helpers for unit tests
"""

import sys
from functools import wraps
from typing import Any
from typing import Callable
from typing import Tuple
from unittest import mock

import anyio

py_version = sys.version_info[:2]

def with_anyio(timeout=10):
AnyFunc = Callable[..., Any]
Decorator = Callable[[AnyFunc], AnyFunc]


def with_anyio(timeout: int = 10) -> Decorator:
	"""
	Create a wrapping decorator to run asynchronous test functions
	"""

	def decorator(testfunc):
		async def test_async_wrapper(args):
	def decorator(testfunc: AnyFunc) -> AnyFunc:
		async def test_async_wrapper(args: Tuple[Any]) -> Any:
			async with anyio.fail_after(timeout):
				return await testfunc(*args)

		@wraps(testfunc)
		def test_wrapper(*args):
		def test_wrapper(*args: Any) -> Any:
			return anyio.run(test_async_wrapper, args)

		return test_wrapper
@@ -46,11 +55,13 @@ class AsyncMock(mock.Mock):
	A Mock class that acts as a coroutine when called
	"""

	def __init__(self, *args, delay=0, **kwargs):
		mock._safe_super(AsyncMock, self).__init__(*args, **kwargs)
	def __init__(self, *args: Any, delay: float = 0.0, **kwargs: Any):
		mock._safe_super(AsyncMock, self).__init__(*args, **kwargs)  # type: ignore
		self.delay = delay

	async def __call__(_mock_self, *args, **kwargs):
	async def __call__(_mock_self, *args: Any, **kwargs: Any) -> Any:
		_mock_self._mock_check_sig(*args, **kwargs)
		if py_version >= (3, 8):
			_mock_self._increment_mock_call(*args, **kwargs)
		await anyio.sleep(_mock_self.delay)
		return _mock_self._mock_call(*args, **kwargs)
+3 −2
Original line number Diff line number Diff line
#  Copyright 2019  Dom Sekotill <dom.sekotill@kodo.org.uk>
#  Copyright 2019-2021  Dom Sekotill <dom.sekotill@kodo.org.uk>
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
@@ -20,7 +20,8 @@ import unittest
from unittest import mock

import anyio
from tests.unit import anyio as anyio_mock

from tests.unit import _anyio as anyio_mock
from wpa_supplicant import errors
from wpa_supplicant.client import base

+3 −2
Original line number Diff line number Diff line
#  Copyright 2019  Dom Sekotill <dom.sekotill@kodo.org.uk>
#  Copyright 2019-2021  Dom Sekotill <dom.sekotill@kodo.org.uk>
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
@@ -20,7 +20,8 @@ import unittest
from unittest.mock import call

import anyio
from tests.unit import anyio as anyio_mock

from tests.unit import _anyio as anyio_mock
from wpa_supplicant import config
from wpa_supplicant.client import interfaces

+3 −2
Original line number Diff line number Diff line
#  Copyright 2019  Dom Sekotill <dom.sekotill@kodo.org.uk>
#  Copyright 2019-2021  Dom Sekotill <dom.sekotill@kodo.org.uk>
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
@@ -21,7 +21,8 @@ import unittest
from unittest.mock import patch

import anyio
from tests.unit import anyio as anyio_mock

from tests.unit import _anyio as anyio_mock
from wpa_supplicant.client import interfaces
from wpa_supplicant.client import master