github.com/elves/elvish@v0.15.0/pkg/eval/builtin_fn_misc_unix_test.go (about)

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