Esempio n. 1
0
def test_story_autoreset_false_and_dropping_rows_should_reset_all_timers():
    # in case an story is configured not to perform an auto-reset, after one
    # execution:
    #  - all executed rows should now be at -1 (dropped or not)
    #  - all non executed rows should have gone down one tick

    population = Population(circus=None,
                            size=10,
                            ids_gen=SequencialGenerator(prefix="ac_",
                                                        max_length=1))

    # 5 populations should trigger in 2 ticks, and 5 more
    init_timers = pd.Series([2] * 5 + [1] * 5, index=population.ids)
    timers_gen = MockTimerGenerator(init_timers)

    story = Story(
        name="tested",
        initiating_population=population,
        member_id_field="ac_id",

        # forcing the timer of all populations to be initialized to 0
        timer_gen=timers_gen,
        auto_reset_timer=False)

    # empty operation list as initialization
    # simulating an operation that drop the last 2 rows
    story.set_operations(MockDropOp(0, 2))

    # we have no auto-reset => all timers should intially be at -1
    all_minus_1 = pd.Series([-1] * 10, index=population.ids)
    assert story.timer["remaining"].equals(all_minus_1)

    # executing once => should do nothing, and leave all timers at -1
    story.execute()
    assert story.timer["remaining"].equals(all_minus_1)

    # triggering explicitaly the story => timers should have the hard-coded
    # values from the mock generator
    story.reset_timers()
    assert story.timer["remaining"].equals(init_timers)

    # after one execution, no population id has been selected and all counters
    # are decreased by 1
    story.execute()
    assert story.timer["remaining"].equals(init_timers - 1)

    # this time, the last 5 should have executed, but we should not have
    # any timer reste => they should go to -1.
    # The other 5 should now be at 0, ready to execute at next step
    story.execute()
    expected_timers = pd.Series([0] * 5 + [-1] * 5, index=population.ids)
    assert story.timer["remaining"].equals(expected_timers)

    # executing once more: the previously at -1 should still be there, and the
    # just executed at this stage should be there too
    story.execute()
    expected_timers = pd.Series([-1] * 10, index=population.ids)
    assert story.timer["remaining"].equals(expected_timers)
Esempio n. 2
0
def test_bugfix_collisions_force_act_next():
    # Previously, resetting the timer of reset populations was cancelling the reset.
    #
    # We typically want to reset the timer when we have change the activity
    # state => we want to generate new timer values that reflect the new state.
    #
    # But force_act_next should still have priority on that: if somewhere else
    # we force some populations to act at the next clock step (e.g. to re-try
    # buying an ER or so), the fact that their activity level changed should
    # not cancel the retry.

    population = Population(circus=None,
                            size=10,
                            ids_gen=SequencialGenerator(prefix="ac_",
                                                        max_length=1))

    # 5 populations should trigger in 2 ticks, and 5 more
    init_timers = pd.Series([2] * 5 + [1] * 5, index=population.ids)
    timers_gen = MockTimerGenerator(init_timers)

    story = Story(
        name="tested",
        initiating_population=population,
        member_id_field="ac_id",

        # forcing the timer of all populations to be initialized to 0
        timer_gen=timers_gen)

    timer_values = story.timer["remaining"].copy()

    forced = pd.Index(["ac_1", "ac_3", "ac_7", "ac_8", "ac_9"])
    not_forced = pd.Index(["ac_0", "ac_2", "ac_4", "ac_4", "ac_6"])

    # force_act_next should only impact those ids
    story.force_act_next(forced)
    assert story.timer.loc[forced]["remaining"].tolist() == [0, 0, 0, 0, 0]
    assert story.timer.loc[not_forced]["remaining"].equals(
        timer_values[not_forced])

    # resetting the timers should not change the timers of the populations that
    # are being forced
    story.reset_timers()
    assert story.timer.loc[forced]["remaining"].tolist() == [0, 0, 0, 0, 0]

    # Ticking the timers should not change the timers of the populations that
    # are being forced.
    # This is important for population forcing themselves to act at the next
    # clock
    # step (typical scenario for retry) => the fact of thick the clock at the
    # end of the execution should not impact them.
    story.timer_tick(population.ids)
    assert story.timer.loc[forced]["remaining"].tolist() == [0, 0, 0, 0, 0]
    assert story.timer.loc[not_forced]["remaining"].equals(
        timer_values[not_forced] - 1)
