[tor-commits] [stem/master] Move utility functions for asynchronous tests to `stem.util.test_tools`

atagar at torproject.org atagar at torproject.org
Thu Jul 16 01:28:59 UTC 2020


commit 2b8b8fd51c74af4509f8745f22d081191dbe63b5
Author: Illia Volochii <illia.volochii at gmail.com>
Date:   Thu Apr 30 19:52:42 2020 +0300

    Move utility functions for asynchronous tests to `stem.util.test_tools`
---
 stem/util/test_tools.py                 | 26 ++++++++++++++++++++++++++
 test/async_util.py                      | 26 --------------------------
 test/integ/connection/authentication.py |  2 +-
 test/integ/connection/connect.py        |  2 +-
 test/integ/control/base_controller.py   |  2 +-
 test/integ/control/controller.py        |  2 +-
 test/integ/manual.py                    |  2 +-
 test/integ/process.py                   |  3 +--
 test/integ/response/protocolinfo.py     |  3 +--
 test/integ/socket/control_message.py    |  2 +-
 test/integ/socket/control_socket.py     |  2 +-
 test/integ/util/connection.py           |  2 +-
 test/integ/util/proc.py                 |  2 +-
 test/integ/version.py                   |  2 +-
 test/unit/connection/authentication.py  |  2 +-
 test/unit/connection/connect.py         |  2 +-
 test/unit/control/controller.py         |  2 +-
 17 files changed, 41 insertions(+), 43 deletions(-)

diff --git a/stem/util/test_tools.py b/stem/util/test_tools.py
index 36b4110e..f3c736a1 100644
--- a/stem/util/test_tools.py
+++ b/stem/util/test_tools.py
@@ -30,7 +30,9 @@ to match just against the prefix or suffix. For instance...
   type_issues - checks for type problems
 """
 
+import asyncio
 import collections
+import functools
 import linecache
 import multiprocessing
 import os
@@ -680,3 +682,27 @@ def _is_ignored(config: Mapping[str, Sequence[str]], path: str, issue: str) -> b
             return True  # suffix match
 
   return False
+
+
+def async_test(func: Callable) -> Callable:
+  @functools.wraps(func)
+  def wrapper(*args, **kwargs):
+    loop = asyncio.new_event_loop()
+    try:
+      result = loop.run_until_complete(func(*args, **kwargs))
+    finally:
+      loop.close()
+    return result
+  return wrapper
+
+
+def coro_func_returning_value(return_value):
+  async def coroutine_func(*args, **kwargs):
+    return return_value
+  return coroutine_func
+
+
+def coro_func_raising_exc(exc):
+  async def coroutine_func(*args, **kwargs):
+    raise exc
+  return coroutine_func
diff --git a/test/async_util.py b/test/async_util.py
deleted file mode 100644
index aa8a6dd0..00000000
--- a/test/async_util.py
+++ /dev/null
@@ -1,26 +0,0 @@
-import asyncio
-import functools
-
-
-def async_test(func):
-  @functools.wraps(func)
-  def wrapper(*args, **kwargs):
-    loop = asyncio.new_event_loop()
-    try:
-      result = loop.run_until_complete(func(*args, **kwargs))
-    finally:
-      loop.close()
-    return result
-  return wrapper
-
-
-def coro_func_returning_value(return_value):
-  async def coroutine_func(*args, **kwargs):
-    return return_value
-  return coroutine_func
-
-
-def coro_func_raising_exc(exc):
-  async def coroutine_func(*args, **kwargs):
-    raise exc
-  return coroutine_func
diff --git a/test/integ/connection/authentication.py b/test/integ/connection/authentication.py
index b992ac9a..683e555f 100644
--- a/test/integ/connection/authentication.py
+++ b/test/integ/connection/authentication.py
@@ -12,7 +12,7 @@ import stem.version
 import test
 import test.require
 import test.runner
-from test.async_util import async_test
+from stem.util.test_tools import async_test
 
 # Responses given by tor for various authentication failures. These may change
 # in the future and if they do then this test should be updated.
diff --git a/test/integ/connection/connect.py b/test/integ/connection/connect.py
index b1d2a672..399598bc 100644
--- a/test/integ/connection/connect.py
+++ b/test/integ/connection/connect.py
@@ -8,7 +8,7 @@ import unittest
 import stem.connection
 import test.require
 import test.runner
-from test.async_util import async_test
+from stem.util.test_tools import async_test
 
 from unittest.mock import patch
 
diff --git a/test/integ/control/base_controller.py b/test/integ/control/base_controller.py
index ff51e2f1..ac5f1e56 100644
--- a/test/integ/control/base_controller.py
+++ b/test/integ/control/base_controller.py
@@ -15,7 +15,7 @@ import stem.socket
 import stem.util.system
 import test.require
 import test.runner
-from test.async_util import async_test
+from stem.util.test_tools import async_test
 
 
 class StateObserver(object):
diff --git a/test/integ/control/controller.py b/test/integ/control/controller.py
index ab1e76c3..73e71fa4 100644
--- a/test/integ/control/controller.py
+++ b/test/integ/control/controller.py
@@ -23,11 +23,11 @@ import test
 import test.network
 import test.require
 import test.runner
-from test.async_util import async_test
 
 from stem import Flag, Signal
 from stem.control import EventType, Listener, State
 from stem.exit_policy import ExitPolicy
+from stem.util.test_tools import async_test
 
 # Router status entry for a relay with a nickname other than 'Unnamed'. This is
 # used for a few tests that need to look up a relay.
diff --git a/test/integ/manual.py b/test/integ/manual.py
index df0c0105..d285c758 100644
--- a/test/integ/manual.py
+++ b/test/integ/manual.py
@@ -14,7 +14,7 @@ import test
 import test.runner
 
 from stem.manual import Category
-from test.async_util import async_test
+from stem.util.test_tools import async_test
 
 EXPECTED_CATEGORIES = set([
   'NAME',
diff --git a/test/integ/process.py b/test/integ/process.py
index a2363fea..30cb0430 100644
--- a/test/integ/process.py
+++ b/test/integ/process.py
@@ -26,8 +26,7 @@ import test.require
 from contextlib import contextmanager
 from unittest.mock import patch, Mock
 
-from stem.util.test_tools import asynchronous, assert_equal, assert_in, skip
-from test.async_util import async_test
+from stem.util.test_tools import async_test, asynchronous, assert_equal, assert_in, skip
 
 BASIC_RELAY_TORRC = """\
 SocksPort 9089
