From e78764caa16af8581f99855ee297d682e14f3600 Mon Sep 17 00:00:00 2001 From: Tamara Dahlgren <35777542+tldahlgren@users.noreply.github.com> Date: Wed, 21 Oct 2020 17:32:04 -0700 Subject: Added _poll_lock exception tests (#19446) --- lib/spack/spack/test/llnl/util/lock.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'lib') diff --git a/lib/spack/spack/test/llnl/util/lock.py b/lib/spack/spack/test/llnl/util/lock.py index 99c1eba588..596a801179 100644 --- a/lib/spack/spack/test/llnl/util/lock.py +++ b/lib/spack/spack/test/llnl/util/lock.py @@ -43,6 +43,8 @@ actually on a shared filesystem. """ import collections +import errno +import fcntl import os import socket import shutil @@ -1277,6 +1279,30 @@ def test_downgrade_write_fails(tmpdir): lock.downgrade_write_to_read() +@pytest.mark.parametrize("err_num,err_msg", + [(errno.EACCES, "Fake EACCES error"), + (errno.EAGAIN, "Fake EAGAIN error"), + (errno.ENOENT, "Fake ENOENT error")]) +def test_poll_lock_exception(tmpdir, monkeypatch, err_num, err_msg): + """Test poll lock exception handling.""" + def _lockf(fd, cmd, len, start, whence): + raise IOError(err_num, err_msg) + + with tmpdir.as_cwd(): + lockfile = 'lockfile' + lock = lk.Lock(lockfile) + + touch(lockfile) + + monkeypatch.setattr(fcntl, 'lockf', _lockf) + + if err_num in [errno.EAGAIN, errno.EACCES]: + assert not lock._poll_lock(fcntl.LOCK_EX) + else: + with pytest.raises(IOError, match=err_msg): + lock._poll_lock(fcntl.LOCK_EX) + + def test_upgrade_read_okay(tmpdir): """Test the lock read-to-write upgrade operation.""" with tmpdir.as_cwd(): -- cgit v1.2.3-60-g2f50