From 6d1f1595007baeeb5776efcaacf6e26fb74b7d04 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Fri, 30 Aug 2019 10:03:22 +0200 Subject: [PATCH] tests: add wait function in place of sleep Add a function to wait for a specific condition instead of sleeping for a fixed number of seconds. --- internal/testutil/utils.go | 17 ++++++++++++ tests/setup_test.go | 56 ++++++++++++++++++++++++++++++++++---- 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/internal/testutil/utils.go b/internal/testutil/utils.go index 8aee1d2..a2bba44 100644 --- a/internal/testutil/utils.go +++ b/internal/testutil/utils.go @@ -526,6 +526,23 @@ func NewTestGitea(t *testing.T, logger *zap.Logger, dir, dockerBridgeAddress str return tgitea, nil } +type CheckFunc func() (bool, error) + +func Wait(timeout time.Duration, f CheckFunc) error { + start := time.Now() + for time.Now().Add(-timeout).Before(start) { + ok, err := f() + if err != nil { + return err + } + if ok { + return nil + } + time.Sleep(sleepInterval) + } + return fmt.Errorf("timeout") +} + func testFreeTCPPort(port int) error { ln, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", port)) if err != nil { diff --git a/tests/setup_test.go b/tests/setup_test.go index 345f574..e969457 100644 --- a/tests/setup_test.go +++ b/tests/setup_test.go @@ -606,8 +606,22 @@ func TestPush(t *testing.T) { push(t, tt.config, giteaRepo.CloneURL, giteaToken) - // TODO(sgotti) add an util to wait for a run phase - time.Sleep(10 * time.Second) + _ = testutil.Wait(30*time.Second, func() (bool, error) { + runs, _, err := gwClient.GetRuns(ctx, nil, nil, []string{path.Join("/project", project.ID)}, nil, "", 0, false) + if err != nil { + return false, nil + } + + if len(runs) == 0 { + return false, nil + } + run := runs[0] + if run.Phase != rstypes.RunPhaseFinished { + return false, nil + } + + return true, nil + }) runs, _, err := gwClient.GetRuns(ctx, nil, nil, []string{path.Join("/project", project.ID)}, nil, "", 0, false) if err != nil { @@ -634,6 +648,7 @@ func TestPush(t *testing.T) { } } } + }) } } @@ -777,8 +792,23 @@ func TestDirectRun(t *testing.T) { directRun(t, dir, config, c.Gateway.APIExposedURL, token, tt.args...) - // TODO(sgotti) add an util to wait for a run phase - time.Sleep(10 * time.Second) + _ = testutil.Wait(30*time.Second, func() (bool, error) { + runs, _, err := gwClient.GetRuns(ctx, nil, nil, []string{path.Join("/user", user.ID)}, nil, "", 0, false) + if err != nil { + return false, nil + } + + if len(runs) != 1 { + return false, nil + } + + run := runs[0] + if run.Phase != rstypes.RunPhaseFinished { + return false, nil + } + + return true, nil + }) runs, _, err := gwClient.GetRuns(ctx, nil, nil, []string{path.Join("/user", user.ID)}, nil, "", 0, false) if err != nil { @@ -916,7 +946,23 @@ func TestDirectRunVariables(t *testing.T) { directRun(t, dir, config, c.Gateway.APIExposedURL, token, tt.args...) // TODO(sgotti) add an util to wait for a run phase - time.Sleep(10 * time.Second) + _ = testutil.Wait(30*time.Second, func() (bool, error) { + runs, _, err := gwClient.GetRuns(ctx, nil, nil, []string{path.Join("/user", user.ID)}, nil, "", 0, false) + if err != nil { + return false, nil + } + + if len(runs) != 1 { + return false, nil + } + + run := runs[0] + if run.Phase != rstypes.RunPhaseFinished { + return false, nil + } + + return true, nil + }) runs, _, err := gwClient.GetRuns(ctx, nil, nil, []string{path.Join("/user", user.ID)}, nil, "", 0, false) if err != nil {