From fde08f91fd25da092acb975b813af0556b2870d1 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Wed, 31 Jan 2024 22:41:02 +0100 Subject: [PATCH] [GITEA] tests: A workaround for the MySQL collation tests Because Forgejo run mysqld with `--innodb-flush-method=nosync` to speed up the test suite, there are situations where a larger, database-wide operation does not always fully manifest until later, not even when it is wrapped in a transaction, nor when we use `FLUSH TABLES` and similar methods. In the case of the MySQL collation test, this *sometimes* results in the database still responding with the old collation to a reader, even after an `ALTER DATABASE ... COLLATE ...`. In order to be able to still use the aforementioned flag and enjoy its benefits, add a five second sleep between `db.ConvertDatabaseTable()` and `db.CheckCollations()` in the `TestDatabaseCollation()` set of tests. This is not a fix - I don't think there is one possible -, but a workaround. If it breaks again, the correct fix will be to remove the flag from `mysqld` (it's not a supported flag to begin with). Signed-off-by: Gergely Nagy (cherry picked from commit af18ed2ba9b1d6e228854b76cc4ffff790b8804b) --- tests/integration/db_collation_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/integration/db_collation_test.go b/tests/integration/db_collation_test.go index 468d13508d..f81529d582 100644 --- a/tests/integration/db_collation_test.go +++ b/tests/integration/db_collation_test.go @@ -5,10 +5,12 @@ package integration import ( "testing" + "time" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/test" + "code.gitea.io/gitea/tests" "github.com/stretchr/testify/assert" "xorm.io/xorm" @@ -48,6 +50,8 @@ func TestDatabaseCollation(t *testing.T) { } t.Run("Default startup makes database collation case-sensitive", func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + r, err := db.CheckCollations(x) assert.NoError(t, err) assert.True(t, r.IsCollationCaseSensitive(r.DatabaseCollation)) @@ -78,8 +82,12 @@ func TestDatabaseCollation(t *testing.T) { } t.Run("Convert tables to utf8mb4_bin", func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + defer test.MockVariableValue(&setting.Database.CharsetCollation, "utf8mb4_bin")() assert.NoError(t, db.ConvertDatabaseTable()) + time.Sleep(5 * time.Second) + r, err := db.CheckCollations(x) assert.NoError(t, err) assert.Equal(t, "utf8mb4_bin", r.DatabaseCollation) @@ -95,8 +103,12 @@ func TestDatabaseCollation(t *testing.T) { }) t.Run("Convert tables to utf8mb4_general_ci", func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + defer test.MockVariableValue(&setting.Database.CharsetCollation, "utf8mb4_general_ci")() assert.NoError(t, db.ConvertDatabaseTable()) + time.Sleep(5 * time.Second) + r, err := db.CheckCollations(x) assert.NoError(t, err) assert.Equal(t, "utf8mb4_general_ci", r.DatabaseCollation) @@ -112,8 +124,12 @@ func TestDatabaseCollation(t *testing.T) { }) t.Run("Convert tables to default case-sensitive collation", func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + defer test.MockVariableValue(&setting.Database.CharsetCollation, "")() assert.NoError(t, db.ConvertDatabaseTable()) + time.Sleep(5 * time.Second) + r, err := db.CheckCollations(x) assert.NoError(t, err) assert.True(t, r.IsCollationCaseSensitive(r.DatabaseCollation))