@@ -18,7 +18,6 @@ import (
1818
1919func TestMemoryTokenBucketRateLimiter_Allow (t * testing.T ) {
2020 gtest .C (t , func (t * gtest.T ) {
21- // 创建限流器
2221 memoryLimiter := glimiter .NewMemoryTokenBucketRateLimiter (glimiter.MemoryTokenBucketRateLimiterOption {
2322 Rate : 10 ,
2423 Capacity : 20 ,
@@ -28,20 +27,16 @@ func TestMemoryTokenBucketRateLimiter_Allow(t *testing.T) {
2827 ctx := context .Background ()
2928 key := "test_key"
3029
31- // 测试允许通过的请求
3230 for i := 0 ; i < 10 ; i ++ {
3331 allowed := memoryLimiter .Allow (ctx , key )
3432 t .Assert (allowed , true )
3533 }
3634
37- // 测试被拒绝的请求
3835 allowed := memoryLimiter .AllowN (ctx , key , 15 )
3936 t .Assert (allowed , false )
4037
41- // 等待一段时间,让令牌重新生成
4238 time .Sleep (time .Second * 2 )
4339
44- // 再次测试允许通过的请求
4540 allowed = memoryLimiter .AllowN (ctx , key , 10 )
4641 t .Assert (allowed , true )
4742 })
@@ -57,19 +52,15 @@ func TestMemoryTokenBucketRateLimiter_AllowN(t *testing.T) {
5752 ctx := context .Background ()
5853 key := "test_key_n"
5954
60- // 测试允许通过的批量请求
6155 allowed := memoryLimiter .AllowN (ctx , key , 5 )
6256 t .Assert (allowed , true )
6357
64- // 测试超过容量的批量请求
6558 allowed = memoryLimiter .AllowN (ctx , key , 8 )
6659 t .Assert (allowed , false )
6760
68- // 测试零个请求
6961 allowed = memoryLimiter .AllowN (ctx , key , 0 )
7062 t .Assert (allowed , true )
7163
72- // 测试负数请求
7364 allowed = memoryLimiter .AllowN (ctx , key , - 1 )
7465 t .Assert (allowed , false )
7566 })
0 commit comments