mirror of https://github.com/fail2ban/fail2ban
observer inherits from JailThread + test cases for bad run;
parent
e70406d5a1
commit
6d2b596bb4
|
@ -47,8 +47,8 @@ class JailThread(Thread):
|
|||
The time the thread sleeps for in the loop.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
super(JailThread, self).__init__()
|
||||
def __init__(self, name=None):
|
||||
super(JailThread, self).__init__(name=name)
|
||||
## Control the state of the thread.
|
||||
self.active = False
|
||||
## Control the idle state of the thread.
|
||||
|
|
|
@ -26,6 +26,7 @@ __copyright__ = "Copyright (c) 2014 Serg G. Brester"
|
|||
__license__ = "GPL"
|
||||
|
||||
import threading
|
||||
from .jailthread import JailThread
|
||||
import os, logging, time, datetime, math, json, random
|
||||
import sys
|
||||
from ..helpers import getLogger
|
||||
|
@ -34,7 +35,7 @@ from .mytime import MyTime
|
|||
# Gets the instance of the logger.
|
||||
logSys = getLogger(__name__)
|
||||
|
||||
class ObserverThread(threading.Thread):
|
||||
class ObserverThread(JailThread):
|
||||
"""Handles observing a database, managing bad ips and ban increment.
|
||||
|
||||
Parameters
|
||||
|
@ -236,7 +237,6 @@ class ObserverThread(threading.Thread):
|
|||
def start(self):
|
||||
with self._queue_lock:
|
||||
if not self.active:
|
||||
self.active = True
|
||||
super(ObserverThread, self).start()
|
||||
|
||||
def stop(self):
|
||||
|
|
|
@ -551,16 +551,15 @@ class BanTimeIncrDB(unittest.TestCase):
|
|||
# stop observer
|
||||
obs.stop()
|
||||
|
||||
class ObserverTest(unittest.TestCase):
|
||||
class ObserverTest(LogCaptureTestCase):
|
||||
|
||||
def setUp(self):
|
||||
"""Call before every test case."""
|
||||
#super(ObserverTest, self).setUp()
|
||||
pass
|
||||
super(ObserverTest, self).setUp()
|
||||
|
||||
def tearDown(self):
|
||||
#super(ObserverTest, self).tearDown()
|
||||
pass
|
||||
"""Call after every test case."""
|
||||
super(ObserverTest, self).tearDown()
|
||||
|
||||
def testObserverBanTimeIncr(self):
|
||||
obs = ObserverThread()
|
||||
|
@ -592,3 +591,24 @@ class ObserverTest(unittest.TestCase):
|
|||
self.assertTrue(obs.is_alive())
|
||||
obs.stop()
|
||||
obs = None
|
||||
|
||||
class _BadObserver(ObserverThread):
|
||||
def run(self):
|
||||
raise RuntimeError('run bad thread exception')
|
||||
|
||||
def testObserverBadRun(self):
|
||||
obs = ObserverTest._BadObserver()
|
||||
# don't wait for empty by stop
|
||||
obs.wait_empty = lambda v:()
|
||||
# save previous hook, prevent write stderr and check hereafter __excepthook__ was executed
|
||||
prev_exchook = sys.__excepthook__
|
||||
x = []
|
||||
sys.__excepthook__ = lambda *args: x.append(args)
|
||||
obs.start()
|
||||
obs.stop()
|
||||
obs = None
|
||||
self.assertTrue(self._is_logged("Unhandled exception"))
|
||||
sys.__excepthook__ = prev_exchook
|
||||
self.assertEqual(len(x), 1)
|
||||
self.assertEqual(x[0][0], RuntimeError)
|
||||
self.assertEqual(str(x[0][1]), 'run bad thread exception')
|
||||
|
|
Loading…
Reference in New Issue