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') diff --git a/tutorius/TProbe.py b/tutorius/TProbe.py index e58dd03..02352af 100644 --- a/tutorius/TProbe.py +++ b/tutorius/TProbe.py @@ -361,7 +361,7 @@ class ProbeProxy: del self._actions[this_action] break - def __update_event(self, event, callback, event_subscribed_cb, address): + def __update_event(self, event_name, event, callback, event_subscribed_cb, address): LOGGER.debug("ProbeProxy :: Registered event %s with address %s", str(hash(event)), str(address)) # Since multiple callbacks could be associated to the same # event signature, we will store multiple callbacks @@ -410,7 +410,7 @@ class ProbeProxy: else: LOGGER.debug("ProbeProxy :: unsubsribe address %s inconsistency : not registered", address) - def subscribe(self, event, notification_cb, event_subscribed_cb, error_cb): + def subscribe(self, event_name, event, notification_cb, event_subscribed_cb, error_cb): """ Register an event listener @param event Event to listen for @@ -427,7 +427,7 @@ class ProbeProxy: # for event types and sources, we will need to revise the lookup # mecanism for which callback function to call self._probe.subscribe(pickle.dumps(event), - reply_handler=save_args(self.__update_event, event, notification_cb, event_subscribed_cb), + reply_handler=save_args(self.__update_event, event_name, event, notification_cb, event_subscribed_cb), error_handler=save_args(error_cb, event)) def unsubscribe(self, address, block=True): @@ -528,7 +528,7 @@ class ProbeManager(object): else: raise RuntimeWarning("No activity attached") - def subscribe(self, event, notification_cb, event_subscribed_cb, error_cb): + def subscribe(self, event_name, event, notification_cb, event_subscribed_cb, error_cb): """ Register an event listener @param event Event to listen for @@ -540,7 +540,7 @@ class ProbeManager(object): @return address identifier used for unsubscribing """ if self.currentActivity: - return self._first_proxy(self.currentActivity).subscribe(event, notification_cb,\ + return self._first_proxy(self.currentActivity).subscribe(event_name, event, notification_cb,\ event_subscribed_cb, error_cb) else: raise RuntimeWarning("No activity attached") 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) diff --git a/tutorius/translator.py b/tutorius/translator.py index bfa1746..2265ebd 100644 --- a/tutorius/translator.py +++ b/tutorius/translator.py @@ -154,8 +154,8 @@ class ResourceTranslator(object): def detach(self, activity_id): self._probe_manager.detach(activity_id) - def subscribe(self, event, notification_cb, event_subscribed_cb, error_cb): - return self._probe_manager.subscribe(event, notification_cb, event_subscribed_cb, error_cb) + def subscribe(self, event_name, event, notification_cb, event_subscribed_cb, error_cb): + return self._probe_manager.subscribe(event_name, event, notification_cb, event_subscribed_cb, error_cb) def unsubscribe(self, address): return self._probe_manager.unsubscribe(address) -- cgit v0.9.1