我正在使用本文中的信息使用 firebase 模拟器为 Firestore 构建测试。
模拟器正确启动,测试运行,但是尽管有 SIGKILL(我尝试了其他信号),但在测试完成后模拟器没有被清理。
这就是我的main_test.go样子:
package main
import (
"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 作为后端)的其他想法?
手掌心
相关分类