def test_lock(self): threads = [] names = ["contender" + str(i) for i in range(5)] contender_bits = {} for name in names: c = get_client_or_skip() c.connect() e = threading.Event() l = ZooLock(c, self.lockpath, name) t = threading.Thread(target=self._thread_lock_acquire_til_event, args=(name, l, e)) contender_bits[name] = (t, e) threads.append(t) # acquire the lock ourselves first to make the others line up lock = ZooLock(self._c, self.lockpath, "test") lock.acquire() for t in threads: t.start() contenders = None # wait for everyone to line up on the lock for _ in until_timeout(5): contenders = lock.get_contenders() if len(contenders) == 6: break self.assertEqual(contenders[0], "test") contenders = contenders[1:] remaining = list(contenders) # release the lock and contenders should claim it in order lock.release() for contender in contenders: thread, event = contender_bits[contender] with self.condition: while not self.active_thread: self.condition.wait() self.assertEqual(self.active_thread, contender) self.assertEqual(lock.get_contenders(), remaining) remaining = remaining[1:] event.set() with self.condition: while self.active_thread: self.condition.wait() thread.join()
def test_lock(self): threads = [] names = ["contender"+str(i) for i in range(5)] contender_bits = {} for name in names: c = get_client_or_skip() c.connect() e = threading.Event() l = ZooLock(c, self.lockpath, name) t = threading.Thread(target=self._thread_lock_acquire_til_event, args=(name, l, e)) contender_bits[name] = (t, e) threads.append(t) # acquire the lock ourselves first to make the others line up lock = ZooLock(self._c, self.lockpath, "test") lock.acquire() for t in threads: t.start() contenders = None # wait for everyone to line up on the lock for _ in until_timeout(5): contenders = lock.get_contenders() if len(contenders) == 6: break self.assertEqual(contenders[0], "test") contenders = contenders[1:] remaining = list(contenders) # release the lock and contenders should claim it in order lock.release() for contender in contenders: thread, event = contender_bits[contender] with self.condition: while not self.active_thread: self.condition.wait() self.assertEqual(self.active_thread, contender) self.assertEqual(lock.get_contenders(), remaining) remaining = remaining[1:] event.set() with self.condition: while self.active_thread: self.condition.wait() thread.join()