def it_stays_locked_for_the_lifetime_of_subprocesses(self, tmpfile): from subprocess import Popen with flock(tmpfile): p = Popen(('sleep', '99999')) assert p.poll() is None assert_locked(tmpfile) assert_locked(tmpfile) p.terminate() assert p.wait() == -15 with flock(tmpfile): print('oh hi there!')
def it_works_fine_with_a_directory(self, tmpfile): import os.path assert not os.path.isdir(tmpfile) os.mkdir(tmpfile) with flock(tmpfile): print('oh, hi!') assert os.path.isdir(tmpfile)
def wait4(tmpdir): """wait for all subprocesses to finish.""" lock = tmpdir.join('pytest-subprocesses.lock') try: from pgctl.flock import flock with flock(lock.strpath): yield finally: from pgctl.fuser import fuser from pgctl.functions import ps fusers = True i = 0 while fusers and i < 10: fusers = tuple(fuser(lock.strpath)) i += 1 # we only hit this when tests are broken. pragma: no cover fusers = ps(fusers) if fusers: raise AssertionError("there's a subprocess that's still running:\n%s" % ps(fusers))
def it_creates_a_file_if_it_didnt_exist(self, tmpfile): from os.path import exists assert not exists(tmpfile) with flock(tmpfile): print('oh, hi!') assert exists(tmpfile)
def it_releases_lock_on_exit(self, tmpfile): with flock(tmpfile): print('oh, hi!') with flock(tmpfile): print('oh, hi!')
def it_disallows_subsequent_callers(self, tmpfile): with flock(tmpfile): print('oh, hi!') assert_locked(tmpfile)
def it_allows_first_caller(self, tmpfile): with flock(tmpfile): print('oh, hi!')
def assert_locked(tmpfile): with ShouldRaise(flock.Locked(tmpfile)): with flock(tmpfile): raise AssertionError('this should not work')
def assert_locked(tmpfile): with ShouldRaise(Locked(11)): with flock(tmpfile): raise AssertionError('this should not work')