2020-04-17 07:59:58 +08:00
|
|
|
// +build !windows,!plan9,!js
|
|
|
|
|
2017-06-01 07:17:04 +08:00
|
|
|
package shell
|
|
|
|
|
2020-04-17 04:48:56 +08:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2017-06-01 07:17:04 +08:00
|
|
|
|
2020-04-17 04:48:56 +08:00
|
|
|
"github.com/elves/elvish/pkg/daemon"
|
|
|
|
"github.com/elves/elvish/pkg/testutil"
|
2020-04-25 05:05:56 +08:00
|
|
|
|
2020-04-25 06:33:18 +08:00
|
|
|
. "github.com/elves/elvish/pkg/prog/progtest"
|
2020-04-17 04:48:56 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestShell_ConnectsToDaemon(t *testing.T) {
|
2020-04-25 05:05:56 +08:00
|
|
|
f := Setup()
|
|
|
|
defer f.Cleanup()
|
2020-04-17 04:48:56 +08:00
|
|
|
|
|
|
|
// Run the daemon in the same process for simplicity.
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
defer wg.Wait()
|
|
|
|
go func() {
|
|
|
|
daemon.Serve("sock", "db")
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
// Block until the socket file exists, so that Elvish will not try to spawn
|
|
|
|
// again.
|
|
|
|
hasSock := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(hasSock)
|
|
|
|
for {
|
|
|
|
_, err := os.Stat("sock")
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-hasSock:
|
|
|
|
// Do nothing
|
|
|
|
case <-time.After(testutil.ScaledMs(100)):
|
|
|
|
t.Fatalf("timed out waiting for daemon to start")
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test uses Script, but it also applies to Interact since the daemon
|
|
|
|
// connection logic is common to both modes.
|
2020-04-25 05:05:56 +08:00
|
|
|
Script(f.Fds(),
|
2020-04-17 04:48:56 +08:00
|
|
|
[]string{"use daemon; print $daemon:pid"},
|
|
|
|
&ScriptConfig{
|
|
|
|
Cmd: true, SpawnDaemon: true,
|
|
|
|
Paths: Paths{Sock: "sock", Db: "db", DaemonLogPrefix: "log-"}})
|
2020-04-25 05:05:56 +08:00
|
|
|
f.TestOut(t, 1, strconv.Itoa(os.Getpid()))
|
|
|
|
f.TestOut(t, 2, "")
|
2017-06-01 07:17:04 +08:00
|
|
|
}
|