-
Notifications
You must be signed in to change notification settings - Fork 1
/
shutdown_test.go
60 lines (46 loc) · 1.04 KB
/
shutdown_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
package main
import (
"context"
"testing"
"google.golang.org/protobuf/types/known/emptypb"
)
type dummyRebooter struct {
cmd int
}
func (d *dummyRebooter) Reboot(cmd int) error {
d.cmd = cmd
return nil
}
type dummySyncer struct {
syncCount int
}
func (d *dummySyncer) Sync() {
d.syncCount += 1
}
func TestDispatcher_Shutdown(t *testing.T) {
d := newDispatcher()
for _, test := range []struct {
name string
cmd func(context.Context, *emptypb.Empty) (*emptypb.Empty, error)
expectCmd int
expectSync int
}{
{"shutdown", d.Shutdown, poweroff, 1},
{"reboot", d.Reboot, restart, 1},
{"halt", d.Halt, halt, 0},
} {
t.Run(test.name, func(t *testing.T) {
r := new(dummyRebooter)
rebooter = r.Reboot
s := new(dummySyncer)
syncer = s.Sync
test.cmd(context.Background(), nil)
if r.cmd != test.expectCmd {
t.Errorf("expected %X, received %X", r.cmd, test.expectCmd)
}
if s.syncCount != test.expectSync {
t.Errorf("expected %X, received %X", s.syncCount, test.expectSync)
}
})
}
}