Some test cleanup

- Tests are based on oslotest.base.BaseTestCase, which uses the
  NestedTempFile fixture, which uses the TempDir fixture, which adds a
  cleanup routine to remove the base temporary directory it creates.
  There's therefore no need for tests to clean up this directory, so all
  the code that does that is removed.
- The eventlet incarnation of tests was trying to make use of the `env`
  external without whitelisting it, resulting in an ugly red deprecation
  warning. This commit adds `env` to whitelist_externals in [testenv].
- A handful of typos in the Beowulf quote are corrected.

Change-Id: I91cc52e00e0a918dadd2a3a771bd322b0f165ed2
This commit is contained in:
Eric Fried 2019-09-17 15:54:42 -05:00
parent 610df387b6
commit fe86f5e4ae
4 changed files with 84 additions and 138 deletions

View File

@ -15,7 +15,6 @@
import collections import collections
import multiprocessing import multiprocessing
import os import os
import shutil
import signal import signal
import subprocess import subprocess
import sys import sys
@ -188,65 +187,45 @@ class LockTestCase(test_base.BaseTestCase):
def test_nested_synchronized_external_works(self): def test_nested_synchronized_external_works(self):
"""We can nest external syncs.""" """We can nest external syncs."""
tempdir = tempfile.mkdtemp() self.config(lock_path=tempfile.mkdtemp(), group='oslo_concurrency')
try: sentinel = object()
self.config(lock_path=tempdir, group='oslo_concurrency')
sentinel = object()
@lockutils.synchronized('testlock1', 'test-', external=True) @lockutils.synchronized('testlock1', 'test-', external=True)
def outer_lock(): def outer_lock():
@lockutils.synchronized('testlock2', 'test-', external=True) @lockutils.synchronized('testlock2', 'test-', external=True)
def inner_lock(): def inner_lock():
return sentinel return sentinel
return inner_lock() return inner_lock()
self.assertEqual(sentinel, outer_lock()) self.assertEqual(sentinel, outer_lock())
finally:
if os.path.exists(tempdir):
shutil.rmtree(tempdir)
def _do_test_lock_externally(self): def _do_test_lock_externally(self):
"""We can lock across multiple processes.""" """We can lock across multiple processes."""
handles_dir = tempfile.mkdtemp() children = []
try: for n in range(50):
children = [] queue = multiprocessing.Queue()
for n in range(50): proc = multiprocessing.Process(
queue = multiprocessing.Queue() target=lock_files,
proc = multiprocessing.Process( args=(tempfile.mkdtemp(), queue))
target=lock_files, proc.start()
args=(handles_dir, queue)) children.append((proc, queue))
proc.start() for child, queue in children:
children.append((proc, queue)) child.join()
for child, queue in children: count = queue.get(block=False)
child.join() self.assertEqual(50, count)
count = queue.get(block=False)
self.assertEqual(50, count)
finally:
if os.path.exists(handles_dir):
shutil.rmtree(handles_dir, ignore_errors=True)
def test_lock_externally(self): def test_lock_externally(self):
lock_dir = tempfile.mkdtemp() self.config(lock_path=tempfile.mkdtemp(), group='oslo_concurrency')
self.config(lock_path=lock_dir, group='oslo_concurrency')
try: self._do_test_lock_externally()
self._do_test_lock_externally()
finally:
if os.path.exists(lock_dir):
shutil.rmtree(lock_dir, ignore_errors=True)
def test_lock_externally_lock_dir_not_exist(self): def test_lock_externally_lock_dir_not_exist(self):
lock_dir = tempfile.mkdtemp() lock_dir = tempfile.mkdtemp()
os.rmdir(lock_dir) os.rmdir(lock_dir)
self.config(lock_path=lock_dir, group='oslo_concurrency') self.config(lock_path=lock_dir, group='oslo_concurrency')
try: self._do_test_lock_externally()
self._do_test_lock_externally()
finally:
if os.path.exists(lock_dir):
shutil.rmtree(lock_dir, ignore_errors=True)
def test_synchronized_with_prefix(self): def test_synchronized_with_prefix(self):
lock_name = 'mylock' lock_name = 'mylock'
@ -264,88 +243,64 @@ class LockTestCase(test_base.BaseTestCase):
self.assertTrue(bar(lock_dir, lock_pfix, lock_name)) self.assertTrue(bar(lock_dir, lock_pfix, lock_name))
def test_synchronized_without_prefix(self): def test_synchronized_without_prefix(self):
lock_dir = tempfile.mkdtemp() self.config(lock_path=tempfile.mkdtemp(), group='oslo_concurrency')
self.config(lock_path=lock_dir, group='oslo_concurrency')
@lockutils.synchronized('lock', external=True) @lockutils.synchronized('lock', external=True)
def test_without_prefix(): def test_without_prefix():
# We can't check much # We can't check much
pass pass
try: test_without_prefix()
test_without_prefix()
finally:
if os.path.exists(lock_dir):
shutil.rmtree(lock_dir, ignore_errors=True)
def test_synchronized_prefix_without_hypen(self): def test_synchronized_prefix_without_hypen(self):
lock_dir = tempfile.mkdtemp() self.config(lock_path=tempfile.mkdtemp(), group='oslo_concurrency')
self.config(lock_path=lock_dir, group='oslo_concurrency')
@lockutils.synchronized('lock', 'hypen', True) @lockutils.synchronized('lock', 'hypen', True)
def test_without_hypen(): def test_without_hypen():
# We can't check much # We can't check much
pass pass
try: test_without_hypen()
test_without_hypen()
finally:
if os.path.exists(lock_dir):
shutil.rmtree(lock_dir, ignore_errors=True)
def test_contextlock(self): def test_contextlock(self):
lock_dir = tempfile.mkdtemp() self.config(lock_path=tempfile.mkdtemp(), group='oslo_concurrency')
self.config(lock_path=lock_dir, group='oslo_concurrency')
try: # Note(flaper87): Lock is not external, which means
# Note(flaper87): Lock is not external, which means # a semaphore will be yielded
# a semaphore will be yielded with lockutils.lock("test") as sem:
with lockutils.lock("test") as sem: if six.PY2:
if six.PY2: self.assertIsInstance(sem, threading._Semaphore)
self.assertIsInstance(sem, threading._Semaphore) else:
else: self.assertIsInstance(sem, threading.Semaphore)
self.assertIsInstance(sem, threading.Semaphore)
# NOTE(flaper87): Lock is external so an InterProcessLock # NOTE(flaper87): Lock is external so an InterProcessLock
# will be yielded. # will be yielded.
with lockutils.lock("test2", external=True) as lock: with lockutils.lock("test2", external=True) as lock:
self.assertTrue(lock.exists()) self.assertTrue(lock.exists())
with lockutils.lock("test1", with lockutils.lock("test1", external=True) as lock1:
external=True) as lock1: self.assertIsInstance(lock1, lockutils.InterProcessLock)
self.assertIsInstance(lock1,
lockutils.InterProcessLock)
finally:
if os.path.exists(lock_dir):
shutil.rmtree(lock_dir, ignore_errors=True)
def test_contextlock_unlocks(self): def test_contextlock_unlocks(self):
lock_dir = tempfile.mkdtemp() self.config(lock_path=tempfile.mkdtemp(), group='oslo_concurrency')
self.config(lock_path=lock_dir, group='oslo_concurrency')
sem = None with lockutils.lock("test") as sem:
if six.PY2:
self.assertIsInstance(sem, threading._Semaphore)
else:
self.assertIsInstance(sem, threading.Semaphore)
try: with lockutils.lock("test2", external=True) as lock:
with lockutils.lock("test") as sem: self.assertTrue(lock.exists())
if six.PY2:
self.assertIsInstance(sem, threading._Semaphore)
else:
self.assertIsInstance(sem, threading.Semaphore)
with lockutils.lock("test2", external=True) as lock:
self.assertTrue(lock.exists())
# NOTE(flaper87): Lock should be free
with lockutils.lock("test2", external=True) as lock:
self.assertTrue(lock.exists())
# NOTE(flaper87): Lock should be free # NOTE(flaper87): Lock should be free
# but semaphore should already exist. with lockutils.lock("test2", external=True) as lock:
with lockutils.lock("test") as sem2: self.assertTrue(lock.exists())
self.assertEqual(sem, sem2)
finally: # NOTE(flaper87): Lock should be free
if os.path.exists(lock_dir): # but semaphore should already exist.
shutil.rmtree(lock_dir, ignore_errors=True) with lockutils.lock("test") as sem2:
self.assertEqual(sem, sem2)
def _test_remove_lock_external_file(self, lock_dir, use_external=False): def _test_remove_lock_external_file(self, lock_dir, use_external=False):
lock_name = 'mylock' lock_name = 'mylock'
@ -361,17 +316,13 @@ class LockTestCase(test_base.BaseTestCase):
for ent in os.listdir(lock_dir): for ent in os.listdir(lock_dir):
self.assertRaises(OSError, ent.startswith, lock_pfix) self.assertRaises(OSError, ent.startswith, lock_pfix)
if os.path.exists(lock_dir):
shutil.rmtree(lock_dir, ignore_errors=True)
def test_remove_lock_external_file(self): def test_remove_lock_external_file(self):
lock_dir = tempfile.mkdtemp() lock_dir = tempfile.mkdtemp()
self.config(lock_path=lock_dir, group='oslo_concurrency') self.config(lock_path=lock_dir, group='oslo_concurrency')
self._test_remove_lock_external_file(lock_dir) self._test_remove_lock_external_file(lock_dir)
def test_remove_lock_external_file_lock_path(self): def test_remove_lock_external_file_lock_path(self):
lock_dir = tempfile.mkdtemp() self._test_remove_lock_external_file(tempfile.mkdtemp(),
self._test_remove_lock_external_file(lock_dir,
use_external=True) use_external=True)
def test_no_slash_in_b64(self): def test_no_slash_in_b64(self):

