From bd5ef9294c66413bb4b4eca9d32aa8be684fd573 Mon Sep 17 00:00:00 2001 From: mike Date: Tue, 24 Nov 2009 03:08:50 +0000 Subject: LP 448319 : Adding engine tests, fixing probe tests, correcting bugs found with tests --- (limited to 'tutorius/engine.py') diff --git a/tutorius/engine.py b/tutorius/engine.py index b8fe988..be0b935 100644 --- a/tutorius/engine.py +++ b/tutorius/engine.py @@ -37,6 +37,7 @@ RUNNER_STATE_SETUP_EVENTS = 2 RUNNER_STATE_AWAITING_NOTIFICATIONS = 3 RUNNER_STATE_UNINSTALLING_ACTIONS = 4 RUNNER_STATE_UNSUBSCRIBING_EVENTS = 5 +RUNNER_STATE_STOPPED = 6 LOGGER = logging.getLogger("sugar.tutorius.engine") @@ -58,6 +59,9 @@ class TutorialRunner(object): # The message queue is a heap, so only heap operations should be done # on it like heappush, heappop, etc... + # The stocked messages are actually a list of parameters that should be + # passed to the appropriate function. E.g. When raising an event notification, + # it saves the (next_state, event) in the message. self._message_queue = [] #State @@ -92,9 +96,8 @@ class TutorialRunner(object): def _execute_stop(self): self.setCurrentActivity() #Temp Hack until activity in events/actions - self.enterState(self._tutorial.END) - self._teardownState() self._state = None + self._runner_state = RUNNER_STATE_STOPPED def _handleEvent(self, next_state, event): # Look if we are actually receiving notifications @@ -114,7 +117,10 @@ class TutorialRunner(object): if self._state is None: #No state, no teardown return - + self._remove_installed_actions() + self._remove_subscribed_events() + + def _remove_installed_actions(self): #Clear the current actions for (action_name, action_address) in self._installed_actions.items(): LOGGER.debug("TutorialRunner :: Uninstalling action %s with address %s"%(action_name, action_address)) @@ -123,6 +129,7 @@ class TutorialRunner(object): self._installed_actions.clear() self._installation_errors.clear() + def _remove_subscribed_events(self): #Clear the EventFilters for (event_name, event) in self._subscribed_events.items(): self._pM.unsubscribe(event) @@ -137,7 +144,7 @@ class TutorialRunner(object): def __install_error(self, action_name, action, exception): # TODO : Fix this as it doesn't warn the user about the problem or anything - LOGGER.debug("TutorialRunner :: Action could not be installed %s, exception was : %s"%(str(action) + str(exception))) + LOGGER.debug("TutorialRunner :: Action could not be installed %s, exception was : %s"%(str(action), str(exception))) self._installation_errors[action_name] = exception self.__verify_action_install_state() @@ -147,7 +154,7 @@ class TutorialRunner(object): install_complete = True for (this_action_name, this_action) in self._actions.items(): if not this_action_name in self._installed_actions.keys() and \ - not this_action in self._installation_errors.keys(): + not this_action_name in self._installation_errors.keys(): # There's at least one uninstalled action, so we still wait install_complete = False break @@ -166,8 +173,8 @@ class TutorialRunner(object): def __subscribe_error(self, event_name, exception): # TODO : Do correct error handling here - LOGGER.debug("TutorialRunner :: Could not subscribe to event %s, got exception : "%(event_name, str(exception))) - self._subscribed_error[event_name] = exception + LOGGER.debug("TutorialRunner :: Could not subscribe to event %s, got exception : %s"%(event_name, str(exception))) + self._subscription_errors[event_name] = exception # Verify if we just completed the subscription of all the events for this state self.__verify_event_install_state() @@ -178,8 +185,8 @@ class TutorialRunner(object): # Check to see if we completed all the event subscriptions subscribe_complete = True for (this_event_name, (this_event, next_state)) in transitions.items(): - if not this_event in self._subscribed_events.keys() and \ - not this_event in self._subscription_errors.keys(): + if not this_event_name in self._subscribed_events.keys() and \ + not this_event_name in self._subscription_errors.keys(): subscribe_complete = False break @@ -206,16 +213,26 @@ class TutorialRunner(object): return # Send all the event registration - for (event, next_state) in transitions.values(): - self._pM.subscribe(event, + for (event_name, (event, next_state)) in transitions.items(): + self._pM.subscribe(event_name, event, save_args(self._handleEvent, next_state), - save_args(self.__event_subscribed, event), - save_args(self.__subscribe_error, event)) + save_args(self.__event_subscribed, event_name), + save_args(self.__subscribe_error, event_name)) def __all_events_subscribed(self): self._runner_state = RUNNER_STATE_AWAITING_NOTIFICATIONS self.__process_pending_messages() + def __uninstall_actions(self): + self._runner_state = RUNNER_STATE_UNINSTALLING_ACTIONS + self._remove_installed_actions() + self._execute_stop() + + def __unsubscribe_events(self): + self._runner_state = RUNNER_STATE_UNSUBSCRIBING_EVENTS + self._remove_subscribed_events() + self.__uninstall_actions() + def __process_pending_messages(self): while len(self._message_queue) != 0: (priority, message) = heappop(self._message_queue) @@ -224,13 +241,13 @@ class TutorialRunner(object): LOGGER.debug("TutorialRunner :: Stop message taken from message queue") # We can safely ignore the rest of the events self._message_queue = [] - self._execute_stop() + #self._execute_stop() # Start removing the installed addons - #if self._runner_state == RUNNER_STATE_AWAITING_NOTIFICATIONS: - # # Start uninstalling the events - # self.__unsubscribe_events() - #if self._runner_state == RUNNER_STATE_SETUP_EVENTS: - # self.__uninstall_actions() + if self._runner_state == RUNNER_STATE_AWAITING_NOTIFICATIONS: + # Start uninstalling the events + self.__unsubscribe_events() + if self._runner_state == RUNNER_STATE_SETUP_EVENTS: + self.__uninstall_actions() elif priority == EVENT_NOTIFICATION_MSG_PRIORITY: LOGGER.debug("TutorialRunner :: Handling stored event notification for next_state %s"%message[0]) self._handle_event(*message) -- cgit v0.9.1