def reset_and_eval():
        alice = Account("alice", 5)
        bob = Account("bob", 5)

        ev = timewinder.Evaluator(
            objects=[alice, bob],
            threads=[
                check_and_withdraw(alice, bob, Set(range(1, 6))),
                check_and_withdraw(alice, bob, Set(range(1, 6))),
            ],
            specs=[no_overdrafts],
        )
        ev.evaluate(steps=10)
        return ev.stats
def test_overdraft_initial_conditions():
    alice = Account("alice", 5)
    bob = Account("bob", 5)

    @timewinder.process
    def withdraw(sender, reciever, amount):
        sender.acc = sender.acc - amount
        yield "deposit"
        reciever.acc = reciever.acc + amount

    no_overdrafts = timewinder.ForAll(Account, lambda a: a.acc >= 0)

    ev = timewinder.Evaluator(
        objects=[alice, bob],
        threads=[withdraw(alice, bob, Set(range(1, 7)))],
        specs=[no_overdrafts],
    )

    got_error = False
    try:
        ev.evaluate()
    except ConstraintError as e:
        got_error = True
        print(e.name)
        print(e.thunk)

    assert got_error
    assert ev.stats.states == 12
示例#3
0
 def alg():
     return timewinder.FuncProcess(
         check_funds(alice),
         withdraw(alice),
         deposit(bob),
         state={"amt": Set(range(1, 6))},
     )
def test_liveness_reinterp():
    @timewinder.process
    def check_and_withdraw(sender, reciever, amt):
        if amt <= sender.acc:
            sender.acc = sender.acc - amt
            yield "deposit"
            reciever.acc = reciever.acc + amt

    no_overdrafts = timewinder.ForAll(Account, lambda a: a.acc >= 0)

    @timewinder.predicate
    def consistent_total(a, b):
        total = a.acc + b.acc
        return total == 10

    alice = Account("alice", 5)
    bob = Account("bob", 5)

    eventually_consistent = timewinder.Eventually(
        timewinder.Always(consistent_total(alice, bob)))

    ev = timewinder.Evaluator(
        objects=[alice, bob],
        threads=[
            check_and_withdraw(alice, bob, Set(range(1, 6))),
            check_and_withdraw(alice, bob, Set(range(1, 6))),
        ],
        specs=[
            no_overdrafts,
            eventually_consistent,
        ],
    )

    got_error = False
    try:
        ev.evaluate(steps=10)
    except StutterConstraintError as s:
        got_error = True
        print("\n" + s.name + "\n")
        ev.replay_thunk(s.thunk)

    assert got_error
def producer(queue, running, id):
    while True:
        if not running.status[id]:
            yield "paused"
            continue
        if queue.is_full():
            running.status[id] = False
            continue
        queue.queue.append(4)  # 4 represents arbitrary data

        sleeping = running.sleeping()
        if len(sleeping) != 0:
            wake_id = Set(sleeping)
            running.notify(wake_id)
def consumer(queue, running, id):
    while True:
        if not running.status[id]:
            yield "paused"
            continue
        if queue.is_empty():
            running.status[id] = False
            continue

        queue.queue.pop()

        sleeping = running.sleeping()
        if len(sleeping) != 0:
            wake_id = Set(sleeping)
            running.notify(wake_id)
示例#7
0
def test_overdraft_initial_conditions():
    @timewinder.object
    class Account:
        def __init__(self, name):
            self.name = name
            self.acc = 5

    alice = Account("alice")
    bob = Account("bob")

    @timewinder.step
    def withdraw(state, sender):
        sender.acc = sender.acc - state["amt"]

    @timewinder.step
    def deposit(state, reciever):
        reciever.acc = reciever.acc + state["amt"]

    alg = timewinder.FuncProcess(
        withdraw(alice),
        deposit(bob),
        state={
            "amt": Set(range(1, 7)),
        },
    )

    no_overdrafts = timewinder.ForAll(Account, lambda a: a.acc >= 0)

    ev = timewinder.Evaluator(
        objects=[alice, bob],
        threads=[alg],
        specs=[no_overdrafts],
    )

    got_error = False
    try:
        ev.evaluate()
    except ConstraintError as e:
        got_error = True
        print(e.name)
        print(e.thunk)

    assert got_error
    assert ev.stats.states == 12