83 lines
1.6 KiB
Go
83 lines
1.6 KiB
Go
package testrunner
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"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) RunTestSet(TSet map[string]func() error) {
|
|
null, _ := os.OpenFile(os.DevNull, os.O_WRONLY, 0)
|
|
defer null.Close()
|
|
|
|
// Backups der originalen Deskriptoren
|
|
oldStdout := os.Stdout
|
|
oldStderr := os.Stderr
|
|
oldLogOut := log.Writer()
|
|
|
|
for name, f := range TSet {
|
|
|
|
// 1. Output komplett abdrehen
|
|
os.Stdout = null
|
|
os.Stderr = null
|
|
log.SetOutput(io.Discard)
|
|
|
|
err := f()
|
|
|
|
// 2. Sofort wiederherstellen für das Log-Resultat
|
|
os.Stdout = oldStdout
|
|
os.Stderr = oldStderr
|
|
log.SetOutput(oldLogOut)
|
|
|
|
if err != nil {
|
|
log.Printf("[%s]: \t\tFailed: %v", name, err)
|
|
continue
|
|
}
|
|
log.Printf("[%s]: \t\tSucceeded", name)
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
}
|