示例#1
0
    def test_custom_lock_timeout(self):
        """
        Tests that the custom lock timeout works when an hour is configured as the timeout.
        """
        with freeze_time('2014-02-01'):
            # Create a lock
            orig_lock = DBMutex.objects.create(lock_id='lock_id')

        # Try to acquire the lock one minute in the future. It should fail
        with freeze_time('2014-02-01 00:01:00'):
            with self.assertRaises(DBMutexError):
                with db_mutex('lock_id'):
                    raise NotImplementedError

        # Try to acquire the lock 31 minutes in the future. It should fail
        with freeze_time('2014-02-01 00:31:00'):
            with self.assertRaises(DBMutexError):
                with db_mutex('lock_id'):
                    raise NotImplementedError

        # Try to acquire the lock 60 minutes in the future. It should pass
        with freeze_time('2014-02-01 01:00:00'):
            with db_mutex('lock_id'):
                self.assertFalse(DBMutex.objects.filter(id=orig_lock.id).exists())
                self.assertEqual(DBMutex.objects.count(), 1)
                m = DBMutex.objects.get(lock_id='lock_id')
                self.assertEqual(m.creation_time, datetime(2014, 2, 1, 1))
示例#2
0
    def test_lock_timeout_default(self):
        """
        Tests that the lock timeout works with the default value of 30 minutes.
        """
        with freeze_time('2014-02-01'):
            # Create a lock
            orig_lock = DBMutex.objects.create(lock_id='lock_id')

        # Try to acquire the lock one minute in the future. It should fail
        with freeze_time('2014-02-01 00:01:00'):
            with self.assertRaises(DBMutexError):
                with db_mutex('lock_id'):
                    raise NotImplementedError

        # Try to acquire the lock 9 minutes in the future. It should fail
        with freeze_time('2014-02-01 00:09:00'):
            with self.assertRaises(DBMutexError):
                with db_mutex('lock_id'):
                    raise NotImplementedError

        # Try to acquire the lock 30 minutes in the future. It should pass since the lock timed out
        with freeze_time('2014-02-01 00:30:00'):
            with db_mutex('lock_id'):
                self.assertFalse(DBMutex.objects.filter(id=orig_lock.id).exists())
                self.assertEqual(DBMutex.objects.count(), 1)
                m = DBMutex.objects.get(lock_id='lock_id')
                self.assertEqual(m.creation_time, datetime(2014, 2, 1, 0, 30))
