Exemplo n.º 1
0
    def test_fake_nesting_rollback(self):
        barrier = Event()

        @transaction
        @inlineCallbacks
        def trans1(txn):
            yield Transaction(name="TEST1").save()
            txn.rollback()  # should propagate to the root transaction

        @transaction
        @inlineCallbacks
        def trans2(txn):
            yield Transaction(name="TEST2").save()
            yield trans1()

            barrier.wait()  # wait here to delay commit

        d = trans2()

        count = yield Transaction.count()
        self.assertEqual(count, 0)

        barrier.set()

        yield d

        count = yield Transaction.count()
        self.assertEqual(count, 0)
Exemplo n.º 2
0
    def test_commit(self):
        barrier = Event()

        @transaction
        @inlineCallbacks
        def trans(txn):
            self.assertFalse(threadable.isInIOThread(), "Transactions must not run in main thread")

            yield Transaction(name="TEST1").save()
            yield Transaction(name="TEST2").save()

            barrier.wait()  # wait here to delay commit
            returnValue("return value")

        d = trans()

        count = yield Transaction.count()
        self.assertEqual(count, 0)

        barrier.set()
        res = yield d
        self.assertEqual(res, "return value")

        count = yield Transaction.count()
        self.assertEqual(count, 2)
Exemplo n.º 3
0
    def test_commit(self):
        barrier = Event()

        @transaction
        @inlineCallbacks
        def trans(txn):
            self.assertFalse(threadable.isInIOThread(),
                             "Transactions must not run in main thread")

            yield Transaction(name="TEST1").save()
            yield Transaction(name="TEST2").save()

            barrier.wait()  # wait here to delay commit
            returnValue("return value")

        d = trans()

        count = yield Transaction.count()
        self.assertEqual(count, 0)

        barrier.set()
        res = yield d
        self.assertEqual(res, "return value")

        count = yield Transaction.count()
        self.assertEqual(count, 2)
Exemplo n.º 4
0
    def test_fake_nesting_commit(self):
        barrier = Event()
        threadIds = []

        @transaction
        @inlineCallbacks
        def trans1(txn):
            threadIds.append(threadable.getThreadID())
            yield Transaction(name="TEST1").save()

        @transaction
        @inlineCallbacks
        def trans2(txn):
            threadIds.append(threadable.getThreadID())
            yield trans1()
            yield Transaction(name="TEST2").save()
            barrier.wait()  # wait here to delay commit

        d = trans2()

        count = yield Transaction.count()
        self.assertEqual(count, 0)

        barrier.set()
        yield d

        self.assertEqual(threadIds[0], threadIds[1], "Nested transactions don't run in same thread")

        count = yield Transaction.count()
        self.assertEqual(count, 2)
Exemplo n.º 5
0
    def test_fake_nesting_rollback(self):
        barrier = Event()

        @transaction
        @inlineCallbacks
        def trans1(txn):
            yield Transaction(name="TEST1").save()
            txn.rollback()  # should propagate to the root transaction

        @transaction
        @inlineCallbacks
        def trans2(txn):
            yield Transaction(name="TEST2").save()
            yield trans1()

            barrier.wait()  # wait here to delay commit

        d = trans2()

        count = yield Transaction.count()
        self.assertEqual(count, 0)

        barrier.set()

        yield d

        count = yield Transaction.count()
        self.assertEqual(count, 0)
Exemplo n.º 6
0
    def test_fake_nesting_commit(self):
        barrier = Event()
        threadIds = []

        @transaction
        @inlineCallbacks
        def trans1(txn):
            threadIds.append(threadable.getThreadID())
            yield Transaction(name="TEST1").save()

        @transaction
        @inlineCallbacks
        def trans2(txn):
            threadIds.append(threadable.getThreadID())
            yield trans1()
            yield Transaction(name="TEST2").save()
            barrier.wait()  # wait here to delay commit

        d = trans2()

        count = yield Transaction.count()
        self.assertEqual(count, 0)

        barrier.set()
        yield d

        self.assertEqual(threadIds[0], threadIds[1],
                         "Nested transactions don't run in same thread")

        count = yield Transaction.count()
        self.assertEqual(count, 2)
