def test_with(self): ''' Tests do-with functionallity ''' with filelock.file_lock(self._lock_file_path): sleep(0.1) sleep(0.1) # Make sure the remove is in effect self.assertFalse(isfile(self._lock_file_path))
def test_timeout(self): ''' Test if a timeout is reached. ''' # Use an impossible timeout try: with filelock.file_lock(self._lock_file_path, timeout=-1): pass self.assertTrue(False, 'Should not reach this point') except filelock.FileLockTimeoutError: pass
def test_exception(self): ''' Tests if the lock-file does not remain if an exception occurs. ''' try: with filelock.file_lock(self._lock_file_path): raise Exception('Breaking out') except Exception: pass self.assertFalse(isfile(self._lock_file_path))
def test_pid_disallow(self): ''' Test if stale-lock files are respected if disallow policy is set. ''' self._fake_crash_other_process() try: with filelock.file_lock(self._lock_file_path, pid_policy=filelock.PID_DISALLOW, timeout=0.1): self.assertTrue(False, 'Should not reach this point') except filelock.FileLockTimeoutError: pass
def backup(min_interval=MIN_INTERVAL, backup_dir=BACKUP_DIR, data_dir=DATA_DIR): """ backup utility """ if backup_dir is None: return # XXX: The timeout is arbitrary but dependent on the back-up, should we start # with a sane default and then refer to how long the last back-up # took? backup_lock = join_path(DATA_DIR, '.backup.lock') with file_lock(backup_lock, pid_policy=PID_WARN, timeout=60): _backup(min_interval, backup_dir, data_dir)
def test_pid_allow(self): ''' Test if a stale lock-file is ignored and un-reported if the allow policy has been set. ''' self._fake_crash_other_process() err_output = StringIO() try: with filelock.file_lock(self._lock_file_path, pid_policy=filelock.PID_ALLOW, err_output=err_output): pass except filelock.FileLockTimeoutError: self.assertTrue(False, 'Should not reach this point') err_output.seek(0) self.assertFalse(err_output.read(), 'Output although allow set')
def test_pid_warn(self): ''' Test if a stale lock-filk causes a warning to stderr and then is ignored if the warn policy is set. ''' self._fake_crash_other_process() err_output = StringIO() try: with filelock.file_lock(self._lock_file_path, pid_policy=filelock.PID_WARN, err_output=err_output): pass except filelock.FileLockTimeoutError: self.assertTrue(False, 'Should not reach this point') err_output.seek(0) self.assertTrue(err_output.read(), 'No output although warn set')
def process_task(path): with filelock.file_lock(path): sleep(1) return 0