Skip to content

Commit 42c8725

Browse files
author
Felix021
authored
fix: retry container cb test (#41)
1 parent e5873bb commit 42c8725

File tree

3 files changed

+92
-23
lines changed

3 files changed

+92
-23
lines changed

thriftrpc/retrycall/retrycall.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,13 @@ func (h *STServiceHandler) TestSTReq(ctx context.Context, req *stability.STReque
4242
Mp: req.StringMap,
4343
FlagMsg: req.FlagMsg,
4444
}
45-
if v := atomic.AddInt32(&testSTReqCount, 1); v%10 == 0 {
46-
time.Sleep(50 * time.Millisecond)
45+
if !skipCounterSleep(ctx) { // better not rely on this since it's a global variable
46+
if v := atomic.AddInt32(&testSTReqCount, 1); v%10 == 0 {
47+
time.Sleep(50 * time.Millisecond)
48+
}
49+
}
50+
if sleepTime := getSleepTimeMS(ctx); sleepTime > 0 {
51+
time.Sleep(sleepTime)
4752
}
4853
return resp, nil
4954
}

thriftrpc/retrycall/retrycall_mockresult.go

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,39 @@ import (
2727
)
2828

2929
const (
30-
retryMsg = "retry"
31-
sleepTimeMsKey = "TIME_SLEEP_TIME_MS"
30+
retryMsg = "retry"
31+
sleepTimeMsKey = "TIME_SLEEP_TIME_MS"
32+
skipCounterSleepKey = "TIME_SKIP_COUNTER_SLEEP"
3233
)
3334

35+
func setSkipCounterSleep(ctx context.Context) context.Context {
36+
return metainfo.WithPersistentValue(ctx, skipCounterSleepKey, "1")
37+
}
38+
39+
func skipCounterSleep(ctx context.Context) bool {
40+
if _, exist := metainfo.GetValue(ctx, skipCounterSleepKey); exist {
41+
return true
42+
}
43+
if _, exist := metainfo.GetPersistentValue(ctx, skipCounterSleepKey); exist {
44+
return true
45+
}
46+
return false
47+
}
48+
49+
func getSleepTimeMS(ctx context.Context) time.Duration {
50+
if value, exist := metainfo.GetPersistentValue(ctx, sleepTimeMsKey); exist {
51+
if sleepTimeMS, err := strconv.Atoi(value); err == nil && sleepTimeMS > 0 {
52+
return time.Duration(sleepTimeMS) * time.Millisecond
53+
}
54+
}
55+
if value, exist := metainfo.GetValue(ctx, sleepTimeMsKey); exist {
56+
if sleepTimeMS, err := strconv.Atoi(value); err == nil && sleepTimeMS > 0 {
57+
return time.Duration(sleepTimeMS) * time.Millisecond
58+
}
59+
}
60+
return 0
61+
}
62+
3463
var _ stability.STService = &STServiceMockResultHandler{}
3564

3665
// STServiceMockResultHandler .
@@ -91,10 +120,8 @@ func (h *STServiceMockResultHandler) CircuitBreakTest(ctx context.Context, req *
91120
// use ttheader
92121
if _, exist := metainfo.GetPersistentValue(ctx, retry.TransitKey); !exist {
93122
sleepTime := 200 * time.Millisecond
94-
if value, exist := metainfo.GetPersistentValue(ctx, sleepTimeMsKey); exist {
95-
if sleepTimeMS, err := strconv.Atoi(value); err == nil && sleepTimeMS > 0 {
96-
sleepTime = time.Duration(sleepTimeMS) * time.Millisecond
97-
}
123+
if v := getSleepTimeMS(ctx); v > 0 {
124+
sleepTime = v
98125
}
99126
time.Sleep(sleepTime)
100127
}

thriftrpc/retrycall/retrycall_test.go

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,15 @@ import (
2424
"time"
2525

2626
"github.com/bytedance/gopkg/cloud/metainfo"
27-
"github.com/cloudwego/kitex/pkg/endpoint"
28-
"github.com/cloudwego/kitex/pkg/kerrors"
29-
3027
"github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability"
3128
"github.com/cloudwego/kitex-tests/kitex_gen/thrift/stability/stservice"
3229
"github.com/cloudwego/kitex-tests/pkg/test"
3330
"github.com/cloudwego/kitex-tests/thriftrpc"
3431
"github.com/cloudwego/kitex/client"
3532
"github.com/cloudwego/kitex/client/callopt"
3633
"github.com/cloudwego/kitex/pkg/circuitbreak"
34+
"github.com/cloudwego/kitex/pkg/endpoint"
35+
"github.com/cloudwego/kitex/pkg/kerrors"
3736
"github.com/cloudwego/kitex/pkg/remote"
3837
"github.com/cloudwego/kitex/pkg/retry"
3938
"github.com/cloudwego/kitex/pkg/rpcinfo"
@@ -79,31 +78,69 @@ func genCBKey(ri rpcinfo.RPCInfo) string {
7978
}
8079

8180
func TestRetryCB(t *testing.T) {
82-
atomic.StoreInt32(&testSTReqCount, -1)
83-
// retry config
84-
fp := retry.NewFailurePolicy()
85-
81+
reqCount := int32(0)
8682
cli = getKitexClient(
87-
transport.PurePayload,
88-
client.WithFailureRetry(fp),
83+
transport.TTHeader,
84+
client.WithRetryContainer(retry.NewRetryContainer()), // use default circuit breaker
85+
client.WithFailureRetry(retry.NewFailurePolicy()),
8986
client.WithRPCTimeout(20*time.Millisecond),
87+
client.WithMiddleware(func(next endpoint.Endpoint) endpoint.Endpoint {
88+
return func(ctx context.Context, req, resp interface{}) (err error) {
89+
count := atomic.AddInt32(&reqCount, 1)
90+
if count%10 == 0 {
91+
time.Sleep(50 * time.Millisecond)
92+
}
93+
return nil // no need to send the real request
94+
}
95+
}),
9096
)
9197

9298
ctx, stReq := thriftrpc.CreateSTRequest(context.Background())
9399
cbCount := 0
94100
for i := 0; i < 300; i++ {
95-
stResp, err := cli.TestSTReq(ctx, stReq)
96-
if err != nil {
97-
test.Assert(t, strings.Contains(err.Error(), "retry circuit break"), err, i)
101+
_, err := cli.TestSTReq(ctx, stReq)
102+
if err != nil && strings.Contains(err.Error(), "retry circuit break") {
98103
cbCount++
99-
} else {
100-
test.Assert(t, err == nil, err, i)
101-
test.Assert(t, stReq.Str == stResp.Str)
102104
}
103105
}
106+
test.Assert(t, reqCount == 300, reqCount)
104107
test.Assert(t, cbCount == 30, cbCount)
105108
}
106109

110+
func TestRetryCBPercentageLimit(t *testing.T) {
111+
reqCount := int32(0)
112+
cli := getKitexClient(
113+
transport.TTHeaderFramed,
114+
client.WithRetryContainer(retry.NewRetryContainerWithPercentageLimit()),
115+
client.WithFailureRetry(retry.NewFailurePolicy()), // cb threshold = 10%
116+
client.WithRPCTimeout(20*time.Millisecond),
117+
client.WithMiddleware(func(next endpoint.Endpoint) endpoint.Endpoint {
118+
return func(ctx context.Context, req, resp interface{}) (err error) {
119+
atomic.AddInt32(&reqCount, 1)
120+
if tm := getSleepTimeMS(ctx); tm > 0 {
121+
time.Sleep(tm)
122+
}
123+
return nil // no need to send a real request
124+
}
125+
}),
126+
)
127+
ctx, stReq := thriftrpc.CreateSTRequest(context.Background())
128+
ctx = setSkipCounterSleep(ctx)
129+
cbCount := 0
130+
for i := 0; i < 300; i++ {
131+
reqCtx := ctx
132+
if i%5 == 0 {
133+
reqCtx = metainfo.WithPersistentValue(ctx, sleepTimeMsKey, "50")
134+
}
135+
_, err := cli.TestSTReq(reqCtx, stReq)
136+
if err != nil && strings.Contains(err.Error(), "retry circuit break") {
137+
cbCount++
138+
}
139+
}
140+
test.Assert(t, reqCount == 333, reqCount)
141+
test.Assert(t, cbCount == 58, cbCount)
142+
}
143+
107144
func TestNoCB(t *testing.T) {
108145
atomic.StoreInt32(&testSTReqCount, -1)
109146
// retry config

0 commit comments

Comments
 (0)