-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_batch_test.go
306 lines (276 loc) · 9.5 KB
/
main_batch_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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package main_test
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"testing"
"time"
"github.com/kava-labs/kava-proxy-service/clients/database"
"github.com/kava-labs/kava-proxy-service/decode"
"github.com/kava-labs/kava-proxy-service/service/cachemdw"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/require"
)
func buildBigBatch(n int, sameBlock bool) []*decode.EVMRPCRequestEnvelope {
batch := make([]*decode.EVMRPCRequestEnvelope, 0, n)
// create n requests
for i := 0; i < n; i++ {
block := "0x1"
if !sameBlock {
block = fmt.Sprintf("0x%s", strconv.FormatInt(int64(i)+1, 16))
}
batch = append(batch, &decode.EVMRPCRequestEnvelope{
JSONRPCVersion: "2.0",
ID: i,
Method: "eth_getBlockByNumber",
Params: []interface{}{block, false},
})
}
return batch
}
func TestE2ETest_ValidBatchEvmRequests(t *testing.T) {
redisClient := redis.NewClient(&redis.Options{
Addr: redisURL,
Password: redisPassword,
DB: 0,
})
cleanUpRedis(t, redisClient)
expectKeysNum(t, redisClient, 0)
db, err := database.NewPostgresClient(databaseConfig)
require.NoError(t, err)
cleanMetricsDb(t, db)
// NOTE! ordering matters for these tests! earlier request responses may end up in the cache.
testCases := []struct {
name string
req []*decode.EVMRPCRequestEnvelope
expectedCacheHeader string
expectedErrStatus int
expectedNumMetrics int
}{
{
name: "first request, valid & not coming from the cache",
req: []*decode.EVMRPCRequestEnvelope{
{
JSONRPCVersion: "2.0",
ID: "magic!",
Method: "eth_getBlockByNumber",
Params: []interface{}{"0x1", false},
},
},
expectedCacheHeader: cachemdw.CacheMissHeaderValue,
expectedNumMetrics: 1,
},
{
name: "multiple requests, valid & none coming from the cache",
req: []*decode.EVMRPCRequestEnvelope{
{
JSONRPCVersion: "2.0",
ID: "magic!",
Method: "eth_getBlockByNumber",
Params: []interface{}{"0x2", false},
},
{
JSONRPCVersion: "2.0",
ID: 123456,
Method: "eth_getBlockByNumber",
Params: []interface{}{"0x3", false},
},
},
expectedCacheHeader: cachemdw.CacheMissHeaderValue,
expectedNumMetrics: 2,
},
{
name: "multiple requests, valid & some coming from the cache",
req: []*decode.EVMRPCRequestEnvelope{
{
JSONRPCVersion: "2.0",
ID: "magic!",
Method: "eth_getBlockByNumber",
Params: []interface{}{"0x2", false},
},
{
JSONRPCVersion: "2.0",
ID: 123456,
Method: "eth_getBlockByNumber",
Params: []interface{}{"0x4", false},
},
},
expectedCacheHeader: cachemdw.CachePartialHeaderValue,
expectedNumMetrics: 2,
},
{
name: "multiple requests, valid & all coming from the cache",
req: []*decode.EVMRPCRequestEnvelope{
{
JSONRPCVersion: "2.0",
ID: "magic!",
Method: "eth_getBlockByNumber",
Params: []interface{}{"0x2", false},
},
{
JSONRPCVersion: "2.0",
ID: nil,
Method: "eth_getBlockByNumber",
Params: []interface{}{"0x1", false},
},
{
JSONRPCVersion: "2.0",
ID: 123456,
Method: "eth_getBlockByNumber",
Params: []interface{}{"0x3", false},
},
},
expectedCacheHeader: cachemdw.CacheHitHeaderValue,
expectedNumMetrics: 3,
},
{
name: "empty request",
req: []*decode.EVMRPCRequestEnvelope{nil}, // <-- empty!
expectedCacheHeader: cachemdw.CacheMissHeaderValue,
expectedNumMetrics: 0,
},
{
name: "empty & non-empty requests, partial cache hit",
req: []*decode.EVMRPCRequestEnvelope{
nil, // <-- empty!
{
JSONRPCVersion: "2.0",
ID: "this block is in the cache",
Method: "eth_getBlockByNumber",
Params: []interface{}{"0x1", false},
},
},
expectedCacheHeader: cachemdw.CachePartialHeaderValue,
expectedNumMetrics: 1,
},
{
name: "empty & non-empty requests, cache miss",
req: []*decode.EVMRPCRequestEnvelope{
nil, // <-- empty!
{
JSONRPCVersion: "2.0",
ID: "this block is NOT in the cache",
Method: "eth_getBlockByNumber",
Params: []interface{}{"0xa", false},
},
},
expectedCacheHeader: cachemdw.CacheMissHeaderValue,
expectedNumMetrics: 1,
},
{
name: "big-as-can-be batch, some cache hits",
req: buildBigBatch(proxyServiceMaxBatchSize, false),
expectedCacheHeader: cachemdw.CachePartialHeaderValue,
expectedNumMetrics: proxyServiceMaxBatchSize,
},
{
name: "big-as-can-be batch, all cache hits",
req: buildBigBatch(proxyServiceMaxBatchSize, true),
expectedCacheHeader: cachemdw.CacheHitHeaderValue,
expectedNumMetrics: proxyServiceMaxBatchSize,
},
{
name: "too-big batch => responds 413",
req: buildBigBatch(proxyServiceMaxBatchSize+1, false),
expectedCacheHeader: cachemdw.CacheHitHeaderValue,
expectedErrStatus: http.StatusRequestEntityTooLarge,
expectedNumMetrics: 0,
},
}
for _, tc := range testCases {
startTime := time.Now()
time.Sleep(1 * time.Second) // ensure startTime will be far from metrics starting
t.Run(tc.name, func(t *testing.T) {
reqInJSON, err := json.Marshal(tc.req)
require.NoError(t, err)
resp, err := http.Post(proxyServiceURL, "application/json", bytes.NewBuffer(reqInJSON))
require.NoError(t, err)
if tc.expectedErrStatus != 0 {
require.Equal(t, tc.expectedErrStatus, resp.StatusCode, "unexpected response status")
return
}
require.Equal(t, http.StatusOK, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err, "failed to read response body")
var decoded []*jsonRpcResponse
err = json.Unmarshal(body, &decoded)
require.NoError(t, err, "failed to unmarshal response into array of responses")
// expect same number of responses as requests
require.Len(t, decoded, len(tc.req))
// expect matching ids
for i, d := range decoded {
var reqId interface{} = nil
if tc.req[i] != nil {
reqId = tc.req[i].ID
}
// EqualValues here because json ints unmarshal as float64s
require.EqualValues(t, reqId, d.Id)
}
// check expected cache status header
require.Equal(t, tc.expectedCacheHeader, resp.Header.Get(cachemdw.CacheHeaderKey))
// verify CORS header (only on cache hits. cache misses return the backend value, if set)
if tc.expectedCacheHeader == cachemdw.CacheHitHeaderValue {
require.Equal(t, resp.Header[accessControlAllowOriginHeaderName], []string{"*"})
}
if shouldSkipMetrics() {
return
}
// wait for all metrics to be created.
// besides verification, waiting for the metrics ensures future tests don't fail b/c metrics are being processed
waitForMetricsInWindow(t, tc.expectedNumMetrics, db, startTime, []string{})
})
}
// clear all metrics & cache state to make future metrics tests less finicky
// (more data increases the read/write to db & redis, and these tests make many db & cache entries)
cleanMetricsDb(t, db)
cleanUpRedis(t, redisClient)
}
func TestE2ETest_BatchEvmRequestErrorHandling(t *testing.T) {
t.Run("no backend configured (bad gateway error)", func(t *testing.T) {
validReq := []*decode.EVMRPCRequestEnvelope{
newJsonRpcRequest(123, "eth_getBlockByNumber", []interface{}{"0x1", false}),
newJsonRpcRequest("another-req", "eth_getBlockByNumber", []interface{}{"0x2", false}),
}
reqInJSON, err := json.Marshal(validReq)
require.NoError(t, err)
resp, err := http.Post(proxyUnconfiguredUrl, "application/json", bytes.NewBuffer(reqInJSON))
require.NoError(t, err)
require.Equal(t, http.StatusBadGateway, resp.StatusCode)
})
t.Run("empty batch", func(t *testing.T) {
emptyReq := []*decode.EVMRPCRequestEnvelope{}
reqInJSON, err := json.Marshal(emptyReq)
require.NoError(t, err)
resp, err := http.Post(proxyServiceURL, "application/json", bytes.NewBuffer(reqInJSON))
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err, "failed to read response body")
var decoded *jsonRpcResponse // <--- NOT an array response
err = json.Unmarshal(body, &decoded)
require.NoError(t, err, "failed to unmarshal response into array of responses")
require.Equal(t, -32600, decoded.JsonRpcError.Code)
require.Equal(t, "empty batch", decoded.JsonRpcError.Message)
})
t.Run("unsupported method", func(t *testing.T) {
resp, err := http.Get(proxyServiceURL) // <--- GET, not POST
require.NoError(t, err)
require.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode)
})
t.Run("invalid JSON", func(t *testing.T) {
req := []byte(`[{id:"almost valid json (missing double quotes around id!)"}]`)
resp, err := http.Post(proxyServiceURL, "application/json", bytes.NewBuffer(req))
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err, "failed to read response body")
var decoded *jsonRpcResponse // <--- NOT an array response
err = json.Unmarshal(body, &decoded)
require.NoError(t, err, "failed to unmarshal response into array of responses")
require.Equal(t, -32700, decoded.JsonRpcError.Code)
require.Equal(t, "parse error", decoded.JsonRpcError.Message)
})
}