|
|
|
@ -9,6 +9,18 @@ import testlib
|
|
|
|
|
class BoolTest(testlib.RouterMixin, testlib.TestCase):
|
|
|
|
|
klass = mitogen.select.Select
|
|
|
|
|
|
|
|
|
|
def test_latch(self):
|
|
|
|
|
latch = mitogen.core.Latch() # oneshot
|
|
|
|
|
select = self.klass()
|
|
|
|
|
self.assertFalse(select)
|
|
|
|
|
select.add(latch)
|
|
|
|
|
self.assertTrue(select)
|
|
|
|
|
|
|
|
|
|
latch.put(123)
|
|
|
|
|
self.assertTrue(select)
|
|
|
|
|
self.assertEquals(123, select.get())
|
|
|
|
|
self.assertFalse(select)
|
|
|
|
|
|
|
|
|
|
def test_receiver(self):
|
|
|
|
|
recv = mitogen.core.Receiver(self.router) # oneshot
|
|
|
|
|
select = self.klass()
|
|
|
|
@ -25,6 +37,14 @@ class BoolTest(testlib.RouterMixin, testlib.TestCase):
|
|
|
|
|
class AddTest(testlib.RouterMixin, testlib.TestCase):
|
|
|
|
|
klass = mitogen.select.Select
|
|
|
|
|
|
|
|
|
|
def test_latch(self):
|
|
|
|
|
latch = mitogen.core.Latch()
|
|
|
|
|
select = self.klass()
|
|
|
|
|
select.add(latch)
|
|
|
|
|
self.assertEquals(1, len(select._receivers))
|
|
|
|
|
self.assertEquals(latch, select._receivers[0])
|
|
|
|
|
self.assertEquals(select._put, latch.notify)
|
|
|
|
|
|
|
|
|
|
def test_receiver(self):
|
|
|
|
|
recv = mitogen.core.Receiver(self.router)
|
|
|
|
|
select = self.klass()
|
|
|
|
@ -98,14 +118,14 @@ class AddTest(testlib.RouterMixin, testlib.TestCase):
|
|
|
|
|
class RemoveTest(testlib.RouterMixin, testlib.TestCase):
|
|
|
|
|
klass = mitogen.select.Select
|
|
|
|
|
|
|
|
|
|
def test_empty(self):
|
|
|
|
|
def test_receiver_empty(self):
|
|
|
|
|
select = self.klass()
|
|
|
|
|
recv = mitogen.core.Receiver(self.router)
|
|
|
|
|
exc = self.assertRaises(mitogen.select.Error,
|
|
|
|
|
lambda: select.remove(recv))
|
|
|
|
|
self.assertEquals(str(exc), self.klass.not_present_msg)
|
|
|
|
|
|
|
|
|
|
def test_absent(self):
|
|
|
|
|
def test_receiver_absent(self):
|
|
|
|
|
select = self.klass()
|
|
|
|
|
recv = mitogen.core.Receiver(self.router)
|
|
|
|
|
recv2 = mitogen.core.Receiver(self.router)
|
|
|
|
@ -114,7 +134,7 @@ class RemoveTest(testlib.RouterMixin, testlib.TestCase):
|
|
|
|
|
lambda: select.remove(recv))
|
|
|
|
|
self.assertEquals(str(exc), self.klass.not_present_msg)
|
|
|
|
|
|
|
|
|
|
def test_present(self):
|
|
|
|
|
def test_receiver_present(self):
|
|
|
|
|
select = self.klass()
|
|
|
|
|
recv = mitogen.core.Receiver(self.router)
|
|
|
|
|
select.add(recv)
|
|
|
|
@ -122,6 +142,30 @@ class RemoveTest(testlib.RouterMixin, testlib.TestCase):
|
|
|
|
|
self.assertEquals(0, len(select._receivers))
|
|
|
|
|
self.assertEquals(None, recv.notify)
|
|
|
|
|
|
|
|
|
|
def test_latch_empty(self):
|
|
|
|
|
select = self.klass()
|
|
|
|
|
latch = mitogen.core.Latch()
|
|
|
|
|
exc = self.assertRaises(mitogen.select.Error,
|
|
|
|
|
lambda: select.remove(latch))
|
|
|
|
|
self.assertEquals(str(exc), self.klass.not_present_msg)
|
|
|
|
|
|
|
|
|
|
def test_latch_absent(self):
|
|
|
|
|
select = self.klass()
|
|
|
|
|
latch = mitogen.core.Latch()
|
|
|
|
|
latch2 = mitogen.core.Latch()
|
|
|
|
|
select.add(latch2)
|
|
|
|
|
exc = self.assertRaises(mitogen.select.Error,
|
|
|
|
|
lambda: select.remove(latch))
|
|
|
|
|
self.assertEquals(str(exc), self.klass.not_present_msg)
|
|
|
|
|
|
|
|
|
|
def test_latch_present(self):
|
|
|
|
|
select = self.klass()
|
|
|
|
|
latch = mitogen.core.Latch()
|
|
|
|
|
select.add(latch)
|
|
|
|
|
select.remove(latch)
|
|
|
|
|
self.assertEquals(0, len(select._receivers))
|
|
|
|
|
self.assertEquals(None, latch.notify)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CloseTest(testlib.RouterMixin, testlib.TestCase):
|
|
|
|
|
klass = mitogen.select.Select
|
|
|
|
@ -130,6 +174,18 @@ class CloseTest(testlib.RouterMixin, testlib.TestCase):
|
|
|
|
|
select = self.klass()
|
|
|
|
|
select.close() # No effect.
|
|
|
|
|
|
|
|
|
|
def test_one_latch(self):
|
|
|
|
|
select = self.klass()
|
|
|
|
|
latch = mitogen.core.Latch()
|
|
|
|
|
select.add(latch)
|
|
|
|
|
|
|
|
|
|
self.assertEquals(1, len(select._receivers))
|
|
|
|
|
self.assertEquals(select._put, latch.notify)
|
|
|
|
|
|
|
|
|
|
select.close()
|
|
|
|
|
self.assertEquals(0, len(select._receivers))
|
|
|
|
|
self.assertEquals(None, latch.notify)
|
|
|
|
|
|
|
|
|
|
def test_one_receiver(self):
|
|
|
|
|
select = self.klass()
|
|
|
|
|
recv = mitogen.core.Receiver(self.router)
|
|
|
|
@ -174,18 +230,35 @@ class EmptyTest(testlib.RouterMixin, testlib.TestCase):
|
|
|
|
|
select = self.klass([recv])
|
|
|
|
|
self.assertTrue(select.empty())
|
|
|
|
|
|
|
|
|
|
def test_nonempty_before_add(self):
|
|
|
|
|
def test_nonempty_receiver_before_add(self):
|
|
|
|
|
recv = mitogen.core.Receiver(self.router)
|
|
|
|
|
recv._on_receive(mitogen.core.Message.pickled('123'))
|
|
|
|
|
select = self.klass([recv])
|
|
|
|
|
self.assertFalse(select.empty())
|
|
|
|
|
|
|
|
|
|
def test_nonempty_after_add(self):
|
|
|
|
|
def test_nonempty__receiver_after_add(self):
|
|
|
|
|
recv = mitogen.core.Receiver(self.router)
|
|
|
|
|
select = self.klass([recv])
|
|
|
|
|
recv._on_receive(mitogen.core.Message.pickled('123'))
|
|
|
|
|
self.assertFalse(select.empty())
|
|
|
|
|
|
|
|
|
|
def test_empty_latch(self):
|
|
|
|
|
latch = mitogen.core.Latch()
|
|
|
|
|
select = self.klass([latch])
|
|
|
|
|
self.assertTrue(select.empty())
|
|
|
|
|
|
|
|
|
|
def test_nonempty_latch_before_add(self):
|
|
|
|
|
latch = mitogen.core.Latch()
|
|
|
|
|
latch.put(123)
|
|
|
|
|
select = self.klass([latch])
|
|
|
|
|
self.assertFalse(select.empty())
|
|
|
|
|
|
|
|
|
|
def test_nonempty__latch_after_add(self):
|
|
|
|
|
latch = mitogen.core.Latch()
|
|
|
|
|
select = self.klass([latch])
|
|
|
|
|
latch.put(123)
|
|
|
|
|
self.assertFalse(select.empty())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class IterTest(testlib.RouterMixin, testlib.TestCase):
|
|
|
|
|
klass = mitogen.select.Select
|
|
|
|
@ -194,18 +267,24 @@ class IterTest(testlib.RouterMixin, testlib.TestCase):
|
|
|
|
|
select = self.klass()
|
|
|
|
|
self.assertEquals([], list(select))
|
|
|
|
|
|
|
|
|
|
def test_nonempty(self):
|
|
|
|
|
def test_nonempty_receiver(self):
|
|
|
|
|
recv = mitogen.core.Receiver(self.router)
|
|
|
|
|
select = self.klass([recv])
|
|
|
|
|
msg = mitogen.core.Message.pickled('123')
|
|
|
|
|
recv._on_receive(msg)
|
|
|
|
|
self.assertEquals([msg], list(select))
|
|
|
|
|
|
|
|
|
|
def test_nonempty_latch(self):
|
|
|
|
|
latch = mitogen.core.Latch()
|
|
|
|
|
select = self.klass([latch])
|
|
|
|
|
latch.put(123)
|
|
|
|
|
self.assertEquals([123], list(select))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class OneShotTest(testlib.RouterMixin, testlib.TestCase):
|
|
|
|
|
klass = mitogen.select.Select
|
|
|
|
|
|
|
|
|
|
def test_true_removed_after_get(self):
|
|
|
|
|
def test_true_receiver_removed_after_get(self):
|
|
|
|
|
recv = mitogen.core.Receiver(self.router)
|
|
|
|
|
select = self.klass([recv])
|
|
|
|
|
msg = mitogen.core.Message.pickled('123')
|
|
|
|
@ -215,7 +294,7 @@ class OneShotTest(testlib.RouterMixin, testlib.TestCase):
|
|
|
|
|
self.assertEquals(0, len(select._receivers))
|
|
|
|
|
self.assertEquals(None, recv.notify)
|
|
|
|
|
|
|
|
|
|
def test_false_persists_after_get(self):
|
|
|
|
|
def test_false_receiver_persists_after_get(self):
|
|
|
|
|
recv = mitogen.core.Receiver(self.router)
|
|
|
|
|
select = self.klass([recv], oneshot=False)
|
|
|
|
|
msg = mitogen.core.Message.pickled('123')
|
|
|
|
@ -226,8 +305,26 @@ class OneShotTest(testlib.RouterMixin, testlib.TestCase):
|
|
|
|
|
self.assertEquals(recv, select._receivers[0])
|
|
|
|
|
self.assertEquals(select._put, recv.notify)
|
|
|
|
|
|
|
|
|
|
def test_true_latch_removed_after_get(self):
|
|
|
|
|
latch = mitogen.core.Latch()
|
|
|
|
|
select = self.klass([latch])
|
|
|
|
|
latch.put(123)
|
|
|
|
|
self.assertEquals(123, select.get())
|
|
|
|
|
self.assertEquals(0, len(select._receivers))
|
|
|
|
|
self.assertEquals(None, latch.notify)
|
|
|
|
|
|
|
|
|
|
class GetTest(testlib.RouterMixin, testlib.TestCase):
|
|
|
|
|
def test_false_latch_persists_after_get(self):
|
|
|
|
|
latch = mitogen.core.Latch()
|
|
|
|
|
select = self.klass([latch], oneshot=False)
|
|
|
|
|
latch.put(123)
|
|
|
|
|
|
|
|
|
|
self.assertEquals(123, select.get())
|
|
|
|
|
self.assertEquals(1, len(select._receivers))
|
|
|
|
|
self.assertEquals(latch, select._receivers[0])
|
|
|
|
|
self.assertEquals(select._put, latch.notify)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GetReceiverTest(testlib.RouterMixin, testlib.TestCase):
|
|
|
|
|
klass = mitogen.select.Select
|
|
|
|
|
|
|
|
|
|
def test_no_receivers(self):
|
|
|
|
@ -285,5 +382,79 @@ class GetTest(testlib.RouterMixin, testlib.TestCase):
|
|
|
|
|
lambda: select.get(timeout=0.0))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GetLatchTest(testlib.RouterMixin, testlib.TestCase):
|
|
|
|
|
klass = mitogen.select.Select
|
|
|
|
|
|
|
|
|
|
def test_no_latches(self):
|
|
|
|
|
select = self.klass()
|
|
|
|
|
exc = self.assertRaises(mitogen.select.Error,
|
|
|
|
|
lambda: select.get())
|
|
|
|
|
self.assertEquals(str(exc), self.klass.empty_msg)
|
|
|
|
|
|
|
|
|
|
def test_timeout_no_receivers(self):
|
|
|
|
|
select = self.klass()
|
|
|
|
|
exc = self.assertRaises(mitogen.select.Error,
|
|
|
|
|
lambda: select.get(timeout=1.0))
|
|
|
|
|
self.assertEquals(str(exc), self.klass.empty_msg)
|
|
|
|
|
|
|
|
|
|
def test_zero_timeout(self):
|
|
|
|
|
latch = mitogen.core.Latch()
|
|
|
|
|
select = self.klass([latch])
|
|
|
|
|
self.assertRaises(mitogen.core.TimeoutError,
|
|
|
|
|
lambda: select.get(timeout=0.0))
|
|
|
|
|
|
|
|
|
|
def test_timeout(self):
|
|
|
|
|
latch = mitogen.core.Latch()
|
|
|
|
|
select = self.klass([latch])
|
|
|
|
|
self.assertRaises(mitogen.core.TimeoutError,
|
|
|
|
|
lambda: select.get(timeout=0.1))
|
|
|
|
|
|
|
|
|
|
def test_nonempty_before_add(self):
|
|
|
|
|
latch = mitogen.core.Latch()
|
|
|
|
|
latch.put(123)
|
|
|
|
|
select = self.klass([latch])
|
|
|
|
|
self.assertEquals(123, select.get())
|
|
|
|
|
|
|
|
|
|
def test_nonempty_after_add(self):
|
|
|
|
|
latch = mitogen.core.Latch()
|
|
|
|
|
select = self.klass([latch])
|
|
|
|
|
latch.put(123)
|
|
|
|
|
self.assertEquals(123, latch.get())
|
|
|
|
|
|
|
|
|
|
def test_drained_by_other_thread(self):
|
|
|
|
|
latch = mitogen.core.Latch()
|
|
|
|
|
latch.put(123)
|
|
|
|
|
select = self.klass([latch])
|
|
|
|
|
self.assertEquals(123, latch.get())
|
|
|
|
|
self.assertRaises(mitogen.core.TimeoutError,
|
|
|
|
|
lambda: select.get(timeout=0.0))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GetEventTest(testlib.RouterMixin, testlib.TestCase):
|
|
|
|
|
klass = mitogen.select.Select
|
|
|
|
|
|
|
|
|
|
def test_empty(self):
|
|
|
|
|
select = self.klass()
|
|
|
|
|
exc = self.assertRaises(mitogen.select.Error,
|
|
|
|
|
lambda: select.get())
|
|
|
|
|
self.assertEquals(str(exc), self.klass.empty_msg)
|
|
|
|
|
|
|
|
|
|
def test_latch(self):
|
|
|
|
|
latch = mitogen.core.Latch()
|
|
|
|
|
latch.put(123)
|
|
|
|
|
select = self.klass([latch])
|
|
|
|
|
event = select.get_event()
|
|
|
|
|
self.assertEquals(latch, event.source)
|
|
|
|
|
self.assertEquals(123, event.data)
|
|
|
|
|
|
|
|
|
|
def test_receiver(self):
|
|
|
|
|
recv = mitogen.core.Receiver(self.router)
|
|
|
|
|
recv._on_receive(mitogen.core.Message.pickled('123'))
|
|
|
|
|
select = self.klass([recv])
|
|
|
|
|
event = select.get_event()
|
|
|
|
|
self.assertEquals(recv, event.source)
|
|
|
|
|
self.assertEquals('123', event.data.unpickle())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest2.main()
|
|
|
|
|