|
15 | 15 | package retry |
16 | 16 |
|
17 | 17 | import ( |
18 | | - "errors" |
19 | 18 | "fmt" |
20 | | - "sync" |
21 | | - "sync/atomic" |
22 | 19 | "testing" |
23 | 20 | "time" |
24 | 21 |
|
@@ -82,59 +79,6 @@ func TestAdaptiveTimeoutRetryStrategy_Next(t *testing.T) { |
82 | 79 | } |
83 | 80 | } |
84 | 81 |
|
85 | | -// 测试场景 |
86 | | -// 阈值是50 |
87 | | -// 2000个请求 有1500个成功的 有500个失败的 最后统计500个失败的有50个可以执行 有450个不能执行 1500成功的都能执行 |
88 | | -func TestAdaptiveTimeoutRetryStrategy_Next_Concurrent(t *testing.T) { |
89 | | - // 创建一个基础策略 |
90 | | - baseStrategy := &MockStrategy{} |
91 | | - |
92 | | - // 创建升级版自适应策略,设置阈值为50 |
93 | | - strategy := NewAdaptiveTimeoutRetryStrategy(baseStrategy, 16, 50) |
94 | | - |
95 | | - var wg sync.WaitGroup |
96 | | - var successCount, errCount int64 |
97 | | - mockErr := errors.New("mock error") |
98 | | - |
99 | | - // 并发执行2000个请求 |
100 | | - for i := 0; i < 2000; i++ { |
101 | | - wg.Add(1) |
102 | | - go func(index int) { |
103 | | - defer wg.Done() |
104 | | - // 前1500个请求成功,后500个失败 |
105 | | - var err error |
106 | | - if index >= 1500 { |
107 | | - err = mockErr |
108 | | - } |
109 | | - strategy.Report(err) |
110 | | - _, allowed := strategy.Next() |
111 | | - if err != nil { |
112 | | - // 失败请求的统计 |
113 | | - if allowed { |
114 | | - atomic.AddInt64(&successCount, 1) |
115 | | - } else { |
116 | | - atomic.AddInt64(&errCount, 1) |
117 | | - } |
118 | | - } |
119 | | - }(i) |
120 | | - } |
121 | | - |
122 | | - // 等待所有goroutine完成 |
123 | | - wg.Wait() |
124 | | - |
125 | | - // 验证结果:期望大约50个失败请求可以执行,450个被拒绝 |
126 | | - // 由于是环形缓冲区和并发执行,可能会有一些误差,这里使用一个合理的范围进行判断 |
127 | | - finalSuccessCount := int(atomic.LoadInt64(&successCount)) |
128 | | - finalErrCount := int(atomic.LoadInt64(&errCount)) |
129 | | - if finalSuccessCount < 45 || finalSuccessCount > 55 { |
130 | | - t.Errorf("期望大约50个失败请求被允许执行,实际允许执行的失败请求数量为: %d", finalSuccessCount) |
131 | | - } |
132 | | - |
133 | | - if finalErrCount < 445 || finalErrCount > 455 { |
134 | | - t.Errorf("期望大约450个失败请求被拒绝执行,实际被拒绝的失败请求数量为: %d", finalErrCount) |
135 | | - } |
136 | | -} |
137 | | - |
138 | 82 | func ExampleAdaptiveTimeoutRetryStrategy_Next() { |
139 | 83 | baseStrategy, err := NewExponentialBackoffRetryStrategy(time.Second, time.Second*5, 10) |
140 | 84 | if err != nil { |
|
0 commit comments