示例#3
0
    def test_no_lock_timeout(self):
        """
        Tests that the lock timeout works when None is configured as the timeout.
        """
        with freeze_time('2014-02-01'):
            # Create a lock
            DBMutex.objects.create(lock_id='lock_id')

        # Try to acquire the lock one minute in the future. It should fail
        with freeze_time('2014-02-01 00:01:00'):
            with self.assertRaises(DBMutexError):
                with db_mutex('lock_id'):
                    raise NotImplementedError

        # Try to acquire the lock 9 minutes in the future. It should fail
        with freeze_time('2014-02-01 00:09:00'):
            with self.assertRaises(DBMutexError):
                with db_mutex('lock_id'):
                    raise NotImplementedError

        # Try to acquire the lock 30 minutes in the future. It should fail
        with freeze_time('2014-02-01 00:30:00'):
            with self.assertRaises(DBMutexError):
                with db_mutex('lock_id'):
                    raise NotImplementedError

        # Try to acquire the lock years in the future. It should fail
        with freeze_time('2016-02-01 00:30:00'):
            with self.assertRaises(DBMutexError):
                with db_mutex('lock_id'):
                    raise NotImplementedError
    def test_lock_timeout_default(self):
        """
        Tests that the lock timeout works with the default value of 30 minutes.
        """
        with freeze_time('2014-02-01'):
            # Create a lock
            orig_lock = DBMutex.objects.create(lock_id='lock_id')

        # Try to acquire the lock one minute in the future. It should fail
        with freeze_time('2014-02-01 00:01:00'):
            with self.assertRaises(DBMutexError):
                with db_mutex('lock_id'):
                    raise NotImplementedError

        # Try to acquire the lock 9 minutes in the future. It should fail
        with freeze_time('2014-02-01 00:09:00'):
            with self.assertRaises(DBMutexError):
                with db_mutex('lock_id'):
                    raise NotImplementedError

        # Try to acquire the lock 30 minutes in the future. It should pass since the lock timed out
        with freeze_time('2014-02-01 00:30:00'):
            with db_mutex('lock_id'):
                self.assertFalse(DBMutex.objects.filter(id=orig_lock.id).exists())
                self.assertEqual(DBMutex.objects.count(), 1)
                m = DBMutex.objects.get(lock_id='lock_id')
                self.assertEqual(m.creation_time, datetime(2014, 2, 1, 0, 30))
    def test_custom_lock_timeout(self):
        """
        Tests that the custom lock timeout works when an hour is configured as the timeout.
        """
        with freeze_time('2014-02-01'):
            # Create a lock
            orig_lock = DBMutex.objects.create(lock_id='lock_id')

        # Try to acquire the lock one minute in the future. It should fail
        with freeze_time('2014-02-01 00:01:00'):
            with self.assertRaises(DBMutexError):
                with db_mutex('lock_id'):
                    raise NotImplementedError

        # Try to acquire the lock 31 minutes in the future. It should fail
        with freeze_time('2014-02-01 00:31:00'):
            with self.assertRaises(DBMutexError):
                with db_mutex('lock_id'):
                    raise NotImplementedError

        # Try to acquire the lock 60 minutes in the future. It should pass
        with freeze_time('2014-02-01 01:00:00'):
            with db_mutex('lock_id'):
                self.assertFalse(DBMutex.objects.filter(id=orig_lock.id).exists())
                self.assertEqual(DBMutex.objects.count(), 1)
                m = DBMutex.objects.get(lock_id='lock_id')
                self.assertEqual(m.creation_time, datetime(2014, 2, 1, 1))
    def test_no_lock_timeout(self):
        """
        Tests that the lock timeout works when None is configured as the timeout.
        """
        with freeze_time('2014-02-01'):
            # Create a lock
            DBMutex.objects.create(lock_id='lock_id')

        # Try to acquire the lock one minute in the future. It should fail
        with freeze_time('2014-02-01 00:01:00'):
            with self.assertRaises(DBMutexError):
                with db_mutex('lock_id'):
                    raise NotImplementedError

        # Try to acquire the lock 9 minutes in the future. It should fail
        with freeze_time('2014-02-01 00:09:00'):
            with self.assertRaises(DBMutexError):
                with db_mutex('lock_id'):
                    raise NotImplementedError

        # Try to acquire the lock 30 minutes in the future. It should fail
        with freeze_time('2014-02-01 00:30:00'):
            with self.assertRaises(DBMutexError):
                with db_mutex('lock_id'):
                    raise NotImplementedError

        # Try to acquire the lock years in the future. It should fail
        with freeze_time('2016-02-01 00:30:00'):
            with self.assertRaises(DBMutexError):
                with db_mutex('lock_id'):
                    raise NotImplementedError
 def test_no_lock_before(self):
     """
     Tests that a lock is succesfully acquired.
     """
     # There should be no locks before and after the context manager
     self.assertEqual(DBMutex.objects.count(), 0)
     with db_mutex('lock_id'):
         self.assertEqual(DBMutex.objects.count(), 1)
         m = DBMutex.objects.get(lock_id='lock_id')
         self.assertEqual(m.creation_time, datetime(2014, 2, 1))
     self.assertEqual(DBMutex.objects.count(), 0)
示例#8
0
 def test_no_lock_before(self):
     """
     Tests that a lock is succesfully acquired.
     """
     # There should be no locks before and after the context manager
     self.assertEqual(DBMutex.objects.count(), 0)
     with db_mutex('lock_id'):
         self.assertEqual(DBMutex.objects.count(), 1)
         m = DBMutex.objects.get(lock_id='lock_id')
         self.assertEqual(m.creation_time, datetime(2014, 2, 1))
     self.assertEqual(DBMutex.objects.count(), 0)
示例#9
0
 def test_lock_before(self):
     """
     Tests when a lock already exists.
     """
     # Create a lock
     m = DBMutex.objects.create(lock_id='lock_id')
     # Try to acquire the lock. It should raise an exception
     with self.assertRaises(DBMutexError):
         with db_mutex('lock_id'):
             raise NotImplementedError
     # The lock should still exist
     self.assertTrue(DBMutex.objects.filter(id=m.id).exists())
