]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/test2json: add signal handler
authorianwoolf <btw515wolf2@gmail.com>
Sun, 24 Jul 2022 10:25:18 +0000 (18:25 +0800)
committerGopher Robot <gobot@golang.org>
Fri, 26 Aug 2022 17:38:40 +0000 (17:38 +0000)
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>
src/cmd/go/testdata/script/test2json_interrupt.txt [new file with mode: 0644]
src/cmd/test2json/main.go
src/cmd/test2json/signal_notunix.go [new file with mode: 0644]
src/cmd/test2json/signal_unix.go [new file with mode: 0644]

diff --git a/src/cmd/go/testdata/script/test2json_interrupt.txt b/src/cmd/go/testdata/script/test2json_interrupt.txt
new file mode 100644 (file)
index 0000000..3e3a1c7
--- /dev/null
@@ -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.
+                       }
+               }
+       })
+}
\ No newline at end of file
index 5e17e0dec3cd4f9b0da8d5f52eb6beb609eabbeb..f9e590cd3636e803e89d4b7be9632b1b5ebb9bb7 100644 (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 {
diff --git a/src/cmd/test2json/signal_notunix.go b/src/cmd/test2json/signal_notunix.go
new file mode 100644 (file)
index 0000000..e5a73be
--- /dev/null
@@ -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}
diff --git a/src/cmd/test2json/signal_unix.go b/src/cmd/test2json/signal_unix.go
new file mode 100644 (file)
index 0000000..ed5ca7e
--- /dev/null
@@ -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}