Wait for slow MySQL service startup

This commit is contained in:
王鹏
2026-10-09 10:54:03 +08:00
parent 433fa81194
commit 7966b061c0
2 changed files with 70 additions and 16 deletions

View File

@@ -462,13 +462,33 @@ class InstallerTests(unittest.TestCase):
self.assertEqual(commands, [["sc.exe", "start", "MySQL80"]])
self.assertTrue(any("已启动" in message for message in messages))
def test_waits_for_mysql_service_when_start_command_reports_failure(self):
with (
patch("deploy_tool.installer.query_mysql_service_state", side_effect=[1, 1, 2, 4]),
patch("deploy_tool.installer.time.sleep"),
):
messages = []
ensure_mysql_service_running(
messages.append,
elevated_runner=lambda _command: 1,
)
self.assertTrue(any("继续等待运行状态" in message for message in messages))
self.assertTrue(any("已启动" in message for message in messages))
def test_builds_privileged_mysql_service_start_diagnostic(self):
script = build_mysql_service_start_script()
script = build_mysql_service_start_script(
mysql_executable=Path(r"C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe")
)
self.assertIn("Start-Service -Name $name", script)
self.assertIn("Get-CimInstance Win32_Service", script)
self.assertIn("ExitCode,ProcessId", script)
self.assertIn("my.ini", script)
self.assertIn("*.err", script)
self.assertIn("Get-Content", script)
self.assertIn("--validate-config", script)
self.assertIn("Get-WinEvent", script)
self.assertIn("mysqld.exe", script)
with patch.dict("os.environ", {"LOCALAPPDATA": r"C:\Users\tester\AppData\Local"}):
log_path = default_mysql_service_log_path()
@@ -495,7 +515,11 @@ class InstallerTests(unittest.TestCase):
"deploy_tool.installer.run_elevated_command_with_log",
side_effect=inspect_command,
):
exit_code = run_mysql_service_start_diagnostic("MySQL80", log_path)
exit_code = run_mysql_service_start_diagnostic(
"MySQL80",
log_path,
mysql_executable=Path("mysqld.exe"),
)
self.assertEqual(exit_code, 1)
@@ -509,7 +533,7 @@ class InstallerTests(unittest.TestCase):
with (
patch.dict("os.environ", {"PROGRAMDATA": str(program_data)}),
patch("deploy_tool.installer.query_mysql_service_state", side_effect=[1, 2, 1, 1]),
patch("deploy_tool.installer.query_mysql_service_state", return_value=1),
patch("deploy_tool.installer.time.sleep"),
):
with self.assertRaisesRegex(RuntimeError, "Data Dictionary initialization failed"):
@@ -517,6 +541,7 @@ class InstallerTests(unittest.TestCase):
lambda _message: None,
mysql_executable=Path("mysql.exe"),
elevated_runner=lambda _command: 0,
attempts=3,
)
self.assertEqual(find_mysql_error_log(Path("mysql.exe")), error_log)
detail = mysql_service_failure_detail(Path("mysql.exe"), 1)