Nir Soffer has uploaded a new change for review.

Change subject: tests: Introduce AssertingLock
......................................................................

tests: Introduce AssertingLock

When we want to test locking, we don't want to poke into object
internals and check the state of objects locks, or even worse, mock out
object lock.

This patch introduces the AssertingLock, which unlike regular locks,
never blocks when you try to acquire a lock somebody else is holding.
Instead, this lock raises AssertionError, failing the test.

Assuming that Foo object implements the context manager and use a lock,
we can verify this by:

    single_thread = AssertingLock()
    foo = Foo()

    def check():
        with foo:
            with single_thread:
                time.sleep(0.1)

Now we can run check in several threads; if foo is implemented
correctly, single thread will be able to enter the foo context. If foo
does not lock properly, multiple thread will enter this context, and
then fail when trying to enter the AssertingLock.

Turns out that in the SamplingMethodTests, I used a BoundedSemaphore for
this purpuse which is incorrect. Incorrect code can be hidden becuase
the testing threads would block instead of failing the tests. This code
uses now an AssertingLock instead.

Change-Id: I3e3aad4015e3410ce2f2b39bba84314cce5580b6
Signed-off-by: Nir Soffer <[email protected]>
---
M tests/miscTests.py
M tests/testlib.py
M tests/testlibTests.py
3 files changed, 35 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/38/30938/1

diff --git a/tests/miscTests.py b/tests/miscTests.py
index 91ef40c..a40afb0 100644
--- a/tests/miscTests.py
+++ b/tests/miscTests.py
@@ -26,6 +26,7 @@
 import signal
 import fcntl
 import errno
+from testlib import AssertingLock
 from testlib import VdsmTestCase as TestCaseBase
 from testlib import temporaryPath
 from testlib import namedTemporaryDir
@@ -1155,7 +1156,7 @@
 
     def setUp(self):
         # Will raise if more then one thread try to enter the sampling method
-        self.single_thread_allowed = threading.BoundedSemaphore(1)
+        self.single_thread_allowed = AssertingLock()
         self.entered_sampling_method = threading.Event()
         self.results = ['result-1', 'result-2']
 
diff --git a/tests/testlib.py b/tests/testlib.py
index 1e1f2b0..6fffb3c 100644
--- a/tests/testlib.py
+++ b/tests/testlib.py
@@ -26,6 +26,7 @@
 import shutil
 import sys
 import tempfile
+import threading
 from contextlib import contextmanager
 
 from nose import config
@@ -335,6 +336,22 @@
     return result[:_MAX_LENGTH] + ' [truncated]...'
 
 
+class AssertingLock(object):
+    """
+    Lock that raises when trying to acquire a locked lock.
+    """
+    def __init__(self):
+        self._lock = threading.Lock()
+
+    def __enter__(self):
+        if not self._lock.acquire(False):
+            raise AssertionError("Lock is already locked")
+        return self
+
+    def __exit__(self, *args):
+        self._lock.release()
+
+
 class VdsmTestRunner(core.TextTestRunner):
     def __init__(self, *args, **kwargs):
         core.TextTestRunner.__init__(self, *args, **kwargs)
diff --git a/tests/testlibTests.py b/tests/testlibTests.py
index 86e3069..0088fac 100644
--- a/tests/testlibTests.py
+++ b/tests/testlibTests.py
@@ -18,6 +18,7 @@
 # Refer to the README and COPYING files for full details of the license
 #
 
+from testlib import AssertingLock
 from testlib import VdsmTestCase
 
 
@@ -42,3 +43,18 @@
         def func():
             pass
         self.assertNotRaises(func)
+
+
+class AssertingLockTests(VdsmTestCase):
+
+    def test_free(self):
+        lock = AssertingLock()
+        with lock:
+            pass
+
+    def test_locked(self):
+        lock = AssertingLock()
+        with self.assertRaises(AssertionError):
+            with lock:
+                with lock:
+                    pass


-- 
To view, visit http://gerrit.ovirt.org/30938
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3e3aad4015e3410ce2f2b39bba84314cce5580b6
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Nir Soffer <[email protected]>
_______________________________________________
vdsm-patches mailing list
[email protected]
https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches

Reply via email to