我正在使用本文中的信息使用 firebase 模擬器為 Firestore 構建測試。模擬器正確啟動,測試運行,但是盡管有 SIGKILL(我嘗試了其他信號),但在測試完成后模擬器沒有被清理。這就是我的main_test.go樣子:package mainimport ( "fmt" "io" "log" "os" "os/exec" "strings" "syscall" "testing" "time")func TestMain(m *testing.M) { // command to start firestore emulator cmd := exec.Command("firebase", "emulators:start") // this makes it killable cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} // we need to capture it's output to know when it's started stdout, err := cmd.StdoutPipe() if err != nil { log.Fatal(err) } defer stdout.Close() if err := cmd.Start(); err != nil { log.Fatal(err) } var result int defer func() { syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) os.Exit(result) }() started := make(chan bool) go func() { buf := make([]byte, 512, 512) for { n, err := stdout.Read(buf[:]) if err != nil { if err == io.EOF { break } log.Fatalf("reading stdout %v", err) } if n > 0 { d := string(buf[:n]) // only required if we want to see the emulator output fmt.Printf("%s", d) // checking for the message that it's started if strings.Contains(d, "All emulators ready") { started <- true break } } } }() done := make(chan error, 1) go func() { done <- cmd.Wait() }() select { case <-time.After(10 * time.Second): log.Fatal("Failed to start the command for 10 seconds") case err := <-done: log.Fatalf("------\nCommand has finished unexpectedly with error: %v", err) case <-started: fmt.Println("--------") log.Print("Command started successully... running tests") }有什么想法有什么問題嗎?...或有關如何測試我的主要功能(使用 Firestore 作為后端)的其他想法?
1 回答

手掌心
TA貢獻1942條經驗 獲得超3個贊
簡短的回答:Goexec
不會殺死子進程。此處有更多詳細信息: 為什么不會正確殺死子進程?
根據這里的建議,我決定使用:
firebase emulators:exec "go test"
測試完成后正確清除模擬器。
- 1 回答
- 0 關注
- 193 瀏覽
添加回答
舉報
0/150
提交
取消