Browse Source

[640] fix race condition in stats test

Jelte Jansen 13 years ago
parent
commit
2da5a3de56
2 changed files with 17 additions and 4 deletions
  1. 5 2
      src/bin/stats/tests/b10-stats_test.py
  2. 12 2
      src/bin/stats/tests/test_utils.py

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

@@ -205,9 +205,12 @@ class TestStats(unittest.TestCase):
         # Override moduleCCSession so we can check if send_stopping is called
         self.stats.mccs = MockModuleCCSession()
         self.assertEqual(send_shutdown("Stats"), (0, None))
-        self.assertTrue(self.stats.mccs.stopped)
         self.assertFalse(self.stats.running)
-        self.stats_server.shutdown()
+        # Call server.shutdown with argument True so the thread.join() call
+        # blocks and we are sure the main loop has finished (and set
+        # mccs.stopped)
+        self.stats_server.shutdown(True)
+        self.assertTrue(self.stats.mccs.stopped)
 
         # start with err
         self.stats = stats.Stats()

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

@@ -72,9 +72,19 @@ class ThreadingServerManager:
         self.server._started.wait()
         self.server._started.clear()
 
-    def shutdown(self):
+    def shutdown(self, blocking=False):
+        """Shut down the server by calling its own shutdown() method.
+           Then wait for its thread to finish. If blocking is True,
+           the thread.join() blocks until the thread finishes. If not,
+           it uses a zero timeout. The latter is necessary in a number
+           of existing tests. We should redo this part (we should not
+           even need threads in most, if not all, of these threads, see
+           ticket #1668)"""
         self.server.shutdown()
-        self.server._thread.join(0) # timeout is 0
+        if blocking:
+            self.server._thread.join()
+        else:
+            self.server._thread.join(0) # timeout is 0
 
 def do_nothing(*args, **kwargs): pass