def test_wait(self, fcntl_mock, tmpdir): """ Test wait parameter override using method acquire. Test different method behaviour """ # Use a lock file inside the testing tempdir lock_file_path = tmpdir.join("test_lock_file1") # set flock to not raise _prepare_fnctl_mock(fcntl_mock) lock_file = LockFile(lock_file_path.strpath, raise_if_fail=False, wait=False) # should succeed assert lock_file.acquire(wait=True) # if the wait flag is set to true we expect a lock_ex flag, that # has a numeric value of 2 fcntl_mock.flock.assert_called_once_with(ANY, fcntl.LOCK_EX) # release the lock lock_file.release() # set flock to not raise _prepare_fnctl_mock(fcntl_mock) # acquire it again with wait flag set to False assert lock_file.acquire(wait=False) # with the wait flag not set flock bust be called with # LOCK_EX and LOCK_NB flags fcntl_mock.flock.assert_called_once_with( ANY, fcntl.LOCK_EX | fcntl.LOCK_NB)
def test_acquire(self, tmpdir): """ Test lock acquisition using direct methods. * Create a LockFile, and acquire the lock. * Create a second LockFile and try to acquire the lock. It should fail. * Release the first lock and try acquiring the lock with the second one. It should now succeed. """ # Use a lock file inside the testing tempdir lock_file_path = tmpdir.join("test_lockfile1") # Acquire the first lock, should succeed first_lock_file = LockFile(lock_file_path.strpath, raise_if_fail=False, wait=False) assert first_lock_file.acquire() # Try to acquire the lock using a second object, must fail second_lock_file = LockFile(lock_file_path.strpath, raise_if_fail=False, wait=False) assert not second_lock_file.acquire() # Release the lock with the first LockFile first_lock_file.release() # The second LockFile is now able to acquire the lock assert second_lock_file.acquire() second_lock_file.release()
def test_wait(self, fcntl_mock, tmpdir): """ Test wait parameter override using method acquire. Test different method behaviour """ # Use a lock file inside the testing tempdir lock_file_path = tmpdir.join("test_lock_file1") # set flock to not raise _prepare_fnctl_mock(fcntl_mock) lock_file = LockFile(lock_file_path.strpath, raise_if_fail=False, wait=False) # should succeed assert lock_file.acquire(wait=True) # if the wait flag is set to true we expect a lock_ex flag, that # has a numeric value of 2 fcntl_mock.flock.assert_called_once_with(ANY, fcntl.LOCK_EX) # release the lock lock_file.release() # set flock to not raise _prepare_fnctl_mock(fcntl_mock) # acquire it again with wait flag set to False assert lock_file.acquire(wait=False) # with the wait flag not set flock bust be called with # LOCK_EX and LOCK_NB flags fcntl_mock.flock.assert_called_once_with(ANY, fcntl.LOCK_EX | fcntl.LOCK_NB)
def test_raise(self, fcntl_mock, tmpdir): """ Test raise_if_fail override using a call to method acquire. Test different method behaviour with different flags """ # Use a lock file inside the testing tempdir lock_file_path = tmpdir.join("test_lock_file1") # set flock to raise OSError exception with errno = EAGAIN _prepare_fcntl_mock(fcntl_mock, OSError(errno.EAGAIN, "", "")) lock_file = LockFile(lock_file_path.strpath, raise_if_fail=False, wait=False) # Expect the acquire method to raise a LockFileBusy exception. # This is the expected behaviour if the raise_if_fail flag is set to # True and the OSError.errno = errno.EAGAIN with pytest.raises(LockFileBusy): lock_file.acquire(raise_if_fail=True) # check for the right call at flock method fcntl_mock.flock.assert_called_once_with( ANY, fcntl_mock.LOCK_EX | fcntl_mock.LOCK_NB ) # set flock to raise OSError exception with errno = EWOULDBLOCK _prepare_fcntl_mock(fcntl_mock, OSError(errno.EWOULDBLOCK, "", "")) # Expect the acquire method to raise a LockFileBusy exception. # This is the expected behaviour if the raise_if_fail flag is set to # True and the OSError.errno = errno.EWOULDBLOCK with pytest.raises(LockFileBusy): lock_file.acquire(raise_if_fail=True) # Check for the call at flock method fcntl_mock.flock.assert_called_once_with( ANY, fcntl_mock.LOCK_EX | fcntl_mock.LOCK_NB ) # set flock to raise OSError exception with errno = EACCES _prepare_fcntl_mock(fcntl_mock, OSError(errno.EACCES, "", "")) # Expect the acquire method to raise a LockFileBusy exception. # This is the expected behaviour if the raise_if_fail flag is set to # True and the OSError.errno = errno.EACCES with pytest.raises(LockFilePermissionDenied): lock_file.acquire(raise_if_fail=True) # Check for the call at flock method fcntl_mock.flock.assert_called_once_with( ANY, fcntl_mock.LOCK_EX | fcntl_mock.LOCK_NB ) # set flock to raise an unexpected OSError exception (errno = EINVAL) _prepare_fcntl_mock(fcntl_mock, OSError(errno.EINVAL, "", "")) # Expect the acquire method to pass the raised exception. # This is the expected behaviour if the raise_if_fail flag is set to # True and an unexpected exception is raised with pytest.raises(OSError): lock_file.acquire(raise_if_fail=True) # Check for the call at flock method fcntl_mock.flock.assert_called_once_with( ANY, fcntl_mock.LOCK_EX | fcntl_mock.LOCK_NB ) # it should not raise if not raise_if_fail, but return False _prepare_fcntl_mock(fcntl_mock, OSError(errno.EWOULDBLOCK, "", "")) assert not lock_file.acquire(raise_if_fail=False)
def test_raise(self, fcntl_mock, tmpdir): """ Test raise_if_fail override using a call to method acquire. Test different method behaviour with different flags """ # Use a lock file inside the testing tempdir lock_file_path = tmpdir.join("test_lock_file1") # set flock to raise OSError exception with errno = EAGAIN _prepare_fnctl_mock(fcntl_mock, OSError(errno.EAGAIN, '', '')) lock_file = LockFile(lock_file_path.strpath, raise_if_fail=False, wait=False) # Expect the acquire method to raise a LockFileBusy exception. # This is the expected behaviour if the raise_if_fail flag is set to # True and the OSError.errno = errno.EAGAIN with pytest.raises(LockFileBusy): lock_file.acquire(raise_if_fail=True) # check for the right call at flock method fcntl_mock.flock.assert_called_once_with( ANY, fcntl_mock.LOCK_EX | fcntl_mock.LOCK_NB) # set flock to raise OSError exception with errno = EWOULDBLOCK _prepare_fnctl_mock(fcntl_mock, OSError(errno.EWOULDBLOCK, '', '')) # Expect the acquire method to raise a LockFileBusy exception. # This is the expected behaviour if the raise_if_fail flag is set to # True and the OSError.errno = errno.EWOULDBLOCK with pytest.raises(LockFileBusy): lock_file.acquire(raise_if_fail=True) # Check for the call at flock method fcntl_mock.flock.assert_called_once_with( ANY, fcntl_mock.LOCK_EX | fcntl_mock.LOCK_NB) # set flock to raise OSError exception with errno = EACCES _prepare_fnctl_mock(fcntl_mock, OSError(errno.EACCES, '', '')) # Expect the acquire method to raise a LockFileBusy exception. # This is the expected behaviour if the raise_if_fail flag is set to # True and the OSError.errno = errno.EACCES with pytest.raises(LockFilePermissionDenied): lock_file.acquire(raise_if_fail=True) # Check for the call at flock method fcntl_mock.flock.assert_called_once_with( ANY, fcntl_mock.LOCK_EX | fcntl_mock.LOCK_NB) # set flock to raise an unexpected OSError exception (errno = EINVAL) _prepare_fnctl_mock(fcntl_mock, OSError(errno.EINVAL, '', '')) # Expect the acquire method to pass teh raised exception. # This is the expected behaviour if the raise_if_fail flag is set to # True and an unexpected exception is raised with pytest.raises(OSError): lock_file.acquire(raise_if_fail=True) # Check for the call at flock method fcntl_mock.flock.assert_called_once_with( ANY, fcntl_mock.LOCK_EX | fcntl_mock.LOCK_NB) # it should not raise if not raise_if_fail, but return False _prepare_fnctl_mock(fcntl_mock, OSError(errno.EWOULDBLOCK, '', '')) assert not lock_file.acquire(raise_if_fail=False)
def test_release(self, fcntl_mock, tmpdir): """ Try to release an already released LockFile object """ # Use a lock file inside the testing tempdir lock_file_path = tmpdir.join("test_lock_file1") # set flock to not raise _prepare_fnctl_mock(fcntl_mock) lock_file = LockFile(lock_file_path.strpath, raise_if_fail=False, wait=False) assert lock_file.acquire() # set flock to not raise _prepare_fnctl_mock(fcntl_mock) # Release the lock lock_file.release() # Check that the fcntl.flock() have been called using the flag LOCK_UN fcntl_mock.flock.assert_called_once_with(ANY, fcntl.LOCK_UN) # set flock to not raise _prepare_fnctl_mock(fcntl_mock) # Try to release the lock again lock_file.release() # The release method should not have called fcntl.flock() assert not fcntl_mock.flock.called
def test_release(self, fcntl_mock, tmpdir): """ Tests for release method """ # Test 1: normal release # Use a lock file inside the testing tempdir lock_file_path = tmpdir.join("test_lock_file1") # set flock to not raise _prepare_fnctl_mock(fcntl_mock) lock_file = LockFile(lock_file_path.strpath, raise_if_fail=False, wait=False) assert lock_file.acquire() # set flock to not raise _prepare_fnctl_mock(fcntl_mock) # Release the lock lock_file.release() # Check that the fcntl.flock() have been called using the flag LOCK_UN fcntl_mock.flock.assert_called_once_with(ANY, fcntl.LOCK_UN) # Test 2: release an already released lock # set flock to not raise _prepare_fnctl_mock(fcntl_mock) # Try to release the lock again lock_file.release() # The release method should not have called fcntl.flock() assert not fcntl_mock.flock.called # Test 3: exceptions during release # set flock to not raise _prepare_fnctl_mock(fcntl_mock) lock_file = LockFile(lock_file_path.strpath, raise_if_fail=False, wait=False) assert lock_file.acquire() # set flock to raise an OSError (no matter what) _prepare_fnctl_mock(fcntl_mock, OSError(errno.EBADF, '', '')) # Release the lock (should not raise any error) lock_file.release() # Check that the fcntl.flock() have been called using the flag LOCK_UN fcntl_mock.flock.assert_called_once_with(ANY, fcntl.LOCK_UN)
def test_acquire(self, fcntl_mock, tmpdir): """ Test for double acquisition using the same LockFile object. """ # Use a lock file inside the testing tempdir lock_file_path = tmpdir.join("test_lock_file1") # set flock to not raise _prepare_fcntl_mock(fcntl_mock) lock_file = LockFile(lock_file_path.strpath, raise_if_fail=False, wait=False) assert lock_file.acquire() # with the wait flag not set flock bust be called with # LOCK_EX and LOCK_NB flags fcntl_mock.flock.assert_called_once_with(ANY, fcntl.LOCK_EX | fcntl.LOCK_NB) # set flock to not raise _prepare_fcntl_mock(fcntl_mock) # Try the acquisition using the same unreleased LockFile. # The acquire method should exit immediately without calling # fcntl.flock() again assert lock_file.acquire() assert not fcntl_mock.flock.called
def test_acquire(self, fcntl_mock, tmpdir): """ Test for double acquisition using the same LockFile object. """ # Use a lock file inside the testing tempdir lock_file_path = tmpdir.join("test_lock_file1") # set flock to not raise _prepare_fnctl_mock(fcntl_mock) lock_file = LockFile(lock_file_path.strpath, raise_if_fail=False, wait=False) assert lock_file.acquire() # with the wait flag not set flock bust be called with # LOCK_EX and LOCK_NB flags fcntl_mock.flock.assert_called_once_with( ANY, fcntl.LOCK_EX | fcntl.LOCK_NB) # set flock to not raise _prepare_fnctl_mock(fcntl_mock) # Try the acquisition using the same unreleased LockFile. # The acquire method should exit immediately without calling # fcntl.flock() again assert lock_file.acquire() assert not fcntl_mock.flock.called