diff --git a/tests/get_or_create/tests.py b/tests/get_or_create/tests.py
index 0a774ef..f32e7cf 100644
a
|
b
|
class UpdateOrCreateTransactionTests(TransactionTestCase):
|
441 | 441 | while it holds the lock. The updated field isn't a field in 'defaults', |
442 | 442 | so update_or_create() shouldn't have an effect on it. |
443 | 443 | """ |
| 444 | |
| 445 | lock_status = {'has_grabbed_lock': False} |
| 446 | |
444 | 447 | def birthday_sleep(): |
445 | | time.sleep(0.3) |
| 448 | lock_status['has_grabbed_lock'] = True |
| 449 | time.sleep(0.5) |
446 | 450 | return date(1940, 10, 10) |
447 | 451 | |
448 | 452 | def update_birthday_slowly(): |
… |
… |
class UpdateOrCreateTransactionTests(TransactionTestCase):
|
450 | 454 | first_name='John', defaults={'birthday': birthday_sleep} |
451 | 455 | ) |
452 | 456 | |
| 457 | def lock_wait(): |
| 458 | # timeout after ~0.5 seconds |
| 459 | for i in range(20): |
| 460 | time.sleep(0.025) |
| 461 | if lock_status['has_grabbed_lock']: |
| 462 | break |
| 463 | else: |
| 464 | return False |
| 465 | return True |
| 466 | |
453 | 467 | Person.objects.create(first_name='John', last_name='Lennon', birthday=date(1940, 10, 9)) |
454 | 468 | |
455 | 469 | # update_or_create in a separate thread |
… |
… |
class UpdateOrCreateTransactionTests(TransactionTestCase):
|
457 | 471 | before_start = datetime.now() |
458 | 472 | t.start() |
459 | 473 | |
460 | | # Wait for lock to begin |
461 | | time.sleep(0.05) |
| 474 | if not lock_wait(): |
| 475 | self.skipTest('Database took too long to lock the row') |
462 | 476 | |
463 | 477 | # Update during lock |
464 | 478 | Person.objects.filter(first_name='John').update(last_name='NotLennon') |
… |
… |
class UpdateOrCreateTransactionTests(TransactionTestCase):
|
469 | 483 | |
470 | 484 | # The update remains and it blocked. |
471 | 485 | updated_person = Person.objects.get(first_name='John') |
472 | | self.assertGreater(after_update - before_start, timedelta(seconds=0.3)) |
| 486 | self.assertGreater(after_update - before_start, timedelta(seconds=0.5)) |
473 | 487 | self.assertEqual(updated_person.last_name, 'NotLennon') |