def test_run(hass, tmpdir): """Test we can run.""" test_dir = tmpdir.mkdir("config") default_config = runner.RuntimeConfig(test_dir) with patch.object(runner, "TASK_CANCELATION_TIMEOUT", 1), patch( "homeassistant.bootstrap.async_setup_hass", return_value=hass), patch("threading._shutdown"), patch( "homeassistant.core.HomeAssistant.async_run") as mock_run: runner.run(default_config) assert mock_run.called
def test_run_executor_shutdown_throws(hass, tmpdir): """Test we can run and we still shutdown if the executor shutdown throws.""" test_dir = tmpdir.mkdir("config") default_config = runner.RuntimeConfig(test_dir) with patch.object( runner, "TASK_CANCELATION_TIMEOUT", 1 ), pytest.raises(RuntimeError), patch( "homeassistant.bootstrap.async_setup_hass", return_value=hass ), patch("threading._shutdown"), patch( "homeassistant.runner.InterruptibleThreadPoolExecutor.shutdown", side_effect=RuntimeError, ) as mock_shutdown, patch( "homeassistant.core.HomeAssistant.async_run") as mock_run: runner.run(default_config) assert mock_shutdown.called assert mock_run.called
def main() -> int: """Start Home Assistant.""" validate_python() # Run a simple daemon runner process on Windows to handle restarts if os.name == "nt" and "--runner" not in sys.argv: nt_args = cmdline() + ["--runner"] while True: try: subprocess.check_call(nt_args) sys.exit(0) except KeyboardInterrupt: sys.exit(0) except subprocess.CalledProcessError as exc: if exc.returncode != RESTART_EXIT_CODE: sys.exit(exc.returncode) args = get_arguments() if args.script is not None: # pylint: disable=import-outside-toplevel from homeassistant import scripts return scripts.run(args.script) config_dir = os.path.abspath(os.path.join(os.getcwd(), args.config)) ensure_config_path(config_dir) # Daemon functions if args.pid_file: check_pid(args.pid_file) if args.daemon: daemonize() if args.pid_file: write_pid(args.pid_file) # pylint: disable=import-outside-toplevel from homeassistant import runner runtime_conf = runner.RuntimeConfig( config_dir=config_dir, verbose=args.verbose, log_rotate_days=args.log_rotate_days, log_file=args.log_file, log_no_color=args.log_no_color, skip_pip=args.skip_pip, safe_mode=args.safe_mode, debug=args.debug, open_ui=args.open_ui, ) exit_code = runner.run(runtime_conf) if exit_code == RESTART_EXIT_CODE and not args.runner: try_to_restart() return exit_code
def test_run_does_not_block_forever_with_shielded_task(hass, tmpdir, caplog): """Test we can shutdown and not block forever.""" test_dir = tmpdir.mkdir("config") default_config = runner.RuntimeConfig(test_dir) created_tasks = False async def _async_create_tasks(*_): nonlocal created_tasks async def async_raise(*_): try: await asyncio.sleep(2) except asyncio.CancelledError: raise Exception async def async_shielded(*_): try: await asyncio.sleep(2) except asyncio.CancelledError: await asyncio.sleep(2) asyncio.ensure_future(asyncio.shield(async_shielded())) asyncio.ensure_future(asyncio.sleep(2)) asyncio.ensure_future(async_raise()) await asyncio.sleep(0.1) created_tasks = True return 0 with patch.object(runner, "TASK_CANCELATION_TIMEOUT", 1), patch( "homeassistant.bootstrap.async_setup_hass", return_value=hass ), patch("threading._shutdown"), patch( "homeassistant.core.HomeAssistant.async_run", _async_create_tasks ): runner.run(default_config) assert created_tasks is True assert ( "Task could not be canceled and was still running after shutdown" in caplog.text )