def test_dirty_protection(self): Session.flush() # should not throw an exception serialized_call(lambda: None)() Session.flush() # nothing happened, no exception serialized_call(add_something)() self.assertRaises(DirtyReadOnlySession, lambda: Session.flush)
def test_non_collision(self): # same as above, but this time the second session is read only and # should therefore not have an impact on existing code def commit(): add_something() transaction.commit() serialized_call(commit)() try: def change_allocation(): allocation = Session.query(Allocation).one() allocation.group = uuid() def read_allocation(): allocation = Session.query(Allocation).one() allocation.resource t1 = ExceptionThread(serialized_call(change_allocation)) t2 = ExceptionThread(read_allocation) t1.start() t2.start() t1.join() t2.join() exceptions = (t1.exception, t2.exception) is_rollback = lambda ex: \ ex and isinstance(ex.orig, TransactionRollbackError) rollbacks = filter(is_rollback, exceptions) self.assertEqual(0, len(rollbacks)) finally: def drop(): Session.query(Allocation).delete() transaction.commit() serialized_call(drop)()
def test_collission(self): # for this test to work we need something commited (delete it later) def commit(): add_something() transaction.commit() serialized_call(commit)() try: def change_allocation(): allocation = Session.query(Allocation).one() allocation.group = uuid() t1 = ExceptionThread(serialized_call(change_allocation)) t2 = ExceptionThread(serialized_call(change_allocation)) t1.start() t2.start() t1.join() t2.join() exceptions = (t1.exception, t2.exception) is_rollback = lambda ex: \ ex and isinstance(ex.orig, TransactionRollbackError) is_nothing = lambda ex: not is_rollback(ex) rollbacks = filter(is_rollback, exceptions) updates = filter(is_nothing, exceptions) self.assertEqual(1, len(rollbacks)) self.assertEqual(1, len(updates)) finally: def drop(): Session.query(Allocation).delete() transaction.commit() serialized_call(drop)()