observer inherits from JailThread + test cases for bad run;

pull/716/head
sebres 2014-07-08 10:56:19 +02:00
parent e70406d5a1
commit 6d2b596bb4
3 changed files with 29 additions and 9 deletions

View File

@ -47,8 +47,8 @@ class JailThread(Thread):
The time the thread sleeps for in the loop. The time the thread sleeps for in the loop.
""" """
def __init__(self): def __init__(self, name=None):
super(JailThread, self).__init__() super(JailThread, self).__init__(name=name)
## Control the state of the thread. ## Control the state of the thread.
self.active = False self.active = False
## Control the idle state of the thread. ## Control the idle state of the thread.

View File

@ -26,6 +26,7 @@ __copyright__ = "Copyright (c) 2014 Serg G. Brester"
__license__ = "GPL" __license__ = "GPL"
import threading import threading
from .jailthread import JailThread
import os, logging, time, datetime, math, json, random import os, logging, time, datetime, math, json, random
import sys import sys
from ..helpers import getLogger from ..helpers import getLogger
@ -34,7 +35,7 @@ from .mytime import MyTime
# Gets the instance of the logger. # Gets the instance of the logger.
logSys = getLogger(__name__) logSys = getLogger(__name__)
class ObserverThread(threading.Thread): class ObserverThread(JailThread):
"""Handles observing a database, managing bad ips and ban increment. """Handles observing a database, managing bad ips and ban increment.
Parameters Parameters
@ -236,7 +237,6 @@ class ObserverThread(threading.Thread):
def start(self): def start(self):
with self._queue_lock: with self._queue_lock:
if not self.active: if not self.active:
self.active = True
super(ObserverThread, self).start() super(ObserverThread, self).start()
def stop(self): def stop(self):

View File

@ -551,16 +551,15 @@ class BanTimeIncrDB(unittest.TestCase):
# stop observer # stop observer
obs.stop() obs.stop()
class ObserverTest(unittest.TestCase): class ObserverTest(LogCaptureTestCase):
def setUp(self): def setUp(self):
"""Call before every test case.""" """Call before every test case."""
#super(ObserverTest, self).setUp() super(ObserverTest, self).setUp()
pass
def tearDown(self): def tearDown(self):
#super(ObserverTest, self).tearDown() """Call after every test case."""
pass super(ObserverTest, self).tearDown()
def testObserverBanTimeIncr(self): def testObserverBanTimeIncr(self):
obs = ObserverThread() obs = ObserverThread()
@ -592,3 +591,24 @@ class ObserverTest(unittest.TestCase):
self.assertTrue(obs.is_alive()) self.assertTrue(obs.is_alive())
obs.stop() obs.stop()
obs = None 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')