cleaner_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package cache
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/wuntsong-org/go-zero-plus/core/collection"
  7. "github.com/wuntsong-org/go-zero-plus/core/proc"
  8. "github.com/wuntsong-org/go-zero-plus/core/timex"
  9. )
  10. func TestNextDelay(t *testing.T) {
  11. tests := []struct {
  12. name string
  13. input time.Duration
  14. output time.Duration
  15. ok bool
  16. }{
  17. {
  18. name: "second",
  19. input: time.Second,
  20. output: time.Second * 5,
  21. ok: true,
  22. },
  23. {
  24. name: "5 seconds",
  25. input: time.Second * 5,
  26. output: time.Minute,
  27. ok: true,
  28. },
  29. {
  30. name: "minute",
  31. input: time.Minute,
  32. output: time.Minute * 5,
  33. ok: true,
  34. },
  35. {
  36. name: "5 minutes",
  37. input: time.Minute * 5,
  38. output: time.Hour,
  39. ok: true,
  40. },
  41. {
  42. name: "hour",
  43. input: time.Hour,
  44. output: 0,
  45. ok: false,
  46. },
  47. }
  48. for _, test := range tests {
  49. t.Run(test.name, func(t *testing.T) {
  50. old := timingWheel.Load()
  51. ticker := timex.NewFakeTicker()
  52. tw, err := collection.NewTimingWheelWithTicker(
  53. time.Millisecond, timingWheelSlots, func(key, value any) {
  54. clean(key, value)
  55. }, ticker)
  56. timingWheel.Store(tw)
  57. assert.NoError(t, err)
  58. t.Cleanup(func() {
  59. timingWheel.Store(old)
  60. })
  61. next, ok := nextDelay(test.input)
  62. assert.Equal(t, test.ok, ok)
  63. assert.Equal(t, test.output, next)
  64. proc.Shutdown()
  65. })
  66. }
  67. }