-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathwait_test.go
435 lines (372 loc) · 9.46 KB
/
wait_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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// Copyright ©2016 The ev3go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ev3dev_test
import (
"fmt"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"github.com/ev3go/sisyphus"
. "github.com/ev3go/ev3dev"
)
var stateIsOKTests = []struct {
state MotorState
mask MotorState
want MotorState
not MotorState
any bool
wantOK bool
}{
{
state: 0,
wantOK: true,
},
{
state: Running,
mask: ^Running,
wantOK: true,
},
{
state: Running,
mask: Running,
want: 0,
wantOK: false,
},
{
state: Running,
mask: Running,
want: Running,
wantOK: true,
},
{
state: Running,
mask: Running | Stalled,
want: Running,
not: Stalled,
wantOK: true,
},
{
state: Running | Stalled,
mask: Running | Stalled,
want: Running,
not: Stalled,
wantOK: false,
},
{
state: Ramping,
mask: Running | Stalled | Overloaded | Ramping,
any: true,
wantOK: true,
},
{
state: Ramping,
mask: Running | Stalled | Overloaded | Ramping,
any: true,
not: Ramping,
wantOK: false,
},
{
state: Running | Stalled,
mask: Running | Stalled | Overloaded | Ramping,
any: true,
not: Ramping,
wantOK: true,
},
{
state: Running | Stalled | Overloaded,
mask: Running | Stalled | Overloaded,
any: true,
not: Stalled | Overloaded,
wantOK: false,
},
}
func TestStateIsOK(t *testing.T) {
for _, test := range stateIsOKTests {
gotOK := StateIsOK(test.state, test.mask, test.want, test.not, test.any)
if gotOK != test.wantOK {
t.Errorf("unexpected result for state=%s mask=%s want=%s not=%s any=%t: got:%t want:%t",
test.state, test.mask, test.want, test.not, test.any, gotOK, test.wantOK)
}
}
}
// waitMotor is a tacho motor sysfs directory.
type waitMotor struct {
address string
driver string
mu sync.Mutex
state MotorState
t *testing.T
}
func (m *waitMotor) setState(s MotorState) {
m.mu.Lock()
m.state = s
m.mu.Unlock()
}
// waitMotorAddress is the address attribute.
type waitMotorAddress waitMotor
// ReadAt satisfies the io.ReaderAt interface.
func (m *waitMotorAddress) ReadAt(b []byte, offset int64) (int, error) {
return readAt(b, offset, m.address)
}
// Size returns the length of the backing data and a nil error.
func (m *waitMotorAddress) Size() (int64, error) {
return size(m.address), nil
}
// waitMotorDriver is the driver_name attribute.
type waitMotorDriver waitMotor
// ReadAt satisfies the io.ReaderAt interface.
func (m *waitMotorDriver) ReadAt(b []byte, offset int64) (int, error) {
return readAt(b, offset, m.driver)
}
// Size returns the length of the backing data and a nil error.
func (m *waitMotorDriver) Size() (int64, error) {
return size(m.driver), nil
}
// waitMotorCommands is the commands attribute.
type waitMotorCommands waitMotor
// ReadAt satisfies the io.ReaderAt interface.
func (m *waitMotorCommands) ReadAt(b []byte, offset int64) (int, error) {
return readAt(b, offset, "none")
}
// Size returns the length of the backing data and a nil error.
func (m *waitMotorCommands) Size() (int64, error) {
return size("none"), nil
}
// waitMotorStopActions is the stop_actions attribute.
type waitMotorStopActions waitMotor
// ReadAt satisfies the io.ReaderAt interface.
func (m *waitMotorStopActions) ReadAt(b []byte, offset int64) (int, error) {
return readAt(b, offset, "none")
}
// Size returns the length of the backing data and a nil error.
func (m *waitMotorStopActions) Size() (int64, error) {
return size("none"), nil
}
// waitMotorMaxSpeed is the max_speed attribute.
type waitMotorMaxSpeed waitMotor
// ReadAt satisfies the io.ReaderAt interface.
func (m *waitMotorMaxSpeed) ReadAt(b []byte, offset int64) (int, error) {
return readAt(b, offset, "1200")
}
// Size returns the length of the backing data and a nil error.
func (m *waitMotorMaxSpeed) Size() (int64, error) {
return size("1200"), nil
}
// waitMotorCountsPerRot is the count_per_rot attribute.
type waitMotorCountPerRot waitMotor
// ReadAt satisfies the io.ReaderAt interface.
func (m *waitMotorCountPerRot) ReadAt(b []byte, offset int64) (int, error) {
return readAt(b, offset, "360")
}
// Size returns the length of the backing data and a nil error.
func (m *waitMotorCountPerRot) Size() (int64, error) {
return size("360"), nil
}
// waitMotorState is the state attribute.
type waitMotorState waitMotor
// ReadAt satisfies the io.ReaderAt interface.
func (m *waitMotorState) ReadAt(b []byte, offset int64) (int, error) {
return readAt(b, offset, m)
}
// Size returns the length of the backing data and a nil error.
func (m *waitMotorState) Size() (int64, error) {
return size(m), nil
}
// String returns a string representation of the attribute.
func (m *waitMotorState) String() string {
m.mu.Lock()
defer m.mu.Unlock()
s := strings.Replace(m.state.String(), "|", " ", -1)
if s == MotorState(0).String() {
return ""
}
return s
}
type waitMotorConn struct {
id int
waitMotor *waitMotor
}
func connectedWaitMotors(c ...waitMotorConn) []sisyphus.Node {
n := make([]sisyphus.Node, len(c))
for i, m := range c {
n[i] = d(fmt.Sprintf("motor%d", m.id), 0775).With(
ro(AddressName, 0444, (*waitMotorAddress)(m.waitMotor)),
ro(DriverNameName, 0444, (*waitMotorDriver)(m.waitMotor)),
ro(CommandsName, 0444, (*waitMotorCommands)(m.waitMotor)),
ro(CountPerRotName, 0444, (*waitMotorCountPerRot)(m.waitMotor)),
ro(MaxSpeedName, 0444, (*waitMotorMaxSpeed)(m.waitMotor)),
ro(StopActionsName, 0444, (*waitMotorStopActions)(m.waitMotor)),
ro(StateName, 0444, (*waitMotorState)(m.waitMotor)),
)
}
return n
}
func waitmotorsysfs(m ...waitMotorConn) *sisyphus.FileSystem {
return sisyphus.NewFileSystem(0775, clock).With(
d("sys", 0775).With(
d("class", 0775).With(
d("tacho-motor", 0775).With(
connectedWaitMotors(m...)...,
),
),
),
).Sync()
}
type period struct {
s MotorState
d time.Duration
}
func sequenceDuration(s []period) time.Duration {
var d time.Duration
for _, p := range s {
d += p.d
}
return d
}
var waitTests = []struct {
states []period
mask MotorState
query MotorState
not MotorState
any bool
timeout time.Duration
want MotorState
wantOK bool
}{
{
states: []period{
{s: 0, d: 200 * time.Millisecond},
{s: Running, d: 200 * time.Millisecond},
},
mask: Running,
query: Running,
timeout: 100 * time.Millisecond,
want: 0,
wantOK: false,
},
{
states: []period{
{s: 0, d: 200 * time.Millisecond},
{s: Running, d: 200 * time.Millisecond},
},
mask: Running,
query: Running,
timeout: time.Second,
want: Running,
wantOK: true,
},
{
states: []period{
{s: 0, d: 200 * time.Millisecond},
{s: Running | Stalled, d: 200 * time.Millisecond},
{s: Running, d: 200 * time.Millisecond},
},
mask: Running | Stalled,
query: Running,
not: Stalled,
timeout: time.Second,
want: Running,
wantOK: true,
},
{
states: []period{
{s: Running | Stalled | Overloaded, d: 200 * time.Millisecond},
{s: Holding, d: 200 * time.Millisecond},
},
mask: Running | Stalled | Overloaded,
any: true,
not: Stalled | Overloaded,
timeout: time.Second,
want: Holding,
wantOK: true,
},
{
states: []period{
{s: 0, d: 200 * time.Millisecond},
{s: Running | Stalled, d: 200 * time.Millisecond},
{s: Running, d: 200 * time.Millisecond},
},
mask: Running | Stalled,
any: true,
timeout: time.Second,
want: Running | Stalled,
wantOK: true,
},
{
states: []period{
{s: 0, d: 200 * time.Millisecond},
{s: Running, d: 200 * time.Millisecond},
},
mask: Running,
query: Running,
timeout: -1,
want: Running,
wantOK: true,
},
}
func TestWaitMotor(t *testing.T) {
const driver = "lego-ev3-l-motor"
conn := waitMotorConn{
id: 5,
waitMotor: &waitMotor{
address: "outA",
driver: driver,
t: t,
},
}
fs := waitmotorsysfs(conn)
unmount := serve(fs, t)
defer unmount()
t.Run("State", func(t *testing.T) {
m, err := TachoMotorFor(conn.waitMotor.address, conn.waitMotor.driver)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
for _, test := range waitTests {
timeout := 10 * sequenceDuration(test.states)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for _, p := range test.states {
conn.waitMotor.setState(p.s)
err = fs.InvalidatePath(filepath.Join(m.Path(), m.String(), StateName))
if err != nil {
t.Fatalf("unexpected error invalidating state: %v", err)
}
time.Sleep(p.d)
}
}()
ok := make(chan bool, 1)
go func() {
select {
case <-time.After(timeout):
t.Fatalf("failed to timeout after %v", timeout)
case <-ok:
}
}()
got, gotOK, err := Wait(m, test.mask, test.query, test.not, test.any, test.timeout)
ok <- true
if err != nil {
// FIXME(kortschak): Occasionally FUSE appears to be in a state
// where the reported size of the attribute is 1 ("\n") but the value
// of the attribute is "running\n". The first cached byte is read
// and the rest of the attribute bytes are appended giving "\nunning".
// This causes an error state to be returned by Wait.
// I suspect that this would be fixed with FUSE direct_io.
goto next
}
if got != test.want {
t.Errorf("unexpected motor state: got:%v want:%v", got, test.want)
}
if gotOK != test.wantOK {
t.Errorf("unexpected success: got:%t want:%t", gotOK, test.wantOK)
}
next:
wg.Wait()
}
})
}