From 80932af4062b169341b1de581d9a781435546f0c Mon Sep 17 00:00:00 2001 From: sebres Date: Fri, 22 Dec 2017 13:29:35 +0100 Subject: [PATCH] coverage: testErrorsInLoop should avoid sporadic coverage changes, if some communication errors not occurred sometimes. --- fail2ban/server/asyncserver.py | 4 +++- fail2ban/tests/sockettestcase.py | 19 +++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/fail2ban/server/asyncserver.py b/fail2ban/server/asyncserver.py index 11c81649..e254979d 100644 --- a/fail2ban/server/asyncserver.py +++ b/fail2ban/server/asyncserver.py @@ -122,7 +122,9 @@ def loop(active, timeout=None, use_poll=False): if timeout is None: timeout = Utils.DEFAULT_SLEEP_TIME poll = asyncore.poll - if use_poll and asyncore.poll2 and hasattr(asyncore.select, 'poll'): # pragma: no cover + if callable(use_poll): + poll = use_poll + elif use_poll and asyncore.poll2 and hasattr(asyncore.select, 'poll'): # pragma: no cover logSys.debug('Server listener (select) uses poll') # poll2 expected a timeout in milliseconds (but poll and loop in seconds): timeout = float(timeout) / 1000 diff --git a/fail2ban/tests/sockettestcase.py b/fail2ban/tests/sockettestcase.py index 1a94a952..4f9b9d7a 100644 --- a/fail2ban/tests/sockettestcase.py +++ b/fail2ban/tests/sockettestcase.py @@ -31,8 +31,10 @@ import threading import time import unittest +from .utils import LogCaptureTestCase + from .. import protocol -from ..server.asyncserver import AsyncServer, AsyncServerException +from ..server.asyncserver import AsyncServer, AsyncServerException, loop from ..server.utils import Utils from ..client.csocket import CSocket @@ -126,7 +128,20 @@ class Socket(unittest.TestCase): self.assertFalse(os.path.exists(self.sock_name)) -class ClientMisc(unittest.TestCase): +class ClientMisc(LogCaptureTestCase): + + def testErrorsInLoop(self): + phase = {'cntr': 0} + def _active(): + return phase['cntr'] < 40 + def _poll(*args): + phase['cntr'] += 1 + raise Exception('test *%d*' % phase['cntr']) + # test errors "catched" and logged: + loop(_active, use_poll=_poll) + self.assertLogged("test *1*", "test *10*", "test *20*", all=True) + self.assertLogged("Too many errors - stop logging connection errors") + self.assertNotLogged("test *21*", "test *22*", "test *23*", all=True) def testPrintFormattedAndWiki(self): # redirect stdout to devnull