]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Remove public APIs Semaphore.counter and locked().
authorA. Jesse Jiryu Davis <jesse@mongodb.com>
Wed, 25 Feb 2015 20:54:06 +0000 (15:54 -0500)
committerA. Jesse Jiryu Davis <jesse@mongodb.com>
Wed, 25 Feb 2015 20:54:06 +0000 (15:54 -0500)
tornado/locks.py
tornado/test/locks_test.py

index f6852ea08f861410118c4340091b08314ea6b539..a6d99be426a40168fa921975e1631ea5ef7c36b6 100644 (file)
@@ -158,14 +158,10 @@ class Semaphore(object):
     >>> @gen.coroutine
     ... def f():
     ...    with (yield semaphore.acquire()):
-    ...        assert semaphore.locked()
+    ...        # Do something holding the semaphore.
+    ...        pass
     ...
-    ...    assert not semaphore.locked()
-
-    .. note:: Unlike the standard `threading.Semaphore`, a Tornado `.Semaphore`
-      can tell you the current value of its `.counter`, because code in a
-      single-threaded Tornado application can check this value and act upon
-      it without fear of interruption from another thread.
+    ...    # Now the semaphore is released.
     """
     def __init__(self, value=1):
         if value < 0:
@@ -177,23 +173,14 @@ class Semaphore(object):
 
     def __repr__(self):
         res = super(Semaphore, self).__repr__()
-        extra = 'locked' if self.locked() else 'unlocked,value:{0}'.format(
+        extra = 'locked' if self._value == 0 else 'unlocked,value:{0}'.format(
             self._value)
         if self._waiters:
             extra = '{0},waiters:{1}'.format(extra, len(self._waiters))
         return '<{0} [{1}]>'.format(res[1:-1], extra)
 
-    @property
-    def counter(self):
-        """An integer, the current semaphore value."""
-        return self._value
-
-    def locked(self):
-        """True if the semaphore cannot be acquired immediately."""
-        return self._value == 0
-
     def release(self):
-        """Increment `.counter` and wake one waiter."""
+        """Increment the counter and wake one waiter."""
         self._value += 1
         for waiter in self._waiters:
             if not waiter.done():
@@ -209,7 +196,7 @@ class Semaphore(object):
                 break
 
     def acquire(self, timeout=None):
-        """Decrement `.counter`. Returns a Future.
+        """Decrement the counter. Returns a Future.
 
         Block if the counter is zero and wait for a `.release`. The Future
         raises `.TimeoutError` after the deadline.
index cb0365a29a479eac94738d8b18b2b5b9ec6f438b..45db095ff374854e0a354532ec7649aa6256c994 100644 (file)
@@ -232,13 +232,12 @@ class SemaphoreTest(AsyncTestCase):
 
     def test_acquire(self):
         sem = locks.Semaphore()
-        self.assertFalse(sem.locked())
         f0 = sem.acquire()
         self.assertTrue(f0.done())
-        self.assertTrue(sem.locked())
 
         # Wait for release().
         f1 = sem.acquire()
+        self.assertFalse(f1.done())
         f2 = sem.acquire()
         sem.release()
         self.assertTrue(f1.done())
@@ -267,7 +266,12 @@ class SemaphoreTest(AsyncTestCase):
         sem = locks.Semaphore()
         sem.release()
         sem.release()
-        self.assertEqual(3, sem.counter)
+
+        # Now the counter is 3. We can acquire three times before blocking.
+        self.assertTrue(sem.acquire().done())
+        self.assertTrue(sem.acquire().done())
+        self.assertTrue(sem.acquire().done())
+        self.assertFalse(sem.acquire().done())
 
 
 class SemaphoreContextManagerTest(AsyncTestCase):
@@ -275,10 +279,10 @@ class SemaphoreContextManagerTest(AsyncTestCase):
     def test_context_manager(self):
         sem = locks.Semaphore()
         with (yield sem.acquire()) as yielded:
-            self.assertTrue(sem.locked())
             self.assertTrue(yielded is None)
 
-        self.assertFalse(sem.locked())
+        # Semaphore was released and can be acquired again.
+        self.assertTrue(sem.acquire().done())
 
     @gen_test
     def test_context_manager_exception(self):
@@ -287,8 +291,8 @@ class SemaphoreContextManagerTest(AsyncTestCase):
             with (yield sem.acquire()):
                 1 / 0
 
-        # Context manager released semaphore.
-        self.assertFalse(sem.locked())
+        # Semaphore was released and can be acquired again.
+        self.assertTrue(sem.acquire().done())
 
     @gen_test
     def test_context_manager_timeout(self):