mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-13 18:07:51 +08:00
e9d328aa16
This allows turning the following pattern in tests: value, cleanup := setupSomething() defer cleanup() into the following: value := setupSomething(t)
67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
package daemon
|
|
|
|
import (
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
|
|
"src.elv.sh/pkg/daemon/client"
|
|
"src.elv.sh/pkg/daemon/internal/api"
|
|
"src.elv.sh/pkg/prog"
|
|
. "src.elv.sh/pkg/prog/progtest"
|
|
"src.elv.sh/pkg/store/storetest"
|
|
"src.elv.sh/pkg/testutil"
|
|
)
|
|
|
|
func TestDaemon(t *testing.T) {
|
|
// Set up filesystem.
|
|
testutil.InTempDir(t)
|
|
|
|
// Set up server.
|
|
serverDone := make(chan struct{})
|
|
go func() {
|
|
Serve("sock", "db")
|
|
close(serverDone)
|
|
}()
|
|
defer func() { <-serverDone }()
|
|
|
|
// Set up client.
|
|
client := client.NewClient("sock")
|
|
defer client.Close()
|
|
for i := 0; i < 100; i++ {
|
|
client.ResetConn()
|
|
_, err := client.Version()
|
|
if err == nil {
|
|
break
|
|
} else if i == 99 {
|
|
t.Fatal("Failed to connect after 1s")
|
|
}
|
|
time.Sleep(testutil.ScaledMs(10))
|
|
}
|
|
|
|
// Server state requests.
|
|
gotVersion, err := client.Version()
|
|
if gotVersion != api.Version || err != nil {
|
|
t.Errorf(".Version() -> (%v, %v), want (%v, nil)", gotVersion, err, api.Version)
|
|
}
|
|
|
|
gotPid, err := client.Pid()
|
|
wantPid := syscall.Getpid()
|
|
if gotPid != wantPid || err != nil {
|
|
t.Errorf(".Pid() -> (%v, %v), want (%v, nil)", gotPid, err, wantPid)
|
|
}
|
|
|
|
// Store requests.
|
|
storetest.TestCmd(t, client)
|
|
storetest.TestDir(t, client)
|
|
storetest.TestSharedVar(t, client)
|
|
}
|
|
|
|
func TestProgram_SpuriousArgument(t *testing.T) {
|
|
f := Setup(t)
|
|
|
|
exit := prog.Run(f.Fds(), Elvish("-daemon", "x"), Program)
|
|
|
|
TestError(t, f, exit, "arguments are not allowed with -daemon")
|
|
}
|