diff --git a/backend/internal/middleware/rabbitmq/integration_test.go b/backend/internal/middleware/rabbitmq/integration_test.go index 0b3d983..3c715d8 100644 --- a/backend/internal/middleware/rabbitmq/integration_test.go +++ b/backend/internal/middleware/rabbitmq/integration_test.go @@ -11,6 +11,7 @@ import ( "net/url" "os" "os/exec" + "runtime" "strconv" "testing" "time" @@ -71,6 +72,16 @@ func integrationTestManager(t *testing.T) (*RabbitMQ, config.RabbitMQConfig) { return mgr, cfg } +// runShell 执行由环境变量提供的"模拟故障"命令(重启 broker / 删除队列)。 +// 这些命令在 Windows 开发机与 Unix CI 上都要能跑,因此按 GOOS 选择 shell—— +// 硬编码 "sh -c" 会让本用例在 Windows 上直接失败。 +func runShell(cmdline string) ([]byte, error) { + if runtime.GOOS == "windows" { + return exec.Command("cmd", "/c", cmdline).CombinedOutput() + } + return exec.Command("sh", "-c", cmdline).CombinedOutput() +} + func randSuffix(t *testing.T) string { t.Helper() b := make([]byte, 6) @@ -134,7 +145,7 @@ func TestIntegration_ConsumerRecoversAfterBrokerRestart(t *testing.T) { // —— 制造故障:重启 broker —— genBefore := mgr.Stats().Generation t.Logf("restarting broker: %s", restartCmd) - if out, err := exec.Command("sh", "-c", restartCmd).CombinedOutput(); err != nil { + if out, err := runShell(restartCmd); err != nil { t.Fatalf("restart command failed: %v\n%s", err, out) } @@ -200,7 +211,7 @@ func TestIntegration_TopologyIsRedeclaredAfterQueueDeleted(t *testing.T) { genBefore := mgr.Stats().Generation cmd := fmt.Sprintf(deleteCmd, topo.Queue) t.Logf("deleting queue: %s", cmd) - if out, err := exec.Command("sh", "-c", cmd).CombinedOutput(); err != nil { + if out, err := runShell(cmd); err != nil { t.Fatalf("delete command failed: %v\n%s", err, out) }