mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-05 03:17:50 +08:00
7cd2745c03
The test contained a race condition: when the mock TimeAfter implementation sends the process a SIGINT, the "sleep" function may not have entered the "select" block, meaning that the signal will be ignored. This commit works around this by waiting 1ms (scaled with $ELVISH_TEST_TIME_SCALE) before sending SIGINT. The test now consistently succeeds on my local laptop.
39 lines
955 B
Go
39 lines
955 B
Go
// +build !windows,!plan9,!js
|
|
|
|
package eval_test
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
. "github.com/elves/elvish/pkg/eval"
|
|
"github.com/elves/elvish/pkg/testutil"
|
|
|
|
. "github.com/elves/elvish/pkg/eval/evaltest"
|
|
)
|
|
|
|
func interruptedTimeAfterMock(fm *Frame, d time.Duration) <-chan time.Time {
|
|
if d == time.Second {
|
|
// Special-case intended to verity that a sleep can be interrupted.
|
|
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.
|
|
time.Sleep(testutil.ScaledMs(1))
|
|
p, _ := os.FindProcess(os.Getpid())
|
|
p.Signal(os.Interrupt)
|
|
}()
|
|
return time.After(1 * time.Second)
|
|
}
|
|
panic("unreachable")
|
|
}
|
|
|
|
func TestInterruptedSleep(t *testing.T) {
|
|
TimeAfter = interruptedTimeAfterMock
|
|
Test(t,
|
|
// Special-case that should result in the sleep being interrupted. See
|
|
// timeAfterMock above.
|
|
That(`sleep 1s`).Throws(ErrInterrupted, "sleep 1s"),
|
|
)
|
|
}
|