timer_test.go 1015 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package time
  2. import (
  3. "testing"
  4. "time"
  5. "go-common/library/log"
  6. )
  7. func TestTimer(t *testing.T) {
  8. timer := NewTimer(100)
  9. tds := make([]*TimerData, 100)
  10. for i := 0; i < 100; i++ {
  11. tds[i] = timer.Add(time.Duration(i)*time.Second+5*time.Minute, nil)
  12. }
  13. printTimer(timer)
  14. for i := 0; i < 100; i++ {
  15. log.Info("td: %s, %s, %d", tds[i].Key, tds[i].ExpireString(), tds[i].index)
  16. timer.Del(tds[i])
  17. }
  18. printTimer(timer)
  19. for i := 0; i < 100; i++ {
  20. tds[i] = timer.Add(time.Duration(i)*time.Second+5*time.Minute, nil)
  21. }
  22. printTimer(timer)
  23. for i := 0; i < 100; i++ {
  24. timer.Del(tds[i])
  25. }
  26. printTimer(timer)
  27. timer.Add(time.Second, nil)
  28. time.Sleep(time.Second * 2)
  29. if len(timer.timers) != 0 {
  30. t.FailNow()
  31. }
  32. }
  33. func printTimer(timer *Timer) {
  34. log.Info("----------timers: %d ----------", len(timer.timers))
  35. for i := 0; i < len(timer.timers); i++ {
  36. log.Info("timer: %s, %s, index: %d", timer.timers[i].Key, timer.timers[i].ExpireString(), timer.timers[i].index)
  37. }
  38. log.Info("--------------------")
  39. }