我正在使用本文中的信息使用 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 关注
- 172 浏览
添加回答
举报
0/150
提交
取消