MM-23063: Fix flaky test TestTermsOfServiceStore (#15321)

* MM-23063: Fix flaky test TestTermsOfServiceStore

The call to testGetLatestTermsOfService would happen after testSaveTermsOfService
which would persist the data between calls. Therefore, if they were to happen under
a milisecond, there would be 2 rows with the same CreateAt timestamp and the DB
would randomly return any row.

If this were to happen, then the wrong row would not match the user id and would fail.

To fix this, we just clear the table data before proceeding with the test.

https://mattermost.atlassian.net/browse/MM-23063

* Use t.Cleanup to refactor things

* Fix cleanup of tables

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Agniva De Sarker
2020-08-24 12:50:57 +05:30
коммит произвёл GitHub
родитель 94d09e1336
Коммит 456bd4cd70

Просмотреть файл

@@ -18,7 +18,19 @@ func TestTermsOfServiceStore(t *testing.T, ss store.Store) {
t.Run("TestGetTermsOfService", func(t *testing.T) { testGetTermsOfService(t, ss) })
}
func cleanUpTOS(ss store.Store) {
// Clearing out the table before starting the test.
// Otherwise the row inserted by the previous Save call from testSaveTermsOfService
// gets picked up.
// We call DropAllTables but we actually need to delete only TermsOfService.
// However, there is no straightforward way to just clear that table without introducing
// new methods. So we use the hammer.
ss.DropAllTables()
}
func testSaveTermsOfService(t *testing.T, ss store.Store) {
t.Cleanup(func() { cleanUpTOS(ss) })
u1 := model.User{}
u1.Username = model.NewId()
u1.Email = MakeEmail()
@@ -36,6 +48,8 @@ func testSaveTermsOfService(t *testing.T, ss store.Store) {
}
func testGetLatestTermsOfService(t *testing.T, ss store.Store) {
t.Cleanup(func() { cleanUpTOS(ss) })
u1 := model.User{}
u1.Username = model.NewId()
u1.Email = MakeEmail()
@@ -43,7 +57,7 @@ func testGetLatestTermsOfService(t *testing.T, ss store.Store) {
_, appErr := ss.User().Save(&u1)
require.Nil(t, appErr)
termsOfService := &model.TermsOfService{Text: "terms of service", UserId: u1.Id}
termsOfService := &model.TermsOfService{Text: "terms of service 2", UserId: u1.Id}
_, err := ss.TermsOfService().Save(termsOfService)
require.Nil(t, err)
@@ -54,6 +68,8 @@ func testGetLatestTermsOfService(t *testing.T, ss store.Store) {
}
func testGetTermsOfService(t *testing.T, ss store.Store) {
t.Cleanup(func() { cleanUpTOS(ss) })
u1 := model.User{}
u1.Username = model.NewId()
u1.Email = MakeEmail()