View File

@ -13,7 +13,6 @@
# under the License. # under the License.
import os import os
import shutil
import tempfile import tempfile
import eventlet import eventlet
@ -28,32 +27,27 @@ class TestFileLocks(test_base.BaseTestCase):
def test_concurrent_green_lock_succeeds(self): def test_concurrent_green_lock_succeeds(self):
"""Verify spawn_n greenthreads with two locks run concurrently.""" """Verify spawn_n greenthreads with two locks run concurrently."""
tmpdir = tempfile.mkdtemp() tmpdir = tempfile.mkdtemp()
try: self.completed = False
self.completed = False
def locka(wait): def locka(wait):
a = lockutils.InterProcessLock(os.path.join(tmpdir, 'a')) a = lockutils.InterProcessLock(os.path.join(tmpdir, 'a'))
with a: with a:
wait.wait() wait.wait()
self.completed = True self.completed = True
def lockb(wait): def lockb(wait):
b = lockutils.InterProcessLock(os.path.join(tmpdir, 'b')) b = lockutils.InterProcessLock(os.path.join(tmpdir, 'b'))
with b: with b:
wait.wait() wait.wait()
wait1 = eventlet.event.Event() wait1 = eventlet.event.Event()
wait2 = eventlet.event.Event() wait2 = eventlet.event.Event()
pool = greenpool.GreenPool() pool = greenpool.GreenPool()
pool.spawn_n(locka, wait1) pool.spawn_n(locka, wait1)
pool.spawn_n(lockb, wait2) pool.spawn_n(lockb, wait2)
wait2.send() wait2.send()
eventlet.sleep(0) eventlet.sleep(0)
wait1.send() wait1.send()
pool.waitall() pool.waitall()
self.assertTrue(self.completed) self.assertTrue(self.completed)
finally:
if os.path.exists(tmpdir):
shutil.rmtree(tmpdir)

