コード例 #1
0
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()
コード例 #2
0
def test_lock_release(index_dir, monkeypatch):
    """Test the lock release functionality."""
    os.makedirs(index_dir)
    lock1 = PIDLockFile(os.path.join(index_dir, 'writer'))
    lock2 = PIDLockFile(os.path.join(index_dir, 'writer'))

    with pytest.raises(NotLocked):
        lock1.release()

    lock1.acquire()
    lock1.release()
    assert not lock1.is_locked()

    lock1.acquire()
    monkeypatch.setattr(os, "getpid", lambda: 1)  # Modify os.getpid() to get the error we want.
    with pytest.raises(NotMyLock):
        lock2.release()