-
Notifications
You must be signed in to change notification settings - Fork 4
/
ps_test.go
131 lines (112 loc) · 2.56 KB
/
ps_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2013 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.
package ps_test
import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"sync"
"testing"
"github.com/alexbrainman/ps"
"github.com/alexbrainman/ps/winapi"
)
type children struct {
mu sync.Mutex
procs []*ps.Process
}
func newChildren() *children {
return &children{
procs: make([]*ps.Process, 0),
}
}
func (c *children) add(t *testing.T, pid int) {
c.mu.Lock()
defer c.mu.Unlock()
p, err := ps.OpenProcess(pid)
if err != nil {
t.Fatalf("ps.OpenProcess failed: %v", err)
}
c.procs = append(c.procs, p)
}
func (c *children) printAll(t *testing.T) {
c.mu.Lock()
defer c.mu.Unlock()
for _, p := range c.procs {
s, err := p.Stats()
if err != nil {
t.Fatalf("p.Stats failed: %v", err)
}
t.Logf("handle=%d stats=%+v\n", p.Handle, s)
}
}
func (c *children) closeAll() {
c.mu.Lock()
defer c.mu.Unlock()
for _, p := range c.procs {
p.Close()
}
}
func TestJobObjectMonitor(t *testing.T) {
jo, err := ps.CreateJobObject("")
if err != nil {
t.Fatalf("ps.CreateJobObject failed: %v", err)
}
defer jo.Close()
m, err := jo.Monitor()
if err != nil {
t.Fatalf("jo.AddMonitor failed: %v", err)
}
defer m.Close()
c := newChildren()
go func() {
// collect all new processes as they get started
for {
// TODO: this loop does not exit properly, but it is ok for now
event, o, err := m.GetEvent()
if err != nil {
t.Fatalf("m.GetEvent failed: %v", err)
}
switch event {
case winapi.JOB_OBJECT_MSG_NEW_PROCESS:
c.add(t, int(o))
t.Logf("new process(pid=%d) started\n", o)
case winapi.JOB_OBJECT_MSG_EXIT_PROCESS:
t.Logf("existing process(pid=%d) exited\n", o)
default:
t.Fatalf("m.GetEvent returns unexpected message (id=%d)", o)
}
}
}()
err = jo.AddCurrentProcess()
if err != nil {
t.Fatalf("jo.AddCurrentProcess failed: %v", err)
}
runGoCmd(t)
defer c.closeAll()
c.printAll(t)
}
func runGoCmd(t *testing.T) string {
const src = `
package main
func main() {
println("hello")
}
`
dir, err := ioutil.TempDir("", "go-build")
if err != nil {
t.Fatalf("failed to create temp directory: %v", err)
}
defer os.RemoveAll(dir)
path := filepath.Join(dir, "main.go")
err = ioutil.WriteFile(path, []byte(src), 0644)
if err != nil {
t.Fatalf("failed to create source file: %v", err)
}
got, err := exec.Command("go", "run", path).CombinedOutput()
if err != nil {
t.Fatalf("failed to run command: %v %v", err, string(got))
}
return string(got)
}