Added Dummy Tests

This commit is contained in:
simon 2026-01-27 17:36:41 +01:00
parent 3c011e272b
commit 028b6feae8
4 changed files with 117 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"log" "log"
"time" "time"
@ -8,28 +9,61 @@ import (
"alox.tool/api" "alox.tool/api"
"alox.tool/eventbus" "alox.tool/eventbus"
"alox.tool/frontend" "alox.tool/frontend"
"alox.tool/testrunner"
"alox.tool/uart" "alox.tool/uart"
) )
var (
Tests bool
)
func main() { func main() {
flag.BoolVar(&Tests, "t", false, "Tests")
flag.Parse()
config := Config{ config := Config{
Port: 8000, Port: 8000,
Host: "0.0.0.0", Host: "0.0.0.0",
UartPort: "/dev/ttyUSB0", UartPort: "/dev/ttyUSB0",
Baudrate: 115200, Baudrate: 115200,
} }
if Tests {
StartTests(config)
return
}
StartApp(config) StartApp(config)
} }
func StartApp(config Config) { func StartTests(config Config) {
bus := eventbus.New() bus := eventbus.New()
com, err := uart.Connect(bus, config.UartPort, config.Baudrate) com, err := uart.Connect(bus, config.UartPort, config.Baudrate)
if err != nil { if err != nil {
log.Printf("Could not Connect with Uart Device %v", err) log.Printf("Could not Connect with Uart Device %v", err)
} }
defer com.Close() defer com.Close()
tr := testrunner.New(bus, com)
testTest1 := make(map[string]func() error)
testTest1["Echo Test"] = tr.RunEchoTest
testTest1["Version Test"] = tr.RunVersionTest
testTest1["Info Test"] = tr.RunClientInfoTest
testTest1["Input Test"] = tr.RunClientInputTest
tr.RunTestSet(testTest1)
}
func StartApp(config Config) {
bus := eventbus.New()
com, err := uart.Connect(bus, config.UartPort, config.Baudrate)
if err != nil {
log.Printf("Could not Connect with Uart Device %v", err)
}
defer com.Close()
tr := testrunner.New(bus, com)
tr.RunVersionTest()
go func() { go func() {
for { for {
select { select {
@ -52,7 +86,7 @@ func StartApp(config Config) {
log.Printf("Could not Parse Version %v", err) log.Printf("Could not Parse Version %v", err)
continue continue
} }
log.Printf("Version Info %d %X", v.Version, v.Buildhash) log.Printf("Version Info %d %s", v.Version, v.Buildhash)
case api.CmdClientInfo: case api.CmdClientInfo:
v, err := uart.ParseFrameClientInfo(val) v, err := uart.ParseFrameClientInfo(val)
if err != nil { if err != nil {

View File

@ -3,6 +3,9 @@ package testrunner
import ( import (
"context" "context"
"fmt" "fmt"
"io"
"log"
"os"
"time" "time"
"alox.tool/api" "alox.tool/api"
@ -22,6 +25,37 @@ func New(bus eventbus.EventBus, com *uart.Com) *TestRunner {
} }
} }
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) { func (tr *TestRunner) Expect(idToSend byte, payload []byte, expectedID byte, timeout time.Duration) (*api.Frame, error) {
rxChan := tr.bus.Subscribe(api.TopicUARTRx) rxChan := tr.bus.Subscribe(api.TopicUARTRx)
defer tr.bus.Unsubscribe(api.TopicUARTRx, rxChan) defer tr.bus.Unsubscribe(api.TopicUARTRx, rxChan)

View File

@ -5,17 +5,58 @@ import (
"time" "time"
"alox.tool/api" "alox.tool/api"
"alox.tool/uart"
) )
func (tr *TestRunner) RunVersionTest() error { func (tr *TestRunner) RunEchoTest() error {
fmt.Println("Starte Version Test...") frame, err := tr.Expect(api.CmdEcho, []byte{0x01}, api.CmdEcho, 1*time.Second)
if err != nil {
return err
}
// Sende VersionRequest (0x02), erwarte VersionResponse (0x02) if frame.Data[0] != 0x01 {
return fmt.Errorf("Got % X expected % X", frame.Data[0], 0x01)
}
return nil
}
func (tr *TestRunner) RunVersionTest() error {
frame, err := tr.Expect(api.CmdVersion, nil, api.CmdVersion, 1*time.Second) frame, err := tr.Expect(api.CmdVersion, nil, api.CmdVersion, 1*time.Second)
if err != nil { if err != nil {
return err return err
} }
fmt.Printf("Test bestanden! Hardware Version ist: %s\n", string(frame.Data)) // TODO: Check Version and Buildhash?
_, err = uart.ParseFrameVersion(*frame)
if err != nil {
return err
}
v, err := uart.ParseFrameVersion(*frame)
if v.Version != 1 {
return fmt.Errorf("Got %d expected %d", v.Version, 1)
}
return nil
}
func (tr *TestRunner) RunClientInfoTest() error {
_, err := tr.Expect(api.CmdClientInfo, nil, api.CmdClientInfo, 1*time.Second)
if err != nil {
return err
}
// TODO: Real Check
return nil
}
func (tr *TestRunner) RunClientInputTest() error {
_, err := tr.Expect(api.CmdClientInput, nil, api.CmdClientInput, 1*time.Second)
if err != nil {
return err
}
// TODO: Real Check
return nil return nil
} }

View File

@ -35,7 +35,7 @@ func Connect(bus eventbus.EventBus, portName string, baudrate int) (*Com, error)
default: default:
n, err := port.Read(buff) n, err := port.Read(buff)
if err != nil { if err != nil {
log.Print("Read Error:", err) log.Print("[Warning]: Read Error:", err)
return // Loop beenden bei Hardware-Fehler return // Loop beenden bei Hardware-Fehler
} }
if n > 0 { if n > 0 {