Esempio n. 3
0
def test_bugfix_force_populations_should_only_act_once():

    population = Population(circus=None,
                            size=10,
                            ids_gen=SequencialGenerator(prefix="ac_",
                                                        max_length=1))

    # 5 populations should trigger in 2 ticks, and 5 more
    init_timers = pd.Series([2] * 5 + [5] * 5, index=population.ids)
    timers_gen = MockTimerGenerator(init_timers)

    story = Story(
        name="tested",
        initiating_population=population,
        member_id_field="ac_id",

        # forcing the timer of all populations to be initialized to 0
        timer_gen=timers_gen)

    recording_op = FakeRecording()
    story.set_operations(recording_op)

    forced = pd.Index(["ac_1", "ac_3", "ac_7", "ac_8", "ac_9"])

    # force_act_next should only impact those ids
    story.force_act_next(forced)

    assert story.timer["remaining"].tolist() == [2, 0, 2, 0, 2, 5, 5, 0, 0, 0]

    story.execute()
    assert recording_op.last_seen_population_ids == [
        "ac_1", "ac_3", "ac_7", "ac_8", "ac_9"
    ]
    print(story.timer["remaining"].tolist())
    assert story.timer["remaining"].tolist() == [1, 2, 1, 2, 1, 4, 4, 5, 5, 5]
    recording_op.reset()

    story.execute()
    assert recording_op.last_seen_population_ids == []
    assert story.timer["remaining"].tolist() == [0, 1, 0, 1, 0, 3, 3, 4, 4, 4]

    story.execute()
    assert recording_op.last_seen_population_ids == ["ac_0", "ac_2", "ac_4"]
    assert story.timer["remaining"].tolist() == [2, 0, 2, 0, 2, 2, 2, 3, 3, 3]
Esempio n. 4
0
def test_active_inactive_ids_should_mark_all_populations_active_when_all_timers_0(
):
    population = Population(circus=None,
                            size=10,
                            ids_gen=SequencialGenerator(prefix="ac_",
                                                        max_length=1))

    # 5 populations should trigger in 2 ticks, and 5 more
    init_timers = pd.Series([0] * 10, index=population.ids)
    timers_gen = MockTimerGenerator(init_timers)

    story = Story(
        name="tested",
        initiating_population=population,
        member_id_field="ac_id",

        # forcing the timer of all populations to be initialized to 0
        timer_gen=timers_gen,
        auto_reset_timer=True)
    assert (population.ids.tolist(), []) == story.active_inactive_ids()
Esempio n. 5
0
def test_story_autoreset_true_and_dropping_rows_should_reset_all_timers():
    # in case an story is configured to perform an auto-reset, but also
    # drops some rows, after one execution,
    #  - all executed rows (dropped or not) should have a timer back to some
    # positive value
    #  - all non executed rows should have gone down one tick

    population = Population(circus=None,
                            size=10,
                            ids_gen=SequencialGenerator(prefix="ac_",
                                                        max_length=1))

    # 5 populations should trigger in 2 ticks, and 5 more
    init_timers = pd.Series([2] * 5 + [1] * 5, index=population.ids)
    timers_gen = MockTimerGenerator(init_timers)

    story = Story(
        name="tested",
        initiating_population=population,
        member_id_field="ac_id",

        # forcing the timer of all populations to be initialized to 0
        timer_gen=timers_gen,
        auto_reset_timer=True)

    # simulating an operation that drop the last 2 rows
    story.set_operations(MockDropOp(0, 2))

    # initial timers should be those provided by the generator
    assert story.timer["remaining"].equals(init_timers)

    # after one execution, no population id has been selected and all counters
    # are decreased by 1
    story.execute()
    assert story.timer["remaining"].equals(init_timers - 1)

    # this time, the last 5 should have executed => and the last 3 of them
    # should have been dropped. Nonetheless, all 5 of them should be back to 1
    story.execute()
    expected_timers = pd.Series([0] * 5 + [1] * 5, index=population.ids)
    assert story.timer["remaining"].equals(expected_timers)
Esempio n. 6
0
def test_story_autoreset_true_not_dropping_rows_should_reset_all_timers():
    # in case an story is configured to perform an auto-reset, after one
    # execution,
    #  - all executed rows should have a timer back to some positive value
    #  - all non executed rows should have gone down one tick

    population = Population(circus=None,
                            size=10,
                            ids_gen=SequencialGenerator(prefix="ac_",
                                                        max_length=1))

    # 5 populations should trigger in 2 ticks, and 5 more
    init_timers = pd.Series([2] * 5 + [1] * 5, index=population.ids)
    timers_gen = MockTimerGenerator(init_timers)

    story = Story(
        name="tested",
        initiating_population=population,
        member_id_field="ac_id",

        # forcing the timer of all populations to be initialized to 0
        timer_gen=timers_gen,
        auto_reset_timer=True)

    # empty operation list as initialization
    story.set_operations(Operation())

    # initial timers should be those provided by the generator
    assert story.timer["remaining"].equals(init_timers)

    # after one execution, no population id has been selected and all counters
    # are decreased by 1
    story.execute()
    assert story.timer["remaining"].equals(init_timers - 1)

    # this time, the last 5 should have executed => go back up to 1. The
    # other 5 should now be at 0, ready to execute at next step
    story.execute()
    expected_timers = pd.Series([0] * 5 + [1] * 5, index=population.ids)
    assert story.timer["remaining"].equals(expected_timers)