-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdispatcher_test.go
202 lines (190 loc) · 5.25 KB
/
dispatcher_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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package grpc
import (
"errors"
"fmt"
"os"
"testing"
"time"
"github.com/gojek/fiber"
fiberError "github.com/gojek/fiber/errors"
"github.com/gojek/fiber/http"
testproto "github.com/gojek/fiber/internal/testdata/gen/testdata/proto"
testutils "github.com/gojek/fiber/internal/testutils/grpc"
"github.com/gojek/fiber/protocol"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
)
const (
port = 50055
serviceMethod = "testproto.UniversalPredictionService/PredictValues"
)
var mockResponse *testproto.PredictValuesResponse
func TestMain(m *testing.M) {
mockResponse = &testproto.PredictValuesResponse{
Predictions: []*testproto.PredictionResult{
{
RowId: "1",
Value: &testproto.NamedValue{
Name: "str",
Type: testproto.NamedValue_TYPE_STRING,
StringValue: "213",
},
},
{
RowId: "2",
Value: &testproto.NamedValue{
Name: "double",
Type: testproto.NamedValue_TYPE_DOUBLE,
DoubleValue: 123.45,
},
},
{
RowId: "3",
Value: &testproto.NamedValue{
Name: "int",
Type: testproto.NamedValue_TYPE_INTEGER,
IntegerValue: 2,
},
},
},
Metadata: &testproto.ResponseMetadata{
PredictionId: "abc",
ModelName: "linear",
ModelVersion: "1.2",
ExperimentId: "1",
TreatmentId: "2",
},
}
//Test server will run upi server at port 50055
testutils.RunTestUPIServer(
testutils.GrpcTestServer{
Port: port,
MockResponse: mockResponse,
},
)
os.Exit(m.Run())
}
func TestNewDispatcher(t *testing.T) {
tests := []struct {
name string
dispatcherConfig DispatcherConfig
expected *Dispatcher
expectedErr *fiberError.FiberError
}{
{
name: "empty endpoint",
dispatcherConfig: DispatcherConfig{
ServiceMethod: serviceMethod,
},
expected: nil,
expectedErr: fiberError.ErrInvalidInput(
protocol.GRPC,
errors.New("grpc dispatcher: missing config (endpoint/serviceMethod)")),
},
{
name: "empty serviceMethod",
dispatcherConfig: DispatcherConfig{
Endpoint: fmt.Sprintf(":%d", port),
},
expected: nil,
expectedErr: fiberError.ErrInvalidInput(
protocol.GRPC,
errors.New("grpc dispatcher: missing config (endpoint/serviceMethod)")),
},
{
name: "ok response",
dispatcherConfig: DispatcherConfig{
ServiceMethod: serviceMethod,
Endpoint: fmt.Sprintf(":%d", port),
Timeout: time.Second * 5,
},
expected: &Dispatcher{
timeout: time.Second * 5,
serviceMethod: fmt.Sprintf("/%s", serviceMethod),
endpoint: fmt.Sprintf(":%d", port),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewDispatcher(tt.dispatcherConfig)
if tt.expectedErr != nil {
fiberErr, ok := err.(*fiberError.FiberError)
require.True(t, ok, "error not fiber error")
require.Equal(t, tt.expectedErr, fiberErr)
} else {
require.NoError(t, err)
// responseProto and conn are ignored as they have pointer which value will not be identical
diff := cmp.Diff(tt.expected, got,
cmpopts.IgnoreFields(Dispatcher{}, "conn"),
cmp.AllowUnexported(Dispatcher{}),
)
require.Empty(t, diff)
}
})
}
}
func TestDispatcher_Do(t *testing.T) {
dispatcherConfig := DispatcherConfig{
ServiceMethod: serviceMethod,
Endpoint: fmt.Sprintf(":%d", port),
Timeout: time.Second * 5,
}
dispatcher, err := NewDispatcher(dispatcherConfig)
require.NoError(t, err, "unable to create dispatcher")
tests := []struct {
name string
dispatcherConfig *DispatcherConfig
responseProto proto.Message
input fiber.Request
expected fiber.Response
}{
{
name: "non grpc request",
input: &http.Request{},
expected: fiber.NewErrorResponse(fiberError.FiberError{
Code: int(codes.InvalidArgument),
Message: "fiber: grpc dispatcher: only grpc.Request type of requests are supported",
}),
},
{
name: "success",
input: &Request{
Message: []byte{},
},
expected: &Response{
Metadata: metadata.New(map[string]string{
"content-type": "application/grpc",
}),
Status: *status.New(codes.OK, "Success"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
response := dispatcher.Do(tt.input)
errResponse, ok := response.(*fiber.ErrorResponse)
if ok {
assert.EqualValues(t, tt.expected, errResponse)
} else {
grpcResponse, ok := response.(*Response)
if !ok {
assert.FailNow(t, "Fail to type assert response")
}
require.EqualValues(t, tt.expected.StatusCode(), grpcResponse.StatusCode())
require.EqualValues(t, tt.expected.BackendName(), grpcResponse.BackendName())
require.EqualValues(t, tt.expected.IsSuccess(), grpcResponse.IsSuccess())
responseProto := &testproto.PredictValuesResponse{}
err = proto.Unmarshal(grpcResponse.Payload(), responseProto)
require.NoError(t, err)
assert.True(t, proto.Equal(mockResponse, responseProto), "actual proto response don't match expected")
}
})
}
}