tests/test-lock.py
author Kyle Lippincott <spectral@google.com>
Tue, 19 Nov 2019 18:38:17 -0800
changeset 43778 888bd39ed555
parent 43076 2372284d9457
child 45519 9b16bb3b2349
permissions -rw-r--r--
lock: pass "success" boolean to _afterlock callbacks This lets the callback decide if it should actually run or not. I suspect that most callbacks (and hooks) *should not* run in this scenario, but I'm trying to not break any existing behavior. `persistmanifestcache`, however, seems actively dangerous to run: we just encountered an exception and the repo is in an unknown state (hopefully a consistent one due to transactions, but this is not 100% guaranteed), and the data we cache may be based on this unknown state. This was observed by our users since we wrap some of the functions that persistmanifestcache calls and it expects that the repo object is in a certain state that we'd set up earlier. If the user hits ctrl-c before we establish that state, we end up crashing there. I'm going to make that extension resilient to this issue, but figured it might be a common issue and should be handled here as well instead of just working around the issue. Differential Revision: https://phab.mercurial-scm.org/D7459
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     1
from __future__ import absolute_import
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     2
26386
146cccdb282b test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents: 26385
diff changeset
     3
import copy
32088
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
     4
import errno
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     5
import tempfile
26386
146cccdb282b test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents: 26385
diff changeset
     6
import types
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     7
import unittest
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
     8
40204
5d50c9ffaebb tests: fix style issue of importing order in test-lock.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 39948
diff changeset
     9
import silenttestrunner
5d50c9ffaebb tests: fix style issue of importing order in test-lock.py
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 39948
diff changeset
    10
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    11
from mercurial import (
39948
5ee3146c1b20 py3: byteify test-lock.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 34726
diff changeset
    12
    encoding,
26498
e8564e04382d lock: add a way to prevent locks from being inherited
Siddharth Agarwal <sid0@fb.com>
parents: 26474
diff changeset
    13
    error,
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    14
    lock,
31249
e067741d4607 vfs: use 'vfs' module directly in 'test-lock'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 28027
diff changeset
    15
    vfs as vfsmod,
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    16
)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    17
39948
5ee3146c1b20 py3: byteify test-lock.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 34726
diff changeset
    18
testlockname = b'testlock'
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    19
26386
146cccdb282b test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents: 26385
diff changeset
    20
# work around http://bugs.python.org/issue1515
146cccdb282b test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents: 26385
diff changeset
    21
if types.MethodType not in copy._deepcopy_dispatch:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    22
26386
146cccdb282b test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents: 26385
diff changeset
    23
    def _deepcopy_method(x, memo):
34726
daf12f69699f python3: replace im_{self,func} with __{self,func}__ globally
Augie Fackler <augie@google.com>
parents: 32279
diff changeset
    24
        return type(x)(x.__func__, copy.deepcopy(x.__self__, memo), x.im_class)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    25
26386
146cccdb282b test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents: 26385
diff changeset
    26
    copy._deepcopy_dispatch[types.MethodType] = _deepcopy_method
146cccdb282b test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents: 26385
diff changeset
    27
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    28
26384
ad6e56d01c30 test-lock.py: add a lock wrapper that allows faking the PID
Siddharth Agarwal <sid0@fb.com>
parents: 26382
diff changeset
    29
class lockwrapper(lock.lock):
ad6e56d01c30 test-lock.py: add a lock wrapper that allows faking the PID
Siddharth Agarwal <sid0@fb.com>
parents: 26382
diff changeset
    30
    def __init__(self, pidoffset, *args, **kwargs):
ad6e56d01c30 test-lock.py: add a lock wrapper that allows faking the PID
Siddharth Agarwal <sid0@fb.com>
parents: 26382
diff changeset
    31
        # lock.lock.__init__() calls lock(), so the pidoffset assignment needs
ad6e56d01c30 test-lock.py: add a lock wrapper that allows faking the PID
Siddharth Agarwal <sid0@fb.com>
parents: 26382
diff changeset
    32
        # to be earlier
ad6e56d01c30 test-lock.py: add a lock wrapper that allows faking the PID
Siddharth Agarwal <sid0@fb.com>
parents: 26382
diff changeset
    33
        self._pidoffset = pidoffset
ad6e56d01c30 test-lock.py: add a lock wrapper that allows faking the PID
Siddharth Agarwal <sid0@fb.com>
parents: 26382
diff changeset
    34
        super(lockwrapper, self).__init__(*args, **kwargs)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    35
