From b1f03149f03d25d1808bf11c6fb4e8f2e4dca61f Mon Sep 17 00:00:00 2001 From: Alistair Coles Date: Mon, 20 Dec 2021 10:16:05 +0000 Subject: [PATCH] Attempt fix test timeouts Zuul unit test jobs have sometimes been timing out, often while executing a test that attempts getaddrinfo. Mock the getaddrinfo call to see if that helps. Change-Id: I9ea43bb079bef5aba0aeee899c224da13d34f918 --- test/unit/common/test_utils.py | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/test/unit/common/test_utils.py b/test/unit/common/test_utils.py index 52763a25cd..97be1251f1 100644 --- a/test/unit/common/test_utils.py +++ b/test/unit/common/test_utils.py @@ -5580,17 +5580,34 @@ class TestStatsdLogging(unittest.TestCase): # instantiation so we don't call getaddrinfo() too often and don't have # to call bind() on our socket to detect IPv4/IPv6 on every send. # - # This test uses the real getaddrinfo, so we patch over the mock to - # put the real one back. If we just stop the mock, then - # unittest.exit() blows up, but stacking real-fake-real works okay. - with mock.patch.object(utils.socket, 'getaddrinfo', - self.real_getaddrinfo): + # This test patches over the existing mock. If we just stop the + # existing mock, then unittest.exit() blows up, but stacking + # real-fake-fake works okay. + calls = [] + + def fake_getaddrinfo(host, port, family, *args): + calls.append(family) + if len(calls) == 1: + raise socket.gaierror + # this is what a real getaddrinfo('::1', port, + # socket.AF_INET6) returned once + return [(socket.AF_INET6, + socket.SOCK_STREAM, + socket.IPPROTO_TCP, + '', ('::1', port, 0, 0)), + (socket.AF_INET6, + socket.SOCK_DGRAM, + socket.IPPROTO_UDP, + '', + ('::1', port, 0, 0))] + + with mock.patch.object(utils.socket, 'getaddrinfo', fake_getaddrinfo): logger = utils.get_logger({ 'log_statsd_host': '::1', 'log_statsd_port': '9876', }, 'some-name', log_route='some-route') statsd_client = logger.logger.statsd_client - + self.assertEqual([socket.AF_INET, socket.AF_INET6], calls) self.assertEqual(statsd_client._sock_family, socket.AF_INET6) self.assertEqual(statsd_client._target, ('::1', 9876, 0, 0))