go-emlid/reach/discovery/discovery_test.go

54 lines
948 B
Go
Raw Normal View History

2024-07-30 14:28:39 +02:00
package discovery
import (
"context"
"regexp"
"testing"
"time"
"forge.cadoles.com/cadoles/go-emlid/reach"
)
func TestDiscovery(t *testing.T) {
reach.AssertIntegrationTests(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
services, err := Discover(ctx)
if err != nil {
t.Fatal(err)
}
if g, e := len(services), 1; g < e {
t.Fatalf("len(services): got '%d', expected > %d", g, e)
}
t.Logf("Found %d services", len(services))
patterns := []string{"reach", "Reach", "^RS.*"}
for i, s := range services {
t.Logf("Service #%d: %s - %s:%d", i, s.Name, s.AddrV4.String(), s.Port)
matched := false
for _, p := range patterns {
re, err := regexp.Compile(p)
if err != nil {
t.Fatal(err)
}
if re.Match([]byte(s.Name)) {
matched = true
break
}
}
if !matched {
t.Errorf("services[%d].Name ('%s') to match on of '%v'", i, s.Name, patterns)
}
}
}