def test_dispatch_to_final_no_event(self):
     entry_action = mock.Mock()
     do_action = mock.Mock()
     exit_action = mock.Mock()
     transition = mock.Mock()
     state = State('name',
                   entry_action=entry_action,
                   do_action=do_action,
                   exit_action=exit_action,
                   final=True)
     state.add_transition(transition, 'event')
     context = Context('name', initial_state=state)
     context['foo'] = 'bar'
     event = state.dispatch(context, 'event', 'obj')
     self.assertIsNone(event)
 def test_dispatch(self):
     entry_action = mock.Mock()
     do_action = mock.Mock()
     exit_action = mock.Mock()
     transition = mock.Mock()
     state = State('name',
                   entry_action=entry_action,
                   do_action=do_action,
                   exit_action=exit_action)
     state.add_transition(transition, 'event')
     context = Context('name',
                       initial_state=state,
                       initial_system_context={'sys': 'tem'},
                       initial_user_context={'foo': 'bar'})
     event = state.dispatch(context, 'event', 'obj')
     exit_action.execute.assert_called_with({'foo': 'bar'}, 'obj')
     do_action.execute.assert_called_with({'foo': 'bar'}, 'obj')
     entry_action.execute.assert_called_with({'foo': 'bar'}, 'obj')
     transition.execute.assert_called_with({'foo': 'bar'}, 'obj')
     self.assertIsNotNone(event)
Пример #3
0
    def _init_(self, config_dict):
        """
        Initializes the State/Transition caches.

        :param config_dict: a dict as returned by aws_lambda_fsm.config.get_current_configuration
        """
        self.machines = {} if (self.machines is None) else self.machines
        for machine_dict in config_dict[CONFIG.MACHINES]:

            if 'import' in machine_dict:
                another_config_dict = get_current_configuration(
                    filename=machine_dict[CONFIG.IMPORT])
                self._init_(another_config_dict)
                continue

            try:
                # initialize the machine dict
                machine_name = machine_dict[CONFIG.NAME]
                if machine_name not in self.machines:
                    self.machines[machine_name] = {
                        MACHINE.STATES: {},
                        MACHINE.TRANSITIONS: {}
                    }
                    # pseudo-init, pseudo-final
                    self.machines[machine_name][MACHINE.STATES][
                        STATE.PSEUDO_INIT] = State(STATE.PSEUDO_INIT)
                    self.machines[machine_name][MACHINE.STATES][
                        STATE.PSEUDO_FINAL] = State(STATE.PSEUDO_FINAL)

                # set the max-retries
                self.machines[machine_name][MACHINE.MAX_RETRIES] = \
                    int(machine_dict.get(CONFIG.MAX_RETRIES, CONFIG.DEFAULT_MAX_RETRIES))

                # fetch these for use later
                pseudo_init = self.machines[machine_name][MACHINE.STATES][
                    STATE.PSEUDO_INIT]
                pseudo_final = self.machines[machine_name][MACHINE.STATES][
                    STATE.PSEUDO_FINAL]

                # iterate over each state, creating a singleton
                for state_dict in machine_dict[CONFIG.STATES]:
                    state_name = state_dict[CONFIG.NAME]
                    entry_action = self._get_action(
                        state_dict.get(CONFIG.ENTRY_ACTION))
                    do_action = self._get_action(
                        state_dict.get(CONFIG.DO_ACTION))
                    exit_action = self._get_action(
                        state_dict.get(CONFIG.EXIT_ACTION))
                    state = State(state_name,
                                  entry_action=entry_action,
                                  do_action=do_action,
                                  exit_action=exit_action,
                                  initial=state_dict.get(CONFIG.INITIAL),
                                  final=state_dict.get(CONFIG.FINAL))
                    self.machines[machine_name][
                        MACHINE.STATES][state_name] = state

                    # pseudo-transitions
                    if state_dict.get(CONFIG.INITIAL):
                        self._add_transition(machine_name, pseudo_init, state,
                                             STATE.PSEUDO_INIT)
                    if state_dict.get(CONFIG.FINAL):
                        self._add_transition(machine_name, state, pseudo_final,
                                             STATE.PSEUDO_FINAL)

                # iterate over each transition, creating a singleton
                for state_dict in machine_dict[CONFIG.STATES]:
                    state_name = state_dict[CONFIG.NAME]
                    state = self.machines[machine_name][
                        MACHINE.STATES][state_name]
                    for transition_dict in state_dict.get(
                            CONFIG.TRANSITIONS, []):
                        action = self._get_action(
                            transition_dict.get(CONFIG.ACTION))
                        event = transition_dict[CONFIG.EVENT]
                        target_name = transition_dict[CONFIG.TARGET]
                        target = self.machines[machine_name][
                            MACHINE.STATES][target_name]
                        self._add_transition(machine_name,
                                             state,
                                             target,
                                             event,
                                             action=action)

            except (ImportError, ValueError), e:  # pragma: no cover
                logger.warning('Problem importing machine "%s": %s',
                               machine_name, e)
                self.machines.pop(machine_name, None)
 def test_get_transition(self):
     state = State('name')
     state.add_transition('transition', 'event')
     self.assertEqual('transition', state.get_transition('event'))
 def test_get_transition_missing(self):
     state = State('name')
     self.assertRaises(KeyError, state.get_transition, 'event')
 def test_add_transition(self):
     state = State('name')
     state.add_transition('transition', 'event')
     self.assertEqual({'event': 'transition'}, state._event_2_transition)