2021-08-23 07:19:49 +08:00
|
|
|
//go:build !windows && !plan9 && !js
|
2020-08-18 13:40:30 +08:00
|
|
|
// +build !windows,!plan9,!js
|
|
|
|
|
2020-09-03 13:48:39 +08:00
|
|
|
package eval_test
|
2020-08-18 13:40:30 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2020-09-03 13:48:39 +08:00
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
. "src.elv.sh/pkg/eval"
|
|
|
|
"src.elv.sh/pkg/testutil"
|
2020-09-03 13:48:39 +08:00
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
. "src.elv.sh/pkg/eval/evaltest"
|
2020-08-18 13:40:30 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func interruptedTimeAfterMock(fm *Frame, d time.Duration) <-chan time.Time {
|
2021-01-02 09:45:55 +08:00
|
|
|
if d == time.Second {
|
2020-08-18 13:40:30 +08:00
|
|
|
// Special-case intended to verity that a sleep can be interrupted.
|
2021-01-02 09:45:55 +08:00
|
|
|
go func() {
|
|
|
|
// Wait a little bit to ensure that the control flow in the "sleep"
|
|
|
|
// function is in the select block when the interrupt is sent.
|
2021-10-05 08:02:54 +08:00
|
|
|
time.Sleep(testutil.Scaled(time.Millisecond))
|
2021-01-02 09:45:55 +08:00
|
|
|
p, _ := os.FindProcess(os.Getpid())
|
|
|
|
p.Signal(os.Interrupt)
|
|
|
|
}()
|
2020-08-18 13:40:30 +08:00
|
|
|
return time.After(1 * time.Second)
|
|
|
|
}
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInterruptedSleep(t *testing.T) {
|
2020-09-03 13:48:39 +08:00
|
|
|
TimeAfter = interruptedTimeAfterMock
|
2020-08-18 13:40:30 +08:00
|
|
|
Test(t,
|
|
|
|
// Special-case that should result in the sleep being interrupted. See
|
|
|
|
// timeAfterMock above.
|
2021-01-02 09:45:55 +08:00
|
|
|
That(`sleep 1s`).Throws(ErrInterrupted, "sleep 1s"),
|
2020-08-18 13:40:30 +08:00
|
|
|
)
|
|
|
|
}
|