Changeset 0fe2130 in mod_gnutls


Ignore:
Timestamp:
Dec 16, 2020, 1:12:16 PM (9 months ago)
Author:
Fiona Klute <fiona.klute@…>
Branches:
master
Children:
319588a
Parents:
514058d
Message:

Simplify lockfile handling

It's much easier to just not enter the lockfile context if it's not
needed than to have a special case in the context manager.

Location:
test
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • test/mgstest/__init__.py

    r514058d r0fe2130  
    3333
    3434@contextmanager
    35 def lockfile(file, nolock=False):
     35def lockfile(file):
    3636    """Context manager for an optional file-based mutex.
    3737
    38     Unless nolock=True the process must hold a lock on the given file
    39     before entering the context. The lock is released when leaving the
    40     context.
     38    The process must hold a lock on the given file before entering the
     39    context. The lock is released when leaving the context.
    4140
    4241    """
    43     if nolock:
     42    with open(file, 'w') as lockfile:
    4443        try:
    45             yield None
     44            print(f'Aquiring lock on {file}...', file=sys.stderr)
     45            fcntl.flock(lockfile, fcntl.LOCK_EX)
     46            print(f'Got lock on {file}.', file=sys.stderr)
     47            yield lockfile
    4648        finally:
    47             pass
    48     else:
    49         with open(file, 'w') as lockfile:
    50             try:
    51                 print(f'Aquiring lock on {file}...', file=sys.stderr)
    52                 fcntl.flock(lockfile, fcntl.LOCK_EX)
    53                 print(f'Got lock on {file}.', file=sys.stderr)
    54                 yield lockfile
    55             finally:
    56                 print(f'Unlocking {file}...', file=sys.stderr)
    57                 fcntl.flock(lockfile, fcntl.LOCK_UN)
    58                 print(f'Unlocked {file}.', file=sys.stderr)
     49            print(f'Unlocking {file}...', file=sys.stderr)
     50            fcntl.flock(lockfile, fcntl.LOCK_UN)
     51            print(f'Unlocked {file}.', file=sys.stderr)
    5952
    6053
  • test/runtest.py

    r514058d r0fe2130  
    164164        if cleanup_callback:
    165165            service_stack.callback(cleanup_callback)
    166         service_stack.enter_context(
    167             lockfile('test.lock', nolock='MGS_NETNS_ACTIVE' in os.environ))
     166        if 'MGS_NETNS_ACTIVE' not in os.environ:
     167            service_stack.enter_context(lockfile('test.lock'))
    168168
    169169        wait_timeout = float(os.environ.get('TEST_SERVICE_MAX_WAIT', 10))
Note: See TracChangeset for help on using the changeset viewer.