periodic_test.go 623 B

123456789101112131415161718192021222324252627282930313233343536
  1. package task_test
  2. import (
  3. "testing"
  4. "time"
  5. . "v2ray.com/core/common/task"
  6. . "v2ray.com/ext/assert"
  7. "v2ray.com/core/common"
  8. )
  9. func TestPeriodicTaskStop(t *testing.T) {
  10. assert := With(t)
  11. value := 0
  12. task := &Periodic{
  13. Interval: time.Second * 2,
  14. Execute: func() error {
  15. value++
  16. return nil
  17. },
  18. }
  19. common.Must(task.Start())
  20. time.Sleep(time.Second * 5)
  21. common.Must(task.Close())
  22. assert(value, Equals, 3)
  23. time.Sleep(time.Second * 4)
  24. assert(value, Equals, 3)
  25. common.Must(task.Start())
  26. time.Sleep(time.Second * 3)
  27. if value != 5 {
  28. t.Fatal("Expected 5, but ", value)
  29. }
  30. common.Must(task.Close())
  31. }