go-emlid/reach/client/protocol/testsuite/operations.go

126 lines
2.6 KiB
Go
Raw Normal View History

2024-07-30 14:28:39 +02:00
package testsuite
import (
"context"
"os"
"testing"
"time"
"forge.cadoles.com/cadoles/go-emlid/reach/client/protocol"
"github.com/davecgh/go-spew/spew"
"github.com/pkg/errors"
)
type OperationsFactoryFunc func(addr string) (protocol.Operations, error)
type operationTestCase struct {
Name string
Run func(t *testing.T, ops protocol.Operations)
}
var testCases = []operationTestCase{
{
Name: "Connect to ReachView",
Run: func(t *testing.T, ops protocol.Operations) {
ctx := context.Background()
if err := ops.Connect(ctx); err != nil {
t.Errorf("%+v", errors.WithStack(err))
return
}
defer func() {
if err := ops.Close(ctx); err != nil {
t.Errorf("%+v", errors.WithStack(err))
}
}()
alive, err := ops.Alive(ctx)
if err != nil {
t.Errorf("%+v", errors.WithStack(err))
return
}
if e, g := true, alive; e != g {
t.Errorf("alive: expected '%v', got '%v'", e, g)
}
},
},
{
Name: "Retrieve ReachView version",
Run: func(t *testing.T, ops protocol.Operations) {
ctx := context.Background()
version, stable, err := ops.Version(ctx)
if err != nil {
t.Errorf("%+v", errors.WithStack(err))
return
}
t.Logf("ReachView version: %s", version)
t.Logf("ReachView stable channel: %v", stable)
},
},
{
Name: "Listen to ReachView 'broadcast' messages",
Run: func(t *testing.T, ops protocol.Operations) {
ctx := context.Background()
if err := ops.Connect(ctx); err != nil {
t.Errorf("%+v", errors.WithStack(err))
return
}
defer func() {
if err := ops.Close(ctx); err != nil {
t.Errorf("%+v", errors.WithStack(err))
}
}()
broadcastCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
messages, err := ops.On(broadcastCtx, "broadcast")
if err != nil {
t.Errorf("%+v", errors.WithStack(err))
return
}
count := 0
for m := range messages {
t.Logf("new message: %s", spew.Sdump(m))
count++
}
if e, g := 1, count; g < e {
t.Errorf("expected total messages > %d, got %d", e, g)
}
},
},
}
func TestOperations(t *testing.T, opsFactory OperationsFactoryFunc) {
assertIntegrationTests(t)
addr := os.Getenv("REACHRS_HOST")
if addr == "" {
addr = "192.168.42.1"
t.Logf("Targeting '%s'. You can modify targeted host by specifying environment variable REACHRS_HOST", addr)
} else {
t.Logf("Targeting '%s'", addr)
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
ops, err := opsFactory(addr)
if err != nil {
t.Fatalf("could not initialize protocol operations: %+v", errors.WithStack(err))
}
tc.Run(t, ops)
})
}
}