49 lines
974 B
Go
49 lines
974 B
Go
package testrunner
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"alox.tool/api"
|
|
"alox.tool/eventbus"
|
|
"alox.tool/uart"
|
|
)
|
|
|
|
type TestRunner struct {
|
|
bus eventbus.EventBus
|
|
com *uart.Com
|
|
}
|
|
|
|
func New(bus eventbus.EventBus, com *uart.Com) *TestRunner {
|
|
return &TestRunner{
|
|
bus: bus,
|
|
com: com,
|
|
}
|
|
}
|
|
|
|
func (tr *TestRunner) Expect(idToSend byte, payload []byte, expectedID byte, timeout time.Duration) (*api.Frame, error) {
|
|
rxChan := tr.bus.Subscribe(api.TopicUARTRx)
|
|
defer tr.bus.Unsubscribe(api.TopicUARTRx, rxChan)
|
|
|
|
if err := tr.com.Send(idToSend, payload); err != nil {
|
|
return nil, fmt.Errorf("send error: %w", err)
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
|
defer cancel()
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil, fmt.Errorf("timeout waiting for ID 0x%02X", expectedID)
|
|
case frame := <-rxChan:
|
|
f := frame.(api.Frame)
|
|
if f.ID == expectedID {
|
|
return &f, nil
|
|
}
|
|
// Ignore other IDs and Messages on the Bus
|
|
}
|
|
}
|
|
}
|