diff options
author | Pavlo Strokov <pstrokov@gitlab.com> | 2020-12-08 19:07:53 +0300 |
---|---|---|
committer | Pavlo Strokov <pstrokov@gitlab.com> | 2020-12-16 18:51:33 +0300 |
commit | 8f6ad9c603b86487dff648294f39169a308d2af0 (patch) | |
tree | 187312ab2d64dd3ef7d5a94f9cce15b50f5e759c | |
parent | 2dbc86a6542d5dc335ade8ecfd2dad39d6bd055f (diff) |
Fix flakyness in tests that rely on PostgreSQL database
The set of tests uses a PostgreSQL database instance for implementation
verification. The setup uses a PgBouncer in between tests and the
database. The tests fail pretty frequently on CI because of the next error:
'pq: canceling statement due to user request'. The thing is that there is no
requests to cancel the statement that is failing. As the failing statement
is a TRUNCATE operation this change makes an attempt to replace it with
the DELETE statement in pair with restarting of the tables sequences if any.
Closes: https://gitlab.com/gitlab-org/gitaly/-/issues/3208
-rw-r--r-- | internal/praefect/datastore/glsql/testing.go | 13 |
1 files changed, 5 insertions, 8 deletions
diff --git a/internal/praefect/datastore/glsql/testing.go b/internal/praefect/datastore/glsql/testing.go index 169b62baa..00e4c02f7 100644 --- a/internal/praefect/datastore/glsql/testing.go +++ b/internal/praefect/datastore/glsql/testing.go @@ -3,7 +3,6 @@ package glsql import ( "database/sql" "errors" - "fmt" "os" "os/exec" "strconv" @@ -11,7 +10,6 @@ import ( "sync" "testing" - "github.com/lib/pq" "github.com/stretchr/testify/require" "gitlab.com/gitlab-org/gitaly/internal/praefect/config" ) @@ -32,14 +30,13 @@ type DB struct { func (db DB) Truncate(t testing.TB, tables ...string) { t.Helper() - params := make([]string, len(tables)) - for i, table := range tables { - params[i] = pq.QuoteIdentifier(table) + " *" + for _, table := range tables { + _, err := db.DB.Exec("DELETE FROM " + table) + require.NoError(t, err, "database cleanup failed: %s", tables) } - query := fmt.Sprintf("TRUNCATE TABLE %s RESTART IDENTITY CASCADE", strings.Join(params, ", ")) - _, err := db.DB.Exec(query) - require.NoError(t, err, "database truncation failed: %s", tables) + _, err := db.DB.Exec("SELECT setval(relname::TEXT, 1, false) from pg_class where relkind = 'S'") + require.NoError(t, err, "database cleanup failed: %s", tables) } // RequireRowsInTable verifies that `tname` table has `n` amount of rows in it. |