-
Notifications
You must be signed in to change notification settings - Fork 9
/
interrupt_test.go
330 lines (312 loc) · 8.22 KB
/
interrupt_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
// Copyright © 2017 Kent Gibson <[email protected]>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
//
// Test suite for interrupt module.
//
// Tests use Raspberry Pi J8 pins 15 and 16 which must be jumpered together.
//
package gpio
import (
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func waitInterrupt(ch chan int, timeout time.Duration) (int, error) {
select {
case v := <-ch:
return v, nil
case <-time.After(timeout):
return 0, errors.New("timeout")
}
}
func setupIntr(t *testing.T) (pinIn *Pin, pinOut *Pin, watcher *Watcher) {
assert.Nil(t, Open())
pinIn = NewPin(J8p15)
pinOut = NewPin(J8p16)
watcher = getDefaultWatcher()
pinIn.SetMode(Input)
pinOut.Write(Low)
pinOut.SetMode(Output)
return
}
func teardownIntr(pinIn *Pin, pinOut *Pin, watcher *Watcher) {
pinOut.SetMode(Input)
watcher.UnregisterPin(pinIn)
Close()
}
func TestRegister(t *testing.T) {
pinIn, pinOut, watcher := setupIntr(t)
defer teardownIntr(pinIn, pinOut, watcher)
ich := make(chan int)
count := 0
assert.Nil(t, watcher.RegisterPin(pinIn, EdgeRising, func(pin *Pin) {
count++
ich <- count
}))
v, err := waitInterrupt(ich, 10*time.Millisecond)
assert.Nil(t, err)
assert.Equal(t, 1, v)
_, err = waitInterrupt(ich, 10*time.Millisecond)
assert.NotNil(t, err, "Spurious interrupt")
}
func TestReregister(t *testing.T) {
pinIn, pinOut, watcher := setupIntr(t)
defer teardownIntr(pinIn, pinOut, watcher)
ich := make(chan int)
assert.Nil(t, watcher.RegisterPin(pinIn, EdgeRising, func(pin *Pin) {
ich <- 1
}))
v, err := waitInterrupt(ich, 10*time.Millisecond)
assert.Nil(t, err)
assert.Equal(t, 1, v)
assert.NotNil(t, watcher.RegisterPin(pinIn, EdgeRising, func(pin *Pin) {
ich <- 2
}), "Reregistration didn't fail.")
pinOut.High()
v, err = waitInterrupt(ich, 10*time.Millisecond)
assert.Nil(t, err)
assert.Equal(t, 1, v)
}
func TestUnregister(t *testing.T) {
pinIn, pinOut, watcher := setupIntr(t)
defer teardownIntr(pinIn, pinOut, watcher)
ich := make(chan int)
assert.Nil(t, watcher.RegisterPin(pinIn, EdgeRising, func(pin *Pin) {
ich <- 1
}), "Registration failed")
v, err := waitInterrupt(ich, 10*time.Millisecond)
assert.Nil(t, err)
assert.Equal(t, 1, v)
watcher.UnregisterPin(pinIn)
pinOut.High()
_, err = waitInterrupt(ich, 10*time.Millisecond)
assert.NotNil(t, err)
// And again just for coverage.
watcher.UnregisterPin(pinIn)
}
func TestEdgeRising(t *testing.T) {
pinIn, pinOut, watcher := setupIntr(t)
defer teardownIntr(pinIn, pinOut, watcher)
ich := make(chan int)
assert.Nil(t, watcher.RegisterPin(pinIn, EdgeRising, func(pin *Pin) {
if pin.Read() == High {
ich <- 1
} else {
ich <- 0
}
}))
v, err := waitInterrupt(ich, 10*time.Millisecond)
assert.Nil(t, err)
assert.Equal(t, 0, v)
// Can take a while for the init to be applied before it starts triggering
// interrupts, so wait a bit...
time.Sleep(time.Millisecond)
for i := 0; i < 10; i++ {
pinOut.High()
v, err := waitInterrupt(ich, 10*time.Millisecond)
if err != nil {
t.Error("Missed high at", i)
} else if v == 0 {
t.Error("Triggered while low at", i)
}
pinOut.Low()
_, err = waitInterrupt(ich, 10*time.Millisecond)
if err == nil {
t.Error("Spurious or delayed trigger at", i)
}
}
}
func TestEdgeFalling(t *testing.T) {
pinIn, pinOut, watcher := setupIntr(t)
defer teardownIntr(pinIn, pinOut, watcher)
ich := make(chan int)
assert.Nil(t, watcher.RegisterPin(pinIn, EdgeFalling, func(pin *Pin) {
if pin.Read() == High {
ich <- 1
} else {
ich <- 0
}
}))
v, err := waitInterrupt(ich, 10*time.Millisecond)
assert.Nil(t, err)
assert.Equal(t, 0, v)
for i := 0; i < 10; i++ {
pinOut.High()
_, err := waitInterrupt(ich, 10*time.Millisecond)
if err == nil {
t.Error("Spurious or delayed trigger at", i)
}
pinOut.Low()
v, err = waitInterrupt(ich, 10*time.Millisecond)
if err != nil {
t.Error("Missed low at", i)
} else if v == 1 {
t.Error("Triggered while low at", i)
}
}
}
func TestEdgeBoth(t *testing.T) {
pinIn, pinOut, watcher := setupIntr(t)
defer teardownIntr(pinIn, pinOut, watcher)
ich := make(chan int)
assert.Nil(t, watcher.RegisterPin(pinIn, EdgeBoth, func(pin *Pin) {
if pin.Read() == High {
ich <- 1
} else {
ich <- 0
}
}))
v, err := waitInterrupt(ich, 10*time.Millisecond)
assert.Nil(t, err)
assert.Equal(t, 0, v)
for i := 0; i < 10; i++ {
pinOut.High()
v, err := waitInterrupt(ich, 10*time.Millisecond)
if err != nil {
t.Error("Missed high at", i)
} else if v == 0 {
t.Error("Triggered while low at", i)
}
pinOut.Low()
v, err = waitInterrupt(ich, 10*time.Millisecond)
if err != nil {
t.Error("Missed low at", i)
} else if v == 1 {
t.Error("Triggered while high at", i)
}
}
}
func TestEdgeNone(t *testing.T) {
pinIn, pinOut, watcher := setupIntr(t)
defer teardownIntr(pinIn, pinOut, watcher)
ich := make(chan int)
assert.Nil(t, watcher.RegisterPin(pinIn, EdgeNone, func(pin *Pin) {
if pin.Read() == High {
ich <- 1
} else {
ich <- 0
}
}))
v, err := waitInterrupt(ich, 10*time.Millisecond)
assert.Nil(t, err)
assert.Equal(t, 0, v)
for i := 0; i < 10; i++ {
pinOut.High()
v, err := waitInterrupt(ich, 10*time.Millisecond)
if err == nil {
t.Error("Spurious or delayed trigger at", i, v)
}
pinOut.Low()
v, err = waitInterrupt(ich, 10*time.Millisecond)
if err == nil {
t.Error("Spurious or delayed trigger at", i, v)
}
}
}
func TestUnexportedEdge(t *testing.T) {
pinIn, pinOut, watcher := setupIntr(t)
assert.NotNil(t, setEdge(pinIn, EdgeNone))
defer teardownIntr(pinIn, pinOut, watcher)
}
func TestCloseInterrupts(t *testing.T) {
pinIn, pinOut, watcher := setupIntr(t)
defer teardownIntr(pinIn, pinOut, watcher)
ich := make(chan int)
assert.Nil(t, watcher.RegisterPin(pinIn, EdgeNone, func(pin *Pin) {
if pin.Read() == High {
ich <- 1
} else {
ich <- 0
}
}))
// absorb state sync interrupt
v, err := waitInterrupt(ich, 10*time.Millisecond)
assert.Nil(t, err, "Missing sync interrupt")
assert.Equal(t, 0, v)
closeInterrupts()
// check no interrupts triggered by close
_, err = waitInterrupt(ich, 10*time.Millisecond)
assert.NotNil(t, err, "Interrupt triggered by close")
// confirm that no further interrupts can be triggered.
pinOut.High()
_, err = waitInterrupt(ich, 10*time.Millisecond)
assert.NotNil(t, err, "Interrupts still active after close")
}
func TestWatchExists(t *testing.T) {
assert.Nil(t, Open())
defer Close()
pinIn := NewPin(J8p15)
pinIn.SetMode(Input)
count := 0
assert.Nil(t, pinIn.Watch(EdgeFalling, func(pin *Pin) {
count++
}))
assert.NotNil(t, pinIn.Watch(EdgeFalling, func(pin *Pin) {
count++
}))
time.Sleep(2 * time.Millisecond)
if count != 1 {
t.Error("Second handler called")
}
}
// Looped tests require a jumper across Raspberry Pi J8 pins 15 and 16.
// This is just a smoke test for the Watch and Unwatch methods.
func TestWatchLooped(t *testing.T) {
assert.Nil(t, Open())
defer Close()
pinIn := NewPin(J8p15)
pinOut := NewPin(J8p16)
pinIn.SetMode(Input)
defer pinOut.SetMode(Input)
pinOut.Write(Low)
pinOut.SetMode(Output)
mode := pinOut.Mode()
assert.Equal(t, Output, mode)
called := false
assert.Nil(t, pinIn.Watch(EdgeFalling, func(pin *Pin) {
called = true
}))
time.Sleep(2 * time.Millisecond)
assert.True(t, called)
called = false
pinOut.High()
time.Sleep(2 * time.Millisecond)
assert.False(t, called)
pinOut.Low()
time.Sleep(2 * time.Millisecond)
assert.True(t, called)
pinIn.Unwatch()
called = false
pinOut.High()
pinOut.Low()
time.Sleep(2 * time.Millisecond)
assert.False(t, called)
}
// This provides a coarse estimate of the interrupt latency,
// i.e. the time between an interrupt being triggered and handled.
// There is some overhead in there due to the handshaking via a channel etc...
// so this provides an upper bound.
func BenchmarkInterruptLatency(b *testing.B) {
assert.Nil(b, Open())
defer Close()
pinIn := NewPin(J8p15)
pinOut := NewPin(J8p16)
pinIn.SetMode(Input)
defer pinOut.SetMode(Input)
pinOut.Write(Low)
pinOut.SetMode(Output)
mode := pinOut.Mode()
assert.Equal(b, Output, mode)
ich := make(chan int)
assert.Nil(b, pinIn.Watch(EdgeBoth, func(pin *Pin) {
ich <- 1
}))
defer pinIn.Unwatch()
for i := 0; i < b.N; i++ {
pinOut.Toggle()
<-ich
}
}