-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
hcsr04_driver_test.go
365 lines (354 loc) · 9.92 KB
/
hcsr04_driver_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package gpio
import (
"errors"
"fmt"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gobot.io/x/gobot/v2/drivers/aio"
"gobot.io/x/gobot/v2/system"
)
func initTestHCSR04DriverWithStubbedAdaptor(triggerPinID string, echoPinID string) (*HCSR04Driver, *digitalPinMock) {
a := newGpioTestAdaptor()
tpin := a.addDigitalPin(triggerPinID)
_ = a.addDigitalPin(echoPinID)
d := NewHCSR04Driver(a, triggerPinID, echoPinID)
if err := d.Start(); err != nil {
panic(err)
}
return d, tpin
}
func TestNewHCSR04Driver(t *testing.T) {
// arrange
const (
triggerPinID = "3"
echoPinID = "4"
)
a := newGpioTestAdaptor()
tpin := a.addDigitalPin(triggerPinID)
epin := a.addDigitalPin(echoPinID)
// act
d := NewHCSR04Driver(a, triggerPinID, echoPinID)
// assert
assert.IsType(t, &HCSR04Driver{}, d)
// assert: gpio.driver attributes
assert.NotNil(t, d.driver)
assert.True(t, strings.HasPrefix(d.driverCfg.name, "HCSR04"))
assert.Equal(t, a, d.connection)
require.NoError(t, d.afterStart())
require.NoError(t, d.beforeHalt())
assert.NotNil(t, d.Commander)
assert.NotNil(t, d.mutex)
// assert: driver specific attributes
assert.False(t, d.hcsr04Cfg.useEdgePolling)
assert.Equal(t, triggerPinID, d.triggerPinID)
assert.Equal(t, echoPinID, d.echoPinID)
assert.NotNil(t, d.measureMutex)
assert.Equal(t, tpin, d.triggerPin)
assert.Equal(t, epin, d.echoPin)
}
func TestNewHCSR04Driver_options(t *testing.T) {
// This is a general test, that options are applied in constructor by using the common WithName() option, least one
// option of this driver and one of another driver (which should lead to panic). Further tests for options can also
// be done by call of "WithOption(val).apply(cfg)".
// arrange
const (
myName = "count up"
cycReadDur = 30 * time.Millisecond
)
panicFunc := func() {
NewHCSR04Driver(newGpioTestAdaptor(), "1", "2", WithName("crazy"),
aio.WithActuatorScaler(func(float64) int { return 0 }))
}
// act
d := NewHCSR04Driver(newGpioTestAdaptor(), "1", "2", WithName(myName), WithHCSR04UseEdgePolling())
// assert
assert.True(t, d.hcsr04Cfg.useEdgePolling)
assert.Equal(t, myName, d.Name())
assert.PanicsWithValue(t, "'scaler option for analog actuators' can not be applied on 'crazy'", panicFunc)
}
func TestHCSR04MeasureDistance(t *testing.T) {
tests := map[string]struct {
measureMicroSec int64
simulateWriteErr string
wantCallsWrite int
wantVal float64
wantErr string
}{
"measure_ok": {
measureMicroSec: 5831,
wantCallsWrite: 2,
wantVal: 1.0,
},
"error_timeout": {
measureMicroSec: 170000, // > 160 ms
wantCallsWrite: 2,
wantErr: "timeout 160ms reached",
},
"error_write": {
measureMicroSec: 5831,
simulateWriteErr: "write error",
wantCallsWrite: 1,
wantErr: "write error",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// arrange
d, tpin := initTestHCSR04DriverWithStubbedAdaptor("3", "4")
// arrange sensor and event handler simulation
waitForTriggerChan := make(chan struct{})
loopWg := sync.WaitGroup{}
defer func() {
close(waitForTriggerChan)
loopWg.Wait()
}()
loopWg.Add(1)
go func() {
<-waitForTriggerChan
m := tc.measureMicroSec // to prevent data race together with wait group
loopWg.Done()
time.Sleep(time.Duration(m) * time.Microsecond)
d.delayMicroSecChan <- m
}()
// arrange writes
numCallsWrite := 0
var oldVal int
tpin.writeFunc = func(val int) error {
numCallsWrite++
if val == 0 && oldVal == 1 {
// falling edge detected
waitForTriggerChan <- struct{}{}
}
oldVal = val
var err error
if tc.simulateWriteErr != "" {
err = errors.New(tc.simulateWriteErr)
}
return err
}
// act
got, err := d.MeasureDistance()
// assert
assert.Equal(t, tc.wantCallsWrite, numCallsWrite)
if tc.wantErr != "" {
require.ErrorContains(t, err, tc.wantErr)
} else {
require.NoError(t, err)
}
assert.InDelta(t, tc.wantVal, got, 0.0)
})
}
}
func TestHCSR04Distance(t *testing.T) {
tests := map[string]struct {
measureMicroSec int64
simulateWriteErr string
wantVal float64
wantErr string
}{
"distance_0mm": {
measureMicroSec: 0, // no validity test yet
wantVal: 0.0,
},
"distance_2cm": {
measureMicroSec: 117, // 117us ~ 0.12ms => ~2cm
wantVal: 0.02,
},
"distance_4m": {
measureMicroSec: 23324, // 23324us ~ 24ms => ~4m
wantVal: 4.0,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// arrange
d := HCSR04Driver{lastMeasureMicroSec: tc.measureMicroSec}
// act
got := d.Distance()
// assert
assert.InDelta(t, tc.wantVal, got, 0.0)
})
}
}
func TestHCSR04StartDistanceMonitor(t *testing.T) {
tests := map[string]struct {
simulateIsStarted bool
simulateWriteErr bool
wantErr string
}{
"start_ok": {},
"start_ok_measure_error": {
simulateWriteErr: true,
},
"error_already_started": {
simulateIsStarted: true,
wantErr: "already started for 'HCSR04-",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// arrange
d, tpin := initTestHCSR04DriverWithStubbedAdaptor("3", "4")
defer func() {
if d.distanceMonitorStopChan != nil {
close(d.distanceMonitorStopChan)
}
if d.distanceMonitorStopWaitGroup != nil {
d.distanceMonitorStopWaitGroup.Wait()
}
}()
if tc.simulateIsStarted {
d.distanceMonitorStopChan = make(chan struct{})
}
tpin.writeFunc = func(val int) error {
if tc.simulateWriteErr {
return fmt.Errorf("write error")
}
return nil
}
// act
err := d.StartDistanceMonitor()
time.Sleep(1 * time.Millisecond) // < 160 ms
// assert
if tc.wantErr != "" {
require.ErrorContains(t, err, tc.wantErr)
} else {
require.NoError(t, err)
assert.NotNil(t, d.distanceMonitorStopChan)
assert.NotNil(t, d.distanceMonitorStopWaitGroup)
}
})
}
}
func TestHCSR04StopDistanceMonitor(t *testing.T) {
tests := map[string]struct {
start bool
wantErr string
}{
"stop_ok": {
start: true,
},
"error_not_started": {
wantErr: "not yet started for 'HCSR04-",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// arrange
d, _ := initTestHCSR04DriverWithStubbedAdaptor("3", "4")
defer func() {
if d.distanceMonitorStopChan != nil {
close(d.distanceMonitorStopChan)
}
if d.distanceMonitorStopWaitGroup != nil {
d.distanceMonitorStopWaitGroup.Wait()
}
}()
if tc.start {
err := d.StartDistanceMonitor()
require.NoError(t, err)
}
// act
err := d.StopDistanceMonitor()
time.Sleep(1 * time.Millisecond) // < 160 ms
// assert
if tc.wantErr != "" {
require.ErrorContains(t, err, tc.wantErr)
} else {
require.NoError(t, err)
assert.Nil(t, d.distanceMonitorStopChan)
}
})
}
}
func TestHCSR04_createEventHandler(t *testing.T) {
type eventCall struct {
timeStamp time.Duration
eventType string
}
tests := map[string]struct {
calls []eventCall
wants []int64
}{
"only_rising": {
calls: []eventCall{
{timeStamp: 1 * time.Microsecond, eventType: system.DigitalPinEventRisingEdge},
{timeStamp: 2 * time.Microsecond, eventType: system.DigitalPinEventRisingEdge},
},
},
"only_falling": {
calls: []eventCall{
{timeStamp: 2 * time.Microsecond, eventType: system.DigitalPinEventFallingEdge},
{timeStamp: 3 * time.Microsecond, eventType: system.DigitalPinEventFallingEdge},
},
},
"event_normal": {
calls: []eventCall{
{timeStamp: 1 * time.Microsecond, eventType: system.DigitalPinEventRisingEdge},
{timeStamp: 10 * time.Microsecond, eventType: system.DigitalPinEventFallingEdge},
},
wants: []int64{9},
},
"event_falling_before": {
calls: []eventCall{
{timeStamp: 1 * time.Microsecond, eventType: system.DigitalPinEventFallingEdge},
{timeStamp: 2 * time.Microsecond, eventType: system.DigitalPinEventRisingEdge},
{timeStamp: 10 * time.Microsecond, eventType: system.DigitalPinEventFallingEdge},
},
wants: []int64{8},
},
"event_falling_after": {
calls: []eventCall{
{timeStamp: 1 * time.Microsecond, eventType: system.DigitalPinEventRisingEdge},
{timeStamp: 10 * time.Microsecond, eventType: system.DigitalPinEventFallingEdge},
{timeStamp: 12 * time.Microsecond, eventType: system.DigitalPinEventFallingEdge},
},
wants: []int64{9},
},
"event_rising_before": {
calls: []eventCall{
{timeStamp: 1 * time.Microsecond, eventType: system.DigitalPinEventRisingEdge},
{timeStamp: 5 * time.Microsecond, eventType: system.DigitalPinEventRisingEdge},
{timeStamp: 10 * time.Microsecond, eventType: system.DigitalPinEventFallingEdge},
},
wants: []int64{5},
},
"event_rising_after": {
calls: []eventCall{
{timeStamp: 1 * time.Microsecond, eventType: system.DigitalPinEventRisingEdge},
{timeStamp: 10 * time.Microsecond, eventType: system.DigitalPinEventFallingEdge},
{timeStamp: 12 * time.Microsecond, eventType: system.DigitalPinEventRisingEdge},
},
wants: []int64{9},
},
"event_multiple": {
calls: []eventCall{
{timeStamp: 1 * time.Microsecond, eventType: system.DigitalPinEventRisingEdge},
{timeStamp: 10 * time.Microsecond, eventType: system.DigitalPinEventFallingEdge},
{timeStamp: 11 * time.Microsecond, eventType: system.DigitalPinEventRisingEdge},
{timeStamp: 13 * time.Microsecond, eventType: system.DigitalPinEventFallingEdge},
},
wants: []int64{9, 2},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
// arrange
d := HCSR04Driver{delayMicroSecChan: make(chan int64, len(tc.wants))}
// act
eh := d.createEventHandler()
for _, call := range tc.calls {
eh(0, call.timeStamp, call.eventType, 0, 0)
}
// assert
for _, want := range tc.wants {
got := <-d.delayMicroSecChan
assert.Equal(t, want, got)
}
})
}
}