예제 #1
0
def test_resource_timeout():
    resource = Resource(1)
    log = []

    def take_but_balk(name, delay_balk):
        try:
            with resource.using(timeout=delay_balk):
                advance(20.0)
                log.append((name, "finish"))
        except Timeout:
            log.append((name, "balk"))

    sim = Simulator()
    sim.add(take_but_balk, "a", 10.0)
    sim.add_in(5.0, take_but_balk, "b", 10.0)
    sim.run()
    assert log == [("b", "balk"), ("a", "finish")]
예제 #2
0
def test_cancel_timeout():
    queue = Queue()
    log = []

    def join_and_balk(name):
        try:
            queue.join(100)
            log.append((name, "finish"))
        except Timeout:
            log.append((name, "balk"))

    def pop():
        queue.pop()

    sim = Simulator()
    sim.add(join_and_balk, "a")
    sim.add_in(50, pop)
    sim.run()
    assert log == [("a", "finish")]