-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsignal_windows_test.go
85 lines (80 loc) · 2.04 KB
/
signal_windows_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//go:build windows
// +build windows
package sigintwindows
import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"syscall"
"testing"
"time"
)
const processToSignal = "ctrlbreak"
func TestSendCtrlBreak(t *testing.T) {
_ = os.Remove(processToSignal + ".log")
src := processToSignal + ".go"
exe := processToSignal + ".exe"
defer func() {
if err := os.Remove(filepath.Join(processToSignal, exe)); err != nil {
t.Log(err)
}
}()
o, err := exec.Command("go", "install", filepath.Join(processToSignal, src)).CombinedOutput()
if err != nil {
t.Fatalf("Failed to compile: %v\n%v", err, string(o))
}
cmd := exec.Command(exe)
cmd.SysProcAttr = &syscall.SysProcAttr{
CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP,
}
if err := cmd.Start(); err != nil {
t.Fatalf("Start failed: %v", err)
}
// If interrupted here while cmd waits, "exit status STATUS_CONTROL_C_EXIT" displays
// See go/src/os/exec_posix.go:109
d := time.Duration(5)
t.Logf("waiting %d seconds before goroutine. No log to find.\n", d)
time.Sleep(d * time.Second)
go func() {
t.Logf("waiting %d seconds in goroutine. Log displays unless interrupted.\n", d)
time.Sleep(d * time.Second)
if err := SendCtrlBreak(cmd.Process.Pid); err != nil {
t.Log(err)
}
}()
if err := cmd.Wait(); err != nil {
t.Errorf("Wait failed: %v", err)
}
if testing.Verbose() {
f, err := os.Open("ctrlbreak.log")
if err == nil {
scanner := bufio.NewScanner(f)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
} else {
t.Error("cannot access log")
}
}
if err != nil {
t.Fatalf("Program exited with error: %v\n", err)
}
}
func TestSendCtrlBreakNoPid(t *testing.T) {
var nonExistingPid uint32 = 999000
const READ_CONTROL uint32 = 0x00020000
var err error
for err == nil {
_, err = syscall.OpenProcess(READ_CONTROL, false, nonExistingPid)
nonExistingPid++
if nonExistingPid > 999999 {
t.Skipf("no invalid process id found")
}
}
err = SendCtrlBreak(int(nonExistingPid))
if err == nil {
t.Fatalf("Sending Ctrl Break with an invalid process id did not fail")
}
}