Пример #1
0
def test_xor(make_timeline):
    eggs_exp = Mark("eggs")
    ham_exp = Mark("ham")
    cheese_exp = Mark("cheese")

    timeline, _ = make_timeline()
    t1 = timeline.beginning

    with timeline.frozen():
        assert t1 >> (eggs_exp ^ ham_exp) not in timeline
        assert t1 >> (ham_exp ^ eggs_exp) not in timeline
        assert t1 >> (cheese_exp ^ ham_exp ^ eggs_exp) not in timeline

    timeline.mark("eggs")
    with timeline.frozen():
        assert t1 >> (eggs_exp ^ ham_exp) in timeline
        assert t1 >> (ham_exp ^ eggs_exp) in timeline
        assert t1 >> (cheese_exp ^ ham_exp ^ eggs_exp) in timeline
        t2 = timeline.last

    timeline.mark("ham")
    with timeline.frozen():
        assert t1 >> (eggs_exp ^ ham_exp) not in timeline
        assert t2 >> (eggs_exp ^ ham_exp) in timeline
        assert t1 >> (ham_exp ^ eggs_exp) not in timeline
        assert t2 >> (ham_exp ^ eggs_exp) in timeline
        assert t1 >> (cheese_exp ^ ham_exp ^ eggs_exp) not in timeline
        assert t2 >> (cheese_exp ^ ham_exp ^ eggs_exp) in timeline
Пример #2
0
def test_and(make_timeline):
    eggs_exp = Mark("eggs")
    ham_exp = Mark("ham")
    cheese_exp = Mark("cheese")

    timeline, _ = make_timeline()
    t = timeline.beginning

    with timeline.frozen():
        assert t >> (eggs_exp & ham_exp) not in timeline
        assert t >> (ham_exp & eggs_exp) not in timeline
        assert t >> (cheese_exp & ham_exp & eggs_exp) not in timeline

    timeline.mark("eggs")
    with timeline.frozen():
        assert t >> (eggs_exp & ham_exp) not in timeline
        assert t >> (ham_exp & eggs_exp) not in timeline
        assert t >> (cheese_exp & ham_exp & eggs_exp) not in timeline

    timeline.mark("ham")
    with timeline.frozen():
        assert t >> (eggs_exp & ham_exp) in timeline
        assert t >> (ham_exp & eggs_exp) in timeline
        assert t >> (cheese_exp & ham_exp & eggs_exp) not in timeline

    timeline.mark("cheese")
    with timeline.frozen():
        assert t >> (eggs_exp & ham_exp) in timeline
        assert t >> (ham_exp & eggs_exp) in timeline
        assert t >> (cheese_exp & ham_exp & eggs_exp) in timeline
Пример #3
0
def test_lower_bound(make_timeline):
    timeline, _ = make_timeline()
    timeline.mark("1")
    timeline.mark("2")
    timeline.mark("3")
    timeline.freeze()

    assert (Mark("2") >> (Mark("1") >> Mark("3"))) not in timeline
    assert (Mark("2") >> (Mark("1") & Mark("3"))) not in timeline
    assert (Mark("2") >> (Mark("1") ^ Mark("3"))) in timeline
Пример #4
0
def test_after(make_timeline):
    timeline, _ = make_timeline()
    first = timeline.mark("first")

    second_exp = first >> Mark("second")
    with timeline.frozen():
        assert second_exp not in timeline

    timeline.mark("second")
    with timeline.frozen():
        assert second_exp in timeline

    timeline.mark("first")
    with timeline.frozen():
        assert Mark("second") >> Mark("first") in timeline
Пример #5
0
def test_concurrency(make_timeline, daemon, order):
    timeline, initial_history = make_timeline()

    occurrences = []
    worker_can_proceed = threading.Event()

    @daemon
    def worker():
        worker_can_proceed.wait()
        mark = timeline.mark("tada")
        occurrences.append(mark)

    if order == "mark_then_wait":
        worker_can_proceed.set()
        unblock_worker_later = None
    else:

        @daemon
        def unblock_worker_later():
            time.sleep(0.1)
            worker_can_proceed.set()

    mark = timeline.wait_for_next(Mark("tada"), freeze=True)

    worker.join()
    assert mark is occurrences[0]
    assert timeline.last is mark
    assert timeline.history() == initial_history + occurrences

    if unblock_worker_later:
        unblock_worker_later.join()
Пример #6
0
def make_timeline(request):
    """Provides a timeline factory. All timelines created by this factory
    are automatically finalized and checked for basic consistency after the
    end of the test.
    """

    timelines = []

    def factory():
        timeline = Timeline()
        timelines.append(timeline)
        with timeline.frozen():
            assert timeline.beginning is not None
            initial_history = [some.object.same_as(timeline.beginning)]
            assert timeline.history() == initial_history
        return timeline, initial_history

    yield factory
    log.newline()

    try:
        failed = request.node.call_result.failed
    except AttributeError:
        pass
    else:
        if not failed:
            for timeline in timelines:
                timeline.finalize()
                history = timeline.history()
                history.sort(key=lambda occ: occ.timestamp)
                assert history == timeline.history()
                assert history[-1] == Mark("FINISH")
                timeline.close()
