summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark McLoughlin <markmc@redhat.com>2013-03-22 10:14:48 +0000
committerMark McLoughlin <markmc@redhat.com>2013-03-22 10:14:48 +0000
commitf689e1cb5cfe9b43b3e2fec0e28b1ce80a0cccae (patch)
treede1ec443f0daf7a01dadf25ac54f52ee7947efa3
parentbf70726d4228ebf3e65aa75b8f0ca6cab5e1e159 (diff)
downloadoslo-f689e1cb5cfe9b43b3e2fec0e28b1ce80a0cccae.tar.gz
oslo-f689e1cb5cfe9b43b3e2fec0e28b1ce80a0cccae.tar.xz
oslo-f689e1cb5cfe9b43b3e2fec0e28b1ce80a0cccae.zip
lockutils: improve the external locks test
Use a separate directory for the lockutils external locks and for the test's flocks which are used to check that serialization is actually occurring. By using the same directory, we can't test the code in lockutils which auto-creates this directory because the dir gets deleted by a process by lockutils cleanup while another directory is using it for flocks. Also, assume the the handles directory has been created in the parent rather than having each child attempt to create it. Related, add a try/finally block so that when a child process throws an exception it immediately exits rather than deleting the temporary directories created by the parent. Change-Id: I32d7e8e05fb3f22cf38fa586f8bc97646c83f182
-rw-r--r--tests/unit/test_lockutils.py24
1 files changed, 13 insertions, 11 deletions
diff --git a/tests/unit/test_lockutils.py b/tests/unit/test_lockutils.py
index 685b0b8..e1f0375 100644
--- a/tests/unit/test_lockutils.py
+++ b/tests/unit/test_lockutils.py
@@ -127,18 +127,15 @@ class LockTestCase(utils.BaseTestCase):
def test_synchronized_externally(self):
"""We can lock across multiple processes"""
- tempdir = tempfile.mkdtemp()
- self.config(lock_path=tempdir)
+ lock_dir = tempfile.mkdtemp()
+ self.config(lock_path=lock_dir)
@lockutils.synchronized('external', 'test-', external=True)
- def lock_files(tempdir):
- if not os.path.exists(tempdir):
- os.makedirs(tempdir)
-
+ def lock_files(handles_dir):
# Open some files we can use for locking
handles = []
for n in range(50):
- path = os.path.join(tempdir, ('file-%s' % n))
+ path = os.path.join(handles_dir, ('file-%s' % n))
handles.append(open(path, 'w'))
# Loop over all the handles and try locking the file
@@ -159,6 +156,7 @@ class LockTestCase(utils.BaseTestCase):
# Check if we were able to open all files
self.assertEqual(50, count)
+ handles_dir = tempfile.mkdtemp()
try:
children = []
for n in range(50):
@@ -166,13 +164,17 @@ class LockTestCase(utils.BaseTestCase):
if pid:
children.append(pid)
else:
- lock_files(tempdir)
- os._exit(0)
+ try:
+ lock_files(handles_dir)
+ finally:
+ os._exit(0)
for i, child in enumerate(children):
(pid, status) = os.waitpid(child, 0)
if pid:
self.assertEqual(0, status)
finally:
- if os.path.exists(tempdir):
- shutil.rmtree(tempdir, ignore_errors=True)
+ if os.path.exists(handles_dir):
+ shutil.rmtree(handles_dir, ignore_errors=True)
+ if os.path.exists(lock_dir):
+ shutil.rmtree(lock_dir, ignore_errors=True)