1
0
mirror of https://github.com/golang/go synced 2024-11-17 00:24:48 -07:00

cmd/test2json: add signal handler

Updates #53563

Change-Id: I35a3fd56718e198f68cbf73075a78b2fbc66bd7d
Reviewed-on: https://go-review.googlesource.com/c/go/+/419295
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Ryan Schuster <shuey19831@gmail.com>
Reviewed-by: David Chase <drchase@google.com>
This commit is contained in:
ianwoolf 2022-07-24 18:25:18 +08:00 committed by Gopher Robot
parent bc1d0d8eb1
commit f1f9e45143
4 changed files with 89 additions and 0 deletions

View File

@ -0,0 +1,55 @@
[short] skip 'links and runs a test binary'
[!fuzz] skip 'tests SIGINT behavior for interrupting fuzz tests'
[windows] skip 'windows does not support os.Interrupt'
? go test -json -fuzz FuzzInterrupt -run '^$' -parallel 1
stdout -count=1 '"Action":"pass","Package":"example","Test":"FuzzInterrupt"'
stdout -count=1 '"Action":"pass","Package":"example","Elapsed":'
mkdir $WORK/fuzzcache
go test -c . -fuzz=. -o test2json_interrupt_obj
? go tool test2json -p example -t ./test2json_interrupt_obj -test.v -test.paniconexit0 -test.fuzzcachedir $WORK/fuzzcache -test.fuzz FuzzInterrupt -test.run '^$' -test.parallel 1
stdout -count=1 '"Action":"pass","Package":"example","Test":"FuzzInterrupt"'
stdout -count=1 '"Action":"pass","Package":"example","Elapsed":'
-- go.mod --
module example
go 1.20
-- example_test.go --
package example_test
import (
"fmt"
"os"
"strconv"
"testing"
"strings"
"time"
)
func FuzzInterrupt(f *testing.F) {
pids := os.Getenv("GO_TEST_INTERRUPT_PIDS")
if pids == "" {
// This is the main test process.
// Set the environment variable for fuzz workers.
pid := os.Getpid()
ppid := os.Getppid()
os.Setenv("GO_TEST_INTERRUPT_PIDS", fmt.Sprintf("%d,%d", ppid, pid))
}
f.Fuzz(func(t *testing.T, orig string) {
// Simulate a ctrl-C on the keyboard by sending SIGINT
// to the main test process and its parent.
for _, pid := range strings.Split(pids, ",") {
i, err := strconv.Atoi(pid)
if err != nil {
t.Fatal(err)
}
if p, err := os.FindProcess(i); err == nil {
p.Signal(os.Interrupt)
time.Sleep(10 * time.Millisecond)
pids = "" // Only interrupt once.
}
}
})
}

View File

@ -88,6 +88,7 @@ import (
"io"
"os"
"os/exec"
"os/signal"
"cmd/internal/test2json"
)
@ -102,6 +103,11 @@ func usage() {
os.Exit(2)
}
// ignoreSignals ignore the interrupt signals.
func ignoreSignals() {
signal.Ignore(signalsToIgnore...)
}
func main() {
flag.Usage = usage
flag.Parse()
@ -121,6 +127,7 @@ func main() {
w := &countWriter{0, c}
cmd.Stdout = w
cmd.Stderr = w
ignoreSignals()
err := cmd.Run()
if err != nil {
if w.n > 0 {

View File

@ -0,0 +1,13 @@
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build plan9 || windows
package main
import (
"os"
)
var signalsToIgnore = []os.Signal{os.Interrupt}

View File

@ -0,0 +1,14 @@
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build unix || js
package main
import (
"os"
"syscall"
)
var signalsToIgnore = []os.Signal{os.Interrupt, syscall.SIGQUIT}