Esempio n. 1
0
def test_queue_timeout():
    result = 0
    queue = Queue()
    sim = Simulator()

    def join_then_balk():
        nonlocal result
        try:
            queue.join(10.0)
            result = -1
        except Timeout:
            result = 1
            sim.stop()

    def would_pop():
        nonlocal result
        advance(20.0)
        queue.pop()
        result += 1

    sim.add(join_then_balk)
    sim.add(would_pop)
    sim.run()
    assert result == 1
    assert (sim.now() == pytest.approx(10.0))
    assert len(queue) == 0
    sim.run()
    assert result == 2
    assert (sim.now() == pytest.approx(20.0))
Esempio n. 2
0
def test_progress_capture():
    log = []

    def capture(progress_min, _rt_remaining, mc):
        log.append(progress_min)

    a = 0
    b = 0

    def set_ab(new_a, new_b):
        nonlocal a, b
        a = new_a
        b = new_b

    def measure():
        return (a, b)

    sim = Simulator()
    sim.add(track_progress, measure, [10, 10], 10.0, capture)
    sim._schedule(15.0, set_ab, 2, 0)
    sim._schedule(25.0, set_ab, 4, 1)
    sim._schedule(35.0, set_ab, 4, 6)
    sim._schedule(45.0, set_ab, 5, 9)
    sim._schedule(55.0, set_ab, 10, 10)
    sim.run(100.0)

    assert sim.now() == pytest.approx(60.0)
    assert log == pytest.approx([0.0, 0.0, 0.1, 0.4, 0.5, 1.0])
Esempio n. 3
0
def test_schedule_multiple_events():
    ll = []
    sim = Simulator()
    sim._schedule(1.0, append, 1, ll)
    sim._schedule(0.7, append, 2, ll)
    sim._schedule(10.0, append, 3, ll)
    sim.run()
    assert ll == [2, 1, 3]
    assert sim.now() == 10.0
Esempio n. 4
0
def test_happens_named():
    @happens([5], name="my-process")
    def process():
        advance(5)

    sim = Simulator()
    proc = sim.add(process)
    sim.run()
    assert proc.local.name == "my-process"
    assert 10.0 == pytest.approx(sim.now())
Esempio n. 5
0
def do_test_with_interrupter(main, ll_expected, now_expected):
    def interrupter(main):
        advance(10.1)
        main.interrupt()

    ll = []
    sim = Simulator()
    proc_main = sim.add(main, ll)
    sim.add(interrupter, proc_main)
    sim.run()
    assert ll == pytest.approx(ll_expected)
    assert sim.now() == pytest.approx(now_expected)
Esempio n. 6
0
    def __init__(self, sim: Simulator, num: str) -> None:
        self.num = num
        self._num_standing = 0
        self._moment_empty = sim.now()
        self._time_empty = 0
        self._travelers_waiting = Queue()
        self._traveler_ready = Signal()
        self._agents_working = Signal()

        sim.add(self._work_then_break)
        for name in ["alpha", "beta"]:
            sim.add(self._agent_accepting_travelers, name)
Esempio n. 7
0
def test_process_pause_resume():
    counter = 0

    def pausing():
        nonlocal counter
        advance(1.0)
        counter += 1
        pause()
        advance(1.0)
        counter += 1

    sim = Simulator()
    process = sim.add(pausing)
    sim.run()
    assert sim.now() == pytest.approx(1.0)
    assert counter == 1
    sim.run()
    assert sim.now() == pytest.approx(1.0)
    assert counter == 1
    process.resume()
    sim.run()
    assert sim.now() == pytest.approx(2.0)
    assert counter == 2
Esempio n. 8
0
def test_schedule_recurring():
    ll = [0]

    def _append():
        if sim.now() <= 10.0:
            ll.append(ll[-1] + 1)
            sim._schedule(1.0, _append)
        else:
            sim.stop()

    sim = Simulator()
    sim._schedule(1.0, _append)
    sim.run()
    assert sim.now() == 11.0
    assert ll == list(range(11))
Esempio n. 9
0
def test_tracker_lifecycle():
    def capture_pass(progress, rt_remaining, mc):
        pass

    sim = Simulator()
    tracker = sim.add(track_progress, sim_time, [1000.0], 100.0, capture_pass)
    assert has_tracker(sim, tracker)

    def check_tracker():
        advance(150)
        assert has_tracker(sim, tracker)

    sim.add(check_tracker)
    sim.run(10000.0)
    assert not has_tracker(sim, tracker)
    assert sim.now() == pytest.approx(1000.0)
Esempio n. 10
0
def test_schedule_none():
    sim = Simulator()
    assert 0.0 == sim.now()