def test_acquire06(self): """Occupied by a directory""" lock = Host(self.test_root).get_lock('test') os.makedirs(lock.file) with self.assertRaises(wsb_host.LockGenerateError): lock.acquire()
def test_release03(self): """File not exist""" lock = Host(self.test_root).get_lock('test') lock.acquire() os.remove(lock.file) with self.assertRaises(wsb_host.LockReleaseNotFoundError): lock.release()
def test_release01(self): """Nnormal case""" lock = Host(self.test_root).get_lock('test') lock.acquire() lock.release() self.assertFalse(os.path.lexists(lock.file)) self.assertFalse(lock.locked)
def test_acquire05(self): """Unable to generate upper directory""" lock = Host(self.test_root).get_lock('test') with open(os.path.join(self.test_root, WSB_DIR, 'locks'), 'wb') as fh: pass with self.assertRaises(wsb_host.LockGenerateError): lock.acquire()
def test_acquire01(self): """Normal case""" lock = Host(self.test_root).get_lock('test') lock.acquire() with open(lock.file) as fh: self.assertTrue(fh.read(), lock.id) self.assertTrue(lock.locked)
def test_acquire03(self): """Already exists, timeout as acquire param""" lock = Host(self.test_root).get_lock('test') os.makedirs(os.path.dirname(lock.file)) with open(lock.file, 'w') as fh: pass with self.assertRaises(wsb_host.LockTimeoutError): lock.acquire(timeout=0)
def test_keep01(self): """Lock should be auto-extended until released.""" lock_file = os.path.join(self.test_root, WSB_DIR, 'locks', '098f6bcd4621d373cade4e832627b4f6.lock') lock = Host(self.test_root).get_lock('test', stale=0.01) lock.acquire() lock.keep() mtime = os.stat(lock_file).st_mtime time.sleep(0.005) self.assertGreater(os.stat(lock_file).st_mtime, mtime) lock.release()
def test_extend01(self): """Nnormal case""" lock = Host(self.test_root).get_lock('test') lock.acquire() prev_time = os.stat(lock.file).st_mtime time.sleep(0.05) lock.extend() cur_time = os.stat(lock.file).st_mtime self.assertGreater(cur_time, prev_time) self.assertTrue(lock.locked)
def test_acquire04(self): """Stale lock should be regenerated""" lock = Host(self.test_root).get_lock('test', timeout=1, stale=0) os.makedirs(os.path.dirname(lock.file)) with open(lock.file, 'w') as fh: fh.write('oldid') lock.acquire() with open(lock.file) as fh: self.assertTrue(fh.read(), lock.id) self.assertNotEqual(lock.id, 'oldid') self.assertTrue(lock.locked)
def test_acquire_with(self): """Lock should be released after an with statement.""" lock = Host(self.test_root).get_lock('test') with lock.acquire() as lh: self.assertTrue(os.path.isfile(lock.file)) self.assertTrue(lock.locked) self.assertEqual(lh, lock) self.assertFalse(os.path.exists(lock.file)) self.assertFalse(lock.locked)