core: Latch.empty() improvements

- throw LatchError if the latch is closed.
- wrap with the lock to avoid unexpected weirdness.
This commit is contained in:
David Wilson 2019-01-19 22:23:31 +00:00
parent 388649df97
commit bcd9827c3b
2 changed files with 19 additions and 2 deletions

View File

@ -742,7 +742,8 @@ class Sender(object):
self.context.send(
Message.dead(
reason=self.explicit_close_msg,
handle=self.dst_handle)
handle=self.dst_handle
)
)
def __repr__(self):
@ -1884,8 +1885,17 @@ class Latch(object):
though a subsequent call to :meth:`get` will block, since another
waiting thread may be woken at any moment between :meth:`empty` and
:meth:`get`.
:raises LatchError:
The latch has already been marked closed.
"""
return len(self._queue) == 0
self._lock.acquire()
try:
if self.closed:
raise LatchError()
return len(self._queue) == 0
finally:
self._lock.release()
def _get_socketpair(self):
"""

View File

@ -21,6 +21,13 @@ class EmptyTest(testlib.TestCase):
latch.put(None)
self.assertTrue(not latch.empty())
def test_closed_is_empty(self):
latch = self.klass()
latch.put(None)
latch.close()
self.assertRaises(mitogen.core.LatchError,
lambda: latch.empty())
class GetTest(testlib.TestCase):
klass = mitogen.core.Latch