-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdoorLogic_test.go
172 lines (151 loc) · 3.49 KB
/
doorLogic_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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"log"
"strconv"
"testing"
"time"
)
// If the door is open on start up then the forgot to close message should be sent, but not open or closed
func TestInitialOpen(t *testing.T) {
if !helper(
TestInitialOpenDoorSensor{},
true,
false,
true,
false,
1,
30,
1300) {
t.Error("Failed initial door open test")
}
}
type TestInitialOpenDoorSensor struct{}
func (ds TestInitialOpenDoorSensor) IsOpen() bool {
return true
}
// If the door starts closed and stays closed, then there should be no messages if before the hearbeat window
func TestInitialClosed(t *testing.T) {
if !helper(
TestInitialClosedDoorSensor{},
false,
false,
false,
false,
1,
30,
1300) {
t.Error("Failed initial door closed test")
}
}
// If the door starts closed and stays closed, then eventually the heartbeat should fire
func TestHeartbeat(t *testing.T) {
if !helper(
TestInitialClosedDoorSensor{},
false,
false,
false,
true,
1,
1,
1300) {
t.Error("Failed hearbeat")
}
}
type TestInitialClosedDoorSensor struct{}
func (ds TestInitialClosedDoorSensor) IsOpen() bool {
return false
}
// Test if the door starts open and then closes, there is no forgot message
func TestDoorOpenThenClosed(t *testing.T) {
if !helper(
TestDoorOpenThenClosedSensor{},
true,
false,
false,
false,
1,
30,
1700) {
t.Error("Failed open then closed")
}
}
type TestDoorOpenThenClosedSensor struct{}
var (
TestDoorOpenThenClosedSensorCount = 0
)
func (ds TestDoorOpenThenClosedSensor) IsOpen() bool {
TestDoorOpenThenClosedSensorCount++
// First check it will be open, then the second it will be closed
if TestDoorOpenThenClosedSensorCount == 1 {
return true
}
return false
}
// Test if the door starts open and then closes eventually, there should be an open, forgot, then closed message
func TestDoorOpenThenSlowClosed(t *testing.T) {
if !helper(
TestDoorOpenThenSlowClosedSensor{},
true,
true,
true,
false,
1,
30,
1700) {
t.Error("Failed open then slow closed")
}
}
type TestDoorOpenThenSlowClosedSensor struct{}
var (
TestDoorOpenThenSlowClosedSensorCount = 0
)
func (ds TestDoorOpenThenSlowClosedSensor) IsOpen() bool {
TestDoorOpenThenSlowClosedSensorCount++
// First check it will be open, then the second it will be closed
if TestDoorOpenThenSlowClosedSensorCount < 4 {
return true
}
return false
}
func helper(
doorSensor iDoorSensor,
expectOpen bool,
expectClosed bool,
expectForgot bool,
expectHeartbeat bool,
doorOpenWaitSeconds int,
hearbeatSeconds int,
sleepTimeMillis int) bool {
sendOpen := false
sendClosed := false
sendForgot := false
sendHeartbeat := false
doorLogic := NewDoorLogic(
doorSensor,
func() { sendOpen = true },
func() { sendClosed = true },
func() { sendForgot = true },
func() { sendHeartbeat = true },
doorOpenWaitSeconds,
hearbeatSeconds)
go doorLogic.Run()
sleepDuration, _ := time.ParseDuration(strconv.Itoa(sleepTimeMillis) + "ms")
time.Sleep(sleepDuration)
if sendOpen != expectOpen {
log.Printf("Expected 'Open' to be %v, but was %v", expectOpen, sendOpen)
return false
}
if sendClosed != expectClosed {
log.Printf("Expected 'Closed' to be %v, but was %v", expectClosed, sendClosed)
return false
}
if sendForgot != expectForgot {
log.Printf("Expected 'Forgot' to be %v, but was %v", expectForgot, sendForgot)
return false
}
if sendHeartbeat != expectHeartbeat {
log.Printf("Expected 'Heartbeat' to be %v, but was %v", expectHeartbeat, sendHeartbeat)
return false
}
return true
}