From 7c153c0eb8f379b81523d8225be2e45636d34e9d Mon Sep 17 00:00:00 2001 From: Lee Miller Date: Sat, 2 Dec 2023 00:43:42 +0200 Subject: [PATCH] Mock network.stats for python3 in tests.partial --- src/api.py | 17 +++++++---------- src/mock/network.py | 15 +++++++++------ src/tests/partial.py | 5 +++++ src/tests/test_api_thread.py | 7 +++++++ 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/api.py b/src/api.py index 8b256de3..a4b0aed5 100644 --- a/src/api.py +++ b/src/api.py @@ -94,12 +94,11 @@ from helper_sql import ( from inventory import Inventory try: - import network.stats as network_stats from network import BMConnectionPool except ImportError: - network_stats = None + BMConnectionPool = None -from network import StoppableThread +from network import stats, StoppableThread from version import softwareVersion try: # TODO: write tests for XML vulnerabilities @@ -1448,10 +1447,8 @@ class BMRPCDispatcher(object): or "connectedAndReceivingIncomingConnections". """ - try: - connections_num = len(network_stats.connectedHostsList()) - except AttributeError: - raise APIError(21, 'Could not import network_stats.') + connections_num = len(stats.connectedHostsList()) + if connections_num == 0: networkStatus = 'notConnected' elif state.clientHasReceivedIncomingConnections: @@ -1463,7 +1460,7 @@ class BMRPCDispatcher(object): 'numberOfMessagesProcessed': state.numberOfMessagesProcessed, 'numberOfBroadcastsProcessed': state.numberOfBroadcastsProcessed, 'numberOfPubkeysProcessed': state.numberOfPubkeysProcessed, - 'pendingDownload': network_stats.pendingDownload(), + 'pendingDownload': stats.pendingDownload(), 'networkStatus': networkStatus, 'softwareName': 'PyBitmessage', 'softwareVersion': softwareVersion @@ -1475,8 +1472,8 @@ class BMRPCDispatcher(object): Returns bitmessage connection information as dict with keys *inbound*, *outbound*. """ - if network_stats is None: - raise APIError(21, 'Could not import network_stats.') + if BMConnectionPool is None: + raise APIError(21, 'Could not import BMConnectionPool.') inboundConnections = [] outboundConnections = [] for i in BMConnectionPool().inboundConnections.values(): diff --git a/src/mock/network.py b/src/mock/network.py index 1fb56c30..3f33c91b 100644 --- a/src/mock/network.py +++ b/src/mock/network.py @@ -1,7 +1,7 @@ # pylint: disable=too-few-public-methods """ - Mock Network +Mock Network """ @@ -12,11 +12,14 @@ class objectracker(object): class stats(object): - """Mock network statics""" + """Mock network statistics""" @staticmethod def connectedHostsList(): - """List of all the mock connected hosts""" - return [ - "conn1", "conn2", "conn3", "conn4" - ] + """Mock list of all the connected hosts""" + return ["conn1", "conn2", "conn3", "conn4"] + + @staticmethod + def pendingDownload(): + """Mock pending download count""" + return 0 diff --git a/src/tests/partial.py b/src/tests/partial.py index fd2d3f77..ce39c9df 100644 --- a/src/tests/partial.py +++ b/src/tests/partial.py @@ -15,12 +15,17 @@ class TestPartialRun(unittest.TestCase): @classmethod def setUpClass(cls): + # pylint: disable=import-outside-toplevel,unused-import cls.dirs = (os.path.abspath(os.curdir), pathmagic.setup()) import bmconfigparser import state from debug import logger # noqa:F401 pylint: disable=unused-variable + if sys.hexversion >= 0x3000000: + from mock import network as network_mock + import network + network.stats = network_mock.stats state.shutdown = 0 cls.state = state diff --git a/src/tests/test_api_thread.py b/src/tests/test_api_thread.py index 203a035f..5164ce5d 100644 --- a/src/tests/test_api_thread.py +++ b/src/tests/test_api_thread.py @@ -1,5 +1,6 @@ """TestAPIThread class definition""" +import sys import time from six.moves import queue, xmlrpc_client @@ -58,3 +59,9 @@ class TestAPIThread(TestPartialRun): self.assertEqual(cmd, 'updateStatusBar') self.assertEqual(data, sample_statusbar_msg) + + def test_client_status(self): + status = self.api.clientStatus() + if sys.hexversion >= 0x3000000: + self.assertEqual(status["networkConnections"], 4) + self.assertEqual(status["pendingDownload"], 0)