From b72e1ef1e8d874d47ec9b44d37fdf5de851ecd8f Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Mon, 26 Jul 2010 12:49:21 -0700 Subject: [PATCH 1/2] class based singleton for SharedPool --- nova/process.py | 13 +++++++------ nova/tests/process_unittest.py | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/nova/process.py b/nova/process.py index 8ecef158..2dc56372 100644 --- a/nova/process.py +++ b/nova/process.py @@ -205,12 +205,13 @@ class ProcessPool(object): self._pool.release() return rv -_instance = None -def SharedPool(): - global _instance - if _instance is None: - _instance = ProcessPool() - return _instance +class SharedPool(object): + _instance = None + def __init__(self): + if SharedPool._instance is None: + self.__class__._instance = ProcessPool() + def __getattr__(self, key): + return getattr(self._instance, key) def simple_execute(cmd, **kwargs): return SharedPool().simple_execute(cmd, **kwargs) diff --git a/nova/tests/process_unittest.py b/nova/tests/process_unittest.py index c96bb591..75187e1f 100644 --- a/nova/tests/process_unittest.py +++ b/nova/tests/process_unittest.py @@ -118,7 +118,7 @@ class ProcessTestCase(test.TrialTestCase): def test_shared_pool_is_singleton(self): pool1 = process.SharedPool() pool2 = process.SharedPool() - self.assert_(id(pool1) == id(pool2)) + self.assertEqual(id(pool1._instance), id(pool2._instance)) def test_shared_pool_works_as_singleton(self): d1 = process.simple_execute('sleep 1') From 7265888587c8ff29e5535d664b7badfd23d7e98f Mon Sep 17 00:00:00 2001 From: Ewan Mellor Date: Thu, 29 Jul 2010 09:08:31 +0100 Subject: [PATCH 2/2] Replace the second singleton unit test, lost during a merge. --- nova/tests/process_unittest.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/nova/tests/process_unittest.py b/nova/tests/process_unittest.py index f5f304aa..75187e1f 100644 --- a/nova/tests/process_unittest.py +++ b/nova/tests/process_unittest.py @@ -120,3 +120,10 @@ class ProcessTestCase(test.TrialTestCase): pool2 = process.SharedPool() self.assertEqual(id(pool1._instance), id(pool2._instance)) + def test_shared_pool_works_as_singleton(self): + d1 = process.simple_execute('sleep 1') + d2 = process.simple_execute('sleep 0.005') + # lp609749: would have failed with + # exceptions.AssertionError: Someone released me too many times: + # too many tokens! + return d1