From: Otto Moerbeek Date: Fri, 18 Nov 2022 10:14:53 +0000 (+0100) Subject: Smarter startup delay: wait for listen port to come alive X-Git-Tag: dnsdist-1.8.0-rc1~229^2~2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ffabdc3e5f53239746341cd97946b54e4cdfc706;p=thirdparty%2Fpdns.git Smarter startup delay: wait for listen port to come alive --- diff --git a/regression-tests.dnsdist/dnsdisttests.py b/regression-tests.dnsdist/dnsdisttests.py index 069578a619..6e540b125e 100644 --- a/regression-tests.dnsdist/dnsdisttests.py +++ b/regression-tests.dnsdist/dnsdisttests.py @@ -1,6 +1,7 @@ #!/usr/bin/env python2 import copy +import errno import os import socket import ssl @@ -72,6 +73,21 @@ class DNSDistTest(AssertEqualDNSMessageMixin, unittest.TestCase): _UDPResponder = None _TCPResponder = None + @classmethod + def waitForTCPSocket(cls, ipaddress, port): + for try_number in range(0, 20): + try: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.settimeout(1.0) + sock.connect((ipaddress, port)) + sock.close() + return + except Exception as err: + if err.errno != errno.ECONNREFUSED: + print(f'Error occurred: {try_number} {err}', file=sys.stderr) + time.sleep(0.1) + # We assume the dnsdist instance does not listen. That's fine. + @classmethod def startResponders(cls): print("Launching responders..") @@ -124,16 +140,14 @@ class DNSDistTest(AssertEqualDNSMessageMixin, unittest.TestCase): with open(logFile, 'w') as fdLog: cls._dnsdist = subprocess.Popen(dnsdistcmd, close_fds=True, stdout=fdLog, stderr=fdLog) - if 'DNSDIST_FAST_TESTS' in os.environ: - delay = 0.5 - else: - delay = cls._dnsdistStartupDelay - - time.sleep(delay) + cls.waitForTCPSocket(cls._dnsDistListeningAddr, cls._dnsDistPort); if cls._dnsdist.poll() is not None: - cls._dnsdist.kill() - sys.exit(cls._dnsdist.returncode) + print(f"\n*** startDNSDist log for {logFile} ***") + with open(logFile, 'r') as fdLog: + print(fdLog.read()) + print(f"*** End startDNSDist log for {logFile} ***") + raise AssertionError('%s failed (%d)' % (dnsdistcmd, cls._dnsdist.returncode)) @classmethod def setUpSockets(cls): @@ -176,6 +190,10 @@ class DNSDistTest(AssertEqualDNSMessageMixin, unittest.TestCase): @classmethod def tearDownClass(cls): + cls._sock.close() + # tell the background threads to stop, if any + for backgroundThread in cls._backgroundThreads: + cls._backgroundThreads[backgroundThread] = False cls.killProcess(cls._dnsdist) @classmethod diff --git a/regression-tests.dnsdist/test_Carbon.py b/regression-tests.dnsdist/test_Carbon.py index 924abf47ad..85ce6361ce 100644 --- a/regression-tests.dnsdist/test_Carbon.py +++ b/regression-tests.dnsdist/test_Carbon.py @@ -71,6 +71,17 @@ class TestCarbon(DNSDistTest): cls._CarbonResponder2.setDaemon(True) cls._CarbonResponder2.start() + @classmethod + def setUpClass(cls): + + cls.startResponders() + cls.startDNSDist() + cls.setUpSockets() + time.sleep(1) + + print("Launching tests..") + + def testCarbon(self): """ Carbon: send data to 2 carbon servers diff --git a/regression-tests.dnsdist/test_TLS.py b/regression-tests.dnsdist/test_TLS.py index 61624a5097..73ea381587 100644 --- a/regression-tests.dnsdist/test_TLS.py +++ b/regression-tests.dnsdist/test_TLS.py @@ -10,6 +10,16 @@ from dnsdisttests import DNSDistTest class TLSTests(object): + @classmethod + def setUpClass(cls): + + cls.startResponders() + cls.startDNSDist() + cls.setUpSockets() + time.sleep(1) + + print("Launching tests..") + def getServerCertificate(self): conn = self.openTLSConnection(self._tlsServerPort, self._serverName, self._caCert) cert = conn.getpeercert()