builtin_fn_misc_unix_test.go 936 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. //go:build !windows && !plan9 && !js
  2. package eval_test
  3. import (
  4. "os"
  5. "testing"
  6. "time"
  7. . "src.elv.sh/pkg/eval"
  8. "src.elv.sh/pkg/testutil"
  9. . "src.elv.sh/pkg/eval/evaltest"
  10. )
  11. func interruptedTimeAfterMock(fm *Frame, d time.Duration) <-chan time.Time {
  12. if d == time.Second {
  13. // Special-case intended to verity that a sleep can be interrupted.
  14. go func() {
  15. // Wait a little bit to ensure that the control flow in the "sleep"
  16. // function is in the select block when the interrupt is sent.
  17. time.Sleep(testutil.Scaled(time.Millisecond))
  18. p, _ := os.FindProcess(os.Getpid())
  19. p.Signal(os.Interrupt)
  20. }()
  21. return time.After(1 * time.Second)
  22. }
  23. panic("unreachable")
  24. }
  25. func TestInterruptedSleep(t *testing.T) {
  26. TimeAfter = interruptedTimeAfterMock
  27. Test(t,
  28. // Special-case that should result in the sleep being interrupted. See
  29. // timeAfterMock above.
  30. That(`sleep 1s`).Throws(ErrInterrupted, "sleep 1s"),
  31. )
  32. }