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
This commit is contained in:
parent
82cb089eb2
commit
74fc415f44
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue