Adam Litke has uploaded a new change for review. Change subject: storage: add a context manager for the domainLock ......................................................................
storage: add a context manager for the domainLock Change-Id: Id7b831d4fe5a67f6998f31978f2399fdebdb3ceb Signed-off-by: Adam Litke <ali...@redhat.com> --- M tests/manifest_tests.py M vdsm/storage/sd.py 2 files changed, 50 insertions(+), 1 deletion(-) git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/72/50272/1 diff --git a/tests/manifest_tests.py b/tests/manifest_tests.py index cfb5ee7..1e0e18a 100644 --- a/tests/manifest_tests.py +++ b/tests/manifest_tests.py @@ -20,7 +20,7 @@ import os import uuid -from testlib import VdsmTestCase, namedTemporaryDir, make_file +from testlib import VdsmTestCase, namedTemporaryDir, make_file, recorded from monkeypatch import MonkeyPatchScope from storagefakelib import FakeLVM from storagetestlib import make_filesd_manifest, make_blocksd, make_file_volume @@ -169,3 +169,43 @@ with manifest.acquireVolumeMetadataSlot(None, 1): acquired = manifest._lvTagMetaSlotLock.acquire(False) self.assertFalse(acquired) + + +class FakeStorageDomainManifest(sd.StorageDomainManifest): + def __init__(self): + pass + + @recorded + def acquireDomainLock(self, host_id): + pass + + @recorded + def releaseDomainLock(self): + pass + + +class FakeDomainManifestTests(VdsmTestCase): + + def test_domainlock_contextmanager(self): + expected_calls = [] + manifest = FakeStorageDomainManifest() + with manifest.domain_lock(1): + expected_calls.append(("acquireDomainLock", (1,), {})) + self.assertEqual(manifest.__calls__, expected_calls) + expected_calls.append(("releaseDomainLock", (), {})) + self.assertEqual(manifest.__calls__, expected_calls) + + def test_domainlock_contextmanager_exception(self): + class InjectedFailure(Exception): + pass + + expected_calls = [] + manifest = FakeStorageDomainManifest() + try: + with manifest.domain_lock(1): + expected_calls.append(("acquireDomainLock", (1,), {})) + self.assertEqual(manifest.__calls__, expected_calls) + raise InjectedFailure() + except InjectedFailure: + expected_calls.append(("releaseDomainLock", (), {})) + self.assertEqual(manifest.__calls__, expected_calls) diff --git a/vdsm/storage/sd.py b/vdsm/storage/sd.py index 68fa285..807d92a 100644 --- a/vdsm/storage/sd.py +++ b/vdsm/storage/sd.py @@ -24,6 +24,7 @@ import threading from collections import namedtuple import codecs +from contextlib import contextmanager import image import storage_exception as se @@ -422,6 +423,14 @@ def releaseDomainLock(self): self._domainLock.release() + @contextmanager + def domain_lock(self, host_id): + self.acquireDomainLock(host_id) + try: + yield + finally: + self.releaseDomainLock() + def inquireDomainLock(self): return self._domainLock.inquire() -- To view, visit https://gerrit.ovirt.org/50272 To unsubscribe, visit https://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Id7b831d4fe5a67f6998f31978f2399fdebdb3ceb Gerrit-PatchSet: 1 Gerrit-Project: vdsm Gerrit-Branch: master Gerrit-Owner: Adam Litke <ali...@redhat.com> _______________________________________________ vdsm-patches mailing list vdsm-patches@lists.fedorahosted.org https://lists.fedorahosted.org/mailman/listinfo/vdsm-patches