Ejemplo n.º 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))
Ejemplo n.º 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))
Ejemplo n.º 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
Ejemplo n.º 4
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))
Ejemplo n.º 5
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))
Ejemplo n.º 6
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
Ejemplo n.º 7
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)
Ejemplo n.º 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)
Ejemplo n.º 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())
Ejemplo n.º 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())
Ejemplo n.º 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())
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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()
Ejemplo n.º 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()
Ejemplo n.º 16
0
 def run(self, *args, **kwargs):
     with db_mutex('send-unsent-scheduled-emails'):
         self.run_worker(*args, **kwargs)
Ejemplo n.º 17
0
 def run(self, *args, **kwargs):
     with db_mutex("send-unsent-scheduled-emails"):
         self.run_worker(*args, **kwargs)
Ejemplo n.º 18
0
 def run(self, *args, **kwargs):
     with db_mutex("convert-events-to-emails"):
         self.run_worker(*args, **kwargs)
Ejemplo n.º 19
0
 def run(self, *args, **kwargs):
     with db_mutex('push-slack-events'):
         self.run_worker(*args, **kwargs)