diff --git a/test/integ/response/protocolinfo.py b/test/integ/response/protocolinfo.py
index f824be5d..244c9e81 100644
--- a/test/integ/response/protocolinfo.py
+++ b/test/integ/response/protocolinfo.py
@@ -13,11 +13,10 @@ import test
 import test.integ.util.system
 import test.require
 import test.runner
+from stem.util.test_tools import async_test
 
 from unittest.mock import Mock, patch
 
-from test.async_util import async_test
-
 
 class TestProtocolInfo(unittest.TestCase):
   @test.require.controller
diff --git a/test/integ/socket/control_message.py b/test/integ/socket/control_message.py
index 80bf4762..8511877b 100644
--- a/test/integ/socket/control_message.py
+++ b/test/integ/socket/control_message.py
@@ -9,7 +9,7 @@ import stem.socket
 import stem.version
 import test.require
 import test.runner
-from test.async_util import async_test
+from stem.util.test_tools import async_test
 
 
 class TestControlMessage(unittest.TestCase):
diff --git a/test/integ/socket/control_socket.py b/test/integ/socket/control_socket.py
index bb2d8873..14797589 100644
--- a/test/integ/socket/control_socket.py
+++ b/test/integ/socket/control_socket.py
@@ -18,7 +18,7 @@ import stem.socket
 import test
 import test.require
 import test.runner
-from test.async_util import async_test
+from stem.util.test_tools import async_test
 
 
 class TestControlSocket(unittest.TestCase):
diff --git a/test/integ/util/connection.py b/test/integ/util/connection.py
index c35d8448..3e22667e 100644
--- a/test/integ/util/connection.py
+++ b/test/integ/util/connection.py
@@ -13,7 +13,7 @@ import test.require
 import test.runner
 
 from stem.util.connection import Resolver
-from test.async_util import async_test
+from stem.util.test_tools import async_test
 
 
 class TestConnection(unittest.TestCase):
diff --git a/test/integ/util/proc.py b/test/integ/util/proc.py
index 4038984c..b1006d10 100644
--- a/test/integ/util/proc.py
+++ b/test/integ/util/proc.py
@@ -10,7 +10,7 @@ import test.require
 import test.runner
 
 from stem.util import proc
-from test.async_util import async_test
+from stem.util.test_tools import async_test
 
 
 class TestProc(unittest.TestCase):
diff --git a/test/integ/version.py b/test/integ/version.py
index d02014a5..0df48646 100644
--- a/test/integ/version.py
+++ b/test/integ/version.py
@@ -8,7 +8,7 @@ import unittest
 import stem.version
 import test.require
 import test.runner
-from test.async_util import async_test
+from stem.util.test_tools import async_test
 
 
 class TestVersion(unittest.TestCase):
diff --git a/test/unit/connection/authentication.py b/test/unit/connection/authentication.py
index 5e59adae..700f458c 100644
--- a/test/unit/connection/authentication.py
+++ b/test/unit/connection/authentication.py
@@ -18,7 +18,7 @@ from unittest.mock import patch
 
 from stem.response import ControlMessage
 from stem.util import log
-from test.async_util import (
+from stem.util.test_tools import (
   async_test,
   coro_func_raising_exc,
   coro_func_returning_value,
diff --git a/test/unit/connection/connect.py b/test/unit/connection/connect.py
index 3a0e0767..ec92a6c4 100644
--- a/test/unit/connection/connect.py
+++ b/test/unit/connection/connect.py
@@ -11,7 +11,7 @@ import stem.socket
 
 from unittest.mock import Mock, patch
 
-from test.async_util import (
+from stem.util.test_tools import (
   async_test,
   coro_func_raising_exc,
   coro_func_returning_value,
diff --git a/test/unit/control/controller.py b/test/unit/control/controller.py
index 4c03dea3..e8ef4787 100644
--- a/test/unit/control/controller.py
+++ b/test/unit/control/controller.py
@@ -21,7 +21,7 @@ from stem import ControllerError, DescriptorUnavailable, InvalidArguments, Inval
 from stem.control import MALFORMED_EVENTS, _parse_circ_path, Listener, Controller, EventType
 from stem.response import ControlMessage
 from stem.exit_policy import ExitPolicy
-from test.async_util import (
+from stem.util.test_tools import (
   async_test,
   coro_func_raising_exc,
   coro_func_returning_value,





More information about the tor-commits mailing list