Mike Bayer avatar Mike Bayer committed d40c41c

- correct and test the example RW mutex
- add tests in dbm impl that run without fcntl present, test on windows

Comments (0)

Files changed (3)

docs/build/changelog.rst

       :class:`.FileLock` backend, which builds on ``os.flock()`` and only
       supports Unix platforms.  A new abstract base :class:`.AbstractFileLock`
       has been added to provide a common base for custom lock implementations.
+      The documentation points to an example thread-based rw lock which is
+      now tested on Windows.
 
 .. changelog::
     :version: 0.5.1

dogpile/cache/backends/file.py

                 self.mutex = ReadWriteMutex()
 
             def acquire_read_lock(self, wait):
-                return self.mutex.acquire_read_lock(wait)
+                ret = self.mutex.acquire_read_lock(wait)
+                return wait or ret
 
             def acquire_write_lock(self, wait):
-                return self.mutex.acquire_write_lock(wait)
+                ret = self.mutex.acquire_write_lock(wait)
+                return wait or ret
 
             def release_read_lock(self):
                 return self.mutex.release_read_lock()
 
             def release_write_lock(self):
-                return self.mutex.release_write_lock(wait)
+                return self.mutex.release_write_lock()
 
         from dogpile.cache import make_region
 

tests/cache/test_dbm_backend.py

 import time
 import os
 from nose import SkipTest
+from dogpile.core.readwrite_lock import ReadWriteMutex
+from dogpile.cache.backends.file import AbstractFileLock
 
 try:
     import fcntl
+    has_fcntl = True
 except ImportError:
-    raise SkipTest("fcntl not available")
+    has_fcntl = False
 
-class DBMBackendTest(_GenericBackendTest):
+class MutexLock(AbstractFileLock):
+    def __init__(self, filename):
+        self.mutex = ReadWriteMutex()
+
+    def acquire_read_lock(self, wait):
+        ret = self.mutex.acquire_read_lock(wait)
+        return wait or ret
+
+    def acquire_write_lock(self, wait):
+        ret = self.mutex.acquire_write_lock(wait)
+        return wait or ret
+
+    def release_read_lock(self):
+        return self.mutex.release_read_lock()
+
+    def release_write_lock(self):
+        return self.mutex.release_write_lock()
+
+if has_fcntl:
+    class DBMBackendTest(_GenericBackendTest):
+        backend = "dogpile.cache.dbm"
+
+        config_args = {
+            "arguments": {
+                "filename": "test.dbm"
+            }
+        }
+
+class DBMBackendConditionTest(_GenericBackendTest):
     backend = "dogpile.cache.dbm"
 
     config_args = {
         "arguments": {
-            "filename": "test.dbm"
+            "filename": "test.dbm",
+            "lock_factory": MutexLock
         }
     }
 
+
 class DBMBackendNoLockTest(_GenericBackendTest):
     backend = "dogpile.cache.dbm"
 
     }
 
 
-class DBMMutexTest(_GenericMutexTest):
+class _DBMMutexTest(_GenericMutexTest):
     backend = "dogpile.cache.dbm"
 
-    config_args = {
-        "arguments": {
-            "filename": "test.dbm"
-        }
-    }
-
     def test_release_assertion_thread(self):
         backend = self._backend()
         m1 = backend.get_mutex("foo")
         finally:
             m1.release()
 
+if has_fcntl:
+    class DBMMutexFileTest(_DBMMutexTest):
+        config_args = {
+            "arguments": {
+                "filename": "test.dbm"
+            }
+        }
+
+
+class DBMMutexConditionTest(_DBMMutexTest):
+    config_args = {
+        "arguments": {
+            "filename": "test.dbm",
+            "lock_factory": MutexLock
+        }
+    }
+
 
 def teardown():
     for fname in os.listdir(os.curdir):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.