26384
ad6e56d01c30 test-lock.py: add a lock wrapper that allows faking the PID
Siddharth Agarwal <sid0@fb.com>
parents: 26382
diff changeset
    36
    def _getpid(self):
28027
14033c5dd261 util: enable getpid to be replaced
timeless <timeless@mozdev.org>
parents: 26499
diff changeset
    37
        return super(lockwrapper, self)._getpid() + self._pidoffset
26384
ad6e56d01c30 test-lock.py: add a lock wrapper that allows faking the PID
Siddharth Agarwal <sid0@fb.com>
parents: 26382
diff changeset
    38
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    39
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    40
class teststate(object):
26385
fb1a424e8bff test-lock.py: allow PID to be changed in test state
Siddharth Agarwal <sid0@fb.com>
parents: 26384
diff changeset
    41
    def __init__(self, testcase, dir, pidoffset=0):
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    42
        self._testcase = testcase
26321
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
    43
        self._acquirecalled = False
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    44
        self._releasecalled = False
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    45
        self._postreleasecalled = False
31249
e067741d4607 vfs: use 'vfs' module directly in 'test-lock'
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 28027
diff changeset
    46
        self.vfs = vfsmod.vfs(dir, audit=False)
26385
fb1a424e8bff test-lock.py: allow PID to be changed in test state
Siddharth Agarwal <sid0@fb.com>
parents: 26384
diff changeset
    47
        self._pidoffset = pidoffset
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    48
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    49
    def makelock(self, *args, **kwargs):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    50
        l = lockwrapper(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    51
            self._pidoffset,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    52
            self.vfs,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    53
            testlockname,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    54
            releasefn=self.releasefn,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    55
            acquirefn=self.acquirefn,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    56
            *args,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    57
            **kwargs
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    58
        )
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    59
        l.postrelease.append(self.postreleasefn)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    60
        return l
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    61
26321
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
    62
    def acquirefn(self):
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
    63
        self._acquirecalled = True
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
    64
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    65
    def releasefn(self):
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    66
        self._releasecalled = True
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    67
43778
888bd39ed555 lock: pass "success" boolean to _afterlock callbacks
Kyle Lippincott <spectral@google.com>
parents: 43076
diff changeset
    68
    def postreleasefn(self, success):
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    69
        self._postreleasecalled = True
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    70
26321
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
    71
    def assertacquirecalled(self, called):
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
    72
        self._testcase.assertEqual(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    73
            self._acquirecalled,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    74
            called,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    75
            'expected acquire to be %s but was actually %s'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    76
            % (self._tocalled(called), self._tocalled(self._acquirecalled),),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    77
        )
26321
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
    78
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
    79
    def resetacquirefn(self):
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
    80
        self._acquirecalled = False
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
    81
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    82
    def assertreleasecalled(self, called):
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    83
        self._testcase.assertEqual(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    84
            self._releasecalled,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    85
            called,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    86
            'expected release to be %s but was actually %s'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    87
            % (self._tocalled(called), self._tocalled(self._releasecalled),),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    88
        )
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    89
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    90
    def assertpostreleasecalled(self, called):
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    91
        self._testcase.assertEqual(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    92
            self._postreleasecalled,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    93
            called,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    94
            'expected postrelease to be %s but was actually %s'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    95
            % (
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    96
                self._tocalled(called),
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
    97
                self._tocalled(self._postreleasecalled),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    98
            ),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
    99
        )
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   100
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   101
    def assertlockexists(self, exists):
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   102
        actual = self.vfs.lexists(testlockname)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   103
        self._testcase.assertEqual(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
   104
            actual,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
   105
            exists,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
   106
            'expected lock to %s but actually did %s'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
   107
            % (self._toexists(exists), self._toexists(actual),),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
   108
        )
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   109
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   110
    def _tocalled(self, called):
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   111
        if called:
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   112
            return 'called'
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   113
        else:
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   114
            return 'not called'
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   115
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   116
    def _toexists(self, exists):
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   117
        if exists:
26381
94dc10834b79 test-lock.py: copy-edit assertions about file existing
Siddharth Agarwal <sid0@fb.com>
parents: 26321
diff changeset
   118
            return 'exist'
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   119
        else:
26381
94dc10834b79 test-lock.py: copy-edit assertions about file existing
Siddharth Agarwal <sid0@fb.com>
parents: 26321
diff changeset
   120
            return 'not exist'
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   121
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
   122
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   123
class testlock(unittest.TestCase):
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   124
    def testlock(self):
39948
5ee3146c1b20 py3: byteify test-lock.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 34726
diff changeset
   125
        state = teststate(self, tempfile.mkdtemp(dir=encoding.getcwd()))
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   126
        lock = state.makelock()
26321
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
   127
        state.assertacquirecalled(True)
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   128
        lock.release()
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   129
        state.assertreleasecalled(True)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   130
        state.assertpostreleasecalled(True)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   131
        state.assertlockexists(False)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   132
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   133
    def testrecursivelock(self):
39948
5ee3146c1b20 py3: byteify test-lock.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 34726
diff changeset
   134
        state = teststate(self, tempfile.mkdtemp(dir=encoding.getcwd()))
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   135
        lock = state.makelock()
26321
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
   136
        state.assertacquirecalled(True)
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
   137
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
   138
        state.resetacquirefn()
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   139
        lock.lock()
26321
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
   140
        # recursive lock should not call acquirefn again
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
   141
        state.assertacquirecalled(False)
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
   142
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
   143
        lock.release()  # brings lock refcount down from 2 to 1
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   144
        state.assertreleasecalled(False)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   145
        state.assertpostreleasecalled(False)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   146
        state.assertlockexists(True)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   147
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
   148
        lock.release()  # releases the lock
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   149
        state.assertreleasecalled(True)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   150
        state.assertpostreleasecalled(True)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   151
        state.assertlockexists(False)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   152
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   153
    def testlockfork(self):
39948
5ee3146c1b20 py3: byteify test-lock.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 34726
diff changeset
   154
        state = teststate(self, tempfile.mkdtemp(dir=encoding.getcwd()))
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   155
        lock = state.makelock()
26321
db4c192cb9b3 lock: move acquirefn call to inside the lock
Siddharth Agarwal <sid0@fb.com>
parents: 26289
diff changeset
   156
        state.assertacquirecalled(True)
26386
146cccdb282b test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents: 26385
diff changeset
   157
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   158
        # fake a fork
41481
5880b4e762cd tests: perform a shallow copy instead of a deep copy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40204
diff changeset
   159
        forklock = copy.copy(lock)
26386
146cccdb282b test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents: 26385
diff changeset
   160
        forklock._pidoffset = 1
146cccdb282b test-lock.py: fix testing for forks
Siddharth Agarwal <sid0@fb.com>
parents: 26385
diff changeset
   161
        forklock.release()
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   162
        state.assertreleasecalled(False)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   163
        state.assertpostreleasecalled(False)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   164
        state.assertlockexists(True)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   165
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   166
        # release the actual lock
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   167
        lock.release()
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   168
        state.assertreleasecalled(True)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   169
        state.assertpostreleasecalled(True)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   170
        state.assertlockexists(False)
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   171
26387
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   172
    def testinheritlock(self):
39948
5ee3146c1b20 py3: byteify test-lock.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 34726
diff changeset
   173
        d = tempfile.mkdtemp(dir=encoding.getcwd())
26387
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   174
        parentstate = teststate(self, d)
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   175
        parentlock = parentstate.makelock()
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   176
        parentstate.assertacquirecalled(True)
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   177
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   178
        # set up lock inheritance
26473
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   179
        with parentlock.inherit() as lockname:
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   180
            parentstate.assertreleasecalled(True)
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   181
            parentstate.assertpostreleasecalled(False)
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   182
            parentstate.assertlockexists(True)
26387
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   183
26473
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   184
            childstate = teststate(self, d, pidoffset=1)
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   185
            childlock = childstate.makelock(parentlock=lockname)
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   186
            childstate.assertacquirecalled(True)
26387
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   187
26473
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   188
            childlock.release()
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   189
            childstate.assertreleasecalled(True)
26474
431094a3b21f lock.release: don't call postrelease functions for inherited locks
Siddharth Agarwal <sid0@fb.com>
parents: 26473
diff changeset
   190
            childstate.assertpostreleasecalled(False)
26473
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   191
            childstate.assertlockexists(True)
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   192
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   193
            parentstate.resetacquirefn()
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   194
26387
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   195
        parentstate.assertacquirecalled(True)
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   196
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   197
        parentlock.release()
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   198
        parentstate.assertreleasecalled(True)
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   199
        parentstate.assertpostreleasecalled(True)
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   200
        parentstate.assertlockexists(False)
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   201
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   202
    def testmultilock(self):