示例#10
0
 def test_lock_before(self):
     """
     Tests when a lock already exists.
     """
     # Create a lock
     m = DBMutex.objects.create(lock_id='lock_id')
     # Try to acquire the lock. It should raise an exception
     with self.assertRaises(DBMutexError):
         with db_mutex('lock_id'):
             raise NotImplementedError
     # The lock should still exist
     self.assertTrue(DBMutex.objects.filter(id=m.id).exists())
示例#11
0
 def test_lock_before_suppress_acquisition_errors(self):
     """
     Tests when a lock already exists. Verifies that an exception is thrown when
     suppress_acquisition_errors is True. The exception is still thrown because
     we are using it as a context manager
     """
     # Create a lock
     m = DBMutex.objects.create(lock_id='lock_id')
     # Try to acquire the lock. It should neither acquire nor release it
     with self.assertRaises(DBMutexError):
         with db_mutex('lock_id', suppress_acquisition_exceptions=True):
             raise NotImplementedError
     # The lock should still exist
     self.assertTrue(DBMutex.objects.filter(id=m.id).exists())
示例#12
0
 def test_lock_different_id(self):
     """
     Tests that the lock still works even when another lock with a different id exists.
     """
     # Create a lock
     m = DBMutex.objects.create(lock_id='lock_id')
     # Try to acquire the lock with a different ID
     with db_mutex('lock_id2'):
         self.assertEqual(DBMutex.objects.count(), 2)
         m2 = DBMutex.objects.get(lock_id='lock_id2')
         self.assertEqual(m2.creation_time, datetime(2014, 2, 1))
     # The original lock should still exist but the other one should be gone
     self.assertTrue(DBMutex.objects.filter(id=m.id).exists())
     self.assertEqual(DBMutex.objects.count(), 1)
示例#13
0
 def test_lock_different_id(self):
     """
     Tests that the lock still works even when another lock with a different id exists.
     """
     # Create a lock
     m = DBMutex.objects.create(lock_id='lock_id')
     # Try to acquire the lock with a different ID
     with db_mutex('lock_id2'):
         self.assertEqual(DBMutex.objects.count(), 2)
         m2 = DBMutex.objects.get(lock_id='lock_id2')
         self.assertEqual(m2.creation_time, datetime(2014, 2, 1))
     # The original lock should still exist but the other one should be gone
     self.assertTrue(DBMutex.objects.filter(id=m.id).exists())
     self.assertEqual(DBMutex.objects.count(), 1)
示例#14
0
    def test_lock_timeout_error(self):
        """
        Tests the case when a lock expires while the context manager is executing.
        """
        with freeze_time('2014-02-01'):
            # Acquire a lock at the given time and release it before it is finished. It
            # should result in an error
            with self.assertRaises(DBMutexTimeoutError):
                with db_mutex('lock_id'):
                    self.assertEqual(DBMutex.objects.count(), 1)
                    m = DBMutex.objects.get(lock_id='lock_id')
                    self.assertEqual(m.creation_time, datetime(2014, 2, 1))

                    # Release the lock before the context manager finishes
                    m.delete()
示例#15
0
    def test_lock_timeout_error(self):
        """
        Tests the case when a lock expires while the context manager is executing.
        """
        with freeze_time('2014-02-01'):
            # Acquire a lock at the given time and release it before it is finished. It
            # should result in an error
            with self.assertRaises(DBMutexTimeoutError):
                with db_mutex('lock_id'):
                    self.assertEqual(DBMutex.objects.count(), 1)
                    m = DBMutex.objects.get(lock_id='lock_id')
                    self.assertEqual(m.creation_time, datetime(2014, 2, 1))

                    # Release the lock before the context manager finishes
                    m.delete()
 def run(self, *args, **kwargs):
     with db_mutex('send-unsent-scheduled-emails'):
         self.run_worker(*args, **kwargs)
示例#17
0
 def run(self, *args, **kwargs):
     with db_mutex("send-unsent-scheduled-emails"):
         self.run_worker(*args, **kwargs)
示例#18
0
 def run(self, *args, **kwargs):
     with db_mutex("convert-events-to-emails"):
         self.run_worker(*args, **kwargs)
 def run(self, *args, **kwargs):
     with db_mutex('push-slack-events'):
         self.run_worker(*args, **kwargs)