Skip to content

Commit

Permalink
Merge branch 'main' into fix_6271
Browse files Browse the repository at this point in the history
  • Loading branch information
ichorid authored Sep 20, 2021
2 parents 3316d99 + ef9c133 commit 2f95df8
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 35 deletions.
1 change: 0 additions & 1 deletion src/tribler-core/tribler_core/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import collections
import os
from unittest.mock import Mock

Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
29 changes: 2 additions & 27 deletions src/tribler-gui/tribler_gui/qt_resources/debugwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<item>
<widget class="QTabWidget" name="debug_tab_widget">
<property name="currentIndex">
<number>3</number>
<number>10</number>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
Expand Down Expand Up @@ -1744,7 +1744,7 @@
<item>
<widget class="QTabWidget" name="channels_tab_widget">
<property name="currentIndex">
<number>0</number>
<number>1</number>
</property>
<widget class="QWidget" name="tab_3">
<attribute name="title">
Expand Down Expand Up @@ -1806,31 +1806,6 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_10">
<attribute name="title">
<string notr="true">Stub</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QWidget" name="stub" native="true"/>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
Expand Down

0 comments on commit 2f95df8

Please sign in to comment.