-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a signal handler to forward SIGINT to 'go test'
Some test binaries may want to handle SIGINT to perform cleanup of external resources. This signal handler will forward the interrupt signal to the 'go test' process to give it a chance to cleanup before shutting down.
- Loading branch information
Showing
3 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"os/signal" | ||
"strconv" | ||
"syscall" | ||
"time" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) < 2 { | ||
log("missing required filename argument") | ||
os.Exit(1) | ||
} | ||
|
||
pid := []byte(strconv.Itoa(os.Getpid())) | ||
if err := ioutil.WriteFile(os.Args[1], pid, 0644); err != nil { | ||
log("failed to write file:", err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
c := make(chan os.Signal, 1) | ||
signal.Notify(c) | ||
|
||
var s os.Signal | ||
select { | ||
case s = <-c: | ||
case <-time.After(time.Minute): | ||
log("timeout waiting for signal") | ||
os.Exit(1) | ||
} | ||
|
||
log("Received signal:", s) | ||
switch n := s.(type) { | ||
case syscall.Signal: | ||
os.Exit(100 + int(n)) | ||
default: | ||
log("failed to parse signal number") | ||
os.Exit(3) | ||
} | ||
} | ||
|
||
func log(v ...interface{}) { | ||
fmt.Fprintln(os.Stderr, v...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters