periodic_test.go 668 B

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