def test_contextlock(self): lock_dir = tempfile.mkdtemp() self.config(lock_path=lock_dir) try: # Note(flaper87): Lock is not external, which means # a semaphore will be yielded with lockutils.lock("test") as sem: if six.PY2: self.assertTrue(isinstance(sem, threading._Semaphore)) else: self.assertTrue(isinstance(sem, threading.Semaphore)) # NOTE(flaper87): Lock is external so an InterProcessLock # will be yielded. with lockutils.lock("test2", external=True) as lock: self.assertTrue(lock.exists()) with lockutils.lock("test1", external=True) as lock1: self.assertTrue(isinstance(lock1, lockutils.InterProcessLock)) finally: if os.path.exists(lock_dir): shutil.rmtree(lock_dir, ignore_errors=True)
def test_contextlock_unlocks(self): lock_dir = tempfile.mkdtemp() sem = None try: with lockutils.lock("test") as sem: semaphores = (semaphore.Semaphore, threading._Semaphore) self.assertTrue(isinstance(sem, semaphores)) with lockutils.lock("test2", external=True, lock_path=lock_dir): path = os.path.join(lock_dir, "test2") self.assertTrue(os.path.exists(path)) # NOTE(flaper87): Lock should be free with lockutils.lock("test2", external=True, lock_path=lock_dir): path = os.path.join(lock_dir, "test2") self.assertTrue(os.path.exists(path)) # NOTE(flaper87): Lock should be free # but semaphore should already exist. with lockutils.lock("test") as sem2: self.assertEqual(sem, sem2) finally: if os.path.exists(lock_dir): shutil.rmtree(lock_dir, ignore_errors=True)
def test_contextlock_unlocks(self): lock_dir = tempfile.mkdtemp() self.config(lock_path=lock_dir) sem = None try: with lockutils.lock("test") as sem: if six.PY2: self.assertTrue(isinstance(sem, threading._Semaphore)) else: self.assertTrue(isinstance(sem, threading.Semaphore)) with lockutils.lock("test2", external=True) as lock: self.assertTrue(lock.exists()) # NOTE(flaper87): Lock should be free with lockutils.lock("test2", external=True) as lock: self.assertTrue(lock.exists()) # NOTE(flaper87): Lock should be free # but semaphore should already exist. with lockutils.lock("test") as sem2: self.assertEqual(sem, sem2) finally: if os.path.exists(lock_dir): shutil.rmtree(lock_dir, ignore_errors=True)
def lock_files(handles_dir): with lockutils.lock('external', 'test-', external=True): # Open some files we can use for locking handles = [] for n in range(50): path = os.path.join(handles_dir, ('file-%s' % n)) handles.append(open(path, 'w')) # Loop over all the handles and try locking the file # without blocking, keep a count of how many files we # were able to lock and then unlock. If the lock fails # we get an IOError and bail out with bad exit code count = 0 for handle in handles: try: fcntl.flock(handle, fcntl.LOCK_EX | fcntl.LOCK_NB) count += 1 fcntl.flock(handle, fcntl.LOCK_UN) except IOError: os._exit(2) finally: handle.close() # Check if we were able to open all files self.assertEqual(50, count)
def _set(self, key, value, ttl=0, not_exists=False): with lockutils.lock(key): # NOTE(flaper87): This is needed just in `set` # calls, hence it's not in `_set_unlocked` if not_exists and self._exists_unlocked(key): return False self._set_unlocked(key, value, ttl) return True
def _incr_append(self, key, other): with lockutils.lock(key): timeout, value = self._get_unlocked(key) if value is None: return None ttl = timeutils.utcnow_ts() - timeout new_value = value + other self._set_unlocked(key, new_value, ttl) return new_value
def test_contextlock(self): lock_dir = tempfile.mkdtemp() try: # Note(flaper87): Lock is not external, which means # a semaphore will be yielded with lockutils.lock("test") as sem: self.assertTrue(isinstance(sem, semaphore.Semaphore)) # NOTE(flaper87): Lock is external so an InterProcessLock # will be yielded. with lockutils.lock("test2", external=True, lock_path=lock_dir): path = os.path.join(lock_dir, "test2") self.assertTrue(os.path.exists(path)) with lockutils.lock("test1", external=True, lock_path=lock_dir) as lock1: self.assertTrue(isinstance(lock1, lockutils.InterProcessLock)) finally: if os.path.exists(lock_dir): shutil.rmtree(lock_dir, ignore_errors=True)
def test_contextlock(self): lock_dir = tempfile.mkdtemp() try: # Note(flaper87): Lock is not external, which means # a semaphore will be yielded with lockutils.lock("test") as sem: semaphores = (semaphore.Semaphore, threading._Semaphore) self.assertTrue(isinstance(sem, semaphores)) # NOTE(flaper87): Lock is external so an InterProcessLock # will be yielded. with lockutils.lock("test2", external=True, lock_path=lock_dir): path = os.path.join(lock_dir, "test2") self.assertTrue(os.path.exists(path)) with lockutils.lock("test1", external=True, lock_path=lock_dir) as lock1: self.assertTrue( isinstance(lock1, lockutils.InterProcessLock)) finally: if os.path.exists(lock_dir): shutil.rmtree(lock_dir, ignore_errors=True)
def test_no_slash_in_b64(self): # base64(sha1(foobar)) has a slash in it with lockutils.lock("foobar"): pass
def __contains__(self, key): with lockutils.lock(key): return self._exists_unlocked(key)
def f(_id): with lockutils.lock('testlock2', 'test-', external=False): for x in range(10): seen_threads.append(_id)
def __init__(self, name, lock_file_prefix=None): self.mgr = lockutils.lock(name, lock_file_prefix, True)
def _get(self, key, default=None): with lockutils.lock(key): return self._get_unlocked(key, default)[1]
def f(_id): with lockutils.lock("testlock2", "test-", external=False): for x in range(10): seen_threads.append(_id) greenthread.sleep(0)
def f(_id): with lockutils.lock('testlock2', 'test-', external=False): for x in range(10): seen_threads.append(_id) greenthread.sleep(0)