Merge "lockutils: split tests and run in Python 3"

This commit is contained in:
Jenkins 2014-07-25 23:09:57 +00:00 committed by Gerrit Code Review
commit d92aeeb5da
2 changed files with 80 additions and 51 deletions

View File

@ -22,12 +22,9 @@ import tempfile
import threading
import time
import eventlet
eventlet.monkey_patch()
from eventlet import greenpool
from eventlet import greenthread
from oslo.config import cfg
from oslotest import base as test_base
import six
from six import moves
from openstack.common.fixture import config
@ -35,42 +32,6 @@ from openstack.common.fixture import lockutils as fixtures
from openstack.common import lockutils
class TestFileLocks(test_base.BaseTestCase):
def test_concurrent_green_lock_succeeds(self):
"""Verify spawn_n greenthreads with two locks run concurrently."""
tmpdir = tempfile.mkdtemp()
try:
self.completed = False
def locka(wait):
a = lockutils.InterProcessLock(os.path.join(tmpdir, 'a'))
with a:
wait.wait()
self.completed = True
def lockb(wait):
b = lockutils.InterProcessLock(os.path.join(tmpdir, 'b'))
with b:
wait.wait()
wait1 = eventlet.event.Event()
wait2 = eventlet.event.Event()
pool = greenpool.GreenPool()
pool.spawn_n(locka, wait1)
pool.spawn_n(lockb, wait2)
wait2.send()
eventlet.sleep(0)
wait1.send()
pool.waitall()
self.assertTrue(self.completed)
finally:
if os.path.exists(tmpdir):
shutil.rmtree(tmpdir)
class LockTestCase(test_base.BaseTestCase):
def setUp(self):
@ -124,7 +85,7 @@ class LockTestCase(test_base.BaseTestCase):
self.assertNotEqual(0, acquired_children)
def test_lock_internally(self):
"""We can lock across multiple green threads."""
"""We can lock across multiple threads."""
saved_sem_num = len(lockutils._semaphores)
seen_threads = list()
@ -132,15 +93,15 @@ class LockTestCase(test_base.BaseTestCase):
with lockutils.lock('testlock2', 'test-', external=False):
for x in range(10):
seen_threads.append(_id)
greenthread.sleep(0)
threads = []
pool = greenpool.GreenPool(10)
for i in range(10):
threads.append(pool.spawn(f, i))
thread = threading.Thread(target=f, args=(i,))
threads.append(thread)
thread.start()
for thread in threads:
thread.wait()
thread.join()
self.assertEqual(len(seen_threads), 100)
# Looking at the seen threads, split it into chunks of 10, and verify
@ -299,7 +260,10 @@ class LockTestCase(test_base.BaseTestCase):
# Note(flaper87): Lock is not external, which means
# a semaphore will be yielded
with lockutils.lock("test") as sem:
self.assertTrue(isinstance(sem, threading._Semaphore))
if six.PY2:
self.assertTrue(isinstance(sem, threading._Semaphore))
else:
self.assertTrue(isinstance(sem, threading.Semaphore))
# NOTE(flaper87): Lock is external so an InterProcessLock
# will be yielded.
@ -322,7 +286,10 @@ class LockTestCase(test_base.BaseTestCase):
try:
with lockutils.lock("test") as sem:
self.assertTrue(isinstance(sem, threading._Semaphore))
if six.PY2:
self.assertTrue(isinstance(sem, threading._Semaphore))
else:
self.assertTrue(isinstance(sem, threading.Semaphore))
with lockutils.lock("test2", external=True) as lock:
self.assertTrue(lock.exists())
@ -448,16 +415,18 @@ class FileBasedLockingTestCase(test_base.BaseTestCase):
def other(param):
foo(param)
thread = eventlet.spawn(other, 'other')
thread = threading.Thread(target=other, args=('other',))
thread.start()
# Make sure the other thread grabs the lock
start = time.time()
while not os.path.exists(os.path.join(self.lock_dir, 'foo')):
if time.time() - start > 5:
self.fail('Timed out waiting for thread to grab lock')
time.sleep(0)
thread1 = eventlet.spawn(other, 'main')
thread1.wait()
thread.wait()
thread1 = threading.Thread(target=other, args=('main',))
thread1.start()
thread1.join()
thread.join()
self.assertEqual(call_list, ['other', 'other', 'main', 'main'])

View File

@ -0,0 +1,60 @@
# Copyright 2011 Justin Santa Barbara
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import shutil
import tempfile
import eventlet
eventlet.monkey_patch()
from eventlet import greenpool
from oslotest import base as test_base
from openstack.common import lockutils
class TestFileLocks(test_base.BaseTestCase):
def test_concurrent_green_lock_succeeds(self):
"""Verify spawn_n greenthreads with two locks run concurrently."""
tmpdir = tempfile.mkdtemp()
try:
self.completed = False
def locka(wait):
a = lockutils.InterProcessLock(os.path.join(tmpdir, 'a'))
with a:
wait.wait()
self.completed = True
def lockb(wait):
b = lockutils.InterProcessLock(os.path.join(tmpdir, 'b'))
with b:
wait.wait()
wait1 = eventlet.event.Event()
wait2 = eventlet.event.Event()
pool = greenpool.GreenPool()
pool.spawn_n(locka, wait1)
pool.spawn_n(lockb, wait2)
wait2.send()
eventlet.sleep(0)
wait1.send()
pool.waitall()
self.assertTrue(self.completed)
finally:
if os.path.exists(tmpdir):
shutil.rmtree(tmpdir)