39948
5ee3146c1b20 py3: byteify test-lock.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 34726
diff changeset
   203
        d = tempfile.mkdtemp(dir=encoding.getcwd())
26387
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   204
        state0 = teststate(self, d)
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   205
        lock0 = state0.makelock()
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   206
        state0.assertacquirecalled(True)
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   207
26473
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   208
        with lock0.inherit() as lock0name:
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   209
            state0.assertreleasecalled(True)
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   210
            state0.assertpostreleasecalled(False)
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   211
            state0.assertlockexists(True)
26387
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   212
26473
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   213
            state1 = teststate(self, d, pidoffset=1)
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   214
            lock1 = state1.makelock(parentlock=lock0name)
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   215
            state1.assertacquirecalled(True)
26387
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   216
26473
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   217
            # from within lock1, acquire another lock
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   218
            with lock1.inherit() as lock1name:
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   219
                # since the file on disk is lock0's this should have the same
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   220
                # name
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   221
                self.assertEqual(lock0name, lock1name)
26387
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   222
26473
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   223
                state2 = teststate(self, d, pidoffset=2)
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   224
                lock2 = state2.makelock(parentlock=lock1name)
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   225
                state2.assertacquirecalled(True)
26387
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   226
26473
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   227
                lock2.release()
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   228
                state2.assertreleasecalled(True)
26474
431094a3b21f lock.release: don't call postrelease functions for inherited locks
Siddharth Agarwal <sid0@fb.com>
parents: 26473
diff changeset
   229
                state2.assertpostreleasecalled(False)
26473
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   230
                state2.assertlockexists(True)
26387
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   231
26473
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   232
                state1.resetacquirefn()
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   233
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   234
            state1.assertacquirecalled(True)
26387
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   235
26473
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   236
            lock1.release()
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   237
            state1.assertreleasecalled(True)
26474
431094a3b21f lock.release: don't call postrelease functions for inherited locks
Siddharth Agarwal <sid0@fb.com>
parents: 26473
diff changeset
   238
            state1.assertpostreleasecalled(False)
26473
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   239
            state1.assertlockexists(True)
26387
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   240
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   241
        lock0.release()
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   242
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   243
    def testinheritlockfork(self):
39948
5ee3146c1b20 py3: byteify test-lock.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 34726
diff changeset
   244
        d = tempfile.mkdtemp(dir=encoding.getcwd())
26387
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   245
        parentstate = teststate(self, d)
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   246
        parentlock = parentstate.makelock()
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   247
        parentstate.assertacquirecalled(True)
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   248
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   249
        # set up lock inheritance
26473
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   250
        with parentlock.inherit() as lockname:
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   251
            childstate = teststate(self, d, pidoffset=1)
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   252
            childlock = childstate.makelock(parentlock=lockname)
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   253
            childstate.assertacquirecalled(True)
26387
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   254
26473
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   255
            # fork the child lock
41481
5880b4e762cd tests: perform a shallow copy instead of a deep copy
Gregory Szorc <gregory.szorc@gmail.com>
parents: 40204
diff changeset
   256
            forkchildlock = copy.copy(childlock)
26473
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   257
            forkchildlock._pidoffset += 1
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   258
            forkchildlock.release()
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   259
            childstate.assertreleasecalled(False)
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   260
            childstate.assertpostreleasecalled(False)
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   261
            childstate.assertlockexists(True)
26387
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   262
26473
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   263
            # release the child lock
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   264
            childlock.release()
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   265
            childstate.assertreleasecalled(True)
26474
431094a3b21f lock.release: don't call postrelease functions for inherited locks
Siddharth Agarwal <sid0@fb.com>
parents: 26473
diff changeset
   266
            childstate.assertpostreleasecalled(False)
26473
5f94e64f182c lock: turn prepinherit/reacquire into a single context manager
Siddharth Agarwal <sid0@fb.com>
parents: 26387
diff changeset
   267
            childstate.assertlockexists(True)
26387
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   268
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   269
        parentlock.release()
e16f80f89a29 lock: recognize parent locks while acquiring
Siddharth Agarwal <sid0@fb.com>
parents: 26386
diff changeset
   270
26498
e8564e04382d lock: add a way to prevent locks from being inherited
Siddharth Agarwal <sid0@fb.com>
parents: 26474
diff changeset
   271
    def testinheritcheck(self):
