-
Notifications
You must be signed in to change notification settings - Fork 5
/
notifications_test.go
82 lines (75 loc) · 2.12 KB
/
notifications_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
// +build windows
package winjob_test
import (
"errors"
"os"
"syscall"
"testing"
"time"
"github.com/kolesnikovae/go-winjob"
)
const notificationsTestLimit = time.Second * 3
func TestNotifications(t *testing.T) {
runTestWithTestJobObjectWithProcess(t, func(job *winjob.JobObject, p *os.Process) {
c := make(chan winjob.Notification, 1)
s, err := winjob.Notify(c, job)
defer func() {
requireNoError(t, s.Close())
requireNoError(t, s.Err())
}()
requireNoError(t, err)
requireNoError(t, p.Kill())
select {
case n, ok := <-c:
// We expect at least NewProcess/ActiveProcessZero
// notification, whichever occurs first.
if !ok {
t.Fatal("Notification channel is closed")
}
t.Logf("Notification: %#v", n)
case <-time.After(notificationsTestLimit):
t.Fatal("No notifications received")
}
})
}
// The test ensures that the notification channel is closed
// with close of the subscription created.
func TestNotifications_Interruption(t *testing.T) {
runTestWithTestJobObjectWithProcess(t, func(job *winjob.JobObject, p *os.Process) {
c := make(chan winjob.Notification, 1)
s, err := winjob.Notify(c, job)
requireNoError(t, err)
requireNoError(t, s.Close())
requireNoError(t, s.Err())
select {
case n, ok := <-c:
if ok {
t.Logf("Notification: %#v", n)
}
case <-time.After(notificationsTestLimit):
t.Fatal("No notifications received")
}
})
}
// The test ensures that the notification channel is closed on completion
// port error and the error can be retrieved by Err call.
func TestNotifications_Error(t *testing.T) {
runTestWithTestJobObjectWithProcess(t, func(job *winjob.JobObject, p *os.Process) {
c := make(chan winjob.Notification, 1)
s, err := winjob.Notify(c, job)
requireNoError(t, err)
requireNoError(t, s.Port.Close())
select {
case n, ok := <-c:
if ok {
t.Logf("Notification: %#v", n)
}
case <-time.After(notificationsTestLimit):
t.Fatal("No notifications received")
}
expectedError := syscall.Errno(0x6) // The handle is invalid.
if !errors.Is(s.Err(), expectedError) {
t.Fatalf("Expected %#v, got %#v", expectedError, s.Err())
}
})
}