-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusbevent_windows_test.go
68 lines (62 loc) · 1.59 KB
/
usbevent_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
package usbevent_test
import (
"context"
"log"
"testing"
"time"
usbevent "github.com/christowolf/usb-event"
"github.com/christowolf/usb-event/internal/winuser"
)
// Example shows how to use the package.
func Example() {
n, err := usbevent.Register()
if err != nil {
log.Fatalf("registration failed: %v\n", err)
}
go func() {
for e := range n.Channel {
log.Printf("%+v\n\n", e)
}
}()
n.Run(context.Background())
}
// TestRegister verifies that the Register function
// returns a valid Notifier.
// This test is not parallel because it
// registers a window class.
func TestRegister(t *testing.T) {
n, err := usbevent.Register()
if err != nil {
t.Errorf("registration failed: %v", err)
}
if n == nil {
t.Errorf("got nil notifier")
}
}
// TestRegisterFailure verifies that the Register function
// returns an error when the window class registration fails.
// This test is not parallel because it
// registers a window class.
func TestRegisterFailure(t *testing.T) {
// We don't care what happens at first.
usbevent.Register()
// But latest by now, registration should fail.
_, err := usbevent.Register()
if err == nil {
t.Errorf("registration succeeded, but should not")
}
}
// TestRun verifies that the Run function
// can be cancelled/timed out using a context.
func TestRun(t *testing.T) {
t.Parallel()
timeout := 100 * time.Millisecond
n := &usbevent.Notifier{Channel: make(chan winuser.EventInfo)}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
start := time.Now()
n.Run(ctx)
if time.Since(start) < timeout {
t.Errorf("timed out too early")
}
cancel()
}