chore: use Getter in WarnOfExpensiveReadOnlyTxnRequest

The pb provides an accessor method to get field and it will not panic if
the owner is nil. And add non-empty RangeRespone into the test case.

Signed-off-by: Wei Fu <fuweid89@gmail.com>
This commit is contained in:
Wei Fu 2022-12-07 11:23:15 +08:00
parent d59c9b82d0
commit f59896c735
2 changed files with 20 additions and 4 deletions

View File

@ -61,10 +61,10 @@ func WarnOfExpensiveReadOnlyTxnRequest(lg *zap.Logger, warningApplyDuration time
if !isNil(txnResponse) { if !isNil(txnResponse) {
var resps []string var resps []string
for _, r := range txnResponse.Responses { for _, r := range txnResponse.Responses {
switch op := r.Response.(type) { switch r.Response.(type) {
case *pb.ResponseOp_ResponseRange: case *pb.ResponseOp_ResponseRange:
if op.ResponseRange != nil { if op := r.GetResponseRange(); op != nil {
resps = append(resps, fmt.Sprintf("range_response_count:%d", len(op.ResponseRange.Kvs))) resps = append(resps, fmt.Sprintf("range_response_count:%d", len(op.GetKvs())))
} else { } else {
resps = append(resps, "range_response:nil") resps = append(resps, "range_response:nil")
} }

View File

@ -19,12 +19,19 @@ import (
"time" "time"
pb "go.etcd.io/etcd/api/v3/etcdserverpb" pb "go.etcd.io/etcd/api/v3/etcdserverpb"
"go.etcd.io/etcd/api/v3/mvccpb"
"go.uber.org/zap/zaptest" "go.uber.org/zap/zaptest"
) )
// TestWarnOfExpensiveReadOnlyTxnRequest verifies WarnOfExpensiveReadOnlyTxnRequest // TestWarnOfExpensiveReadOnlyTxnRequest verifies WarnOfExpensiveReadOnlyTxnRequest
// never panic no matter what data the txnResponse contains. // never panic no matter what data the txnResponse contains.
func TestWarnOfExpensiveReadOnlyTxnRequest(t *testing.T) { func TestWarnOfExpensiveReadOnlyTxnRequest(t *testing.T) {
kvs := []*mvccpb.KeyValue{
&mvccpb.KeyValue{Key: []byte("k1"), Value: []byte("v1")},
&mvccpb.KeyValue{Key: []byte("k2"), Value: []byte("v2")},
}
testCases := []struct { testCases := []struct {
name string name string
txnResp *pb.TxnResponse txnResp *pb.TxnResponse
@ -35,7 +42,9 @@ func TestWarnOfExpensiveReadOnlyTxnRequest(t *testing.T) {
Responses: []*pb.ResponseOp{ Responses: []*pb.ResponseOp{
{ {
Response: &pb.ResponseOp_ResponseRange{ Response: &pb.ResponseOp_ResponseRange{
ResponseRange: &pb.RangeResponse{}, ResponseRange: &pb.RangeResponse{
Kvs: kvs,
},
}, },
}, },
{ {
@ -60,6 +69,13 @@ func TestWarnOfExpensiveReadOnlyTxnRequest(t *testing.T) {
ResponseRange: nil, ResponseRange: nil,
}, },
}, },
{
Response: &pb.ResponseOp_ResponseRange{
ResponseRange: &pb.RangeResponse{
Kvs: kvs,
},
},
},
}, },
}, },
}, },