Fix floating bugs in test_api #1771

Merged
g1itch merged 3 commits from api into v0.6 2021-08-02 20:51:22 +02:00

View File

@ -5,16 +5,12 @@ Tests using API.
import base64
import json
import time
from .common import skip_python3
skip_python3()
from six.moves import xmlrpc_client # nosec
try: # nosec
from xmlrpclib import ServerProxy, ProtocolError
except ImportError:
from xmlrpc.client import ServerProxy, ProtocolError
import psutil
from .test_process import TestProcessProto, TestProcessShutdown
from .test_process import TestProcessProto
class TestAPIProto(TestProcessProto):
@ -26,7 +22,7 @@ class TestAPIProto(TestProcessProto):
"""Setup XMLRPC proxy for pybitmessage API"""
super(TestAPIProto, cls).setUpClass()
cls.addresses = []
cls.api = ServerProxy(
cls.api = xmlrpc_client.ServerProxy(
"http://username:password@127.0.0.1:8442/")
for _ in range(5):
if cls._get_readline('.api_started'):
@ -34,18 +30,16 @@ class TestAPIProto(TestProcessProto):
time.sleep(1)
class TestAPIShutdown(TestAPIProto, TestProcessShutdown):
class TestAPIShutdown(TestAPIProto):
"""Separate test case for API command 'shutdown'"""
def test_shutdown(self):
"""Shutdown the pybitmessage"""
self.assertEqual(self.api.shutdown(), 'done')
for _ in range(5):
if not self.process.is_running():
break
time.sleep(2)
else:
try:
self.process.wait(20)
except psutil.TimeoutExpired:
self.fail(
'%s has not stopped in 10 sec' % ' '.join(self._process_cmd))
'%s has not stopped in 20 sec' % ' '.join(self._process_cmd))
# TODO: uncovered API commands
@ -72,8 +66,9 @@ class TestAPI(TestAPIProto):
def test_user_password(self):
"""Trying to connect with wrong username/password"""
api_wrong = ServerProxy("http://test:wrong@127.0.0.1:8442/")
with self.assertRaises(ProtocolError):
api_wrong = xmlrpc_client.ServerProxy(
"http://test:wrong@127.0.0.1:8442/")
with self.assertRaises(xmlrpc_client.ProtocolError):
api_wrong.clientStatus()
def test_connection(self):
@ -281,6 +276,7 @@ class TestAPI(TestAPIProto):
msg = base64.encodestring('test broadcast')
ackdata = self.api.sendBroadcast(
addr, base64.encodestring('test_subject'), msg)
try:
int(ackdata, 16)
status = self.api.getStatus(ackdata)
@ -288,6 +284,15 @@ class TestAPI(TestAPIProto):
raise KeyError
self.assertIn(status, (
'doingbroadcastpow', 'broadcastqueued', 'broadcastsent'))
start = time.time()
while status == 'doingbroadcastpow':
spent = int(time.time() - start)
if spent > 30:
self.fail('PoW is taking too much time: %ss' % spent)
time.sleep(1) # wait for PoW to get final msgid on next step
status = self.api.getStatus(ackdata)
# Find the message and its ID in sent
for m in json.loads(self.api.getAllSentMessages())['sentMessages']:
if m['ackData'] == ackdata: