Exemplo n.º 1
0
    def test_closeLogging(self):
        """A basic test of the close() functionality"""
        def validate_loggers(log):
            """little test helper, to make sure our loggers still look right"""
            handlers = [str(h) for h in log.logger.handlers]
            self.assertEqual(len(handlers), 1, msg=",".join(handlers))

            stderrHandlers = [str(h) for h in log.stderrLogger.handlers]
            self.assertEqual(len(stderrHandlers),
                             1,
                             msg=",".join(stderrHandlers))

        # init logger
        log = runLog.LOG = runLog._RunLog(777)
        validate_loggers(log)

        # start the logging for real
        log.startLog("test_closeLogging")
        context.createLogDir(0)
        validate_loggers(log)

        # close() and test that we have correctly nullified our loggers
        runLog.close(1)
        validate_loggers(log)

        # in a real run, the parent process would close() after all the children
        runLog.close(0)
Exemplo n.º 2
0
    def test_parentRunLogging(self):
        """A basic test of the logging of the parent runLog"""
        # init the _RunLog object
        log = runLog.LOG = runLog._RunLog(0)
        log.startLog("test_parentRunLogging")
        context.createLogDir(0)
        log.setVerbosity(logging.INFO)

        # divert the logging to a stream, to make testing easier
        stream = StringIO()
        handler = logging.StreamHandler(stream)
        log.logger.handlers = [handler]

        # log some things
        log.log("debug", "You shouldn't see this.", single=False, label=None)
        log.log("warning", "Hello, ", single=False, label=None)
        log.log("error", "world!", single=False, label=None)
        log.logger.flush()
        log.logger.close()
        runLog.close(99)

        # test what was logged
        streamVal = stream.getvalue()
        self.assertIn("Hello", streamVal, msg=streamVal)
        self.assertIn("world", streamVal, msg=streamVal)
Exemplo n.º 3
0
    def test_warningReport(self):
        """A simple test of the warning tracking and reporting logic"""
        # create the logger and do some logging
        log = runLog.LOG = runLog._RunLog(321)
        log.startLog("test_warningReport")
        context.createLogDir(0)

        # divert the logging to a stream, to make testing easier
        stream = StringIO()
        handler = logging.StreamHandler(stream)
        log.logger.handlers = [handler]

        # log some things
        log.setVerbosity(logging.INFO)
        log.log("warning", "test_warningReport", single=True, label=None)
        log.log("debug", "invisible due to log level", single=False, label=None)
        log.log("warning", "test_warningReport", single=True, label=None)
        log.log("error", "high level something", single=False, label=None)

        # test that the logging found some duplicate outputs
        dupsFilter = log.getDuplicatesFilter()
        self.assertTrue(dupsFilter is not None)
        warnings = dupsFilter.singleWarningMessageCounts
        self.assertGreater(len(warnings), 0)

        # run the warning report
        log.warningReport()
        runLog.close(1)
        runLog.close(0)

        # test what was logged
        streamVal = stream.getvalue()
        self.assertIn("test_warningReport", streamVal, msg=streamVal)
        self.assertIn("Final Warning Count", streamVal, msg=streamVal)
        self.assertEqual(streamVal.count("test_warningReport"), 2, msg=streamVal)
Exemplo n.º 4
0
    def operate(self):
        """
        Operate method for all nodes.

        Calls _mainOperate or workerOperate depending on which MPI rank we are, and
        handles errors.
        """
        runLog.debug("OperatorMPI.operate")
        if armi.MPI_RANK == 0:
            # this is the master
            try:
                # run the regular old operate function
                Operator.operate(self)
                runLog.important(time.ctime())
            except Exception as ee:
                runLog.error(
                    "Error in Master Node. Check STDERR for a traceback.\n{}".
                    format(ee))
                raise
            finally:
                if armi.MPI_SIZE > 0:
                    runLog.important(
                        "Stopping all MPI worker nodes and cleaning temps.")
                    armi.MPI_COMM.bcast(
                        "quit",
                        root=0)  # send the quit command to the workers.
                    runLog.debug("Waiting for all nodes to close down")
                    armi.MPI_COMM.bcast(
                        "finished",
                        root=0)  # wait until they're done cleaning up.
                    runLog.important("All worker nodes stopped.")
                time.sleep(
                    1
                )  # even though we waited, still need more time to close stdout.
                runLog.debug("Main operate finished")
                runLog.close()  # concatenate all logs.
        else:
            try:
                self.workerOperate()
            except:
                # grab the final command
                runLog.warning(
                    "An error has occurred in one of the worker nodes. See STDERR for traceback."
                )
                # bcasting quit won't work if the main is sitting around waiting for a
                # different bcast or gather.
                traceback.print_exc()
                runLog.debug("Worker failed")
                runLog.close()
                raise
Exemplo n.º 5
0
    def test_warningReportInvalid(self):
        """A test of warningReport in an invalid situation"""
        # create the logger and do some logging
        testName = "test_warningReportInvalid"
        log = runLog.LOG = runLog._RunLog(323)
        log.startLog(testName)
        runLog.createLogDir(0)

        # divert the logging to a stream, to make testing easier
        stream = StringIO()
        handler = logging.StreamHandler(stream)
        log.logger.handlers = [handler]

        # log some things
        log.setVerbosity(logging.INFO)
        log.log("warning", testName, single=True, label=None)
        log.log("debug",
                "invisible due to log level",
                single=False,
                label=None)
        log.log("warning", testName, single=True, label=None)
        log.log("error", "high level something", single=False, label=None)

        # test that the logging found some duplicate outputs
        def returnNone(*args, **kwargs):
            return None

        log.logger.getDuplicatesFilter = returnNone
        self.assertIsNone(log.logger.getDuplicatesFilter())

        # run the warning report
        log.warningReport()
        runLog.close(1)
        runLog.close(0)

        # test what was logged
        streamVal = stream.getvalue()
        self.assertIn(testName, streamVal, msg=streamVal)
        self.assertIn("None Found", streamVal, msg=streamVal)
        self.assertNotIn("invisible", streamVal, msg=streamVal)
        self.assertEqual(streamVal.count(testName), 1, msg=streamVal)
Exemplo n.º 6
0
def init(choice=None, fName=None, cs=None):
    """
    Scan a directory for armi inputs and load one to interact with.

    Parameters
    ----------
    choice : int, optional
        Automatically run with this item out of the menu
        that would be produced of existing xml files.

    fName : str, optional
        An actual case name to load. e.g. ntTwr1.xml

    cs : object, optional
        If supplied, supercede the other case input methods and use the object directly

    Examples
    --------
    >>> o = armi.init()

    """
    from armi import cases
    from armi import settings

    if cs is None:
        if fName is None:
            fName = settings.promptForSettingsFile(choice)
        cs = settings.Settings(fName)
    # clear out any old masterCs objects
    settings.setMasterCs(cs)

    armiCase = cases.Case(cs=cs)
    armiCase.checkInputs()

    try:
        return armiCase.initializeOperator()
    except:  # Catch any and all errors. Naked exception on purpose.
        # Concatenate errors to the master log file.
        runLog.close()
        raise
Exemplo n.º 7
0
 def workerQuit():
     runLog.debug("Worker ending")
     runLog.close()  # no more messages.
     # wait until all workers are closed so we can delete them.
     armi.MPI_COMM.bcast("finished", root=0)