From 9676c28f9c6fd3e2eff34a6f621368a2d4c8d783 Mon Sep 17 00:00:00 2001 From: Bodo Petermann Date: Tue, 19 Jan 2021 13:25:24 +0100 Subject: [PATCH] Fix stopping timer in TestTimer Fixes broken functional tests where NamespaceFixture is used and the TestTimer raises TestTimerTimeout even if the namespace was cleaned up in time. The fix makes sure that the alarm is cancelled in __exit__ if there was no alarm before TestTimer's __enter__ (if self._old_timer is 0). It also makes sure to reset the signal handler if the old one was Handler.SIG_DFL (which is treated as false, no we need to check for "is not None" instead). Closes-Bug: #1912320 Change-Id: I9efad8eb5fe6e794235280f8a9a026800513d969 --- neutron/tests/common/helpers.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/neutron/tests/common/helpers.py b/neutron/tests/common/helpers.py index 8b9335a1657..a156f935b7a 100644 --- a/neutron/tests/common/helpers.py +++ b/neutron/tests/common/helpers.py @@ -263,15 +263,16 @@ class TestTimer(object): return self def __exit__(self, exc, value, traceback): - if self._old_handler: + if self._old_handler is not None: signal.signal(signal.SIGALRM, self._old_handler) if self._old_timer == 0: - return + timeout = 0 + else: + # If timer has expired, set the minimum required value (1) to + # activate the SIGALRM event. + timeout = self._old_timer - self._timeout + timeout = 1 if timeout <= 0 else timeout - # If timer has expired, set the minimum required value (1) to activate - # the SIGALRM event. - timeout = self._old_timer - self._timeout - timeout = 1 if timeout <= 0 else timeout if self._alarm_fn: self._alarm_fn(timeout)