在 GoLand IDE 中 运行 测试时捕获 SIGINT

Capture SIGINT when running tests in GoLand IDE

当 运行 从命令行测试时,捕获 SIGINT 工作正常。但是,当 运行 从 GoLand IDE 进行测试时,有没有办法将 SIGINT 信号传递给代码?

当 运行 来自命令行时: go test -v -run TestSth 然后调用 Ctrl + C 它捕获正常。

示例代码:

package main

import (
    "fmt"
    "os"
    "os/signal"
    "syscall"
    "testing"
    "time"
)

func TestMain(m *testing.M) {
    terminate := make(chan os.Signal)
    signal.Notify(terminate, syscall.SIGINT)
    go func() {
        <-terminate
        fmt.Println()
        fmt.Println("CAPTURED!!!") // want to get here when running tests from IDE
    }()
    exitCode := m.Run()
    os.Exit(exitCode)
}

func TestSth(t *testing.T) {
    time.Sleep(time.Second * 5)
}

调用FindProcess on the current PID and signal the interrupt to it using Process.Signal获取当前进程信息

func TestSth(t *testing.T) {
     go func() {
         // Sleep added for demonstrative purposes, can be removed
         time.Sleep(time.Second * 1)
         p, err := os.FindProcess(os.Getpid())
         if err != nil {
             panic(err)
         }
         p.Signal(syscall.SIGINT)

     }()
     time.Sleep(time.Second * 5)
}