2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-10 07:11:06 +00:00

rest: Wait until REST server is started

This commit is contained in:
Alexander Neumann 2017-05-12 21:33:34 +02:00
parent a654f41ddb
commit 88de3cfecc

View File

@ -1,15 +1,15 @@
package rest_test package rest_test
import ( import (
"bufio"
"context" "context"
"io/ioutil" "io/ioutil"
"net"
"net/url" "net/url"
"os" "os"
"os/exec" "os/exec"
"restic" "restic"
"strings"
"testing" "testing"
"time"
"restic/backend/rest" "restic/backend/rest"
"restic/backend/test" "restic/backend/test"
@ -24,20 +24,29 @@ func runRESTServer(ctx context.Context, t testing.TB, dir string) func() {
cmd := exec.CommandContext(ctx, srv, "--path", dir) cmd := exec.CommandContext(ctx, srv, "--path", dir)
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
stderr, err := cmd.StderrPipe() cmd.Stderr = os.Stdout
if err != nil {
t.Fatal(err)
}
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
sc := bufio.NewScanner(stderr) // wait until the TCP port is reachable
for sc.Scan() { var success bool
if strings.HasPrefix(sc.Text(), "Starting server") { for i := 0; i < 10; i++ {
break c, err := net.Dial("tcp", "localhost:8000")
if err != nil {
time.Sleep(200 * time.Millisecond)
continue
} }
success = true
if err := c.Close(); err != nil {
t.Fatal(err)
}
}
if !success {
t.Fatal("unable to connect to rest server")
return nil
} }
return func() { return func() {