From 20faa758b9759e702b6c5272f0ac64a14400d748 Mon Sep 17 00:00:00 2001 From: Soren Hansen Date: Tue, 22 Mar 2011 10:35:43 +0100 Subject: [PATCH] Make synchronized support both external (file based) locks as well as internal (semaphore based) locks. Attempt to make it native thread safe at the expense of never cleaning up semaphores. --- nova/tests/test_misc.py | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/nova/tests/test_misc.py b/nova/tests/test_misc.py index 1fbaf304..961499a6 100644 --- a/nova/tests/test_misc.py +++ b/nova/tests/test_misc.py @@ -16,8 +16,12 @@ import errno import os +import random import select +from eventlet import greenpool +from eventlet import greenthread + from nova import test from nova.utils import parse_mailmap, str_dict_replace, synchronized @@ -72,11 +76,37 @@ class LockTestCase(test.TestCase): self.assertEquals(foo.__name__, 'foo', "Wrapped function's name " "got mangled") - def test_synchronized(self): + def test_synchronized_internally(self): + """We can lock across multiple green threads""" + seen_threads = list() + @synchronized('testlock', external=False) + def f(id): + 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)) + + for thread in threads: + thread.wait() + + self.assertEquals(len(seen_threads), 100) + # Looking at the seen threads, split it into chunks of 10, and verify + # that the last 9 match the first in each chunk. + for i in range(10): + for j in range(9): + self.assertEquals(seen_threads[i*10], seen_threads[i*10+1+j]) + + + def test_synchronized_externally(self): + """We can lock across multiple processes""" rpipe1, wpipe1 = os.pipe() rpipe2, wpipe2 = os.pipe() - @synchronized('testlock') + @synchronized('testlock', external=True) def f(rpipe, wpipe): try: os.write(wpipe, "foo")