def test_locking(self): """Make sure we indicate we're locked if and only if we're inside the function.""" key = decorators._get_lock_key('fake_func', self.pk) self.assertEqual(int(get_lock(key)), self.unlocked) @decorators.lock_and_track def fake_func(import_file_pk): self.assertEqual(int(get_lock(key)), self.locked) fake_func(self.pk) self.assertEqual(int(get_lock(key)), self.unlocked)
def test_locking_w_exception(self): """Make sure we release our lock if we've had an exception.""" key = decorators._get_lock_key('fake_func', self.pk) @decorators.lock_and_track def fake_func(import_file_pk): self.assertEqual(int(get_lock(key)), self.locked) raise TestException('Test exception!') self.assertRaises(TestException, fake_func, self.pk) # Even though execution failed part way through a call, we unlock. self.assertEqual(int(get_lock(key)), self.unlocked)
def test_locking_w_exception(self): """Make sure we release our lock if we have had an exception.""" key = decorators._get_lock_key('fake_func', self.pk) @decorators.lock_and_track def fake_func(import_file_pk): self.assertEqual(int(get_lock(key)), self.locked) raise TestException('Test exception!') self.assertRaises(TestException, fake_func, self.pk) # Even though execution failed part way through a call, we unlock. self.assertEqual(int(get_lock(key)), self.unlocked)
def _wrapped(import_file_pk, *args, **kwargs): """Lock and return progress url for updates.""" lock_key = _get_lock_key(func_name, import_file_pk) prog_key = get_prog_key(func_name, import_file_pk) is_locked = get_lock(lock_key) # If we're already processing a given task, don't proceed. if is_locked: return {'error': 'locked'} # Otherwise, set the lock for 1 minute. lock_cache(lock_key) try: response = fn(import_file_pk, *args, **kwargs) finally: # Unset our lock unlock_cache(lock_key) # If our response is a dict, add our progress URL to it. if isinstance(response, dict): response['progress_key'] = prog_key return response
def fake_func(import_file_pk): self.assertEqual(int(get_lock(key)), self.locked) raise TestException('Test exception!')
def fake_func(import_file_pk): self.assertEqual(int(get_lock(key)), self.locked)