def test_lock_acquire(index_dir): os.makedirs(index_dir) lock = PIDLockFile(os.path.join(index_dir, 'writer')) assert not lock.is_locked() assert not lock.i_am_locking() assert lock.read_pid() is None lock.acquire() assert lock.is_locked() assert lock.i_am_locking() assert lock.read_pid() == os.getpid()
def test_locking_interals(index_dir, monkeypatch): """Test error conditions of various private methods.""" os.makedirs(index_dir) lock = PIDLockFile(os.path.join(index_dir, "writer")) lock.acquire() with open(lock.path, 'w') as lock_file: lock_file.write('xxx') assert lock.read_pid() is None with open(lock.path, 'w') as lock_file: lock_file.write("{}".format(os.getpid())) def error_raiser(code): raise OSError(code, "A message") monkeypatch.setattr(os, "remove", lambda x: error_raiser(errno.ENOENT)) # Simulate file not existing lock.release() monkeypatch.setattr(os, "remove", lambda x: error_raiser(errno.EACCES)) # Simulate other error with pytest.raises(OSError): lock.release()