Пример #7
0
def test_before(make_timeline):
    timeline, _ = make_timeline()
    t = timeline.beginning

    first = timeline.mark("first")
    timeline.mark("second")

    with timeline.frozen():
        assert t >> Mark("second") >> Mark("first") not in timeline
        assert Mark("second") >> first not in timeline

    third = timeline.mark("third")

    with timeline.frozen():
        assert t >> Mark("second") >> Mark("first") not in timeline
        assert Mark("second") >> first not in timeline
        assert t >> Mark("second") >> Mark("third") in timeline
        assert Mark("second") >> third in timeline
Пример #8
0
def test_frozen(make_timeline, daemon):
    timeline, initial_history = make_timeline()
    assert not timeline.is_frozen

    timeline.freeze()
    assert timeline.is_frozen

    timeline.unfreeze()
    assert not timeline.is_frozen

    with timeline.frozen():
        assert timeline.is_frozen
    assert not timeline.is_frozen

    timeline.mark("dum")

    timeline.freeze()
    assert timeline.is_frozen

    worker_started = threading.Event()
    worker_can_proceed = threading.Event()

    @daemon
    def worker():
        worker_started.set()
        worker_can_proceed.wait()
        timeline.mark("dee")

    worker_started.wait()

    assert Mark("dum") in timeline
    assert Mark("dee") not in timeline

    with timeline.unfrozen():
        worker_can_proceed.set()
        worker.join()

    assert Mark("dee") in timeline
Пример #9
0
def test_occurrences(make_timeline):
    timeline, initial_history = make_timeline()

    mark1 = timeline.mark("dum")
    mark2 = timeline.mark("dee")
    mark3 = timeline.mark("dum")

    assert mark1 == Mark("dum")
    assert mark1.id == "dum"
    assert mark2 == Mark("dee")
    assert mark2.id == "dee"
    assert mark3 == Mark("dum")
    assert mark3.id == "dum"

    with timeline.frozen():
        assert timeline.history() == initial_history + [
            some.object.same_as(mark1),
            some.object.same_as(mark2),
            some.object.same_as(mark3),
        ]
        timeline.finalize()

    assert timeline.all_occurrences_of(Mark("dum")) == (
        some.object.same_as(mark1),
        some.object.same_as(mark3),
    )
    assert timeline.all_occurrences_of(
        Mark("dee")) == (some.object.same_as(mark2), )

    assert timeline[:].all_occurrences_of(Mark("dum")) == (
        some.object.same_as(mark1),
        some.object.same_as(mark3),
    )

    # Lower boundary is inclusive.
    assert timeline[mark1:].all_occurrences_of(Mark("dum")) == (
        some.object.same_as(mark1),
        some.object.same_as(mark3),
    )
    assert timeline[mark2:].all_occurrences_of(
        Mark("dum")) == (some.object.same_as(mark3), )
    assert timeline[mark3:].all_occurrences_of(
        Mark("dum")) == (some.object.same_as(mark3), )

    # Upper boundary is exclusive.
    assert timeline[:mark1].all_occurrences_of(Mark("dum")) == ()
    assert timeline[:mark2].all_occurrences_of(
        Mark("dum")) == (some.object.same_as(mark1), )
    assert timeline[:mark3].all_occurrences_of(
        Mark("dum")) == (some.object.same_as(mark1), )
Пример #10
0
def test_new(make_timeline):
    timeline, _ = make_timeline()

    m1 = timeline.mark("1")
    timeline.freeze()

    assert timeline.expect_new(Mark("1")) is m1
    with pytest.raises(Exception):
        timeline.expect_new(Mark("2"))

    timeline.proceed()
    m2 = timeline.mark("2")
    timeline.freeze()

    with pytest.raises(Exception):
        timeline.expect_new(Mark("1"))
    assert timeline.expect_new(Mark("2")) is m2
    with pytest.raises(Exception):
        timeline.expect_new(Mark("3"))

    timeline.unfreeze()
    m3 = timeline.mark("3")
    timeline.freeze()

    with pytest.raises(Exception):
        timeline.expect_new(Mark("1"))
    assert timeline.expect_new(Mark("2")) is m2
    assert timeline.expect_new(Mark("3")) is m3

    timeline.proceed()
    m4 = timeline.mark("4")
    timeline.mark("4")
    timeline.freeze()

    with pytest.raises(Exception):
        timeline.expect_new(Mark("1"))
    with pytest.raises(Exception):
        timeline.expect_new(Mark("2"))
    with pytest.raises(Exception):
        timeline.expect_new(Mark("3"))
    assert timeline.expect_new(Mark("4")) is m4