def testLinuxNanny(self):
        """Tests the linux nanny."""
        self.exit_called = False

        def MockExit(value):
            self.exit_called = value
            # Kill the nanny thread.
            raise RuntimeError("Nannythread exiting.")

        with utils.Stubber(os, "_exit", MockExit):
            nanny_controller = client_utils_linux.NannyController()
            nanny_controller.StartNanny(unresponsive_kill_period=0.5)
            try:
                for _ in range(10):
                    # Unfortunately we really need to sleep because we cant mock out
                    # time.time.
                    time.sleep(0.1)
                    nanny_controller.Heartbeat()

                self.assertEqual(self.exit_called, False)

                # Main thread sleeps for long enough for the nanny to fire.
                time.sleep(1)
                self.assertEqual(self.exit_called, -1)
            finally:
                nanny_controller.StopNanny()
    def testLinuxNannyLog(self):
        """Tests the linux nanny transaction log."""
        with tempfile.NamedTemporaryFile() as fd:
            nanny_controller = client_utils_linux.NannyController()
            nanny_controller.StartNanny(nanny_logfile=fd.name)
            try:
                grr_message = rdf_flows.GrrMessage(session_id="W:test")

                nanny_controller.WriteTransactionLog(grr_message)
                self.assertRDFValuesEqual(grr_message,
                                          nanny_controller.GetTransactionLog())
                nanny_controller.CleanTransactionLog()

                self.assertIsNone(nanny_controller.GetTransactionLog())

            finally:
                nanny_controller.StopNanny()