From 0245c606d77c8b99833ccc2c0923a298fb482236 Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Wed, 31 Aug 2022 17:31:21 +0200 Subject: [chore] Test fixes (#788) * use 'test' value for testrig storage backend * update test dependency * add WaitFor func in testrig * use WaitFor function instead of time.Sleep * tidy up tests * make SentMessages a sync.map * go fmt --- internal/api/client/admin/mediacleanup_test.go | 38 +++++++++++++------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'internal/api/client') diff --git a/internal/api/client/admin/mediacleanup_test.go b/internal/api/client/admin/mediacleanup_test.go index 345402ad5..039bb7598 100644 --- a/internal/api/client/admin/mediacleanup_test.go +++ b/internal/api/client/admin/mediacleanup_test.go @@ -27,6 +27,7 @@ import ( "github.com/stretchr/testify/suite" "github.com/superseriousbusiness/gotosocial/internal/api/client/admin" + "github.com/superseriousbusiness/gotosocial/testrig" ) type MediaCleanupTestSuite struct { @@ -47,15 +48,15 @@ func (suite *MediaCleanupTestSuite) TestMediaCleanup() { // we should have OK because our request was valid suite.Equal(http.StatusOK, recorder.Code) - // Wait for async task to finish - time.Sleep(1 * time.Second) - - // Get media we prunes - prunedAttachment, err := suite.db.GetAttachmentByID(context.Background(), testAttachment.ID) - suite.NoError(err) - - // the media should no longer be cached - suite.False(*prunedAttachment.Cached) + // the attachment should be updated in the database + if !testrig.WaitFor(func() bool { + if prunedAttachment, _ := suite.db.GetAttachmentByID(context.Background(), testAttachment.ID); prunedAttachment != nil { + return !*prunedAttachment.Cached + } + return false + }) { + suite.FailNow("timed out waiting for attachment to be pruned") + } } func (suite *MediaCleanupTestSuite) TestMediaCleanupNoArg() { @@ -73,15 +74,14 @@ func (suite *MediaCleanupTestSuite) TestMediaCleanupNoArg() { // we should have OK because our request was valid suite.Equal(http.StatusOK, recorder.Code) - // Wait for async task to finish - time.Sleep(1 * time.Second) - - // Get media we prunes - prunedAttachment, err := suite.db.GetAttachmentByID(context.Background(), testAttachment.ID) - suite.NoError(err) - - // the media should no longer be cached - suite.False(*prunedAttachment.Cached) + if !testrig.WaitFor(func() bool { + if prunedAttachment, _ := suite.db.GetAttachmentByID(context.Background(), testAttachment.ID); prunedAttachment != nil { + return !*prunedAttachment.Cached + } + return false + }) { + suite.FailNow("timed out waiting for attachment to be pruned") + } } func (suite *MediaCleanupTestSuite) TestMediaCleanupNotOldEnough() { @@ -101,7 +101,7 @@ func (suite *MediaCleanupTestSuite) TestMediaCleanupNotOldEnough() { // Wait for async task to finish time.Sleep(1 * time.Second) - // Get media we prunes + // Get media we pruned prunedAttachment, err := suite.db.GetAttachmentByID(context.Background(), testAttachment.ID) suite.NoError(err) -- cgit v1.2.3