@@ -19,8 +19,10 @@ import (
1919 "testing"
2020 "time"
2121
22+ "github.com/stretchr/testify/assert"
2223 "go.uber.org/zap/zaptest"
2324
25+ pb "go.etcd.io/etcd/api/v3/etcdserverpb"
2426 "go.etcd.io/etcd/client/pkg/v3/types"
2527 "go.etcd.io/etcd/server/v3/etcdserver/api/membership"
2628 "go.etcd.io/etcd/server/v3/etcdserver/api/rafthttp"
@@ -110,3 +112,55 @@ type testStringerFunc func() string
110112func (s testStringerFunc ) String () string {
111113 return s ()
112114}
115+
116+ func TestExceedsRequestLimit (t * testing.T ) {
117+ tests := []struct {
118+ name string
119+ ci uint64
120+ ai uint64
121+ expectedResult bool
122+ req * pb.InternalRaftRequest
123+ }{
124+ {
125+ ci : 1 + maxGapBetweenApplyAndCommitIndex ,
126+ ai : 1 ,
127+ expectedResult : false ,
128+ req : nil ,
129+ name : "Test nil InternalRaftRequest" ,
130+ },
131+ {
132+ ci : 1 + maxGapBetweenApplyAndCommitIndex ,
133+ ai : 1 ,
134+ expectedResult : false ,
135+ req : & pb.InternalRaftRequest {},
136+ name : "Test non-critical request and gap is not larger than maxGapBetweenApplyAndCommitIndex" ,
137+ },
138+ {
139+ ci : 1 + maxGapBetweenApplyAndCommitIndex + 1 ,
140+ ai : 1 ,
141+ expectedResult : true ,
142+ req : & pb.InternalRaftRequest {},
143+ name : "Test non-critical request and gap is larger than maxGapBetweenApplyAndCommitIndex" ,
144+ },
145+ {
146+ ci : 1 + maxGapBetweenApplyAndCommitIndex + 1 ,
147+ ai : 1 ,
148+ expectedResult : false ,
149+ req : & pb.InternalRaftRequest {LeaseRevoke : & pb.LeaseRevokeRequest {}},
150+ name : "Test critical request and gap is larger than maxGapBetweenApplyAndCommitIndex" ,
151+ },
152+ {
153+ ci : 1 + maxGapBetweenApplyAndCommitIndex + 1 + maxGapBetweenApplyAndCommitIndex / 10 ,
154+ ai : 1 ,
155+ expectedResult : true ,
156+ req : & pb.InternalRaftRequest {LeaseRevoke : & pb.LeaseRevokeRequest {}},
157+ name : "Test critical request and gap is larger than 110% maxGapBetweenApplyAndCommitIndex" ,
158+ },
159+ }
160+
161+ for _ , tc := range tests {
162+ t .Run (tc .name , func (t * testing.T ) {
163+ assert .Equal (t , tc .expectedResult , exceedsRequestLimit (tc .ai , tc .ci , tc .req ))
164+ })
165+ }
166+ }
0 commit comments