-
Notifications
You must be signed in to change notification settings - Fork 12
/
combiner_test.go
107 lines (88 loc) · 2.47 KB
/
combiner_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
package fiber_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/gojek/fiber"
testUtilsHttp "github.com/gojek/fiber/internal/testutils/http"
"github.com/gojek/fiber/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type mockFanOut struct {
mock.Mock
*fiber.BaseMultiRouteComponent
}
func (m *mockFanOut) Dispatch(ctx context.Context, req fiber.Request) fiber.ResponseQueue {
args := m.Called(ctx, req)
if queue := args.Get(0); queue != nil {
return queue.(fiber.ResponseQueue)
}
return nil
}
type mockFanIn struct {
mock.Mock
*fiber.BaseFanIn
}
func (fanIn *mockFanIn) Aggregate(ctx context.Context, req fiber.Request, queue fiber.ResponseQueue) fiber.Response {
args := fanIn.Called(ctx, req, queue)
return args.Get(0).(fiber.Response)
}
type combinerTestCase struct {
name string
request fiber.Request
responses fiber.ResponseQueue
expected fiber.Response
}
func (tt *combinerTestCase) MockFanOut() *mockFanOut {
fanOut := &mockFanOut{}
fanOut.On("Dispatch", mock.Anything, tt.request).Once().Return(tt.responses)
return fanOut
}
func (tt *combinerTestCase) MockFanIn() *mockFanIn {
fanIn := &mockFanIn{}
fanIn.On("Aggregate", mock.Anything, tt.request, tt.responses).Once().Return(tt.expected)
return fanIn
}
func TestCombiner_Dispatch(t *testing.T) {
timeout := 200 * time.Millisecond
suite := []combinerTestCase{
{
name: "two routes/two OK responseQueue",
request: testUtilsHttp.MockReq("POST", "http:/combiner:8080", ""),
expected: testUtilsHttp.MockResp(200, "A-OK,B-OK", nil, nil),
},
}
for _, tt := range suite {
fanOut := tt.MockFanOut()
fanIn := tt.MockFanIn()
combiner := fiber.NewCombiner("")
combiner.FanOut = fanOut
combiner.WithFanIn(fanIn)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
received := make([]fiber.Response, 0)
for responsesCh := combiner.Dispatch(ctx, tt.request).Iter(); ; {
select {
case resp, ok := <-responsesCh:
if ok {
received = append(received, resp)
continue
}
case <-time.After(timeout + 5*time.Millisecond):
assert.Fail(t, fmt.Sprintf("[%s] failed: it didn't terminate after a timeout...", tt.name))
}
cancel()
break
}
assert.Equal(t, 1, len(received))
assert.Equal(t, tt.expected, received[0])
fanOut.AssertExpectations(t)
fanIn.AssertExpectations(t)
}
}
func TestCombiner_Id(t *testing.T) {
id := util.UID()
combiner := fiber.NewCombiner(id)
assert.Equal(t, id, combiner.ID())
}