Fix run cleanup if the UI ends without clearing non-Hooke msgs from the Queue.
[hooke.git] / hooke / hooke.py
index ccea160687edaa714e5bbbd15204224682104a12..c2ef159d8bccc4bbbfe5ac5b789ad52ae55132f1 100644 (file)
@@ -5,15 +5,15 @@
 #
 # This file is part of Hooke.
 #
-# Hooke is free software: you can redistribute it and/or
-# modify it under the terms of the GNU Lesser General Public
-# License as published by the Free Software Foundation, either
-# version 3 of the License, or (at your option) any later version.
+# Hooke is free software: you can redistribute it and/or modify it
+# under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
 #
-# Hooke is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU Lesser General Public License for more details.
+# Hooke is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
+# Public License for more details.
 #
 # You should have received a copy of the GNU Lesser General Public
 # License along with Hooke.  If not, see
@@ -59,6 +59,7 @@ import logging.config
 import multiprocessing
 import optparse
 import os.path
+import Queue
 import unittest
 import StringIO
 import sys
@@ -94,7 +95,6 @@ class Hooke (object):
     def load_log(self):
         config_file = StringIO.StringIO()
         self.config.write(config_file)
-        x = config_file.getvalue()
         logging.config.fileConfig(StringIO.StringIO(config_file.getvalue()))
         # Don't attach the logger because it contains an unpicklable
         # thread.lock.  Instead, grab it directly every time you need it.
@@ -157,9 +157,14 @@ class HookeRunner (object):
         return (ui_to_command, command_to_ui, command)
 
     def _cleanup_run(self, ui_to_command, command_to_ui, command):
+        log = logging.getLogger('hooke')
+        log.debug('cleanup sending CloseEngine')
         ui_to_command.put(ui.CloseEngine())
-        hooke = command_to_ui.get()
-        assert isinstance(hooke, Hooke)
+        hooke = None
+        while not isinstance(hooke, Hooke):
+            log.debug('cleanup waiting for Hooke instance from the engine.')
+            hooke = command_to_ui.get(block=True)
+            log.debug('cleanup got %s instance' % type(hooke))
         command.join()
         return hooke