Browse Source

[trac930] refactor unittests
- remove time.sleep from various unittests and add in the "run" method in
ThreadingServerManager
- adjust the sleep time (TIMEOUT_SEC)
- join some small unittests
(test_start_with_err, test_command_status, test_command_shutdown)

Naoki Kambe 13 years ago
parent
commit
0314c7bb66

+ 0 - 7
src/bin/stats/tests/b10-stats-httpd_test.py

@@ -78,7 +78,6 @@ class TestHttpHandler(unittest.TestCase):
         self.assertTrue(type(self.stats_httpd.httpd) is list)
         self.assertEqual(len(self.stats_httpd.httpd), 0)
         statshttpd_server.run()
-        time.sleep(TIMEOUT_SEC*8)
         client = http.client.HTTPConnection(address, port)
         client._http_vsn_str = 'HTTP/1.0\n'
         client.connect()
@@ -175,7 +174,6 @@ class TestHttpHandler(unittest.TestCase):
         statshttpd_server.run()
         self.assertTrue(self.stats_server.server.running)
         self.stats_server.shutdown()
-        time.sleep(TIMEOUT_SEC*2)
         self.assertFalse(self.stats_server.server.running)
         statshttpd.cc_session.set_timeout(milliseconds=TIMEOUT_SEC/1000)
         client = http.client.HTTPConnection(address, port)
@@ -213,7 +211,6 @@ class TestHttpHandler(unittest.TestCase):
             lambda cmd, args: \
                 isc.config.ccsession.create_answer(1, "I have an error.")
             )
-        time.sleep(TIMEOUT_SEC*5)
         client = http.client.HTTPConnection(address, port)
         client.connect()
 
@@ -244,7 +241,6 @@ class TestHttpHandler(unittest.TestCase):
         self.stats_httpd = statshttpd_server.server
         self.stats_httpd.load_config({'listen_on' : [{ 'address': address, 'port' : port }]})
         statshttpd_server.run()
-        time.sleep(TIMEOUT_SEC*5)
         client = http.client.HTTPConnection(address, port)
         client.connect()
         client.putrequest('HEAD', stats_httpd.XML_URL_PATH)
@@ -423,7 +419,6 @@ class TestStatsHttpd(unittest.TestCase):
         self.statshttpd_server = ThreadingServerManager(MyStatsHttpd)
         self.statshttpd_server.server.load_config({'listen_on' : [{ 'address': '127.0.0.1', 'port' : 65454 }]})
         self.statshttpd_server.run()
-        time.sleep(TIMEOUT_SEC)
         self.stats_httpd.load_config({'listen_on' : [{ 'address': '127.0.0.1', 'port' : 65454 }]})
         self.assertRaises(stats_httpd.HttpServerError, self.stats_httpd.open_httpd)
         self.statshttpd_server.shutdown()
@@ -434,7 +429,6 @@ class TestStatsHttpd(unittest.TestCase):
         self.stats_httpd = self.statshttpd_server.server
         self.stats_httpd.load_config({'listen_on' : [{ 'address': '127.0.0.1', 'port' : 65455 }]})
         self.statshttpd_server.run()
-        time.sleep(TIMEOUT_SEC*2)
         self.assertTrue(self.stats_httpd.running)
         self.statshttpd_server.shutdown()
         self.assertFalse(self.stats_httpd.running)
@@ -461,7 +455,6 @@ class TestStatsHttpd(unittest.TestCase):
         statshttpd = statshttpd_server.server
         statshttpd.load_config({'listen_on' : [{ 'address': address, 'port' : port }]})
         statshttpd_server.run()
-        time.sleep(TIMEOUT_SEC*2)
         statshttpd_server.shutdown()
 
     def test_open_template(self):

+ 12 - 11
src/bin/stats/tests/b10-stats_test.py

@@ -189,28 +189,28 @@ class TestStats(unittest.TestCase):
         stats.SPECFILE_LOCATION = orig_spec_location
 
     def test_start(self):
+        # start without err
         statsserver = ThreadingServerManager(MyStats)
-        stats = statsserver.server
-        self.assertFalse(stats.running)
+        statsd = statsserver.server
+        self.assertFalse(statsd.running)
         statsserver.run()
-        time.sleep(TIMEOUT_SEC)
-        self.assertTrue(stats.running)
+        self.assertTrue(statsd.running)
         statsserver.shutdown()
-        self.assertFalse(stats.running)
+        self.assertFalse(statsd.running)
 
-    def test_start_with_err(self):
+        # start with err
         statsd = stats.Stats()
         statsd.update_statistics_data = lambda x,**y: ['an error']
         self.assertRaises(stats.StatsError, statsd.start)
 
-    def test_config_handler(self):
+    def test_handlers(self):
+        # config_handler
         self.assertEqual(self.stats.config_handler({'foo':'bar'}),
                          isc.config.create_answer(0))
 
-    def test_command_handler(self):
+        # command_handler
         statsserver = ThreadingServerManager(MyStats)
         statsserver.run()
-        time.sleep(TIMEOUT_SEC*4)
         self.base.boss.server._started.wait()
         self.assertEqual(
             send_command(
@@ -352,12 +352,13 @@ class TestStats(unittest.TestCase):
         self.assertEqual(self.stats.update_statistics_data(owner='Dummy', foo='bar'),
                          ['unknown module name: Dummy'])
 
-    def test_command_status(self):
+    def test_commands(self):
+        # status
         self.assertEqual(self.stats.command_status(),
                 isc.config.create_answer(
                 0, "Stats is up. (PID " + str(os.getpid()) + ")"))
 
-    def test_command_shutdown(self):
+        # shutdown
         self.stats.running = True
         self.assertEqual(self.stats.command_shutdown(),
                          isc.config.create_answer(0))

+ 4 - 2
src/bin/stats/tests/test_utils.py

@@ -15,9 +15,9 @@ import stats
 import stats_httpd
 
 # TODO: consider appropriate timeout seconds
-TIMEOUT_SEC = 0.01
+TIMEOUT_SEC = 0.05
 
-def send_command(command_name, module_name, params=None, session=None, nonblock=False, timeout=TIMEOUT_SEC*2):
+def send_command(command_name, module_name, params=None, session=None, nonblock=False, timeout=TIMEOUT_SEC):
     if not session:
         cc_session = isc.cc.Session()
     else:
@@ -53,6 +53,8 @@ class ThreadingServerManager:
     def run(self):
         self.server._thread.start()
         self.server._started.wait()
+        # waiting for the server's being ready for listening
+        time.sleep(TIMEOUT_SEC)
 
     def shutdown(self):
         self.server.shutdown()