From 456bd4cd702a72f57433194e7266c0c975ac5f2f Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Mon, 24 Aug 2020 12:50:57 +0530 Subject: [PATCH] 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 --- store/storetest/terms_of_service_store.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/store/storetest/terms_of_service_store.go b/store/storetest/terms_of_service_store.go index 1d6fb04f30..585a400d1d 100644 --- a/store/storetest/terms_of_service_store.go +++ b/store/storetest/terms_of_service_store.go @@ -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()