diff --git a/CMakeLists.txt b/CMakeLists.txt index 55d5830..9a6ed02 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,6 +35,14 @@ endif() option(ENABLE_COVERAGE "Enable coverage" OFF) +# A build without a type is not optimized (no -O3, and asserts on): `cmake ..` is what the README and bin/build_libmonero_python.sh run, +# while the wheels and the packages are built as Release. Coverage has its own flags below, and a debug build is asked with a type +get_property(MONERO_PYTHON_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG) +if(NOT MONERO_PYTHON_MULTI_CONFIG AND NOT CMAKE_BUILD_TYPE AND NOT ENABLE_COVERAGE) + message(STATUS "No CMAKE_BUILD_TYPE given, using Release") + set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE) +endif() + if(ENABLE_COVERAGE) message(STATUS "Coverage enabled") add_compile_options(--coverage -O0 -g -fprofile-update=atomic) diff --git a/README.md b/README.md index 0e64875..2df42aa 100644 --- a/README.md +++ b/README.md @@ -246,6 +246,8 @@ For example: `export LD_PRELOAD=/path/to/libjemalloc.so.2` then run your app. It pip install -r tests/requirements.txt ``` The `monero` module has to be importable in it: install it in the environment (`pip install .`), or set `PYTHONPATH` to the folder that has the built module. + + Run `pytest` from that environment, or as `python3 -m pytest`. The `/usr/bin/pytest` of the `python3-pytest` package of apt calls `pytest.console_main()`, which pytest 9 deprecates, and with a newer pytest installed with pip it prints a `PytestRemovedIn10Warning`. The `pytest` that pip makes in the environment does not. 2. Clone the project repository: ```bash git clone --recurse-submodules https://github.com/libmonero/monero-python.git diff --git a/bin/build_libmonero_python.sh b/bin/build_libmonero_python.sh index 246585b..3572013 100755 --- a/bin/build_libmonero_python.sh +++ b/bin/build_libmonero_python.sh @@ -1,12 +1,20 @@ -#!/bin/sh +#!/usr/bin/env bash + +# ccache for the compilations when it is installed, the plain build otherwise. monero-project and the module look for it themselves +# (USE_CCACHE in their CMakeLists.txt), monero-cpp has to be told to use it +CMAKE_CCACHE_ARGS=() +if command -v ccache > /dev/null 2>&1; then + echo "Using ccache: $(command -v ccache)" + CMAKE_CCACHE_ARGS=(-DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache) +fi # build monero-project dependencies cd ./external/monero-cpp/external/monero-project/ || exit 1 git submodule update --init --force || exit 1 -HOST_NCORES=$(nproc 2>/dev/null || shell nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 1) +HOST_NCORES=$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 1) if [[ $(uname -s) == "MINGW64_NT"* || $(uname -s) == "MSYS"* ]]; then bit=$(getconf LONG_BIT) - FOLDER=$(cd ${MINGW_PREFIX}/.. && pwd -W) + FOLDER=$(cd "${MINGW_PREFIX}"/.. && pwd -W) if [ "$bit" == "64" ]; then cmake -G "MSYS Makefiles" \ -D STATIC=ON \ @@ -37,14 +45,14 @@ else mkdir -p build/release && cd build/release && cmake -DCMAKE_BUILD_TYPE=Release ../../ - make -j$HOST_NCORES wallet cryptonote_protocol || exit 1 + make -j"$HOST_NCORES" wallet cryptonote_protocol || exit 1 fi cd ../../../../ # build libmonero-cpp shared library mkdir -p build && cd build && -cmake .. && +cmake .. "${CMAKE_CCACHE_ARGS[@]}" && cmake --build . && make . cd ../../../ @@ -53,5 +61,5 @@ cd ../../../ mkdir -p build && cd build && cmake .. -make -j$HOST_NCORES +make -j"$HOST_NCORES" diff --git a/tests/common/test_gen_utils.py b/tests/common/test_gen_utils.py index 27d9e67..61d6ef3 100644 --- a/tests/common/test_gen_utils.py +++ b/tests/common/test_gen_utils.py @@ -1,6 +1,7 @@ import pytest import logging import re +import sys import time from monero import GenUtils @@ -26,16 +27,20 @@ def test_get_uuid_format_and_uniqueness(self) -> None: assert uuid1 != uuid2 def test_wait_for_blocks_for_at_least_duration(self) -> None: - start: float = time.monotonic() + # wait_for is std::this_thread::sleep_for in C++. On Windows it follows the timer of the system, which ticks every 15.6 ms, and it + # can return up to a tick before the time that was asked, however it is measured (49.8 ms for wait_for(50) was seen in CI, + # with perf_counter too). Everywhere else it is not shorter. + tolerance_ms: float = 16 if sys.platform == "win32" else 0 + start: float = time.perf_counter() GenUtils.wait_for(50) - elapsed_ms: float = (time.monotonic() - start) * 1000 + elapsed_ms: float = (time.perf_counter() - start) * 1000 logger.debug(f"wait_for(50) actually took {elapsed_ms:.1f} ms") - assert elapsed_ms >= 50 + assert elapsed_ms >= 50 - tolerance_ms def test_wait_for_zero_does_not_block(self) -> None: - start: float = time.monotonic() + start: float = time.perf_counter() GenUtils.wait_for(0) - elapsed_ms: float = (time.monotonic() - start) * 1000 + elapsed_ms: float = (time.perf_counter() - start) * 1000 assert elapsed_ms < 50 def test_wait_for_negative_raises(self) -> None: diff --git a/tests/docker-compose.yml b/tests/docker-compose.yml index 2ddd247..abeaa9b 100644 --- a/tests/docker-compose.yml +++ b/tests/docker-compose.yml @@ -3,7 +3,6 @@ services: # The dev container is not used, it is just handy to run `docker-compose up dev` to start all services dev: image: alpine:3.22.1 - container_name: dev command: [ "/bin/sh", "-c", "trap : TERM INT; while :; do echo Ready to code and debug like a rockstar!!!; sleep 2073600; done & wait" ] depends_on: - node_2 @@ -13,7 +12,6 @@ services: node_1: image: ghcr.io/libmonero/monero-daemon-rpc:${MONERO_TAG:-master} - container_name: node_1 command: [ "--fixed-difficulty=500", "--log-level=3", @@ -42,7 +40,6 @@ services: node_2: image: ghcr.io/libmonero/monero-daemon-rpc:${MONERO_TAG:-master} - container_name: node_2 command: [ "--fixed-difficulty=500", "--log-level=3", @@ -78,7 +75,6 @@ services: xmr_wallet_1: image: ghcr.io/libmonero/monero-wallet-rpc:${MONERO_TAG:-master} - container_name: xmr_wallet_1 command: [ "--log-level=3", "--allow-mismatched-daemon-version", @@ -104,7 +100,6 @@ services: xmr_wallet_2: image: ghcr.io/libmonero/monero-wallet-rpc:${MONERO_TAG:-master} - container_name: xmr_wallet_2 command: [ "--log-level=3", "--allow-mismatched-daemon-version", @@ -130,7 +125,6 @@ services: xmr_wallet_3: image: ghcr.io/libmonero/monero-wallet-rpc:${MONERO_TAG:-master} - container_name: xmr_wallet_3 command: [ "--log-level=3", "--allow-mismatched-daemon-version", diff --git a/tests/utils/__init__.py b/tests/utils/__init__.py index 53a529c..d6209bb 100644 --- a/tests/utils/__init__.py +++ b/tests/utils/__init__.py @@ -37,6 +37,7 @@ from .view_only_and_offline_wallet_tester import ViewOnlyAndOfflineWalletTester from .daemon_notification_collector import DaemonNotificationCollector from .fake_daemon_rpc_server import FakeDaemonRpcServer +from .fake_wallet_rpc_server import FakeWalletRpcServer from .wallet_notification_collector import WalletNotificationCollector from .submit_then_relay_tx_tester import SubmitThenRelayTxTester from .multisig_sample_code_tester import MultisigSampleCodeTester @@ -103,5 +104,6 @@ 'WalletTestUtils', 'TxsStructureTester', 'DaemonNotificationCollector', - 'FakeDaemonRpcServer' + 'FakeDaemonRpcServer', + 'FakeWalletRpcServer' ] diff --git a/tests/utils/base_test_class.py b/tests/utils/base_test_class.py index 596b91a..2d095cf 100644 --- a/tests/utils/base_test_class.py +++ b/tests/utils/base_test_class.py @@ -2,6 +2,7 @@ import pytest from abc import ABC +from time import perf_counter from typing import Any, Generator from monero import MoneroUtils @@ -11,6 +12,9 @@ class BaseTestClass(ABC): """Base test class with common fixtures.""" + _elapsed_ms: float = 0.0 + """Milliseconds that the last test took, set when the test ends and before `after_each`.""" + # Setup and teardown of test class @pytest.fixture(scope="class", autouse=True) @classmethod @@ -36,7 +40,10 @@ def setup_and_teardown(self, request: pytest.FixtureRequest) -> Generator[None, teardown once the test has completed. """ self.before_each(request) + # the time of the test itself: after what before_each does, until the test ends + started: float = perf_counter() yield + self._elapsed_ms = (perf_counter() - started) * 1000 self.after_each(request) # Before all tests @@ -69,6 +76,14 @@ def after_each(self, request: pytest.FixtureRequest) -> None: :param pytest.FixtureRequest request: Request fixture. """ - msg: str = f"After {request.node.name}" # type: ignore + msg: str = self.after_message(request) MoneroUtils.log_info(msg) logger.info(msg) + + def after_message(self, request: pytest.FixtureRequest) -> str: + """The message that says that a test ended, with the time that it took. + + :param pytest.FixtureRequest request: Request fixture. + :returns str: `After ( ms)`. + """ + return f"After {request.node.name} ({self._elapsed_ms:.1f} ms)" # type: ignore diff --git a/tests/utils/daemon_notification_collector.py b/tests/utils/daemon_notification_collector.py index 2b139d3..02a0c34 100644 --- a/tests/utils/daemon_notification_collector.py +++ b/tests/utils/daemon_notification_collector.py @@ -1,6 +1,6 @@ import logging -from typing import override +from typing_extensions import override from monero import MoneroDaemonRpc, MoneroDaemonListener, MoneroBlockHeader diff --git a/tests/utils/fake_daemon_rpc_server.py b/tests/utils/fake_daemon_rpc_server.py index 9490627..9b0584f 100644 --- a/tests/utils/fake_daemon_rpc_server.py +++ b/tests/utils/fake_daemon_rpc_server.py @@ -1,73 +1,40 @@ -import json -import logging -import threading - -from time import sleep - -from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer from typing import Any, override -logger: logging.Logger = logging.getLogger("FakeDaemonRpcServer") +from .fake_rpc_server import FakeRpcHandler, FakeRpcServer -class _Handler(BaseHTTPRequestHandler): +class _Handler(FakeRpcHandler): """Answers the json rpc requests of a daemon poller, with a new block at each request.""" - server: "FakeDaemonRpcServer" # type: ignore[assignment] - - def do_POST(self) -> None: - """Answer a request with the last block header, a block newer than the one of the previous request.""" - try: - length: int = int(self.headers.get("Content-Length", "0")) - body: dict[str, Any] = json.loads(self.rfile.read(length) or b"{}") - height: int = self.server.next_height() - if self.server.response_delay > 0: - sleep(self.server.response_delay) - response: dict[str, Any] = { - "jsonrpc": "2.0", - "id": body.get("id", "0"), - "result": { - "status": "OK", - "untrusted": False, - "block_header": { - "hash": f"{height:064x}", - "prev_hash": f"{height - 1:064x}", - "miner_tx_hash": f"{height + 1:064x}", - "height": height, - "major_version": 16, - "minor_version": 16, - "timestamp": 1700000000 + height, - "nonce": 1, - "orphan_status": False, - "depth": 0, - "difficulty": 5, - "cumulative_difficulty": 5 * height, - "reward": 600000000000, - "block_size": 100, - "block_weight": 100, - "num_txes": 0, - "long_term_weight": 100, - "pow_hash": "" - } - } - } - data: bytes = json.dumps(response).encode() - self.send_response(200) - self.send_header("Content-Type", "application/json") - self.send_header("Content-Length", str(len(data))) - self.end_headers() - self.wfile.write(data) - except Exception as e: - # a request that is not json (the daemon connection probes the server too) - logger.debug(f"Request not answered: {e}") - self.send_error(404) - @override - def log_message(self, format: str, *args: Any) -> None: - pass + def build_result(self, height: int) -> dict[str, Any]: + return { + "status": "OK", + "untrusted": False, + "block_header": { + "hash": f"{height:064x}", + "prev_hash": f"{height - 1:064x}", + "miner_tx_hash": f"{height + 1:064x}", + "height": height, + "major_version": 16, + "minor_version": 16, + "timestamp": 1700000000 + height, + "nonce": 1, + "orphan_status": False, + "depth": 0, + "difficulty": 5, + "cumulative_difficulty": 5 * height, + "reward": 600000000000, + "block_size": 100, + "block_weight": 100, + "num_txes": 0, + "long_term_weight": 100, + "pow_hash": "" + } + } -class FakeDaemonRpcServer(ThreadingHTTPServer): +class FakeDaemonRpcServer(FakeRpcServer): """A daemon rpc server for unit tests, that needs no node: every request for the last block header gets a new block, so the poller of a `MoneroDaemonRpc` notifies its listeners at each poll. @@ -80,43 +47,4 @@ def __init__(self, first_height: int = 100, response_delay: float = 0.0) -> None :param int first_height: height of the first block. :param float response_delay: seconds that the server takes to answer a request, to have requests in flight. """ - super().__init__(("127.0.0.1", 0), _Handler) - self._first_height: int = first_height - self._height: int = first_height - self.response_delay: float = response_delay - self._lock: threading.Lock = threading.Lock() - self._thread: threading.Thread = threading.Thread(target=self.serve_forever, daemon=True) - - @property - def uri(self) -> str: - """Uri of the server. - - :returns str: uri to give to a `MoneroDaemonRpc`. - """ - return f"http://127.0.0.1:{self.server_address[1]}" - - @property - def num_requests(self) -> int: - """Number of requests received so far. - - :returns int: number of requests received. - """ - return self._height - self._first_height - - def next_height(self) -> int: - """Get the height of the block of the next response. - - :returns int: new block height. - """ - with self._lock: - self._height += 1 - return self._height - - def __enter__(self) -> "FakeDaemonRpcServer": - self._thread.start() - return self - - def __exit__(self, *args: Any) -> None: - self.shutdown() - self.server_close() - self._thread.join(timeout=5) + super().__init__(_Handler, first_height, response_delay) diff --git a/tests/utils/fake_rpc_server.py b/tests/utils/fake_rpc_server.py new file mode 100644 index 0000000..d6da135 --- /dev/null +++ b/tests/utils/fake_rpc_server.py @@ -0,0 +1,104 @@ +import json +import logging +import threading + +from time import sleep + +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +from typing import Any, override + +logger: logging.Logger = logging.getLogger("FakeRpcServer") + + +class FakeRpcHandler(BaseHTTPRequestHandler): + """Answers the json rpc requests of a poller. A subclass gives the result of the response.""" + + server: "FakeRpcServer" # type: ignore[assignment] + + def build_result(self, height: int) -> dict[str, Any]: + """Build the result of a response. + + :param int height: height of the response, a new one at each request. + :returns dict[str, Any]: result of the json rpc response. + """ + raise NotImplementedError + + def do_POST(self) -> None: + """Answer a request with the result of the subclass.""" + try: + length: int = int(self.headers.get("Content-Length", "0")) + body: dict[str, Any] = json.loads(self.rfile.read(length) or b"{}") + height: int = self.server.next_height() + if self.server.response_delay > 0: + sleep(self.server.response_delay) + response: dict[str, Any] = {"jsonrpc": "2.0", "id": body.get("id", "0"), "result": self.build_result(height)} + data: bytes = json.dumps(response).encode() + self.send_response(200) + self.send_header("Content-Type", "application/json") + self.send_header("Content-Length", str(len(data))) + self.end_headers() + self.wfile.write(data) + except Exception as e: + # a request that is not json (the connection probes the server too) + logger.debug(f"Request not answered: {e}") + self.send_error(404) + + @override + def log_message(self, format: str, *args: Any) -> None: + pass + + +class FakeRpcServer(ThreadingHTTPServer): + """A rpc server for unit tests, that needs no node or wallet: every request gets a new height, so the poller of what is tested + has always something to notify. + + Use it as a context manager. + """ + + def __init__(self, handler: type[FakeRpcHandler], first_height: int = 100, response_delay: float = 0.0) -> None: + """Initialize a new FakeRpcServer. + + :param type[FakeRpcHandler] handler: handler that answers the requests. + :param int first_height: height of the first response. + :param float response_delay: seconds that the server takes to answer a request, to have requests in flight. + """ + super().__init__(("127.0.0.1", 0), handler) + self._first_height: int = first_height + self._height: int = first_height + self.response_delay: float = response_delay + self._lock: threading.Lock = threading.Lock() + self._thread: threading.Thread = threading.Thread(target=self.serve_forever, daemon=True) + + @property + def uri(self) -> str: + """Uri of the server. + + :returns str: uri to give to the rpc client. + """ + return f"http://127.0.0.1:{self.server_address[1]}" + + @property + def num_requests(self) -> int: + """Number of requests received so far. + + :returns int: number of requests received. + """ + return self._height - self._first_height + + def next_height(self) -> int: + """Get the height of the next response. + + :returns int: new height. + """ + with self._lock: + self._height += 1 + return self._height + + def __enter__(self) -> "FakeRpcServer": + self._thread.start() + return self + + def __exit__(self, *args: Any) -> None: + self.shutdown() + self.server_close() + self._thread.join(timeout=5) diff --git a/tests/utils/fake_wallet_rpc_server.py b/tests/utils/fake_wallet_rpc_server.py new file mode 100644 index 0000000..284bc38 --- /dev/null +++ b/tests/utils/fake_wallet_rpc_server.py @@ -0,0 +1,28 @@ +from typing import Any, override + +from .fake_rpc_server import FakeRpcHandler, FakeRpcServer + + +class _Handler(FakeRpcHandler): + """Answers the json rpc requests of a wallet poller, with a new height at each request. Any method gets the same result: what + the poller asks for and the result does not have, it takes as an error of the request and polls again.""" + + @override + def build_result(self, height: int) -> dict[str, Any]: + return {"height": height, "balance": 0, "unlocked_balance": 0} + + +class FakeWalletRpcServer(FakeRpcServer): + """A wallet rpc server for unit tests, that needs no wallet: every request gets a new height, so the poller of a `MoneroWalletRpc` + is always asking the server. + + Use it as a context manager. + """ + + def __init__(self, first_height: int = 100, response_delay: float = 0.0) -> None: + """Initialize a new FakeWalletRpcServer. + + :param int first_height: height of the first response. + :param float response_delay: seconds that the server takes to answer a request, to have requests in flight. + """ + super().__init__(_Handler, first_height, response_delay) diff --git a/tests/utils/sync_progress_tester.py b/tests/utils/sync_progress_tester.py index 536e6d0..fdc5dfa 100644 --- a/tests/utils/sync_progress_tester.py +++ b/tests/utils/sync_progress_tester.py @@ -1,5 +1,6 @@ -from typing import Optional, override +from typing import Optional +from typing_extensions import override from monero import MoneroWalletFull diff --git a/tests/utils/test_utils.py b/tests/utils/test_utils.py index 3fceea5..ee0a6b2 100644 --- a/tests/utils/test_utils.py +++ b/tests/utils/test_utils.py @@ -524,6 +524,10 @@ def is_wallet_rpc_open(cls, wallet: MoneroWalletRpc) -> bool: MoneroWalletRpc.is_closed() is not used: it takes the error "No wallet file" for a closed wallet only with the code -8, and the server answers it with -13 (WALLET_RPC_ERROR_CODE_NOT_OPEN), so it never says that a server has no wallet open. + TODO workaround for a bug of monero-cpp, fixed in its branch fix/wallet-rpc-is-closed and waiting for the pull request to be merged + and for the submodule to point at it. Then this is `not wallet.is_closed()`, and the strict xfail of + TestMoneroWalletRpcCreation.test_is_closed fails (it passes) to remind of it. + :param MoneroWalletRpc wallet: wallet rpc to ask. :returns bool: `True` if the server has a wallet open, `False` if it has none. """ @@ -570,8 +574,11 @@ def get_wallet_rpc(cls) -> MoneroWalletRpc: assert cls.SEED == cls._WALLET_RPC.get_seed() assert cls.ADDRESS == cls._WALLET_RPC.get_primary_address() - # sync and save wallet + # sync wallet and ask the daemon which of its outputs are spent. The full wallet has the same keys and sends txs that are not + # mined: the server keeps the ones that it saw in the pool as pending, and 500 seconds after, when they are not there, it marks + # them as failed and sets their inputs as unspent, even when a different tx spent them. Its balance and outputs are wrong then cls._WALLET_RPC.sync() + cls._WALLET_RPC.rescan_spent() cls._WALLET_RPC.save() cls._WALLET_RPC.start_syncing(cls.SYNC_PERIOD_IN_MS) diff --git a/tests/utils/wallet_notification_collector.py b/tests/utils/wallet_notification_collector.py index ac8a262..add365d 100644 --- a/tests/utils/wallet_notification_collector.py +++ b/tests/utils/wallet_notification_collector.py @@ -1,6 +1,6 @@ import logging -from typing import override +from typing_extensions import override from monero import ( MoneroWalletListener, MoneroOutputWallet, diff --git a/tests/utils/wallet_sync_tester.py b/tests/utils/wallet_sync_tester.py index 93529d0..747f563 100644 --- a/tests/utils/wallet_sync_tester.py +++ b/tests/utils/wallet_sync_tester.py @@ -1,6 +1,7 @@ import logging -from typing import Optional, override +from typing import Optional +from typing_extensions import override from monero import ( MoneroOutputWallet, MoneroWalletFull, MoneroTxWallet, diff --git a/tests/wallet/test_creation.py b/tests/wallet/test_creation.py index 2949aa7..3833741 100644 --- a/tests/wallet/test_creation.py +++ b/tests/wallet/test_creation.py @@ -831,6 +831,27 @@ def test_close(self, daemon: MoneroDaemonRpc) -> None: # close the wallet self._close_wallet(wallet, True) + # Can tell that the server has no wallet open + @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled") + @pytest.mark.xfail( + reason="TODO monero-cpp: is_closed() looks for the code -8 and the server answers -13 when no wallet is open; fixed in its branch " + "fix/wallet-rpc-is-closed, waiting for the pull request to be merged and the submodule to be updated. Then remove this mark " + "and the workaround Utils.is_wallet_rpc_open()", + raises=AssertionError, strict=True) + def test_is_closed(self) -> None: + # create a test wallet + config: MoneroWalletConfig = MoneroWalletConfig() + config.path = StringUtils.get_random_string() + wallet: MoneroWalletRpc = self._create_wallet(config) + assert wallet.is_closed() is False + + # close the wallet + wallet.close() + Utils.free_wallet_rpc_resource(wallet) + + # the server has no wallet open + assert wallet.is_closed() is True + # Can stop the RPC server @pytest.mark.skip(reason="Disabled so server not actually stopped") def test_stop(self, wallet: MoneroWalletRpc) -> None: diff --git a/tests/wallet/test_listener_lifetime.py b/tests/wallet/test_listener_lifetime.py index c5a2815..665296e 100644 --- a/tests/wallet/test_listener_lifetime.py +++ b/tests/wallet/test_listener_lifetime.py @@ -3,10 +3,12 @@ import gc import weakref +from time import monotonic, sleep + import pytest -from monero import MoneroWalletFull, MoneroWalletListener +from monero import MoneroWalletFull, MoneroWalletListener, MoneroWalletRpc -from utils import BaseTestClass, TestUtils as Utils +from utils import BaseTestClass, FakeWalletRpcServer, TestUtils as Utils @pytest.mark.unit @@ -32,4 +34,24 @@ def test_wallet_keeps_its_listener_alive(self) -> None: wallet.remove_listener(listener_back) assert len(wallet.get_listeners()) == 0 + # The wallet rpc is destroyed while its poller waits for the server + def test_rpc_wallet_is_destroyed_with_a_request_in_flight(self) -> None: + # The server of this process answers from a thread of python, which needs the GIL. The destruction of the wallet waits for the + # request of the poller, and it used to do it with the GIL held: both waited for the other one until the request timed out. + with FakeWalletRpcServer(response_delay=0.5) as server: + wallet: MoneroWalletRpc = MoneroWalletRpc(server.uri, timeout_ms=20000) + wallet.set_poll_period_in_ms(20) + wallet.add_listener(MoneroWalletListener()) + requests: int = server.num_requests + deadline: float = monotonic() + 10 + while server.num_requests == requests and monotonic() < deadline: + sleep(0.005) + assert server.num_requests > requests, "The poller did not ask the server" + + start: float = monotonic() + del wallet + gc.collect() + elapsed: float = monotonic() - start + assert elapsed < 10, f"The destruction of the wallet took {elapsed:.1f} s" + #endregion diff --git a/tests/wallet/test_listeners.py b/tests/wallet/test_listeners.py index e60297d..13a926b 100644 --- a/tests/wallet/test_listeners.py +++ b/tests/wallet/test_listeners.py @@ -79,9 +79,9 @@ def test_create_and_receive(self, daemon: MoneroDaemonRpc, wallet: MoneroWallet) sleep(1) assert len(my_listener.outputs_received) > 0 finally: - logger.debug(f"Closing receiver wallet...") + logger.debug("Closing receiver wallet...") self._close_wallet(receiver) - logger.debug(f"Closed receiver wallet") + logger.debug("Closed receiver wallet") MiningUtils.try_stop_mining() diff --git a/tests/wallet/wallet_support.py b/tests/wallet/wallet_support.py index 430ce97..8264d55 100644 --- a/tests/wallet/wallet_support.py +++ b/tests/wallet/wallet_support.py @@ -35,7 +35,7 @@ def get_wallet_type(cls) -> WalletType: @classmethod @override def wallet(cls) -> MoneroWalletFull: - """Test rpc wallet instance""" + """Test full wallet instance""" return Utils.get_wallet_full() @override @@ -152,6 +152,16 @@ def after_all(self) -> None: super().after_all() Utils.free_wallet_rpc_resources() + @override + def before_each(self, request: pytest.FixtureRequest) -> None: + super().before_each(request) + # The server has the same keys as the full wallet, and what that one sends and flushes from the pool (test_prove_unrelayed_txs) it + # keeps as pending: 500 seconds after, at any test, it marks it failed and sets its inputs as unspent, even when another tx spent + # them. The daemon says which outputs are spent (the ones spent in the pool too), so a test never starts with the wrong ones. + wallet: MoneroWalletRpc = self.get_test_wallet() + if Utils.is_wallet_rpc_open(wallet): + wallet.rescan_spent() + @override def after_each(self, request: pytest.FixtureRequest) -> None: super().after_each(request) @@ -197,7 +207,7 @@ def before_each(self, request: pytest.FixtureRequest) -> None: @override def after_each(self, request: pytest.FixtureRequest) -> None: - logger.info(f"After {request.node.name}") # type: ignore + logger.info(self.after_message(request)) @classmethod @override