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 <forgejo@gergo.csillger.hu>
This commit is contained in:
Gergely Nagy 2024-01-31 22:41:02 +01:00
parent 7420102b1b
commit af18ed2ba9
No known key found for this signature in database

View file

@ -5,10 +5,12 @@ package integration
import ( import (
"testing" "testing"
"time"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/test" "code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/tests"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"xorm.io/xorm" "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) { t.Run("Default startup makes database collation case-sensitive", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
r, err := db.CheckCollations(x) r, err := db.CheckCollations(x)
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, r.IsCollationCaseSensitive(r.DatabaseCollation)) 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) { t.Run("Convert tables to utf8mb4_bin", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
defer test.MockVariableValue(&setting.Database.CharsetCollation, "utf8mb4_bin")() defer test.MockVariableValue(&setting.Database.CharsetCollation, "utf8mb4_bin")()
assert.NoError(t, db.ConvertDatabaseTable()) assert.NoError(t, db.ConvertDatabaseTable())
time.Sleep(5 * time.Second)
r, err := db.CheckCollations(x) r, err := db.CheckCollations(x)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "utf8mb4_bin", r.DatabaseCollation) 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) { 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")() defer test.MockVariableValue(&setting.Database.CharsetCollation, "utf8mb4_general_ci")()
assert.NoError(t, db.ConvertDatabaseTable()) assert.NoError(t, db.ConvertDatabaseTable())
time.Sleep(5 * time.Second)
r, err := db.CheckCollations(x) r, err := db.CheckCollations(x)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "utf8mb4_general_ci", r.DatabaseCollation) 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) { t.Run("Convert tables to default case-sensitive collation", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
defer test.MockVariableValue(&setting.Database.CharsetCollation, "")() defer test.MockVariableValue(&setting.Database.CharsetCollation, "")()
assert.NoError(t, db.ConvertDatabaseTable()) assert.NoError(t, db.ConvertDatabaseTable())
time.Sleep(5 * time.Second)
r, err := db.CheckCollations(x) r, err := db.CheckCollations(x)
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, r.IsCollationCaseSensitive(r.DatabaseCollation)) assert.True(t, r.IsCollationCaseSensitive(r.DatabaseCollation))