Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'internal/praefect/datastore/glsql/testing.go')
-rw-r--r--internal/praefect/datastore/glsql/testing.go30
1 files changed, 30 insertions, 0 deletions
diff --git a/internal/praefect/datastore/glsql/testing.go b/internal/praefect/datastore/glsql/testing.go
index e1f8145e1..8c89babdf 100644
--- a/internal/praefect/datastore/glsql/testing.go
+++ b/internal/praefect/datastore/glsql/testing.go
@@ -1,6 +1,7 @@
package glsql
import (
+ "context"
"database/sql"
"errors"
"os"
@@ -9,6 +10,7 @@ import (
"strings"
"sync"
"testing"
+ "time"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v14/internal/praefect/config"
@@ -201,3 +203,31 @@ func getEnvFromGDK(t testing.TB) {
require.NoError(t, os.Setenv(key, value), "set env var %v", key)
}
}
+
+// WaitForQueries is a helper that waits until a certain number of queries matching the prefix are present in the
+// database. This is useful for ensuring multiple transactions are executing the query when testing concurrent
+// execution.
+func WaitForQueries(ctx context.Context, t testing.TB, db Querier, queryPrefix string, count int) {
+ t.Helper()
+
+ for {
+ var queriesPresent bool
+ require.NoError(t, db.QueryRowContext(ctx, `
+ SELECT COUNT(*) = $2
+ FROM pg_stat_activity
+ WHERE TRIM(e'\n' FROM query) LIKE $1
+ `, queryPrefix+"%", count).Scan(&queriesPresent))
+
+ if queriesPresent {
+ return
+ }
+
+ retry := time.NewTimer(time.Millisecond)
+ select {
+ case <-ctx.Done():
+ retry.Stop()
+ return
+ case <-retry.C:
+ }
+ }
+}