From 4dec6e673574cf1433931e9a2b510e4df9969dcb Mon Sep 17 00:00:00 2001 From: Fedor Korotkiy Date: Mon, 6 Mar 2023 13:57:51 +0400 Subject: [PATCH] Fix flaky test --- shopfront/redis_test.go | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/shopfront/redis_test.go b/shopfront/redis_test.go index 8d89c7c..d7afef2 100644 --- a/shopfront/redis_test.go +++ b/shopfront/redis_test.go @@ -1,10 +1,12 @@ package shopfront_test import ( + "context" "os" "os/exec" "time" + "github.com/go-redis/redis/v8" "github.com/stretchr/testify/require" "gitlab.com/slon/shad-go/tools/testtool" @@ -36,22 +38,39 @@ func StartRedis(tb testingTB) string { cmd.Stderr = os.Stderr require.NoError(tb, cmd.Start()) + tb.Cleanup(func() { + _ = cmd.Process.Kill() + }) finished := make(chan error, 1) go func() { finished <- cmd.Wait() }() - select { - case err := <-finished: - tb.Fatalf("redis server terminated: %v", err) + redisAddress := "localhost:" + port + startTimeout := time.After(time.Second * 5) - case <-time.After(time.Second / 2): +loop: + for { + select { + case err := <-finished: + tb.Fatalf("redis server terminated: %v", err) + + 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() { - _ = cmd.Process.Kill() - }) - - return "localhost:" + port + return redisAddress }