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)
Example #4
0
    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)
Example #5
0
        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 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)
Example #7
0
    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
Example #8
0
    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
Example #9
0
    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
Example #10
0
    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
Example #11
0
    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)
Example #12
0
    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)
Example #13
0
 def test_no_slash_in_b64(self):
     # base64(sha1(foobar)) has a slash in it
     with lockutils.lock("foobar"):
         pass
Example #14
0
 def __contains__(self, key):
     with lockutils.lock(key):
         return self._exists_unlocked(key)
Example #15
0
 def f(_id):
     with lockutils.lock('testlock2', 'test-', external=False):
         for x in range(10):
             seen_threads.append(_id)
Example #16
0
 def __init__(self, name, lock_file_prefix=None):
     self.mgr = lockutils.lock(name, lock_file_prefix, True)
Example #17
0
 def __contains__(self, key):
     with lockutils.lock(key):
         return self._exists_unlocked(key)
Example #18
0
 def _get(self, key, default=None):
     with lockutils.lock(key):
         return self._get_unlocked(key, default)[1]
Example #19
0
 def _get(self, key, default=None):
     with lockutils.lock(key):
         return self._get_unlocked(key, default)[1]
 def __init__(self, name, lock_file_prefix=None):
     self.mgr = lockutils.lock(name, lock_file_prefix, True)
 def f(_id):
     with lockutils.lock("testlock2", "test-", external=False):
         for x in range(10):
             seen_threads.append(_id)
             greenthread.sleep(0)
Example #22
0
 def f(_id):
     with lockutils.lock('testlock2', 'test-', external=False):
         for x in range(10):
             seen_threads.append(_id)
             greenthread.sleep(0)