def test_blocking_lock_with(self): lock1 = Lock(zk=self.zk, path=self.sandbox, name='lock1', simultaneous=1, wait=1) with lock1 as s: status = s self.assertTrue(status) lock1.release()
def test_blocking_lock_with(self): """Make sure that the enter/exit functionality works.""" nd = KazooServiceRegistry(server=self.server) lock1 = Lock(zk=nd._zk, path=self.sandbox, name='lock1', simultaneous=1, wait=1) with lock1 as s: status = s self.assertTrue(status) lock1.release()
def test_non_blocking_lock_with(self): lock1 = Lock(zk=self.zk, path=self.sandbox, name='lock1', simultaneous=1, wait=1) lock1.acquire() lock2 = Lock(zk=self.zk, path=self.sandbox, name='lock2', simultaneous=1, wait=1) with lock2 as s: status = s self.assertFalse(status) lock1.release()
def _get_lock(self, path, name, simultaneous, wait): """Retrieves a lock semaphore-style object from the supplied path. This method creates our Lock object and returns it. It is not meant to be used directly though. Args: path: A string representing the path for the lock. name: Optional string representing the server identifier. simultaneous: Int representing how many concurrent locks can occur on this path. Returns: nd_service_registry.Lock object """ # Return the object from our cache, if it's there log.debug('[%s] Checking for existing object...' % path) if path in self._locks: log.debug('Found [%s] in cache: %s' % (path, str(self._locks[path]))) return self._locks[path] # Go create a Lock object and store it log.debug('[%s] Creating Lock object...' % path) self._locks[path] = Lock(self._zk, path, name, simultaneous, wait) return self._locks[path]
def test_waiting_blocking_lock_wait(self): # Get our first lock object at our path and acquire it. lock1 = Lock(zk=self.zk, path=self.sandbox, name='lock1', simultaneous=1, wait=0) lock1.acquire() self.assertTrue(lock1.status()) # Get our first lock object at our path and try to acquire it. begin = time.time() lock2 = Lock(zk=self.zk, path=self.sandbox, name='lock2', simultaneous=1, wait=2) lock2.acquire() # Make sure that the lock was not successful, and returned within 1 # second of our requested wait time. self.assertFalse(lock2.status()) self.assertTrue((time.time() - begin) >= 2) # lock1.release() # waiting for fix from Kazoo guys lock2.release()
def test_non_blocking_lock(self): # Get our first lock object at our path and acquire it. lock1 = Lock(zk=self.zk, path=self.sandbox, name='lock1', simultaneous=1, wait=0) lock1.acquire() self.assertTrue(lock1.status()) # Get our first lock object at our path and acquire it. lock2 = Lock(zk=self.zk, path=self.sandbox, name='lock2', simultaneous=1, wait=0) lock2.acquire() self.assertFalse(lock2.status()) lock1.release() lock2.release()
def test_non_blocking_lock(self): """Test that a non-blocking lock works""" nd = KazooServiceRegistry(server=self.server) # Get our first lock object at our path and acquire it. lock1 = Lock(zk=nd._zk, path=self.sandbox, name='lock1', simultaneous=1, wait=0) lock1.acquire() self.assertTrue(lock1.status()) # Get our first lock object at our path and acquire it. lock2 = Lock(zk=nd._zk, path=self.sandbox, name='lock2', simultaneous=1, wait=0) lock2.acquire() self.assertFalse(lock2.status()) lock1.release() lock2.release()