Browse Source

[1175] modify b10-stats_test.py, b10-stats-httpd_test.py and test_utils.py

 - exclude the setup of handler in SIGALRM and add it as an external
   function in the SignalHandler class in test_utils.py

 - define the function in that class to reset the handler in
   test_utils.py, and add it in tearDown() in each testcase
Naoki Kambe 13 years ago
parent
commit
fcfe5af9c2

+ 13 - 20
src/bin/stats/tests/b10-stats-httpd_test.py

@@ -32,13 +32,12 @@ import time
 import threading
 import http.client
 import xml.etree.ElementTree
-import signal
 import random
 
 import isc
 import stats_httpd
 import stats
-from test_utils import BaseModules, ThreadingServerManager, MyStats, MyStatsHttpd, send_command, send_shutdown
+from test_utils import BaseModules, ThreadingServerManager, MyStats, MyStatsHttpd, SignalHandler, send_command, send_shutdown
 
 DUMMY_DATA = {
     'Boss' : {
@@ -100,9 +99,8 @@ def is_ipv6_enabled(address='::1', port=8001):
 class TestHttpHandler(unittest.TestCase):
     """Tests for HttpHandler class"""
     def setUp(self):
-        # deadlock will be killed afer 20 secs
-        signal.signal(signal.SIGALRM, self.my_signal_handler)
-        signal.alarm(20)
+        # set the signal handler for deadlock
+        self.sig_handler = SignalHandler(self.fail)
         self.base = BaseModules()
         self.stats_server = ThreadingServerManager(MyStats)
         self.stats = self.stats_server.server
@@ -120,9 +118,8 @@ class TestHttpHandler(unittest.TestCase):
         self.stats_httpd_server.shutdown()
         self.stats_server.shutdown()
         self.base.shutdown()
-
-    def my_signal_handler(self, signal, frame):
-        self.fail("A deadlock might be detected")
+        # reset the signal handler
+        self.sig_handler.reset()
 
     def test_do_GET(self):
         self.assertTrue(type(self.stats_httpd.httpd) is list)
@@ -285,18 +282,16 @@ class TestHttpServerError(unittest.TestCase):
 class TestHttpServer(unittest.TestCase):
     """Tests for HttpServer class"""
     def setUp(self):
-        # deadlock will be killed afer 20 secs
-        signal.signal(signal.SIGALRM, self.my_signal_handler)
-        signal.alarm(20)
+        # set the signal handler for deadlock
+        self.sig_handler = SignalHandler(self.fail)
         self.base = BaseModules()
 
     def tearDown(self):
         if hasattr(self, "stats_httpd"):
             self.stats_httpd.stop()
         self.base.shutdown()
-
-    def my_signal_handler(self, signal, frame):
-        self.fail("A deadlock might be detected")
+        # reset the signal handler
+        self.sig_handler.reset()
 
     def test_httpserver(self):
         self.stats_httpd = MyStatsHttpd(get_availaddr())
@@ -318,9 +313,8 @@ class TestStatsHttpd(unittest.TestCase):
     """Tests for StatsHttpd class"""
 
     def setUp(self):
-        # deadlock will be killed afer 20 secs
-        signal.signal(signal.SIGALRM, self.my_signal_handler)
-        signal.alarm(20)
+        # set the signal handler for deadlock
+        self.sig_handler = SignalHandler(self.fail)
         self.base = BaseModules()
         self.stats_server = ThreadingServerManager(MyStats)
         self.stats_server.run()
@@ -332,9 +326,8 @@ class TestStatsHttpd(unittest.TestCase):
             self.stats_httpd.stop()
         self.stats_server.shutdown()
         self.base.shutdown()
-
-    def my_signal_handler(self, signal, frame):
-        self.fail("A deadlock might be detected")
+        # reset the signal handler
+        self.sig_handler.reset()
 
     def test_init(self):
         server_address = get_availaddr()

+ 5 - 8
src/bin/stats/tests/b10-stats_test.py

@@ -27,11 +27,10 @@ import threading
 import io
 import time
 import imp
-import signal
 
 import stats
 import isc.cc.session
-from test_utils import BaseModules, ThreadingServerManager, MyStats, send_command, send_shutdown
+from test_utils import BaseModules, ThreadingServerManager, MyStats, SignalHandler, send_command, send_shutdown
 
 class TestUtilties(unittest.TestCase):
     items = [
@@ -147,9 +146,8 @@ class TestCallback(unittest.TestCase):
 
 class TestStats(unittest.TestCase):
     def setUp(self):
-        # deadlock will be killed afer 20 secs
-        signal.signal(signal.SIGALRM, self.my_signal_handler)
-        signal.alarm(20)
+        # set the signal handler for deadlock
+        self.sig_handler = SignalHandler(self.fail)
         self.base = BaseModules()
         self.stats = stats.Stats()
         self.const_timestamp = 1308730448.965706
@@ -158,9 +156,8 @@ class TestStats(unittest.TestCase):
 
     def tearDown(self):
         self.base.shutdown()
-
-    def my_signal_handler(self, signal, frame):
-        self.fail("A deadlock might be detected")
+        # reset the signal handler
+        self.sig_handler.reset()
 
     def test_init(self):
         self.assertEqual(self.stats.module_name, 'Stats')

+ 19 - 0
src/bin/stats/tests/test_utils.py

@@ -9,6 +9,7 @@ import sys
 import threading
 import tempfile
 import json
+import signal
 
 import msgq
 import isc.config.cfgmgr
@@ -19,6 +20,24 @@ import stats_httpd
 if 'BIND10_MSGQ_SOCKET_FILE' not in os.environ:
     os.environ['BIND10_MSGQ_SOCKET_FILE'] = tempfile.mktemp(prefix='msgq_socket_')
 
+class SignalHandler():
+    """A signal handler class for deadlock in unittest"""
+    def __init__(self, fail_handler, timeout=20):
+        """sets a schedule in SIGARM for invoking the handler via
+        unittest.TestCase after timeout seconds (default is 20)"""
+        self.fail_handler = fail_handler
+        self.orig_handler = signal.signal(signal.SIGALRM, self.sig_handler)
+        signal.alarm(timeout)
+
+    def reset(self):
+        """resets the schedule in SIGALRM"""
+        signal.alarm(0)
+        signal.signal(signal.SIGALRM, self.orig_handler)
+
+    def sig_handler(self, signal, frame):
+        """envokes unittest.TestCase.fail as a signal handler"""
+        self.fail_handler("A deadlock might be detected")
+
 def send_command(command_name, module_name, params=None, session=None, nonblock=False, timeout=None):
     if session is not None:
         cc_session = session