Mock network.stats for python3 in tests.partial

This commit is contained in:
Lee Miller 2023-12-02 00:43:42 +02:00
parent 2501212a82
commit 7c153c0eb8
Signed by untrusted user: lee.miller
GPG Key ID: 4F97A5EA88F4AB63
4 changed files with 28 additions and 16 deletions

View File

@ -94,12 +94,11 @@ from helper_sql import (
from inventory import Inventory from inventory import Inventory
try: try:
import network.stats as network_stats
from network import BMConnectionPool from network import BMConnectionPool
except ImportError: except ImportError:
network_stats = None BMConnectionPool = None
from network import StoppableThread from network import stats, StoppableThread
from version import softwareVersion from version import softwareVersion
try: # TODO: write tests for XML vulnerabilities try: # TODO: write tests for XML vulnerabilities
@ -1448,10 +1447,8 @@ class BMRPCDispatcher(object):
or "connectedAndReceivingIncomingConnections". or "connectedAndReceivingIncomingConnections".
""" """
try: connections_num = len(stats.connectedHostsList())
connections_num = len(network_stats.connectedHostsList())
except AttributeError:
raise APIError(21, 'Could not import network_stats.')
if connections_num == 0: if connections_num == 0:
networkStatus = 'notConnected' networkStatus = 'notConnected'
elif state.clientHasReceivedIncomingConnections: elif state.clientHasReceivedIncomingConnections:
@ -1463,7 +1460,7 @@ class BMRPCDispatcher(object):
'numberOfMessagesProcessed': state.numberOfMessagesProcessed, 'numberOfMessagesProcessed': state.numberOfMessagesProcessed,
'numberOfBroadcastsProcessed': state.numberOfBroadcastsProcessed, 'numberOfBroadcastsProcessed': state.numberOfBroadcastsProcessed,
'numberOfPubkeysProcessed': state.numberOfPubkeysProcessed, 'numberOfPubkeysProcessed': state.numberOfPubkeysProcessed,
'pendingDownload': network_stats.pendingDownload(), 'pendingDownload': stats.pendingDownload(),
'networkStatus': networkStatus, 'networkStatus': networkStatus,
'softwareName': 'PyBitmessage', 'softwareName': 'PyBitmessage',
'softwareVersion': softwareVersion 'softwareVersion': softwareVersion
@ -1475,8 +1472,8 @@ class BMRPCDispatcher(object):
Returns bitmessage connection information as dict with keys *inbound*, Returns bitmessage connection information as dict with keys *inbound*,
*outbound*. *outbound*.
""" """
if network_stats is None: if BMConnectionPool is None:
raise APIError(21, 'Could not import network_stats.') raise APIError(21, 'Could not import BMConnectionPool.')
inboundConnections = [] inboundConnections = []
outboundConnections = [] outboundConnections = []
for i in BMConnectionPool().inboundConnections.values(): for i in BMConnectionPool().inboundConnections.values():

View File

@ -1,7 +1,7 @@
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
""" """
Mock Network Mock Network
""" """
@ -12,11 +12,14 @@ class objectracker(object):
class stats(object): class stats(object):
"""Mock network statics""" """Mock network statistics"""
@staticmethod @staticmethod
def connectedHostsList(): def connectedHostsList():
"""List of all the mock connected hosts""" """Mock list of all the connected hosts"""
return [ return ["conn1", "conn2", "conn3", "conn4"]
"conn1", "conn2", "conn3", "conn4"
] @staticmethod
def pendingDownload():
"""Mock pending download count"""
return 0

View File

@ -15,12 +15,17 @@ class TestPartialRun(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
# pylint: disable=import-outside-toplevel,unused-import
cls.dirs = (os.path.abspath(os.curdir), pathmagic.setup()) cls.dirs = (os.path.abspath(os.curdir), pathmagic.setup())
import bmconfigparser import bmconfigparser
import state import state
from debug import logger # noqa:F401 pylint: disable=unused-variable 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 state.shutdown = 0
cls.state = state cls.state = state

View File

@ -1,5 +1,6 @@
"""TestAPIThread class definition""" """TestAPIThread class definition"""
import sys
import time import time
from six.moves import queue, xmlrpc_client from six.moves import queue, xmlrpc_client
@ -58,3 +59,9 @@ class TestAPIThread(TestPartialRun):
self.assertEqual(cmd, 'updateStatusBar') self.assertEqual(cmd, 'updateStatusBar')
self.assertEqual(data, sample_statusbar_msg) 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)