From 0e0fe2c994b2f93e772a9d10db6390a34eab690e Mon Sep 17 00:00:00 2001 From: Krithika Sundararajan Date: Fri, 4 Nov 2022 09:31:37 +0800 Subject: [PATCH] Lazy router: Return error if no route succeeded --- lazy_router.go | 3 +-- lazy_router_test.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lazy_router.go b/lazy_router.go index b3a29f0..50eb5be 100644 --- a/lazy_router.go +++ b/lazy_router.go @@ -98,9 +98,8 @@ func (r *LazyRouter) Dispatch(ctx context.Context, req Request) ResponseQueue { } } } - } else { - out <- NewErrorResponse(errors.ErrRouterStrategyReturnedEmptyRoutes(req.Protocol())).WithLabels(labels) } + out <- NewErrorResponse(errors.ErrRouterStrategyReturnedEmptyRoutes(req.Protocol())).WithLabels(labels) }() return queue diff --git a/lazy_router_test.go b/lazy_router_test.go index cf1fc70..93ce6c3 100644 --- a/lazy_router_test.go +++ b/lazy_router_test.go @@ -63,6 +63,24 @@ func TestLazyRouter_Dispatch(t *testing.T) { }, timeout: 100 * time.Millisecond, }, + { + name: "error: no route succeeded", + routes: map[string]fiber.Component{ + "route-a": testutils.NewMockComponent( + "route-a", + testUtilsHttp.DelayedResponse{Response: testUtilsHttp.MockResp(500, "A-NOK", nil, fiberErrors.ErrServiceUnavailable(protocol.HTTP))}), + "route-b": testutils.NewMockComponent( + "route-b", + testUtilsHttp.DelayedResponse{Response: testUtilsHttp.MockResp(500, "B-NOK", nil, nil)}), + }, + strategy: []string{ + "route-a", "route-b", + }, + expected: []fiber.Response{ + testUtilsHttp.MockResp(501, "", nil, fiberErrors.ErrRouterStrategyReturnedEmptyRoutes(protocol.HTTP)), + }, + timeout: 100 * time.Millisecond, + }, { name: "error: routing strategy succeeded, but route timeout exceeded", routes: map[string]fiber.Component{