39948
5ee3146c1b20 py3: byteify test-lock.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 34726
diff changeset
   272
        d = tempfile.mkdtemp(dir=encoding.getcwd())
26498
e8564e04382d lock: add a way to prevent locks from being inherited
Siddharth Agarwal <sid0@fb.com>
parents: 26474
diff changeset
   273
        state = teststate(self, d)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
   274
26498
e8564e04382d lock: add a way to prevent locks from being inherited
Siddharth Agarwal <sid0@fb.com>
parents: 26474
diff changeset
   275
        def check():
e8564e04382d lock: add a way to prevent locks from being inherited
Siddharth Agarwal <sid0@fb.com>
parents: 26474
diff changeset
   276
            raise error.LockInheritanceContractViolation('check failed')
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
   277
26498
e8564e04382d lock: add a way to prevent locks from being inherited
Siddharth Agarwal <sid0@fb.com>
parents: 26474
diff changeset
   278
        lock = state.makelock(inheritchecker=check)
e8564e04382d lock: add a way to prevent locks from being inherited
Siddharth Agarwal <sid0@fb.com>
parents: 26474
diff changeset
   279
        state.assertacquirecalled(True)
e8564e04382d lock: add a way to prevent locks from being inherited
Siddharth Agarwal <sid0@fb.com>
parents: 26474
diff changeset
   280
32279
68c43a416585 tests: use context manager form of assertRaises
Gregory Szorc <gregory.szorc@gmail.com>
parents: 32088
diff changeset
   281
        with self.assertRaises(error.LockInheritanceContractViolation):
26499
e72b62b154b0 localrepo: prevent wlock from being inherited when a transaction is running
Siddharth Agarwal <sid0@fb.com>
parents: 26498
diff changeset
   282
            with lock.inherit():
26498
e8564e04382d lock: add a way to prevent locks from being inherited
Siddharth Agarwal <sid0@fb.com>
parents: 26474
diff changeset
   283
                pass
e8564e04382d lock: add a way to prevent locks from being inherited
Siddharth Agarwal <sid0@fb.com>
parents: 26474
diff changeset
   284
e8564e04382d lock: add a way to prevent locks from being inherited
Siddharth Agarwal <sid0@fb.com>
parents: 26474
diff changeset
   285
        lock.release()
e8564e04382d lock: add a way to prevent locks from being inherited
Siddharth Agarwal <sid0@fb.com>
parents: 26474
diff changeset
   286
32088
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
   287
    def testfrequentlockunlock(self):
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
   288
        """This tests whether lock acquisition fails as expected, even if
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
   289
        (1) lock can't be acquired (makelock fails by EEXIST), and
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
   290
        (2) locker info can't be read in (readlock fails by ENOENT) while
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
   291
        retrying 5 times.
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
   292
        """
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
   293
39948
5ee3146c1b20 py3: byteify test-lock.py
Matt Harbison <matt_harbison@yahoo.com>
parents: 34726
diff changeset
   294
        d = tempfile.mkdtemp(dir=encoding.getcwd())
32088
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
   295
        state = teststate(self, d)
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
   296
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
   297
        def emulatefrequentlock(*args):
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
   298
            raise OSError(errno.EEXIST, "File exists")
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
   299
32088
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
   300
        def emulatefrequentunlock(*args):
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
   301
            raise OSError(errno.ENOENT, "No such file or directory")
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
   302
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
   303
        state.vfs.makelock = emulatefrequentlock
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
   304
        state.vfs.readlock = emulatefrequentunlock
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
   305
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
   306
        try:
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
   307
            state.makelock(timeout=0)
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
   308
            self.fail("unexpected lock acquisition")
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
   309
        except error.LockHeld as why:
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
   310
            self.assertTrue(why.errno == errno.ETIMEDOUT)
41482
b58d608ec6a0 tests: compare against a bytes in test-lock.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 41481
diff changeset
   311
            self.assertTrue(why.locker == b"")
32088
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
   312
            state.assertlockexists(False)
0d892d820a51 lock: avoid unintentional lock acquisition at failure of readlock
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents: 31249
diff changeset
   313
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41482
diff changeset
   314
26289
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   315
if __name__ == '__main__':
c4b667a7a51d tests: add unit tests for locking code
Siddharth Agarwal <sid0@fb.com>
parents:
diff changeset
   316
    silenttestrunner.main(__name__)