Skip to content

Commit 87982ae

Browse files
Ink-33LinuxSuRen
authored andcommitted
chore(grpc_test): add JSON cache bypass
Signed-off-by: Ink33 <[email protected]>
1 parent 1ec00d1 commit 87982ae

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

pkg/runner/grpc_test.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ func doGRPCTest(t *testing.T, l net.Listener, sec *atest.Secure, desc *atest.GRP
302302
Body: "{}",
303303
},
304304
Expect: atest.Response{
305-
Body: getJSONOrCache("unaryneq", &testsrv.HelloReply{
305+
Body: getJSONOrCache(nil, &testsrv.HelloReply{
306306
Message: "Happy!",
307307
}),
308308
},
@@ -376,7 +376,7 @@ func doGRPCTest(t *testing.T, l net.Listener, sec *atest.Secure, desc *atest.GRP
376376
Body: getJSONOrCache("stream", nil),
377377
},
378378
Expect: atest.Response{
379-
Body: getJSONOrCache("streamneq", []*testsrv.StreamMessage{
379+
Body: getJSONOrCache(nil, []*testsrv.StreamMessage{
380380
{MsgID: 1, ExpectLen: 2},
381381
{MsgID: 2, ExpectLen: 2},
382382
}),
@@ -394,7 +394,7 @@ func doGRPCTest(t *testing.T, l net.Listener, sec *atest.Secure, desc *atest.GRP
394394
Body: getJSONOrCache("stream", nil),
395395
},
396396
Expect: atest.Response{
397-
Body: getJSONOrCache("streamneq", []*testsrv.StreamMessage{
397+
Body: getJSONOrCache(nil, []*testsrv.StreamMessage{
398398
{MsgID: 4, ExpectLen: 3},
399399
{MsgID: 5, ExpectLen: 3},
400400
{MsgID: 6, ExpectLen: 3},
@@ -479,7 +479,7 @@ func doGRPCTest(t *testing.T, l net.Listener, sec *atest.Secure, desc *atest.GRP
479479
}),
480480
},
481481
Expect: atest.Response{
482-
Body: getJSONOrCache("advancedneq",
482+
Body: getJSONOrCache(nil,
483483
&testsrv.AdvancedType{
484484
Int32Array: []int32{rand.Int31(), rand.Int31()},
485485
Int64Array: []int64{rand.Int63(), rand.Int63()},
@@ -601,13 +601,18 @@ func TestAPINameMatch(t *testing.T) {
601601
)
602602
}
603603

604-
func getJSONOrCache(k string, s any) (msg string) {
605-
v, ok := cache.Load(k)
606-
if ok {
604+
// getJSONOrCache can store the JSON string of value.
605+
//
606+
// Let key be nil represent not using cache.
607+
func getJSONOrCache(key any, value any) (msg string) {
608+
v, ok := cache.Load(key)
609+
if ok && key != nil {
607610
return v.(string)
608611
}
609-
b, _ := json.Marshal(s)
612+
b, _ := json.Marshal(value)
610613
msg = string(b)
611-
cache.Store(k, msg)
614+
if key != nil {
615+
cache.Store(key, msg)
616+
}
612617
return
613618
}

0 commit comments

Comments
 (0)