[CalendarServer-changes] [10254] CalendarServer/branches/users/glyph/queue-locking-and-timing

source_changes at macosforge.org source_changes at macosforge.org
Fri Jan 4 16:39:14 PST 2013


Revision: 10254
          http://trac.calendarserver.org//changeset/10254
Author:   glyph at apple.com
Date:     2013-01-04 16:39:14 -0800 (Fri, 04 Jan 2013)
Log Message:
-----------
Change design slightly. Basic test for acquire behavior.

Modified Paths:
--------------
    CalendarServer/branches/users/glyph/queue-locking-and-timing/twext/enterprise/locking.py
    CalendarServer/branches/users/glyph/queue-locking-and-timing/twext/enterprise/test/test_locking.py

Property Changed:
----------------
    CalendarServer/branches/users/glyph/queue-locking-and-timing/

Modified: CalendarServer/branches/users/glyph/queue-locking-and-timing/twext/enterprise/locking.py
===================================================================
--- CalendarServer/branches/users/glyph/queue-locking-and-timing/twext/enterprise/locking.py	2013-01-05 00:39:13 UTC (rev 10253)
+++ CalendarServer/branches/users/glyph/queue-locking-and-timing/twext/enterprise/locking.py	2013-01-05 00:39:14 UTC (rev 10254)
@@ -24,6 +24,8 @@
 from twext.enterprise.dal.model import Constraint
 from twext.enterprise.dal.syntax import SchemaSyntax
 from twext.enterprise.dal.model import Schema
+from twext.enterprise.dal.record import Record
+from twext.enterprise.dal.record import fromTable
 
 
 class AlreadyUnlocked(Exception):
@@ -42,7 +44,7 @@
 
     @return: inSchema
     """
-    LockTable = Table(inSchema, 'NAMED_LOCKS')
+    LockTable = Table(inSchema, 'NAMED_LOCK')
 
     LockTable.addColumn("LOCK_NAME", SQLType("varchar", 255))
     LockTable.tableConstraint(Constraint.NOT_NULL, ["LOCK_NAME"])
@@ -55,12 +57,15 @@
 
 
 
-class Locker(object):
+
+class NamedLock(Record, fromTable(LockSchema.NAMED_LOCK)):
     """
-    Acquire named locks against a database.
+    An L{AcquiredLock} lock against a shared data store that the current
+    process holds via the referenced transaction.
     """
 
-    def lock(self, name, wait=False):
+    @classmethod
+    def acquire(cls, txn, name, wait=False):
         """
         Acquire a lock with the given name.
 
@@ -75,16 +80,9 @@
         @return: a L{Deferred} that fires with an L{AcquiredLock} when the lock
             has fired, or fails when the lock has not been acquired.
         """
-        raise NotImplementedError()
+        return cls.create(txn, lockName=name)
 
 
-
-class AcquiredLock(object):
-    """
-    An L{AcquiredLock} lock against a shared data store that the current
-    process holds via the referenced transaction.
-    """
-
     def release(self, ignoreAlreadyUnlocked=False):
         """
         Release this lock.

Modified: CalendarServer/branches/users/glyph/queue-locking-and-timing/twext/enterprise/test/test_locking.py
===================================================================
--- CalendarServer/branches/users/glyph/queue-locking-and-timing/twext/enterprise/test/test_locking.py	2013-01-05 00:39:13 UTC (rev 10253)
+++ CalendarServer/branches/users/glyph/queue-locking-and-timing/twext/enterprise/test/test_locking.py	2013-01-05 00:39:14 UTC (rev 10254)
@@ -14,3 +14,41 @@
 # limitations under the License.
 ##
 
+"""
+Tests for mutual exclusion locks.
+"""
+
+from twisted.internet.defer import inlineCallbacks
+from twisted.trial.unittest import TestCase
+
+from twext.enterprise.fixtures import buildConnectionPool
+from twext.enterprise.locking import NamedLock
+from twext.enterprise.dal.syntax import Select
+from twext.enterprise.locking import LockSchema
+
+schemaText = """
+create table NAMED_LOCK (LOCK_NAME varchar(255) unique primary key);
+"""
+
+class TestLocking(TestCase):
+    """
+    Test locking and unlocking a database row.
+    """
+
+    def setUp(self):
+        """
+        Build a connection pool for the tests to use.
+        """
+        self.pool = buildConnectionPool(self, schemaText)
+
+
+    @inlineCallbacks
+    def test_acquire(self):
+        """
+        Acquiring a lock adds a row in that transaction.
+        """
+        txn = self.pool.connection()
+        yield NamedLock.acquire(txn, u"a test lock")
+        rows = yield Select(From=LockSchema.NAMED_LOCK).on(txn)
+        self.assertEquals(rows, [tuple([u"a test lock"])])
+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20130104/2e46208b/attachment-0001.html>


More information about the calendarserver-changes mailing list