github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/eval/builtin_fn_misc_unix_test.go (about)

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