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
import (
"bufio"
"context"
"io/ioutil"
"net"
"net/url"
"os"
"os/exec"
"restic"
"strings"
"testing"
"time"
"restic/backend/rest"
"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.Stdout = os.Stdout
stderr, err := cmd.StderrPipe()
if err != nil {
t.Fatal(err)
}
cmd.Stderr = os.Stdout
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
sc := bufio.NewScanner(stderr)
for sc.Scan() {
if strings.HasPrefix(sc.Text(), "Starting server") {
break
// wait until the TCP port is reachable
var success bool
for i := 0; i < 10; i++ {
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() {