View File

@ -190,18 +190,18 @@ class ProcessExecutionErrorTest(test_base.BaseTestCase):
stdout = """ stdout = """
Lo, praise of the prowess of people-kings Lo, praise of the prowess of people-kings
of spear-armed Danes, in days long sped, of spear-armed Danes, in days long sped,
we have heard, and what honot the athelings won! we have heard, and what honor the athelings won!
Oft Scyld the Scefing from squadroned foes, Oft Scyld the Scefing from squadroned foes,
from many a tribe, the mead-bench tore, from many a tribe, the mead-bench tore,
awing the earls. Since erse he lay awing the earls. Since erst he lay
friendless, a foundling, fate repaid him: friendless, a foundling, fate repaid him:
for he waxed under welkin, in wealth he trove, for he waxed under welkin, in wealth he throve,
till before him the folk, both far and near, till before him the folk, both far and near,
who house by the whale-path, heard his mandate, who house by the whale-path, heard his mandate,
gabe him gits: a good king he! gave him gifts: a good king he!
To him an heir was afterward born, To him an heir was afterward born,
a son in his halls, whom heaven sent a son in his halls, whom heaven sent
to favor the fol, feeling their woe to favor the folk, feeling their woe
that erst they had lacked an earl for leader that erst they had lacked an earl for leader
so long a while; the Lord endowed him, so long a while; the Lord endowed him,
the Wielder of Wonder, with world's renown. the Wielder of Wonder, with world's renown.

View File

@ -9,6 +9,7 @@ deps =
-r{toxinidir}/test-requirements.txt -r{toxinidir}/test-requirements.txt
-r{toxinidir}/requirements.txt -r{toxinidir}/requirements.txt
# We want to support both vanilla stdlib and eventlet monkey patched # We want to support both vanilla stdlib and eventlet monkey patched
whitelist_externals = env
commands = commands =
lockutils-wrapper stestr run --slowest {posargs} lockutils-wrapper stestr run --slowest {posargs}
env TEST_EVENTLET=1 lockutils-wrapper stestr run --slowest {posargs} env TEST_EVENTLET=1 lockutils-wrapper stestr run --slowest {posargs}