def test_parser_config(): r"""Test parser_config.""" config.get_config_parser(description="test", skip_sections=['testing']) parser = config.get_config_parser(description="test") assert_raises(ValueError, config.resolve_config_parser, parser.parse_args(['--production-run', '--debug'])) args1 = config.resolve_config_parser( parser.parse_args(['--languages', 'c', 'cpp', '--production-run'])) args2 = config.resolve_config_parser( parser.parse_args(['--skip-languages', 'R', '--debug'])) args3 = config.resolve_config_parser( parser.parse_args(['--validate-messages=True'])) assert_equal(args3.validate_messages, True) old_var = { k: os.environ.get(k, None) for k in [ 'YGG_TEST_LANGUAGE', 'YGG_TEST_SKIP_LANGUAGE', 'YGG_VALIDATE_COMPONENTS', 'YGG_VALIDATE_MESSAGES', 'YGG_DEBUG', 'YGG_CLIENT_DEBUG' ] } args1.validate_messages = 'False' args2.validate_messages = 'True' with config.parser_config(args1): assert_equal(json.loads(os.environ.get('YGG_TEST_LANGUAGE', '')), ['c', 'c++']) assert_equal(os.environ.get('YGG_VALIDATE_COMPONENTS', ''), 'false') assert_equal(os.environ.get('YGG_VALIDATE_MESSAGES', ''), 'false') for k, v in old_var.items(): assert_equal(os.environ.get(k, None), v) with config.parser_config(args2): assert_equal(json.loads(os.environ.get('YGG_TEST_SKIP_LANGUAGE', '')), ['R']) assert_equal(os.environ.get('YGG_DEBUG', ''), 'DEBUG') assert_equal(os.environ.get('YGG_CLIENT_DEBUG', ''), 'DEBUG') assert_equal(os.environ.get('YGG_VALIDATE_COMPONENTS', ''), 'true') assert_equal(os.environ.get('YGG_VALIDATE_MESSAGES', ''), 'true') for k, v in old_var.items(): assert_equal(os.environ.get(k, None), v) assert_raises(ValueError, config.acquire_env, dict(production_run=True, debug=True)) with config.temp_config(production_run=True): assert_equal(os.environ.get('YGG_VALIDATE_COMPONENTS', ''), 'false') assert_equal(os.environ.get('YGG_VALIDATE_MESSAGES', ''), 'false') with config.temp_config(debug=True): assert_equal(os.environ.get('YGG_DEBUG', ''), 'DEBUG') assert_equal(os.environ.get('YGG_CLIENT_DEBUG', ''), 'DEBUG') assert_equal(os.environ.get('YGG_VALIDATE_COMPONENTS', ''), 'true') assert_equal(os.environ.get('YGG_VALIDATE_MESSAGES', ''), 'true') with config.temp_config(validate_messages='True'): assert_equal(os.environ.get('YGG_VALIDATE_MESSAGES', ''), 'true')
def test_parser_config(): r"""Test parser_config.""" config.get_config_parser(description="test", skip_sections=['testing']) parser = config.get_config_parser(description="test") with pytest.raises(ValueError): config.resolve_config_parser( parser.parse_args(['--production-run', '--debug'])) args1 = config.resolve_config_parser( parser.parse_args(['--production-run'])) args2 = config.resolve_config_parser(parser.parse_args(['--debug'])) args3 = config.resolve_config_parser( parser.parse_args(['--validate-messages=True'])) assert (args3.validate_messages is True) old_var = { k: os.environ.get(k, None) for k in [ 'YGG_VALIDATE_COMPONENTS', 'YGG_VALIDATE_MESSAGES', 'YGG_DEBUG', 'YGG_CLIENT_DEBUG' ] } args2.validate_messages = 'True' for k, v in old_var.items(): assert (os.environ.get(k, None) == v) with config.parser_config(args1): assert (os.environ.get('YGG_VALIDATE_COMPONENTS', '') == 'false') assert (os.environ.get('YGG_VALIDATE_MESSAGES', '') == 'false') with config.parser_config(args2): assert (os.environ.get('YGG_DEBUG', '') == 'DEBUG') assert (os.environ.get('YGG_CLIENT_DEBUG', '') == 'DEBUG') assert (os.environ.get('YGG_VALIDATE_COMPONENTS', '') == 'true') assert (os.environ.get('YGG_VALIDATE_MESSAGES', '') == 'true') for k, v in old_var.items(): assert (os.environ.get(k, None) == v) with pytest.raises(ValueError): config.acquire_env(dict(production_run=True, debug=True)) with config.temp_config(production_run=True): assert (os.environ.get('YGG_VALIDATE_COMPONENTS', '') == 'false') assert (os.environ.get('YGG_VALIDATE_MESSAGES', '') == 'false') with config.temp_config(debug=True): assert (os.environ.get('YGG_DEBUG', '') == 'DEBUG') assert (os.environ.get('YGG_CLIENT_DEBUG', '') == 'DEBUG') assert (os.environ.get('YGG_VALIDATE_COMPONENTS', '') == 'true') assert (os.environ.get('YGG_VALIDATE_MESSAGES', '') == 'true') with config.temp_config(validate_messages='True'): assert (os.environ.get('YGG_VALIDATE_MESSAGES', '') == 'true')
def run(self, signal_handler=None, timer=None, t0=None): r"""Run all of the models and wait for them to exit. Args: signal_handler (function, optional): Function that should be used as a signal handler. Defaults to None and is set by set_signal_handler. timer (function, optional): Function that should be called to get intermediate timing statistics. Defaults to time.time if not provided. t0 (float, optional): Zero point for timing statistics. Is set using the provided timer if not provided. Returns: dict: Intermediate times from the run. """ with temp_config(production_run=self.production_run): if timer is None: timer = time.time if t0 is None: t0 = timer() times = {} times['init'] = timer() self.loadDrivers() times['load drivers'] = timer() self.startDrivers() times['start drivers'] = timer() self.set_signal_handler(signal_handler) self.waitModels() times['run models'] = timer() self.reset_signal_handler() self.closeChannels() times['close channels'] = timer() self.cleanup() times['clean up'] = timer() tprev = t0 key_order = [ 'init', 'load drivers', 'start drivers', 'run models', 'close channels', 'clean up' ] for k in key_order: self.info('%20s\t%f', k, times[k] - tprev) tprev = times[k] self.info(40 * '=') self.info('%20s\t%f', "Total", tprev - t0) return times
def run(self, signal_handler=None, timer=None, t0=None): r"""Run all of the models and wait for them to exit. Args: signal_handler (function, optional): Function that should be used as a signal handler. Defaults to None and is set by set_signal_handler. timer (function, optional): Function that should be called to get intermediate timing statistics. Defaults to time.time if not provided. t0 (float, optional): Zero point for timing statistics. Is set using the provided timer if not provided. Returns: dict: Intermediate times from the run. """ with temp_config(production_run=self.production_run): if timer is None: timer = time.time if t0 is None: t0 = timer() times = OrderedDict() times['init'] = timer() self.loadDrivers() times['load drivers'] = timer() self.startDrivers() times['start drivers'] = timer() self.set_signal_handler(signal_handler) if not self.complete_partial: self.waitModels() times['run models'] = timer() self.atexit() times['at exit'] = timer() tprev = t0 for k, t in times.items(): self.info('%20s\t%f', k, t - tprev) tprev = t self.info(40 * '=') self.info('%20s\t%f', "Total", tprev - t0) if self.error_flag: raise IntegrationError("Error running the integration.") if self.validate: for v in self.modeldrivers.values(): v['instance'].run_validation() return times