timer_test.go 757 B

12345678910111213141516171819202122232425262728293031323334
  1. package signal_test
  2. import (
  3. "context"
  4. "runtime"
  5. "testing"
  6. "time"
  7. . "v2ray.com/core/common/signal"
  8. . "v2ray.com/ext/assert"
  9. )
  10. func TestActivityTimer(t *testing.T) {
  11. assert := With(t)
  12. ctx, cancel := context.WithCancel(context.Background())
  13. timer := CancelAfterInactivity(ctx, cancel, time.Second*5)
  14. time.Sleep(time.Second * 6)
  15. assert(ctx.Err(), IsNotNil)
  16. runtime.KeepAlive(timer)
  17. }
  18. func TestActivityTimerUpdate(t *testing.T) {
  19. assert := With(t)
  20. ctx, cancel := context.WithCancel(context.Background())
  21. timer := CancelAfterInactivity(ctx, cancel, time.Second*10)
  22. time.Sleep(time.Second * 3)
  23. assert(ctx.Err(), IsNil)
  24. timer.SetTimeout(time.Second * 1)
  25. time.Sleep(time.Second * 2)
  26. assert(ctx.Err(), IsNotNil)
  27. runtime.KeepAlive(timer)
  28. }