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
|
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()
|
|
|
|
|
|
|
|
|
|
|
2019-09-22 20:46:01 +02:00
|
|
|
# some unit tests are modifying globals...
|
2018-05-24 18:57:13 +02:00
|
|
|
class SequentialTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
test_lock = threading.Lock()
|
|
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
super().setUp()
|
|
|
|
|
self.test_lock.acquire()
|
|
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
|
super().tearDown()
|
|
|
|
|
self.test_lock.release()
|
|
|
|
|
|
|
|
|
|
|
2019-09-22 20:46:01 +02:00
|
|
|
class ElectrumTestCase(SequentialTestCase):
|
|
|
|
|
"""Base class for our unit tests."""
|
|
|
|
|
|
|
|
|
|
def setUp(self):
|
2021-02-24 13:32:29 +01:00
|
|
|
super().setUp()
|
2022-04-29 18:48:33 +02:00
|
|
|
self.asyncio_loop, self._stop_loop, self._loop_thread = util.create_and_start_event_loop()
|
2019-09-22 20:46:01 +02:00
|
|
|
self.electrum_path = tempfile.mkdtemp()
|
|
|
|
|
|
|
|
|
|
def tearDown(self):
|
2022-04-29 18:48:33 +02:00
|
|
|
self.asyncio_loop.call_soon_threadsafe(self._stop_loop.set_result, 1)
|
|
|
|
|
self._loop_thread.join(timeout=1)
|
2021-02-24 13:32:29 +01:00
|
|
|
super().tearDown()
|
2019-09-22 20:46:01 +02:00
|
|
|
shutil.rmtree(self.electrum_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestCaseForTestnet(ElectrumTestCase):
|
2018-04-01 15:50:24 +02:00
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def setUpClass(cls):
|
|
|
|
|
super().setUpClass()
|
|
|
|
|
constants.set_testnet()
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def tearDownClass(cls):
|
|
|
|
|
super().tearDownClass()
|
|
|
|
|
constants.set_mainnet()
|