-
Notifications
You must be signed in to change notification settings - Fork 6
/
power_test.go
158 lines (138 loc) · 2.96 KB
/
power_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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package lifxlan_test
import (
"bytes"
"context"
"encoding/binary"
"math"
"math/rand"
"net"
"testing"
"testing/quick"
"time"
"go.yhsif.com/lifxlan"
"go.yhsif.com/lifxlan/mock"
)
func TestPower(t *testing.T) {
t.Run(
"Off",
func(t *testing.T) {
expected := "off"
power := lifxlan.PowerOff
if power.On() {
t.Errorf("%v.On() should return false.", power)
}
s := power.String()
if s != expected {
t.Errorf("Power(%d).String() expected %q, got %q", power, expected, s)
}
},
)
t.Run(
"On",
func(t *testing.T) {
expected := "on"
power := lifxlan.PowerOn
if !power.On() {
t.Errorf("Power(%d).On() should return true.", power)
}
s := power.String()
if s != expected {
t.Errorf("Power(%d).String() expected %q, got %q", power, expected, s)
}
},
)
t.Run(
"RandomOn",
func(t *testing.T) {
// Seed rander
now := time.Now()
rander := rand.New(rand.NewSource(now.Unix() + int64(now.Nanosecond())))
expected := "on"
var n int
f := func() bool {
n++
power := lifxlan.Power(rander.Intn(math.MaxUint16-1) + 1)
pass := true
if !power.On() {
pass = false
t.Logf("Power(%d).On() should return true.", power)
}
s := power.String()
if s != expected {
pass = false
t.Logf("Power(%d).String() expected %q, got %q", power, expected, s)
}
return pass
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
t.Logf("quick did %d checks", n)
},
)
}
func TestGetPower(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
const timeout = time.Millisecond * 200
expected := lifxlan.PowerOn
service, device := mock.StartService(t)
service.RawStatePowerPayload = &lifxlan.RawStatePowerPayload{
Level: expected,
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
power, err := device.GetPower(ctx, nil)
if err != nil {
t.Fatal(err)
}
if expected != power {
t.Errorf(
"Power expected %v(%d), got %v(%d)",
expected,
expected,
power,
power,
)
}
}
func TestSetPower(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
const timeout = time.Millisecond * 200
expected := lifxlan.PowerOff
service, device := mock.StartService(t)
var called bool
service.Handlers[lifxlan.SetPower] = func(
_ *mock.Service,
_ net.PacketConn,
_ net.Addr,
orig *lifxlan.Response,
) {
called = true
var raw lifxlan.RawSetPowerPayload
r := bytes.NewReader(orig.Payload)
if err := binary.Read(r, binary.LittleEndian, &raw); err != nil {
t.Fatal(err)
}
if expected != raw.Level {
t.Errorf(
"Power expected %v(%d), got %v(%d)",
expected,
expected,
raw.Level,
raw.Level,
)
}
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
if err := device.SetPower(ctx, nil, expected, true); err != nil {
t.Fatal(err)
}
if !called {
t.Error("SetPower message not received.")
}
}