Exemplo n.º 7
0
    def test_parallel_transactions(self):
        if DBTYPE == "sqlite":
            raise unittest.SkipTest(
                "Parallel connections are not supported by sqlite")

        threadIds = []

        # trans1 is supposed to pass, trans2 is supposed to fail due to unique constraint
        # regarding synchronization: trans1 has to start INSERT before trans2,
        # because otherwise it would wait for trans2 to finish due to postgres synchronization strategy

        on_trans1_insert = Event()
        barrier1, barrier2 = Event(), Event()

        @transaction
        @inlineCallbacks
        def trans1(txn):
            threadIds.append(threadable.getThreadID())
            yield Transaction(name="TEST1").save()
            on_trans1_insert.set()
            barrier1.wait()  # wait here to delay commit)

        @transaction
        @inlineCallbacks
        def trans2(txn):
            threadIds.append(threadable.getThreadID())
            on_trans1_insert.wait()
            yield Transaction(name="TEST1").save()
            barrier2.wait()  # wait here to delay commit

        d1 = trans1()
        d2 = trans2()

        # commit tran1, should pass:
        barrier1.set()
        yield d1

        count = yield Transaction.count()
        self.assertEqual(count, 1)

        # commit trans2:
        barrier2.set()

        # should fail due to unique constraint violation
        yield self._assertRaises(d2, Exception)

        self.assertNotEqual(
            threadIds[0], threadIds[1],
            "Parallel transactions don't run in different threads")

        count = yield Transaction.count()
        self.assertEqual(count, 1)
Exemplo n.º 8
0
    def test_parallel_transactions(self):
        if DBTYPE == "sqlite":
            raise unittest.SkipTest("Parallel connections are not supported by sqlite")

        threadIds = []

        # trans1 is supposed to pass, trans2 is supposed to fail due to unique constraint
        # regarding synchronization: trans1 has to start INSERT before trans2,
        # because otherwise it would wait for trans2 to finish due to postgres synchronization strategy

        on_trans1_insert = Event()
        barrier1, barrier2 = Event(), Event()

        @transaction
        @inlineCallbacks
        def trans1(txn):
            threadIds.append(threadable.getThreadID())
            yield Transaction(name="TEST1").save()
            on_trans1_insert.set()
            barrier1.wait()  # wait here to delay commit)

        @transaction
        @inlineCallbacks
        def trans2(txn):
            threadIds.append(threadable.getThreadID())
            on_trans1_insert.wait()
            yield Transaction(name="TEST1").save()
            barrier2.wait()  # wait here to delay commit

        d1 = trans1()
        d2 = trans2()

        # commit tran1, should pass:
        barrier1.set()
        yield d1

        count = yield Transaction.count()
        self.assertEqual(count, 1)

        # commit trans2:
        barrier2.set()

        # should fail due to unique constraint violation
        yield self._assertRaises(d2, Exception)

        self.assertNotEqual(threadIds[0], threadIds[1], "Parallel transactions don't run in different threads")

        count = yield Transaction.count()
        self.assertEqual(count, 1)
Exemplo n.º 9
0
    def test_fake_nesting_ctxmgr(self):
        @transaction
        @inlineCallbacks
        def trans1(txn):
            yield Transaction(name="TEST1").save()
            with transaction() as txn2:
                yield Transaction(name="TEST2").save()
                txn2.rollback()

        yield trans1()

        count = yield Transaction.count()
        self.assertEqual(count, 0)
Exemplo n.º 10
0
    def test_fake_nesting_ctxmgr(self):
        @transaction
        @inlineCallbacks
        def trans1(txn):
            yield Transaction(name="TEST1").save()
            with transaction() as txn2:
                yield Transaction(name="TEST2").save()
                txn2.rollback()

        yield trans1()

        count = yield Transaction.count()
        self.assertEqual(count, 0)
Exemplo n.º 11
0
    def test_rollback(self):
        barrier = Event()

        @transaction
        @inlineCallbacks
        def trans(txn):
            yield Transaction(name="TEST1").save()
            yield Transaction(name="TEST2").save()

            barrier.wait()  # wait here to delay commit
            raise ZeroDivisionError()

        d = trans()

        barrier.set()
        yield self._assertRaises(d, ZeroDivisionError)

        count = yield Transaction.count()
        self.assertEqual(count, 0)
Exemplo n.º 12
0
    def test_rollback(self):
        barrier = Event()

        @transaction
        @inlineCallbacks
        def trans(txn):
            yield Transaction(name="TEST1").save()
            yield Transaction(name="TEST2").save()

            barrier.wait()  # wait here to delay commit
            raise ZeroDivisionError()

        d = trans()

        barrier.set()
        yield self._assertRaises(d, ZeroDivisionError)

        count = yield Transaction.count()
        self.assertEqual(count, 0)