【问题标题】:Firebase emulator does not stop when run with Golang exec command使用 Golang exec 命令运行时,Firebase 模拟器不会停止
【发布时间】:2020-10-13 20:59:08
【问题描述】:

我正在使用此 article 中的信息使用 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")
    }

    log.Print("BEFORE running tests")
    result = m.Run()
    time.Sleep(time.Minute) // to simulate that it take some times to run tests
    log.Print("AFTER running tests")
}

go test . -v 的输出看起来没问题:

i  emulators: Starting emulators: firestore
i  firestore: Firestore Emulator logging to firestore-debug.log
i  ui: Emulator UI logging to ui-debug.log

┌───────────────────────────────────────────────────────────────────────┐
│ ✔  All emulators ready! View status and logs at http://localhost:4000 │
└────────────────────────────────────────────────────────────--------
2020/10/13 16:20:29 Command started successully... running tests
2020/10/13 16:20:29 BEFORE running tests
testing: warning: no tests to run
PASS
2020/10/13 16:20:29 AFTER running tests
ok      go/src/test (cached) [no tests to run]

但是 firebase 模拟器没有正确清理(如果我再次启动相同的命令,它会失败并抱怨端口 4000 已在使用中)。发现以下进程挥之不去:

### snippet from netstat -anp:
tcp        0      0 127.0.0.1:4000          0.0.0.0:*               LISTEN      25187/firebase
tcp6       0      0 127.0.0.1:8080          :::*                    LISTEN      25160/cloud-firesto

### snippet from ps axu:
user 25187 /usr/local/bin/firebase /home/.cache/firebase/emulators/ui-v1.1.1/server.bundle.js
...
user 25160 /home/.cache/firebase/emulators/cloud-firestore-emulator-v1.11.7.jar --launcher_javabase=/usr/local/buildtools/java/jdk11 -Duser.language=en run --host localhost --port 8080 --rules /home/firestore.rules

任何想法有什么问题吗? ...或有关如何测试我的主要功能(使用 Firestore 作为后端)的其他想法?

【问题讨论】:

  • syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) 中 pid 前的负号会不会是个问题?
  • 不,这不是问题所在。我试过没有否定,我尝试了不同的信号,我一个接一个地尝试了几个信号。总有一些挥之不去的过程。
  • 我明白了,负 pid 是有效的,可以杀死组中的进程。尝试将 &syscall.SysProcAttr{Setpgid: true}` 行中的 Setpgid 更改为 Setsid 以创建新的会话和进程组。
  • 尝试了将Setpgid 更改为Setsid 的建议...但没有成功。
  • syscall.Kill(...) 函数可能会返回错误,如果不为零,请尝试捕获并记录它。

标签: firebase go firebase-tools


【解决方案1】:

简短回答:Go exec 不会杀死子进程。更多细节在这里:Why won't Go kill a child process correctly?

根据here 中的建议,我决定使用:

firebase emulators:exec "go test"

在测试完成后正确清除模拟器。

【讨论】:

    猜你喜欢
    • 2021-02-06
    • 2013-12-07
    • 2018-07-27
    • 2017-01-22
    • 1970-01-01
    • 2020-09-10
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多