add more tests for router

This commit is contained in:
Joel Wetzell
2026-02-10 20:42:52 -06:00
parent 8bdd338a27
commit 90a14b4d1b

View File

@@ -140,7 +140,147 @@ func TestNewRouterDuplicateModuleId(t *testing.T) {
_, moduleErrors, _ := showbridge.NewRouter(routerConfig, tracer)
if moduleErrors == nil {
t.Fatalf("router should have returned 'duplicate id' module error")
t.Fatalf("router should have returned module error")
}
if len(moduleErrors) != 1 {
t.Fatalf("router should have returned exactly 1 module error, got: %d", len(moduleErrors))
}
if moduleErrors[0].Error.Error() != "module id already exists" {
t.Fatalf("module error did not match expected, got: %s", moduleErrors[0].Error.Error())
}
}
func TestNewRouterRouteWithUnknwonProcessor(t *testing.T) {
routerConfig := config.Config{
Modules: []config.ModuleConfig{
{
Id: "mock",
Type: "mock.counter",
},
},
Routes: []config.RouteConfig{
{
Input: "mock",
Processors: []config.ProcessorConfig{
{
Type: "asdfasdf",
},
},
Output: "mock",
},
},
}
_, _, routeErrors := showbridge.NewRouter(routerConfig, tracer)
if routeErrors == nil {
t.Fatalf("router should have returned a route error")
}
if len(routeErrors) != 1 {
t.Fatalf("router should have returned exactly 1 route error, got: %d", len(routeErrors))
}
if routeErrors[0].Error.Error() != "problem loading processor registration for processor type: asdfasdf" {
t.Fatalf("route error did not match expected, got: %s", routeErrors[0].Error.Error())
}
}
func TestRouterInputUnknownDestinationModule(t *testing.T) {
routerConfig := config.Config{
Modules: []config.ModuleConfig{
{
Id: "mock",
Type: "mock.counter",
},
},
Routes: []config.RouteConfig{
{
Input: "mock",
Output: "test",
},
},
}
router, moduleErrors, routeErrors := showbridge.NewRouter(routerConfig, tracer)
if moduleErrors != nil {
t.Fatalf("router should not have returned any module errors: %v", moduleErrors)
}
if routeErrors != nil {
t.Fatalf("router should not have returned any route errors: %v", routeErrors)
}
routerRunner := sync.WaitGroup{}
routerRunner.Go(func() {
router.Start(t.Context())
fmt.Println("router stopped")
})
time.Sleep(time.Second * 1)
defer router.Stop()
_, routingErrors := router.HandleInput(t.Context(), "mock", "test")
if routingErrors == nil {
t.Fatalf("router should encounter routing errors when trying to route to an unknown module")
}
if len(routingErrors) != 1 {
t.Fatalf("router should have returned exactly 1 routing error, got: %d", len(routingErrors))
}
if routingErrors[0].OutputError.Error() != "no module found for destination id" {
t.Fatalf("routing output error did not match expected, got: %s", routingErrors[0].OutputError.Error())
}
}
func TestRouterInputNoMatchingRoute(t *testing.T) {
routerConfig := config.Config{
Modules: []config.ModuleConfig{
{
Id: "mock",
Type: "mock.counter",
},
},
Routes: []config.RouteConfig{
{
Input: "test",
Output: "mock",
},
},
}
router, moduleErrors, routeErrors := showbridge.NewRouter(routerConfig, tracer)
if moduleErrors != nil {
t.Fatalf("router should not have returned any module errors: %v", moduleErrors)
}
if routeErrors != nil {
t.Fatalf("router should not have returned any route errors: %v", routeErrors)
}
routerRunner := sync.WaitGroup{}
routerRunner.Go(func() {
router.Start(t.Context())
fmt.Println("router stopped")
})
time.Sleep(time.Second * 1)
defer router.Stop()
aRouteFound, _ := router.HandleInput(t.Context(), "mock", "test")
if aRouteFound {
t.Fatalf("router should not have found a matching route for the input")
}
}