def test_present(self): select = self.klass() recv = mitogen.core.Receiver(self.router) select.add(recv) select.remove(recv) self.assertEquals(0, len(select._receivers)) self.assertEquals(None, recv.notify)
def test_receiver(self): recv = mitogen.core.Receiver(self.router) select = self.klass() select.add(recv) self.assertEquals(1, len(select._receivers)) self.assertEquals(recv, select._receivers[0]) self.assertEquals(select._put, recv.notify)
def test_double_add_receiver(self): select = self.klass() recv = mitogen.core.Receiver(self.router) select.add(recv) exc = self.assertRaises(mitogen.select.Error, lambda: select.add(recv)) self.assertEquals(str(exc), self.klass.owned_msg)
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_double_add_subselect(self): select = self.klass() select2 = self.klass() select.add(select2) exc = self.assertRaises(mitogen.select.Error, lambda: select.add(select2)) self.assertEquals(str(exc), self.klass.owned_msg)
def test_subselect_empty(self): select = self.klass() subselect = self.klass() select.add(subselect) self.assertEquals(1, len(select._receivers)) self.assertEquals(subselect, select._receivers[0]) self.assertEquals(select._put, subselect.notify)
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)
def test_absent(self): select = self.klass() recv = mitogen.core.Receiver(self.router) recv2 = mitogen.core.Receiver(self.router) select.add(recv2) exc = self.assertRaises(mitogen.select.Error, lambda: select.remove(recv)) self.assertEquals(str(exc), self.klass.not_present_msg)
def test_channel(self): context = self.router.local() chan = mitogen.core.Channel(self.router, context, 1234) select = self.klass() select.add(chan) self.assertEquals(1, len(select._receivers)) self.assertEquals(chan, select._receivers[0]) self.assertEquals(select._put, chan.notify)
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_subselect_nonempty(self): recv = mitogen.core.Receiver(self.router) select = self.klass() subselect = self.klass() subselect.add(recv) select.add(subselect) self.assertEquals(1, len(select._receivers)) self.assertEquals(subselect, select._receivers[0]) self.assertEquals(select._put, subselect.notify)
def test_one_receiver(self): select = self.klass() recv = mitogen.core.Receiver(self.router) select.add(recv) self.assertEquals(1, len(select._receivers)) self.assertEquals(select._put, recv.notify) select.close() self.assertEquals(0, len(select._receivers)) self.assertEquals(None, recv.notify)
def test_receiver(self): recv = mitogen.core.Receiver(self.router) # oneshot select = self.klass() self.assertFalse(select) select.add(recv) self.assertTrue(select) recv._on_receive(mitogen.core.Message.pickled('123')) self.assertTrue(select) self.assertEquals('123', select.get().unpickle()) self.assertFalse(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_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_subselect_loop_direct(self): select = self.klass() exc = self.assertRaises(mitogen.select.Error, lambda: select.add(select)) self.assertEquals(str(exc), self.klass.loop_msg)
def main(router): """ Main program entry point. @mitogen.main() is just a helper to handle reliable setup/destruction of Broker, Router and the logging package. """ argv = sys.argv[1:] if not len(argv): print('mitop: Need a list of SSH hosts to connect to.') sys.exit(1) delay = 2.0 select = mitogen.select.Select(oneshot=False) hosts = [] # For each hostname on the command line, create a Host instance, a Mitogen # connection, a Receiver to accept messages from the host, and finally # start child_main() on the host to pump messages into the receiver. for hostname in argv: print('Starting on', hostname) host = Host() host.name = hostname if host.name == 'localhost': host.context = router.local() else: host.context = router.ssh(hostname=host.name) # A receiver wires up a handle (via Router.add_handler()) to an # internal thread-safe queue object, which can be drained through calls # to recv.get(). host.recv = mitogen.core.Receiver(router) host.recv.host = host # But we don't want to receive data from just one receiver, we want to # receive data from many. In this case we can use a Select(). It knows # how to efficiently sleep while waiting for the first message sent to # many receivers. select.add(host.recv) # The inverse of a Receiver is a Sender. Unlike receivers, senders are # serializable, so we can call the .to_sender() helper method to create # one equivalent to our host's receiver, and pass it directly to the # host as a function parameter. sender = host.recv.to_sender() # Finally invoke the function in the remote target. Since child_main() # is an infinite loop, using .call() would block the parent, since # child_main() never returns. Instead use .call_async(), which returns # another Receiver. We also want to wait for results from it -- # although child_main() never returns, if it crashes the exception will # be delivered instead. call_recv = host.context.call_async(child_main, sender, delay) call_recv.host = host # Adding call_recv to the select will cause mitogen.core.CallError to # be thrown by .get() if startup of any context fails, causing halt of # master_main(), and the exception to be printed. select.add(call_recv) hosts.append(host) # Painter just wraps up all the prehistory ncurses code and keeps it out of # master_main(). painter = Painter(hosts) try: try: master_main(painter, router, select, delay) except KeyboardInterrupt: # Shut down gracefully when the user presses CTRL+C. pass finally: painter.close()