ratelimiter_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package email
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/mhsanaei/3x-ui/v3/internal/eventbus"
  6. )
  7. func TestRateLimiterAllow(t *testing.T) {
  8. rl := eventbus.NewRateLimiter(time.Minute)
  9. if !rl.Allow(eventbus.EventOutboundDown, "proxy-1") {
  10. t.Error("first call should be allowed")
  11. }
  12. }
  13. func TestRateLimiterCooldown(t *testing.T) {
  14. rl := eventbus.NewRateLimiter(100 * time.Millisecond)
  15. rl.Allow(eventbus.EventOutboundDown, "proxy-1")
  16. if rl.Allow(eventbus.EventOutboundDown, "proxy-1") {
  17. t.Error("should be blocked during cooldown")
  18. }
  19. time.Sleep(110 * time.Millisecond)
  20. if !rl.Allow(eventbus.EventOutboundDown, "proxy-1") {
  21. t.Error("should be allowed after cooldown")
  22. }
  23. }
  24. func TestRateLimiterPerType(t *testing.T) {
  25. rl := eventbus.NewRateLimiter(time.Minute)
  26. rl.Allow(eventbus.EventOutboundDown, "proxy-1")
  27. if !rl.Allow(eventbus.EventOutboundUp, "proxy-1") {
  28. t.Error("different event types should be independent")
  29. }
  30. }
  31. func TestRateLimiterPerSource(t *testing.T) {
  32. rl := eventbus.NewRateLimiter(time.Minute)
  33. rl.Allow(eventbus.EventOutboundDown, "proxy-1")
  34. if !rl.Allow(eventbus.EventOutboundDown, "proxy-2") {
  35. t.Error("different sources should be independent")
  36. }
  37. }