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
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():

View File

@ -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

View File

@ -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

View File

@ -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)