Commit 73c25d35 authored by Michael Hanselmann's avatar Michael Hanselmann

Fix race condition in locking unittest

While writing unittests for the new lock monitor, I made a small mistake and
didn't synchronize acquiring locks properly. This patch takes care of this
issue by adding additional synchronization.
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 19b9ba9a
...@@ -1503,9 +1503,10 @@ class TestLockMonitor(_ThreadedTestCase): ...@@ -1503,9 +1503,10 @@ class TestLockMonitor(_ThreadedTestCase):
tlock.release() tlock.release()
# Test shared acquire # Test shared acquire
def _Acquire(lock, shared, ev): def _Acquire(lock, shared, ev, notify):
lock.acquire(shared=shared) lock.acquire(shared=shared)
try: try:
notify.set()
ev.wait() ev.wait()
finally: finally:
lock.release() lock.release()
...@@ -1513,21 +1514,38 @@ class TestLockMonitor(_ThreadedTestCase): ...@@ -1513,21 +1514,38 @@ class TestLockMonitor(_ThreadedTestCase):
for tlock1 in locks[::11]: for tlock1 in locks[::11]:
for tlock2 in locks[::-15]: for tlock2 in locks[::-15]:
if tlock2 == tlock1: if tlock2 == tlock1:
# Avoid deadlocks
continue continue
for tlock3 in locks[::10]: for tlock3 in locks[::10]:
if tlock3 == tlock2: if tlock3 in (tlock2, tlock1):
# Avoid deadlocks
continue continue
ev = threading.Event() releaseev = threading.Event()
# Acquire locks # Acquire locks
acquireev = []
tthreads1 = [] tthreads1 = []
for i in range(3): for i in range(3):
ev = threading.Event()
tthreads1.append(self._addThread(target=_Acquire, tthreads1.append(self._addThread(target=_Acquire,
args=(tlock1, 1, ev))) args=(tlock1, 1, releaseev, ev)))
tthread2 = self._addThread(target=_Acquire, args=(tlock2, 1, ev)) acquireev.append(ev)
tthread3 = self._addThread(target=_Acquire, args=(tlock3, 0, ev))
ev = threading.Event()
tthread2 = self._addThread(target=_Acquire,
args=(tlock2, 1, releaseev, ev))
acquireev.append(ev)
ev = threading.Event()
tthread3 = self._addThread(target=_Acquire,
args=(tlock3, 0, releaseev, ev))
acquireev.append(ev)
# Wait for all locks to be acquired
for i in acquireev:
i.wait()
# Check query result # Check query result
for (name, mode, owner) in self.lm.QueryLocks(["name", "mode", for (name, mode, owner) in self.lm.QueryLocks(["name", "mode",
...@@ -1552,7 +1570,7 @@ class TestLockMonitor(_ThreadedTestCase): ...@@ -1552,7 +1570,7 @@ class TestLockMonitor(_ThreadedTestCase):
self.assert_(owner is None) self.assert_(owner is None)
# Release locks again # Release locks again
ev.set() releaseev.set()
self._waitThreads() self._waitThreads()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment