gateway_waiting_queue_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //go:build unit
  2. package service
  3. import (
  4. "context"
  5. "errors"
  6. "testing"
  7. "github.com/stretchr/testify/require"
  8. )
  9. // TestDecrementWaitCount_NilCache 确保 nil cache 不会 panic
  10. func TestDecrementWaitCount_NilCache(t *testing.T) {
  11. svc := &ConcurrencyService{cache: nil}
  12. // 不应 panic
  13. svc.DecrementWaitCount(context.Background(), 1)
  14. }
  15. // TestDecrementWaitCount_CacheError 确保 cache 错误不会传播
  16. func TestDecrementWaitCount_CacheError(t *testing.T) {
  17. cache := &stubConcurrencyCacheForTest{}
  18. svc := NewConcurrencyService(cache)
  19. // DecrementWaitCount 使用 background context,错误只记录日志不传播
  20. svc.DecrementWaitCount(context.Background(), 1)
  21. }
  22. // TestDecrementAccountWaitCount_NilCache 确保 nil cache 不会 panic
  23. func TestDecrementAccountWaitCount_NilCache(t *testing.T) {
  24. svc := &ConcurrencyService{cache: nil}
  25. svc.DecrementAccountWaitCount(context.Background(), 1)
  26. }
  27. // TestDecrementAccountWaitCount_CacheError 确保 cache 错误不会传播
  28. func TestDecrementAccountWaitCount_CacheError(t *testing.T) {
  29. cache := &stubConcurrencyCacheForTest{}
  30. svc := NewConcurrencyService(cache)
  31. svc.DecrementAccountWaitCount(context.Background(), 1)
  32. }
  33. // TestWaitingQueueFlow_IncrementThenDecrement 测试完整的等待队列增减流程
  34. func TestWaitingQueueFlow_IncrementThenDecrement(t *testing.T) {
  35. cache := &stubConcurrencyCacheForTest{waitAllowed: true}
  36. svc := NewConcurrencyService(cache)
  37. // 进入等待队列
  38. allowed, err := svc.IncrementWaitCount(context.Background(), 1, 25)
  39. require.NoError(t, err)
  40. require.True(t, allowed)
  41. // 离开等待队列(不应 panic)
  42. svc.DecrementWaitCount(context.Background(), 1)
  43. }
  44. // TestWaitingQueueFlow_AccountLevel 测试账号级等待队列流程
  45. func TestWaitingQueueFlow_AccountLevel(t *testing.T) {
  46. cache := &stubConcurrencyCacheForTest{waitAllowed: true}
  47. svc := NewConcurrencyService(cache)
  48. // 进入账号等待队列
  49. allowed, err := svc.IncrementAccountWaitCount(context.Background(), 42, 10)
  50. require.NoError(t, err)
  51. require.True(t, allowed)
  52. // 离开账号等待队列
  53. svc.DecrementAccountWaitCount(context.Background(), 42)
  54. }
  55. // TestWaitingQueueFull_Returns429Signal 测试等待队列满时返回 false
  56. func TestWaitingQueueFull_Returns429Signal(t *testing.T) {
  57. // waitAllowed=false 模拟队列已满
  58. cache := &stubConcurrencyCacheForTest{waitAllowed: false}
  59. svc := NewConcurrencyService(cache)
  60. // 用户级等待队列满
  61. allowed, err := svc.IncrementWaitCount(context.Background(), 1, 25)
  62. require.NoError(t, err)
  63. require.False(t, allowed, "等待队列满时应返回 false(调用方根据此返回 429)")
  64. // 账号级等待队列满
  65. allowed, err = svc.IncrementAccountWaitCount(context.Background(), 1, 10)
  66. require.NoError(t, err)
  67. require.False(t, allowed, "账号等待队列满时应返回 false")
  68. }
  69. // TestWaitingQueue_FailOpen_OnCacheError 测试 Redis 故障时 fail-open
  70. func TestWaitingQueue_FailOpen_OnCacheError(t *testing.T) {
  71. cache := &stubConcurrencyCacheForTest{waitErr: errors.New("redis connection refused")}
  72. svc := NewConcurrencyService(cache)
  73. // 用户级:Redis 错误时允许通过
  74. allowed, err := svc.IncrementWaitCount(context.Background(), 1, 25)
  75. require.NoError(t, err, "Redis 错误不应向调用方传播")
  76. require.True(t, allowed, "Redis 故障时应 fail-open 放行")
  77. // 账号级:同样 fail-open
  78. allowed, err = svc.IncrementAccountWaitCount(context.Background(), 1, 10)
  79. require.NoError(t, err, "Redis 错误不应向调用方传播")
  80. require.True(t, allowed, "Redis 故障时应 fail-open 放行")
  81. }
  82. // TestCalculateMaxWait_Scenarios 测试最大等待队列大小计算
  83. func TestCalculateMaxWait_Scenarios(t *testing.T) {
  84. tests := []struct {
  85. concurrency int
  86. expected int
  87. }{
  88. {5, 25}, // 5 + 20
  89. {10, 30}, // 10 + 20
  90. {1, 21}, // 1 + 20
  91. {0, 21}, // min(1) + 20
  92. {-1, 21}, // min(1) + 20
  93. {-10, 21}, // min(1) + 20
  94. {100, 120}, // 100 + 20
  95. }
  96. for _, tt := range tests {
  97. result := CalculateMaxWait(tt.concurrency)
  98. require.Equal(t, tt.expected, result, "CalculateMaxWait(%d)", tt.concurrency)
  99. }
  100. }