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