-
Notifications
You must be signed in to change notification settings - Fork 12
/
lazy_router_test.go
170 lines (160 loc) · 5.37 KB
/
lazy_router_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package fiber_test
import (
"context"
"errors"
"fmt"
"testing"
"time"
"github.com/gojek/fiber"
fiberErrors "github.com/gojek/fiber/errors"
"github.com/gojek/fiber/internal/testutils"
testUtilsHttp "github.com/gojek/fiber/internal/testutils/http"
"github.com/gojek/fiber/protocol"
"github.com/stretchr/testify/assert"
)
type lazyRouterTestCase struct {
name string
routes map[string]fiber.Component
strategy []string
strategyLatency time.Duration
strategyException error
expected []fiber.Response
timeout time.Duration
}
func TestLazyRouter_Dispatch(t *testing.T) {
suite := []lazyRouterTestCase{
{
name: "ok: successful response",
routes: map[string]fiber.Component{
"route-a": testutils.NewMockComponent(
"route-a",
testUtilsHttp.DelayedResponse{Response: testUtilsHttp.MockResp(200, "A-OK", nil, nil)}),
"route-b": testutils.NewMockComponent(
"route-b",
testUtilsHttp.DelayedResponse{Response: testUtilsHttp.MockResp(200, "B-OK", nil, nil)}),
},
strategy: []string{
"route-b", "route-a",
},
expected: []fiber.Response{
testUtilsHttp.MockResp(200, "B-OK", nil, nil).WithBackendName("route-b"),
},
timeout: 100 * time.Millisecond,
},
{
name: "ok: first route failed, fallback response succeeded",
routes: map[string]fiber.Component{
"route-a": testutils.NewMockComponent(
"route-a",
testUtilsHttp.DelayedResponse{Response: testUtilsHttp.MockResp(502, "A-NOK", nil, fiberErrors.ErrNoValidResponseFromRoutes(protocol.HTTP))}),
"route-b": testutils.NewMockComponent(
"route-b",
testUtilsHttp.DelayedResponse{Response: testUtilsHttp.MockResp(200, "B-OK", nil, nil)}),
},
strategy: []string{
"route-a", "route-b",
},
expected: []fiber.Response{
testUtilsHttp.MockResp(200, "B-OK", nil, nil).WithBackendName("route-b"),
},
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(502, "A-NOK", nil, fiberErrors.ErrNoValidResponseFromRoutes(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(502, "", nil, fiberErrors.ErrNoValidResponseFromRoutes(protocol.HTTP)),
},
timeout: 100 * time.Millisecond,
},
{
name: "error: routing strategy succeeded, but route timeout exceeded",
routes: map[string]fiber.Component{
"route-a": testutils.NewMockComponent(
"route-a",
testUtilsHttp.DelayedResponse{
Latency: 100 * time.Millisecond,
Response: testUtilsHttp.MockResp(200, "A-OK", nil, nil)}),
"route-b": testutils.NewMockComponent(
"route-b",
testUtilsHttp.DelayedResponse{Response: testUtilsHttp.MockResp(200, "B-OK", nil, nil)}),
},
strategy: []string{
"route-a", "route-b",
},
strategyLatency: 50 * time.Millisecond,
expected: []fiber.Response{
testUtilsHttp.MockResp(408, "", nil, fiberErrors.ErrRequestTimeout(protocol.HTTP)),
},
timeout: 100 * time.Millisecond,
},
{
name: "error: strategy timeout exceeded",
strategyLatency: 200 * time.Millisecond,
expected: []fiber.Response{
testUtilsHttp.MockResp(500, "", nil, fiberErrors.ErrRouterStrategyTimeoutExceeded(protocol.HTTP)),
},
timeout: 100 * time.Millisecond,
},
{
name: "error: routing strategy returned empty routes",
strategy: []string{},
expected: []fiber.Response{
testUtilsHttp.MockResp(501, "", nil, fiberErrors.ErrRouterStrategyReturnedEmptyRoutes(protocol.HTTP)),
},
timeout: 100 * time.Millisecond,
},
{
name: "error: routing strategy responded with exception",
strategyException: errors.New("unexpected exception happened"),
expected: []fiber.Response{
testUtilsHttp.MockResp(500, "", nil, fiberErrors.NewFiberError(protocol.HTTP, errors.New("unexpected exception happened"))),
},
timeout: 100 * time.Millisecond,
},
}
for _, tt := range suite {
t.Run(tt.name, func(t *testing.T) {
router := fiber.NewLazyRouter("lazy-router")
router.SetRoutes(tt.routes)
strategy := testutils.NewMockRoutingStrategy(
tt.routes,
tt.strategy,
tt.strategyLatency,
tt.strategyException)
router.SetStrategy(strategy)
ctx, cancel := context.WithTimeout(context.Background(), tt.timeout)
received := make([]fiber.Response, 0)
request := testUtilsHttp.MockReq("POST", "http://localhost:8080/lazy-router", "payload")
for responsesCh := router.Dispatch(ctx, request).Iter(); ; {
select {
case resp, ok := <-responsesCh:
if ok {
received = append(received, resp)
continue
}
case <-time.After(tt.timeout + tt.timeout/2):
assert.Fail(t, fmt.Sprintf("[%s] failed: it didn't terminate after a timeout...", tt.name))
}
cancel()
break
}
assert.Equal(t, len(tt.expected), len(received), tt.name)
for i := 0; i < len(tt.expected); i++ {
assert.Equal(t, tt.expected[i].Payload(), received[i].Payload(), tt.name)
assert.Equal(t, tt.expected[i].StatusCode(), received[i].StatusCode(), tt.name)
}
strategy.AssertExpectations(t)
})
}
}