-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathref_test.go
72 lines (60 loc) · 1.54 KB
/
ref_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
//go:build windows
// +build windows
package winproc_test
import (
"context"
"fmt"
"os/exec"
"testing"
"time"
"github.com/gentlemanautomaton/winproc"
"github.com/gentlemanautomaton/winproc/processaccess"
)
var delays = []time.Duration{
100 * time.Millisecond,
200 * time.Millisecond,
400 * time.Millisecond,
}
func TestWait(t *testing.T) {
for _, delay := range delays {
delay := delay // Capture range variable
t.Run(fmt.Sprintf("Delay=%s", delay), func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), delay)
defer cancel()
command := exec.CommandContext(ctx, "notepad")
if err := command.Start(); err != nil {
t.Error(err)
}
ref, err := winproc.Open(winproc.ID(command.Process.Pid), processaccess.Synchronize)
if err != nil {
t.Error(err)
}
defer ref.Close()
if err := ref.Wait(context.Background()); err != nil {
t.Error(err)
}
})
}
}
func TestWaitCancellation(t *testing.T) {
for _, delay := range delays {
delay := delay // Capture range variable
t.Run(fmt.Sprintf("Delay=%s", delay), func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), delay)
defer cancel()
command := exec.Command("notepad")
if err := command.Start(); err != nil {
t.Error(err)
}
defer command.Process.Kill()
ref, err := winproc.Open(winproc.ID(command.Process.Pid), processaccess.Synchronize)
if err != nil {
t.Error(err)
}
defer ref.Close()
if err := ref.Wait(ctx); err != context.DeadlineExceeded {
t.Error(err)
}
})
}
}