core: Latch.empty() improvements

- throw LatchError if the latch is closed.
- wrap with the lock to avoid unexpected weirdness.
issue510
David Wilson 6 years ago
parent 388649df97
commit bcd9827c3b

@ -742,7 +742,8 @@ class Sender(object):
self.context.send( self.context.send(
Message.dead( Message.dead(
reason=self.explicit_close_msg, reason=self.explicit_close_msg,
handle=self.dst_handle) handle=self.dst_handle
)
) )
def __repr__(self): def __repr__(self):
@ -1884,8 +1885,17 @@ class Latch(object):
though a subsequent call to :meth:`get` will block, since another though a subsequent call to :meth:`get` will block, since another
waiting thread may be woken at any moment between :meth:`empty` and waiting thread may be woken at any moment between :meth:`empty` and
:meth:`get`. :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): def _get_socketpair(self):
""" """

@ -21,6 +21,13 @@ class EmptyTest(testlib.TestCase):
latch.put(None) latch.put(None)
self.assertTrue(not latch.empty()) 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): class GetTest(testlib.TestCase):
klass = mitogen.core.Latch klass = mitogen.core.Latch

Loading…
Cancel
Save