-
Notifications
You must be signed in to change notification settings - Fork 12
/
fan_out_test.go
156 lines (144 loc) · 4.46 KB
/
fan_out_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
package fiber_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/gojek/fiber"
"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 fanOutTestCase struct {
name string
responses map[string][]testUtilsHttp.DelayedResponse
}
func (tt *fanOutTestCase) Routes() map[string]fiber.Component {
routes := make(map[string]fiber.Component)
for name, resp := range tt.responses {
routes[name] = testutils.NewMockComponent(name, resp...)
}
return routes
}
func (tt *fanOutTestCase) ExpectedResponses(timeout time.Duration) map[string][]fiber.Response {
expectedResponses := make(map[string][]fiber.Response)
for route, responses := range tt.responses {
commutativeLatency := time.Duration(0)
for _, resp := range responses {
if commutativeLatency += resp.Latency; commutativeLatency < timeout {
expectedResponses[route] = append(expectedResponses[route], resp.Response)
} else {
break
}
}
}
return expectedResponses
}
func TestFanOut_Dispatch(t *testing.T) {
timeout := 100 * time.Millisecond
suite := []fanOutTestCase{
{
name: "two routes/two OK responseQueue",
responses: map[string][]testUtilsHttp.DelayedResponse{
"route-a": {
testUtilsHttp.DelayedResponse{Response: testUtilsHttp.MockResp(200, "A-OK", nil, nil)},
},
"route-b": {
testUtilsHttp.DelayedResponse{Response: testUtilsHttp.MockResp(200, "B-OK", nil, nil)},
},
},
},
{
name: "two routes/one OK response",
responses: map[string][]testUtilsHttp.DelayedResponse{
"route-a": {
testUtilsHttp.DelayedResponse{Response: testUtilsHttp.MockResp(200, "A-OK", nil, nil)},
},
"route-b": {},
},
},
{
name: "two routes/two OK responseQueue in each",
responses: map[string][]testUtilsHttp.DelayedResponse{
"route-a": {
testUtilsHttp.DelayedResponse{Response: testUtilsHttp.MockResp(200, "A-OK_1", nil, nil)},
testUtilsHttp.DelayedResponse{Response: testUtilsHttp.MockResp(200, "A-OK_2", nil, nil)},
},
"route-b": {
testUtilsHttp.DelayedResponse{Response: testUtilsHttp.MockResp(200, "B-OK_1", nil, nil)},
testUtilsHttp.DelayedResponse{Response: testUtilsHttp.MockResp(200, "B-OK_2", nil, nil)},
},
},
},
{
name: "one route/one NOK response",
responses: map[string][]testUtilsHttp.DelayedResponse{
"route-a": {
testUtilsHttp.DelayedResponse{Response: testUtilsHttp.MockResp(502, "", nil, errors.ErrNoValidResponseFromRoutes(protocol.HTTP))},
},
},
},
{
name: "one route/multiple responseQueue with delays",
responses: map[string][]testUtilsHttp.DelayedResponse{
"route-a": {
testUtilsHttp.DelayedResponse{
Latency: 10 * time.Millisecond,
Response: testUtilsHttp.MockResp(200, "OK", nil, nil),
},
testUtilsHttp.DelayedResponse{
Latency: timeout / 2,
Response: testUtilsHttp.MockResp(200, "OK", nil, nil),
},
// should never be received
testUtilsHttp.DelayedResponse{
Latency: 2 * timeout,
Response: testUtilsHttp.MockResp(200, "OK", nil, nil),
},
},
},
},
{
name: "two routes/one OK, one timeout",
responses: map[string][]testUtilsHttp.DelayedResponse{
"route-a": {
testUtilsHttp.DelayedResponse{
Latency: 2 * timeout,
Response: testUtilsHttp.MockResp(200, "OK", nil, nil),
},
},
"route-b": {
testUtilsHttp.DelayedResponse{Response: testUtilsHttp.MockResp(200, "OK", nil, nil)},
},
},
},
}
for _, tt := range suite {
fanOut := fiber.NewFanOut("")
fanOut.SetRoutes(tt.Routes())
ctx, cancel := context.WithTimeout(context.Background(), timeout)
var (
receivedResponses = make(map[string][]fiber.Response)
expectedResponses = tt.ExpectedResponses(timeout)
)
for responsesCh := fanOut.Dispatch(ctx, testUtilsHttp.MockReq("GET", "http://test:8080", "")).Iter(); ; {
select {
case resp, ok := <-responsesCh:
if ok {
receivedResponses[resp.BackendName()] = append(receivedResponses[resp.BackendName()], resp)
continue
}
case <-time.After(timeout + timeout/2):
assert.Fail(t, fmt.Sprintf("[%s] failed: it didn't terminate after a timeout...", tt.name))
}
cancel()
break
}
assert.Equal(t, len(expectedResponses), len(receivedResponses))
for name, resp := range expectedResponses {
assert.ElementsMatch(t, resp, receivedResponses[name])
}
}
}