def test_run_until_fatal_error():
    """
    Running the state machine terminates on fatal error.

    """
    graph = create_object_graph("example", testing=True)

    def func(graph):
        raise FatalError()

    state_machine = StateMachine(graph, initial_state=func)
    state_machine.run()
def test_step_to_same_func():
    """
    Test taking a step to the same function.

    """
    graph = create_object_graph("example", testing=True)

    def func(graph):
        pass

    state_machine = StateMachine(graph, initial_state=func)
    next_func = state_machine.step()
    assert_that(next_func, is_(equal_to(func)))
def test_step_to_error_non_strict():
    """
    Test taking a step to an error.

    """
    graph = create_object_graph("example", testing=True)

    def func(graph):
        raise Exception()

    state_machine = StateMachine(graph, initial_state=func)
    assert_that(graph.error_policy.strict, is_(equal_to(False)))

    next_func = state_machine.step()
    assert_that(next_func, is_(equal_to(func)))
def test_step_to_sleep():
    """
    Test taking a step to sleep.

    """
    graph = create_object_graph("example", testing=True)

    def func(graph):
        raise SleepNow()

    state_machine = StateMachine(graph, initial_state=func)
    with patch.object(graph.sleep_policy, "sleep") as mocked_sleep:
        next_func = state_machine.step()

    assert_that(mocked_sleep.call_count, is_(equal_to(1)))
    assert_that(next_func, is_(equal_to(func)))
def test_step_to_different_func():
    """
    Test taking a step to a different function.

    """
    graph = create_object_graph("example", testing=True)

    def func1(graph):
        return func2

    def func2(graph):
        pass

    state_machine = StateMachine(graph, initial_state=func1)
    next_func = state_machine.step()
    assert_that(next_func, is_(equal_to(func2)))
示例#6
0
def test_standby_mixin():
    """
    Daemon that uses `StandByMixin` should alternate.

    """
    daemon = StandByDaemon.create_for_testing()
    state_machine = StateMachine(daemon.graph, daemon.initial_state)
    assert_that_states_alternate(state_machine, repeat(daemon))
示例#7
0
def test_standby():
    """
    StandBy state with alternating condition should alternate.

    """
    graph = create_object_graph("test", testing=True)
    initial_state = StandByState(FirstState(), make_alternating_condition(),
                                 EPSILON)
    state_machine = StateMachine(graph, initial_state, never_reload=True)
    assert_that_states_alternate(state_machine,
                                 cycle([FirstState(),
                                        SecondState()]))
def test_step_to_fatal_non_strict():
    """
    Test taking a step to an error.

    """
    graph = create_object_graph("example", testing=True)

    def func(graph):
        raise FatalError()

    state_machine = StateMachine(graph, initial_state=func)
    assert_that(graph.error_policy.strict, is_(equal_to(False)))

    assert_that(calling(state_machine.step), raises(FatalError))