Skip to content

Commit

Permalink
chore(grpc_test): add JSON cache bypass
Browse files Browse the repository at this point in the history
Signed-off-by: Ink33 <[email protected]>
  • Loading branch information
Ink-33 authored and LinuxSuRen committed Oct 9, 2023
1 parent 1ec00d1 commit 87982ae
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions pkg/runner/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func doGRPCTest(t *testing.T, l net.Listener, sec *atest.Secure, desc *atest.GRP
Body: "{}",
},
Expect: atest.Response{
Body: getJSONOrCache("unaryneq", &testsrv.HelloReply{
Body: getJSONOrCache(nil, &testsrv.HelloReply{
Message: "Happy!",
}),
},
Expand Down Expand Up @@ -376,7 +376,7 @@ func doGRPCTest(t *testing.T, l net.Listener, sec *atest.Secure, desc *atest.GRP
Body: getJSONOrCache("stream", nil),
},
Expect: atest.Response{
Body: getJSONOrCache("streamneq", []*testsrv.StreamMessage{
Body: getJSONOrCache(nil, []*testsrv.StreamMessage{
{MsgID: 1, ExpectLen: 2},
{MsgID: 2, ExpectLen: 2},
}),
Expand All @@ -394,7 +394,7 @@ func doGRPCTest(t *testing.T, l net.Listener, sec *atest.Secure, desc *atest.GRP
Body: getJSONOrCache("stream", nil),
},
Expect: atest.Response{
Body: getJSONOrCache("streamneq", []*testsrv.StreamMessage{
Body: getJSONOrCache(nil, []*testsrv.StreamMessage{
{MsgID: 4, ExpectLen: 3},
{MsgID: 5, ExpectLen: 3},
{MsgID: 6, ExpectLen: 3},
Expand Down Expand Up @@ -479,7 +479,7 @@ func doGRPCTest(t *testing.T, l net.Listener, sec *atest.Secure, desc *atest.GRP
}),
},
Expect: atest.Response{
Body: getJSONOrCache("advancedneq",
Body: getJSONOrCache(nil,
&testsrv.AdvancedType{
Int32Array: []int32{rand.Int31(), rand.Int31()},
Int64Array: []int64{rand.Int63(), rand.Int63()},
Expand Down Expand Up @@ -601,13 +601,18 @@ func TestAPINameMatch(t *testing.T) {
)
}

func getJSONOrCache(k string, s any) (msg string) {
v, ok := cache.Load(k)
if ok {
// getJSONOrCache can store the JSON string of value.
//
// Let key be nil represent not using cache.
func getJSONOrCache(key any, value any) (msg string) {
v, ok := cache.Load(key)
if ok && key != nil {
return v.(string)
}
b, _ := json.Marshal(s)
b, _ := json.Marshal(value)
msg = string(b)
cache.Store(k, msg)
if key != nil {
cache.Store(key, msg)
}
return
}

0 comments on commit 87982ae

Please sign in to comment.