Ticket #26933: 26933.diff

File 26933.diff, 2.1 KB (added by Jensen Cochran, 8 years ago)
  • tests/get_or_create/tests.py

    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):  
    441441        while it holds the lock. The updated field isn't a field in 'defaults',
    442442        so update_or_create() shouldn't have an effect on it.
    443443        """
     444
     445        lock_status = {'has_grabbed_lock': False}
     446
    444447        def birthday_sleep():
    445             time.sleep(0.3)
     448            lock_status['has_grabbed_lock'] = True
     449            time.sleep(0.5)
    446450            return date(1940, 10, 10)
    447451
    448452        def update_birthday_slowly():
    class UpdateOrCreateTransactionTests(TransactionTestCase):  
    450454                first_name='John', defaults={'birthday': birthday_sleep}
    451455            )
    452456
     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
    453467        Person.objects.create(first_name='John', last_name='Lennon', birthday=date(1940, 10, 9))
    454468
    455469        # update_or_create in a separate thread
    class UpdateOrCreateTransactionTests(TransactionTestCase):  
    457471        before_start = datetime.now()
    458472        t.start()
    459473
    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')
    462476
    463477        # Update during lock
    464478        Person.objects.filter(first_name='John').update(last_name='NotLennon')
    class UpdateOrCreateTransactionTests(TransactionTestCase):  
    469483
    470484        # The update remains and it blocked.
    471485        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))
    473487        self.assertEqual(updated_person.last_name, 'NotLennon')
Back to Top