From aa1bebf21207bb96c319ebc232b1f1b7f5ea387d Mon Sep 17 00:00:00 2001 From: "V.G. Bulavintsev" Date: Fri, 17 Sep 2021 10:40:06 +0200 Subject: [PATCH 1/2] Remove dangling stub tab in Channels Debug pane --- .../tribler_gui/qt_resources/debugwindow.ui | 29 ++----------------- 1 file changed, 2 insertions(+), 27 deletions(-) diff --git a/src/tribler-gui/tribler_gui/qt_resources/debugwindow.ui b/src/tribler-gui/tribler_gui/qt_resources/debugwindow.ui index bf74ddfae68..e693e3086f2 100644 --- a/src/tribler-gui/tribler_gui/qt_resources/debugwindow.ui +++ b/src/tribler-gui/tribler_gui/qt_resources/debugwindow.ui @@ -21,7 +21,7 @@ - 3 + 10 @@ -1744,7 +1744,7 @@ - 0 + 1 @@ -1806,31 +1806,6 @@ - - - Stub - - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - - From 499b017e5497afcb6b2ab266a1d37090edd3b6c8 Mon Sep 17 00:00:00 2001 From: "V.G. Bulavintsev" Date: Sat, 18 Sep 2021 12:13:45 +0200 Subject: [PATCH 2/2] Add tests for Download get_extended_status --- src/tribler-core/tribler_core/conftest.py | 1 - .../restapi/tests/test_downloads_endpoint.py | 119 +++++++++++++++++- .../libtorrent/tests/test_download_state.py | 6 - 3 files changed, 118 insertions(+), 8 deletions(-) diff --git a/src/tribler-core/tribler_core/conftest.py b/src/tribler-core/tribler_core/conftest.py index 70b39cffccf..74b97d574bd 100644 --- a/src/tribler-core/tribler_core/conftest.py +++ b/src/tribler-core/tribler_core/conftest.py @@ -264,7 +264,6 @@ async def test_download(mock_dlmgr, test_tdef): @pytest.fixture def mock_lt_status(): lt_status = Mock() - lt_status.state = 3 lt_status.upload_rate = 123 lt_status.download_rate = 43 lt_status.total_upload = 100 diff --git a/src/tribler-core/tribler_core/modules/libtorrent/restapi/tests/test_downloads_endpoint.py b/src/tribler-core/tribler_core/modules/libtorrent/restapi/tests/test_downloads_endpoint.py index e3cd1757391..66456161bbc 100644 --- a/src/tribler-core/tribler_core/modules/libtorrent/restapi/tests/test_downloads_endpoint.py +++ b/src/tribler-core/tribler_core/modules/libtorrent/restapi/tests/test_downloads_endpoint.py @@ -1,3 +1,4 @@ +import collections import os from unittest.mock import Mock @@ -7,8 +8,10 @@ import pytest +from tribler_common.simpledefs import DLSTATUS_CIRCUITS, DLSTATUS_DOWNLOADING, DLSTATUS_EXIT_NODES, DLSTATUS_STOPPED + from tribler_core.modules.libtorrent.download_state import DownloadState -from tribler_core.modules.libtorrent.restapi.downloads_endpoint import DownloadsEndpoint +from tribler_core.modules.libtorrent.restapi.downloads_endpoint import DownloadsEndpoint, get_extended_status from tribler_core.restapi.base_api_test import do_request from tribler_core.restapi.rest_manager import error_middleware from tribler_core.tests.tools.common import TESTS_DATA_DIR @@ -30,6 +33,120 @@ def get_hex_infohash(tdef): return hexlify(tdef.get_infohash()) +ExtendedStatusConfig = collections.namedtuple("ExtendedStatusConfig", + ["hops", "candidates", "has_status"], + defaults=[0, 0, True]) + + +@pytest.fixture(name="mock_extended_status", scope="function") +def fixture_extended_status(request, mock_lt_status) -> int: + """ + Fixture to provide an extended status for a DownloadState that uses a mocked TunnelCommunity and a mocked Download. + + Parameterization options: + + - Set Tribler's configured hops through the ``hops`` parameter. + - Set the numer of exit candidates for the ``TunnelCommunity`` through the ``candidates`` parameter. + - Set whether the DownloadState has a ``lt_status``, that is not ``None``, through the ``has_status``. + + :param request: PyTest's parameterization of this test, using ExtendedStatusConfig. + :type request: SubRequest + :param mock_lt_status: fixture that provides a mocked libtorrent status. + """ + tunnel_community = Mock() + download = Mock() + state = DownloadState(download, mock_lt_status, None) + download.get_state = lambda: state + + # Test parameterization + download.config.get_hops = lambda: request.param.hops + if not request.param.has_status: + state.lt_status = None + tunnel_community.get_candidates = lambda _: request.param.candidates + + return get_extended_status(tunnel_community, download) + + +@pytest.mark.parametrize("mock_extended_status", + [ExtendedStatusConfig(hops=0, candidates=0, has_status=True)], + indirect=["mock_extended_status"]) +def test_get_extended_status_downloading_nohops_nocandidates(mock_extended_status): + """ + Testing whether a non-anonymous download with state is considered "DOWNLOADING" without candidates. + """ + assert mock_extended_status == DLSTATUS_DOWNLOADING + + +@pytest.mark.parametrize("mock_extended_status", + [ExtendedStatusConfig(hops=0, candidates=1, has_status=True)], + indirect=["mock_extended_status"]) +def test_get_extended_status_downloading_nohops_candidates(mock_extended_status): + """ + Testing whether a non-anonymous download with state is considered "DOWNLOADING" with candidates. + """ + assert mock_extended_status == DLSTATUS_DOWNLOADING + + +@pytest.mark.parametrize("mock_extended_status", + [ExtendedStatusConfig(hops=1, candidates=0, has_status=True)], + indirect=["mock_extended_status"]) +def test_get_extended_status_downloading_hops_nocandidates(mock_extended_status): + """ + Testing whether an anonymous download with state is considered "DOWNLOADING" without candidates. + """ + assert mock_extended_status == DLSTATUS_DOWNLOADING + + +@pytest.mark.parametrize("mock_extended_status", + [ExtendedStatusConfig(hops=1, candidates=1, has_status=True)], + indirect=["mock_extended_status"]) +def test_get_extended_status_downloading_hops_candidates(mock_extended_status): + """ + Testing whether an anonymous download with state is considered "DOWNLOADING" with candidates. + """ + assert mock_extended_status == DLSTATUS_DOWNLOADING + + +@pytest.mark.parametrize("mock_extended_status", + [ExtendedStatusConfig(hops=0, candidates=0, has_status=False)], + indirect=["mock_extended_status"]) +def test_get_extended_status_stopped(mock_extended_status): + """ + Testing whether a non-anonymous download without state is considered "STOPPED" without candidates. + """ + assert mock_extended_status == DLSTATUS_STOPPED + + +@pytest.mark.parametrize("mock_extended_status", + [ExtendedStatusConfig(hops=0, candidates=1, has_status=False)], + indirect=["mock_extended_status"]) +def test_get_extended_status_stopped_hascandidates(mock_extended_status): + """ + Testing whether a non-anonymous download without state is considered "STOPPED" with candidates. + """ + assert mock_extended_status == DLSTATUS_STOPPED + + +@pytest.mark.parametrize("mock_extended_status", + [ExtendedStatusConfig(hops=1, candidates=0, has_status=False)], + indirect=["mock_extended_status"]) +def test_get_extended_status_exit_nodes(mock_extended_status): + """ + Testing whether an anonymous download without state is considered looking for "EXIT_NODES" without candidates. + """ + assert mock_extended_status == DLSTATUS_EXIT_NODES + + +@pytest.mark.parametrize("mock_extended_status", + [ExtendedStatusConfig(hops=1, candidates=1, has_status=False)], + indirect=["mock_extended_status"]) +def test_get_extended_status_circuits(mock_extended_status): + """ + Testing whether an anonymous download without state is considered looking for "CIRCUITS" with candidates. + """ + assert mock_extended_status == DLSTATUS_CIRCUITS + + async def test_get_downloads_no_downloads(mock_dlmgr, rest_api): """ Testing whether the API returns an empty list when downloads are fetched but no downloads are active diff --git a/src/tribler-core/tribler_core/modules/libtorrent/tests/test_download_state.py b/src/tribler-core/tribler_core/modules/libtorrent/tests/test_download_state.py index 4e322d16dba..bace1ff3ed3 100644 --- a/src/tribler-core/tribler_core/modules/libtorrent/tests/test_download_state.py +++ b/src/tribler-core/tribler_core/modules/libtorrent/tests/test_download_state.py @@ -39,12 +39,6 @@ def test_getters_setters_1(mock_download): assert download_state.get_num_seeds_peers() == (0, 0) assert download_state.get_peerlist() == [] - # TODO: move this to do downloads endpoint test, testing get_extended_status - #assert download_state.get_status() == DLSTATUS_WAITING4HASHCHECK - #mock_download.config.get_hops = lambda: 1 - #download_state = DownloadState(mock_download, None, None) - #assert download_state.get_status() == DLSTATUS_EXIT_NODES - def test_getters_setters_2(mock_download, mock_lt_status): """