Fix flaky test

This commit is contained in:
Fedor Korotkiy 2023-03-06 13:57:51 +04:00
parent 9e2fbf54f1
commit 4dec6e6735

View file

@ -1,10 +1,12 @@
package shopfront_test package shopfront_test
import ( import (
"context"
"os" "os"
"os/exec" "os/exec"
"time" "time"
"github.com/go-redis/redis/v8"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"gitlab.com/slon/shad-go/tools/testtool" "gitlab.com/slon/shad-go/tools/testtool"
@ -36,22 +38,39 @@ func StartRedis(tb testingTB) string {
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
require.NoError(tb, cmd.Start()) require.NoError(tb, cmd.Start())
tb.Cleanup(func() {
_ = cmd.Process.Kill()
})
finished := make(chan error, 1) finished := make(chan error, 1)
go func() { go func() {
finished <- cmd.Wait() finished <- cmd.Wait()
}() }()
redisAddress := "localhost:" + port
startTimeout := time.After(time.Second * 5)
loop:
for {
select { select {
case err := <-finished: case err := <-finished:
tb.Fatalf("redis server terminated: %v", err) tb.Fatalf("redis server terminated: %v", err)
case <-time.After(time.Second / 2): case <-startTimeout:
tb.Fatalf("redis not started after timeout")
default:
time.Sleep(time.Millisecond * 50)
rdb := redis.NewClient(&redis.Options{Addr: redisAddress})
status := rdb.Ping(context.Background())
_ = rdb.Close()
if status.Err() == nil {
break loop
}
}
} }
tb.Cleanup(func() { return redisAddress
_ = cmd.Process.Kill()
})
return "localhost:" + port
} }