Fix logic in test_process

This commit is contained in:
Lee Miller 2022-09-23 04:25:24 +03:00
parent d145143e4b
commit 23769a8bf3
Signed by untrusted user: lee.miller
GPG Key ID: 4F97A5EA88F4AB63
1 changed files with 25 additions and 14 deletions

View File

@ -11,8 +11,8 @@ import psutil
class TestProcessProto(unittest.TestCase): class TestProcessProto(unittest.TestCase):
"""Test process attributes, common flow""" """Test process attributes, common flow"""
_process_cmd = ['minode'] _process_cmd = ['minode']
_connection_limit = 8 if sys.platform.startswith('win') else 16 _connection_limit = 4 if sys.platform.startswith('win') else 10
_listen = None _listen = False
_listening_port = None _listening_port = None
home = None home = None
@ -25,11 +25,10 @@ class TestProcessProto(unittest.TestCase):
'--data-dir', cls.home, '--data-dir', cls.home,
'--connection-limit', str(cls._connection_limit) '--connection-limit', str(cls._connection_limit)
] ]
if cls._listen is True: if not cls._listen:
if cls._listening_port:
cmd += ['-p', cls._listening_port]
elif cls._listen is False:
cmd += ['--no-incoming'] cmd += ['--no-incoming']
elif cls._listening_port:
cmd += ['-p', str(cls._listening_port)]
cls.process = psutil.Popen(cmd, stderr=subprocess.STDOUT) # nosec cls.process = psutil.Popen(cmd, stderr=subprocess.STDOUT) # nosec
@classmethod @classmethod
@ -69,26 +68,38 @@ class TestProcess(TestProcessProto):
def test_connections(self): def test_connections(self):
"""Check minode process connections""" """Check minode process connections"""
_started = time.time() _started = time.time()
connections = []
for t in range(40): def connections():
connections = self.process.connections() return [
if len(connections) > self._connection_limit / 2: c for c in self.process.connections()
if c.status == 'ESTABLISHED']
for t in range(120):
if len(connections()) > self._connection_limit / 2:
_time_to_connect = round(time.time() - _started) _time_to_connect = round(time.time() - _started)
break break
time.sleep(0.5) time.sleep(0.5)
else: else:
self.fail( self.fail(
'Failed establish at least %s connections in 20 sec' 'Failed establish at least %s connections in 60 sec'
% (self._connection_limit / 2)) % (self._connection_limit / 2))
for t in range(_time_to_connect * 2): for t in range(_time_to_connect * 2):
self.assertLessEqual( self.assertLessEqual(
len(connections), self._connection_limit + 1, # one listening len(connections()),
# shared.outgoing_connections, one listening
# TODO: find the cause of one extra
(min(self._connection_limit, 8) if not self._listen
else self._connection_limit) + 1,
'Opened more connections than required by --connection-limit') 'Opened more connections than required by --connection-limit')
time.sleep(0.5) time.sleep(1)
for c in connections:
for c in self.process.connections():
if c.status == 'LISTEN': if c.status == 'LISTEN':
if self._listen is False: if self._listen is False:
return self.fail( return self.fail(
'Listening while started with --no-incoming') 'Listening while started with --no-incoming')
self.assertEqual(c.laddr[1], self._listening_port or 8444) self.assertEqual(c.laddr[1], self._listening_port or 8444)
break break
else:
if self._listen:
self.fail('No listening connection found')