|
3 | 3 |
|
4 | 4 | from twisted.trial import unittest |
5 | 5 | from twisted.internet import reactor |
6 | | -from twisted.internet.defer import Deferred, inlineCallbacks, returnValue, maybeDeferred |
| 6 | +from twisted.internet.defer import Deferred, inlineCallbacks, returnValue, maybeDeferred, DeferredList |
7 | 7 | from twisted.python import threadable |
8 | 8 |
|
9 | 9 | from twistar.transaction import transaction, nested_transaction |
@@ -232,6 +232,35 @@ def trans2(txn): |
232 | 232 | count = yield Transaction.count() |
233 | 233 | self.assertEqual(count, 1) |
234 | 234 |
|
| 235 | + @inlineCallbacks |
| 236 | + def test_parallel_massive(self): |
| 237 | + # Make sure that everything works alright even when starting a massive amount of parallel transactions |
| 238 | + if DBTYPE == "sqlite": |
| 239 | + raise unittest.SkipTest("Parallel connections are not supported by sqlite") |
| 240 | + |
| 241 | + N = 100 |
| 242 | + |
| 243 | + @transaction |
| 244 | + @inlineCallbacks |
| 245 | + def trans(txn, i): |
| 246 | + yield Transaction(name=str(i)).save() |
| 247 | + if i % 2 == 1: |
| 248 | + txn.rollback() |
| 249 | + else: |
| 250 | + txn.commit() |
| 251 | + |
| 252 | + deferreds = [trans(i) for i in range(N)] |
| 253 | + |
| 254 | + results = yield DeferredList(deferreds) |
| 255 | + self.assertTrue(all(success for success, result in results)) |
| 256 | + |
| 257 | + objects = yield Transaction.all() |
| 258 | + actual = sorted(int(obj.name) for obj in objects) |
| 259 | + actual = [str(i) for i in actual] |
| 260 | + expected = [str(i) for i in range(0, N, 2)] |
| 261 | + |
| 262 | + self.assertEquals(actual, expected) |
| 263 | + |
235 | 264 | @inlineCallbacks |
236 | 265 | def test_savepoints_commit(self): |
237 | 266 | if DBTYPE == "sqlite": |
|
0 commit comments