2023-02-18 10:01:21 +00:00
|
|
|
import asyncio
|
2023-02-20 16:47:12 +00:00
|
|
|
import os
|
2018-04-01 15:50:24 +02:00
|
|
|
import unittest
|
2018-05-24 18:57:13 +02:00
|
|
|
import threading
|
2019-09-22 20:46:01 +02:00
|
|
|
import tempfile
|
|
|
|
|
import shutil
|
2018-04-01 15:50:24 +02:00
|
|
|
|
2021-04-14 07:01:23 +02:00
|
|
|
import electrum
|
|
|
|
|
import electrum.logging
|
2018-07-11 17:38:47 +02:00
|
|
|
from electrum import constants
|
2022-04-29 18:48:33 +02:00
|
|
|
from electrum import util
|
2024-02-15 14:12:03 +00:00
|
|
|
from electrum.logging import Logger
|
2018-04-01 15:50:24 +02:00
|
|
|
|
|
|
|
|
|
2018-05-28 14:02:07 +02:00
|
|
|
# Set this locally to make the test suite run faster.
|
|
|
|
|
# If set, unit tests that would normally test functions with multiple implementations,
|
|
|
|
|
# will only be run once, using the fastest implementation.
|
|
|
|
|
# e.g. libsecp256k1 vs python-ecdsa. pycryptodomex vs pyaes.
|
2019-05-03 03:13:43 +02:00
|
|
|
FAST_TESTS = False
|
2018-05-28 14:02:07 +02:00
|
|
|
|
|
|
|
|
|
2021-04-14 07:01:23 +02:00
|
|
|
electrum.logging._configure_stderr_logging()
|
|
|
|
|
|
2023-02-18 10:01:21 +00:00
|
|
|
electrum.util.AS_LIB_USER_I_WANT_TO_MANAGE_MY_OWN_ASYNCIO_LOOP = True
|
2021-04-14 07:01:23 +02:00
|
|
|
|
2023-02-18 10:01:21 +00:00
|
|
|
|
2024-02-15 14:12:03 +00:00
|
|
|
class ElectrumTestCase(unittest.IsolatedAsyncioTestCase, Logger):
|
2019-09-22 20:46:01 +02:00
|
|
|
"""Base class for our unit tests."""
|
|
|
|
|
|
2023-02-18 06:44:30 +00:00
|
|
|
TESTNET = False
|
2023-02-18 06:56:43 +00:00
|
|
|
# maxDiff = None # for debugging
|
|
|
|
|
|
|
|
|
|
# some unit tests are modifying globals... so we run sequentially:
|
|
|
|
|
_test_lock = threading.Lock()
|
2022-10-31 17:14:21 +00:00
|
|
|
|
2024-02-15 14:12:03 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
Logger.__init__(self)
|
|
|
|
|
unittest.IsolatedAsyncioTestCase.__init__(self, *args, **kwargs)
|
|
|
|
|
|
2023-02-18 06:44:30 +00:00
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
|
|
|
|
super().setUpClass()
|
|
|
|
|
if cls.TESTNET:
|
|
|
|
|
constants.set_testnet()
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def tearDownClass(cls):
|
|
|
|
|
super().tearDownClass()
|
|
|
|
|
if cls.TESTNET:
|
|
|
|
|
constants.set_mainnet()
|
|
|
|
|
|
2019-09-22 20:46:01 +02:00
|
|
|
def setUp(self):
|
2023-02-18 06:56:43 +00:00
|
|
|
self._test_lock.acquire()
|
2021-02-24 13:32:29 +01:00
|
|
|
super().setUp()
|
2019-09-22 20:46:01 +02:00
|
|
|
self.electrum_path = tempfile.mkdtemp()
|
2023-09-06 14:05:45 +00:00
|
|
|
assert util._asyncio_event_loop is None, "global event loop already set?!"
|
2019-09-22 20:46:01 +02:00
|
|
|
|
2023-02-20 16:47:12 +00:00
|
|
|
async def asyncSetUp(self):
|
2023-03-20 17:49:50 +00:00
|
|
|
await super().asyncSetUp()
|
2023-02-20 16:47:12 +00:00
|
|
|
loop = util.get_asyncio_loop()
|
|
|
|
|
# IsolatedAsyncioTestCase creates event loops with debug=True, which makes the tests take ~4x time
|
|
|
|
|
if not (os.environ.get("PYTHONASYNCIODEBUG") or os.environ.get("PYTHONDEVMODE")):
|
|
|
|
|
loop.set_debug(False)
|
2023-09-06 14:05:45 +00:00
|
|
|
util._asyncio_event_loop = loop
|
2023-02-20 16:47:12 +00:00
|
|
|
|
2019-09-22 20:46:01 +02:00
|
|
|
def tearDown(self):
|
|
|
|
|
shutil.rmtree(self.electrum_path)
|
2023-02-18 06:56:43 +00:00
|
|
|
super().tearDown()
|
2023-09-06 14:05:45 +00:00
|
|
|
util._asyncio_event_loop = None # cleared here, at the ~last possible moment. asyncTearDown is too early.
|
2023-02-18 06:56:43 +00:00
|
|
|
self._test_lock.release()
|
2019-09-22 20:46:01 +02:00
|
|
|
|
|
|
|
|
|
2022-10-31 18:44:38 +00:00
|
|
|
def as_testnet(func):
|
|
|
|
|
"""Function decorator to run a single unit test in testnet mode.
|
|
|
|
|
|
|
|
|
|
NOTE: this is inherently sequential; tests running in parallel would break things
|
|
|
|
|
"""
|
2023-02-18 10:01:21 +00:00
|
|
|
old_net = constants.net
|
|
|
|
|
if asyncio.iscoroutinefunction(func):
|
|
|
|
|
async def run_test(*args, **kwargs):
|
|
|
|
|
try:
|
|
|
|
|
constants.set_testnet()
|
|
|
|
|
return await func(*args, **kwargs)
|
|
|
|
|
finally:
|
|
|
|
|
constants.net = old_net
|
|
|
|
|
else:
|
|
|
|
|
def run_test(*args, **kwargs):
|
|
|
|
|
try:
|
|
|
|
|
constants.set_testnet()
|
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
|
finally:
|
|
|
|
|
constants.net = old_net
|
2022-10-31 18:44:38 +00:00
|
|
|
return run_test
|