github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/shell/shell_unix_test.go (about)

     1  // +build !windows,!plan9,!js
     2  
     3  package shell
     4  
     5  import (
     6  	"os"
     7  	"strconv"
     8  	"sync"
     9  	"testing"
    10  	"time"
    11  
    12  	"src.elv.sh/pkg/daemon"
    13  	"src.elv.sh/pkg/testutil"
    14  
    15  	. "src.elv.sh/pkg/prog/progtest"
    16  )
    17  
    18  func TestShell_ConnectsToDaemon(t *testing.T) {
    19  	f := Setup()
    20  	defer f.Cleanup()
    21  
    22  	// Run the daemon in the same process for simplicity.
    23  	var wg sync.WaitGroup
    24  	wg.Add(1)
    25  	defer wg.Wait()
    26  	go func() {
    27  		daemon.Serve("sock", "db")
    28  		wg.Done()
    29  	}()
    30  	// Block until the socket file exists, so that Elvish will not try to spawn
    31  	// again.
    32  	hasSock := make(chan struct{})
    33  	go func() {
    34  		defer close(hasSock)
    35  		for {
    36  			_, err := os.Stat("sock")
    37  			if err == nil {
    38  				return
    39  			}
    40  			time.Sleep(time.Millisecond)
    41  		}
    42  	}()
    43  	select {
    44  	case <-hasSock:
    45  	// Do nothing
    46  	case <-time.After(testutil.ScaledMs(100)):
    47  		t.Fatalf("timed out waiting for daemon to start")
    48  	}
    49  
    50  	// This test uses Script, but it also applies to Interact since the daemon
    51  	// connection logic is common to both modes.
    52  	Script(f.Fds(),
    53  		[]string{"use daemon; print $daemon:pid"},
    54  		&ScriptConfig{
    55  			Cmd: true, SpawnDaemon: true,
    56  			Paths: Paths{Sock: "sock", Db: "db", RunDir: "."}})
    57  	f.TestOut(t, 1, strconv.Itoa(os.Getpid()))
    58  	f.TestOut(t, 2, "")
    59  }