Changeset b1261cb in mod_gnutls


Ignore:
Timestamp:
Dec 12, 2019, 4:58:01 PM (10 months ago)
Author:
Fiona Klute <fiona.klute@…>
Branches:
master, proxy-ticket
Children:
f3a3f6f
Parents:
4459cdd
Message:

mgstest: Context manager for an optional file-based mutex

This can be used to replace the various flock instances in runtests,
and possibly the .NOPARALLEL mode too, if the lockf-based emulation
mentioned in the Python documentation supports all relevant platforms.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • test/mgstest/__init__.py

    r4459cdd rb1261cb  
    1717"""Python modules for the mod_gnutls test suite."""
    1818
     19import sys
     20
     21import fcntl
     22from contextlib import contextmanager
     23
    1924class TestExpectationFailed(Exception):
    2025    """Raise if a test failed. The constructor should be called with a
    2126    string describing the problem."""
    2227    pass
     28
     29
     30
     31@contextmanager
     32def lockfile(file, nolock=False):
     33    """Context manager for an optional file-based mutex.
     34
     35    Unless nolock=True the process must hold a lock on the given file
     36    before entering the context. The lock is released when leaving the
     37    context.
     38
     39    """
     40    if nolock:
     41        yield
     42    else:
     43        with open(file, 'w') as lockfile:
     44            try:
     45                print(f'Aquiring lock on {file}...', file=sys.stderr)
     46                fcntl.flock(lockfile, fcntl.LOCK_EX)
     47                print(f'Got lock on {file}.', file=sys.stderr)
     48                yield
     49            finally:
     50                print(f'Unlocking {file}...', file=sys.stderr)
     51                fcntl.flock(lockfile, fcntl.LOCK_UN)
     52                print(f'Unlocked {file}.', file=sys.stderr)
Note: See TracChangeset for help on using the changeset viewer.