Exemplo n.º 1
0
    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!')
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
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))
Exemplo n.º 4
0
 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)
Exemplo n.º 5
0
    def it_releases_lock_on_exit(self, tmpfile):
        with flock(tmpfile):
            print('oh, hi!')

        with flock(tmpfile):
            print('oh, hi!')
Exemplo n.º 6
0
    def it_disallows_subsequent_callers(self, tmpfile):
        with flock(tmpfile):
            print('oh, hi!')

            assert_locked(tmpfile)
Exemplo n.º 7
0
 def it_allows_first_caller(self, tmpfile):
     with flock(tmpfile):
         print('oh, hi!')
Exemplo n.º 8
0
def assert_locked(tmpfile):
    with ShouldRaise(flock.Locked(tmpfile)):
        with flock(tmpfile):
            raise AssertionError('this should not work')
Exemplo n.º 9
0
 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)
Exemplo n.º 10
0
    def it_releases_lock_on_exit(self, tmpfile):
        with flock(tmpfile):
            print('oh, hi!')

        with flock(tmpfile):
            print('oh, hi!')
Exemplo n.º 11
0
    def it_disallows_subsequent_callers(self, tmpfile):
        with flock(tmpfile):
            print('oh, hi!')

            assert_locked(tmpfile)
Exemplo n.º 12
0
 def it_allows_first_caller(self, tmpfile):
     with flock(tmpfile):
         print('oh, hi!')
Exemplo n.º 13
0
def assert_locked(tmpfile):
    with ShouldRaise(Locked(11)):
        with flock(tmpfile):
            raise AssertionError('this should not work')