Source

arigo / hack / stm / transactionmodule / test_transaction.py

Full commit
import py, transaction

def test_simple():
    lst = []
    transaction.add(lst.append, 5)
    transaction.add(lst.append, 6)
    transaction.add(lst.append, 7)
    transaction.run()
    assert sorted(lst) == [5, 6, 7]

def test_add_more():
    lst = []
    def do(x):
        lst.append(x)
        if x < 10:
            transaction.add(do, x + 1)
    transaction.add(do, 0)
    transaction.run()
    assert lst == range(11)

def test_exception():
    def do(x):
        raise ValueError
    transaction.add(do, 5)
    py.test.raises(ValueError, transaction.run)

def test_no_recursion():
    def do(x):
        transaction.run()
    transaction.add(do, 5)
    py.test.raises(transaction.TransactionError, transaction.run)