Skip to content

Commit aaf37b8

Browse files
committed
Better test
1 parent 340a2c5 commit aaf37b8

File tree

1 file changed

+57
-4
lines changed

1 file changed

+57
-4
lines changed

src/os/signal/signal_test.go

+57-4
Original file line numberDiff line numberDiff line change
@@ -923,8 +923,8 @@ func TestResetIgnore(t *testing.T) {
923923
}
924924

925925
sigs := []syscall.Signal{
926-
syscall.SIGINT,
927926
syscall.SIGHUP,
927+
syscall.SIGINT,
928928
syscall.SIGUSR1,
929929
syscall.SIGTERM,
930930
syscall.SIGCHLD,
@@ -934,6 +934,10 @@ func TestResetIgnore(t *testing.T) {
934934
for _, notify := range []bool{false, true} {
935935
for _, sig := range sigs {
936936
t.Run(fmt.Sprintf("%s[notify=%t]", sig, notify), func(t *testing.T) {
937+
if Ignored(sig) {
938+
t.Fatalf("expected %q to not be ignored initially", sig)
939+
}
940+
937941
Ignore(sig)
938942
if notify {
939943
c := make(chan os.Signal, 1)
@@ -948,9 +952,7 @@ func TestResetIgnore(t *testing.T) {
948952

949953
// Child processes inherit the ignored status of signals, so verify that it
950954
// is indeed not ignored.
951-
testenv.MustHaveExec(t)
952-
953-
cmd := testenv.Command(t, os.Args[0], "-test.run=^TestResetIgnore$")
955+
cmd := testenv.Command(t, testenv.Executable(t), "-test.run=^TestResetIgnore$")
954956
cmd.Env = append(os.Environ(), "GO_TEST_RESET_IGNORE="+strconv.Itoa(int(sig)))
955957
err := cmd.Run()
956958
if _, ok := err.(*exec.ExitError); ok {
@@ -969,3 +971,54 @@ func resetIgnoreTestProgram(t *testing.T, sig os.Signal) {
969971
}
970972
os.Exit(0)
971973
}
974+
975+
// #46321 test Reset correctly undoes the effect of Ignore when the child
976+
// process is started with a signal ignored.
977+
func TestInitiallyIgnoredResetIgnore(t *testing.T) {
978+
testenv.MustHaveExec(t)
979+
980+
if os.Getenv("GO_TEST_INITIALLY_IGNORED_RESET_IGNORE") != "" {
981+
s, err := strconv.Atoi(os.Getenv("GO_TEST_INITIALLY_IGNORED_RESET_IGNORE"))
982+
if err != nil {
983+
t.Fatalf("failed to parse signal: %v", err)
984+
}
985+
initiallyIgnoredResetIgnoreTestProgram(t, syscall.Signal(s))
986+
}
987+
988+
sigs := []syscall.Signal{
989+
syscall.SIGINT,
990+
syscall.SIGHUP,
991+
}
992+
993+
for _, sig := range sigs {
994+
t.Run(fmt.Sprint(sig), func(t *testing.T) {
995+
Ignore(sig)
996+
defer Reset(sig)
997+
998+
cmd := testenv.Command(t, testenv.Executable(t), "-test.run=^TestInitiallyIgnoredResetIgnore$")
999+
cmd.Env = append(os.Environ(), "GO_TEST_INITIALLY_IGNORED_RESET_IGNORE="+strconv.Itoa(int(sig)))
1000+
err := cmd.Run()
1001+
if _, ok := err.(*exec.ExitError); ok {
1002+
t.Fatalf("expected %q to be ignored in child process", sig)
1003+
} else if err != nil {
1004+
t.Fatalf("child process failed to launch: %v", err)
1005+
}
1006+
})
1007+
}
1008+
}
1009+
1010+
func initiallyIgnoredResetIgnoreTestProgram(t *testing.T, sig os.Signal) {
1011+
if !Ignored(sig) {
1012+
os.Exit(1)
1013+
}
1014+
Reset(sig)
1015+
if !Ignored(sig) {
1016+
os.Exit(1)
1017+
}
1018+
Ignore(sig)
1019+
Reset(sig)
1020+
if !Ignored(sig) {
1021+
os.Exit(1)
1022+
}
1023+
os.Exit(0)
1024+
}

0 commit comments

Comments
 (0)