diff options
author | 2022-08-31 17:31:21 +0200 | |
---|---|---|
committer | 2022-08-31 17:31:21 +0200 | |
commit | 0245c606d77c8b99833ccc2c0923a298fb482236 (patch) | |
tree | 16311e89656894f09cfaeb8b0f21b5ac9e4de502 /internal | |
parent | [feature] add rate limit middleware (#741) (diff) | |
download | gotosocial-0245c606d77c8b99833ccc2c0923a298fb482236.tar.xz |
[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
Diffstat (limited to 'internal')
-rw-r--r-- | internal/api/client/admin/mediacleanup_test.go | 38 | ||||
-rw-r--r-- | internal/api/s2s/user/inboxpost_test.go | 17 | ||||
-rw-r--r-- | internal/api/s2s/user/outboxget_test.go | 11 | ||||
-rw-r--r-- | internal/api/s2s/user/repliesget_test.go | 11 | ||||
-rw-r--r-- | internal/api/s2s/user/statusget_test.go | 24 | ||||
-rw-r--r-- | internal/api/s2s/user/userget_test.go | 38 | ||||
-rw-r--r-- | internal/federation/dereferencing/media_test.go | 12 | ||||
-rw-r--r-- | internal/federation/federatingactor_test.go | 20 | ||||
-rw-r--r-- | internal/media/manager_test.go | 10 | ||||
-rw-r--r-- | internal/processing/account_test.go | 43 | ||||
-rw-r--r-- | internal/processing/followrequest_test.go | 39 | ||||
-rw-r--r-- | internal/processing/fromfederator_test.go | 51 | ||||
-rw-r--r-- | internal/processing/media/getfile_test.go | 23 | ||||
-rw-r--r-- | internal/trans/trans_test.go | 4 |
14 files changed, 177 insertions, 164 deletions
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) diff --git a/internal/api/s2s/user/inboxpost_test.go b/internal/api/s2s/user/inboxpost_test.go index 506fa270c..ff3ec47d3 100644 --- a/internal/api/s2s/user/inboxpost_test.go +++ b/internal/api/s2s/user/inboxpost_test.go @@ -444,14 +444,15 @@ func (suite *InboxPostTestSuite) TestPostDelete() { suite.Empty(b) suite.Equal(http.StatusOK, result.StatusCode) - // sleep for a sec so side effects can process in the background - time.Sleep(2 * time.Second) - - // local account 2 blocked foss_satan, that block should be gone now - testBlock := suite.testBlocks["local_account_2_block_remote_account_1"] - dbBlock := >smodel.Block{} - err = suite.db.GetByID(ctx, testBlock.ID, dbBlock) - suite.ErrorIs(err, db.ErrNoEntries) + if !testrig.WaitFor(func() bool { + // local account 2 blocked foss_satan, that block should be gone now + testBlock := suite.testBlocks["local_account_2_block_remote_account_1"] + dbBlock := >smodel.Block{} + err = suite.db.GetByID(ctx, testBlock.ID, dbBlock) + return suite.ErrorIs(err, db.ErrNoEntries) + }) { + suite.FailNow("timed out waiting for block to be removed") + } // no statuses from foss satan should be left in the database dbStatuses, err := suite.db.GetAccountStatuses(ctx, deletedAccount.ID, 0, false, false, "", "", false, false, false) diff --git a/internal/api/s2s/user/outboxget_test.go b/internal/api/s2s/user/outboxget_test.go index 58b57954c..24c0e2cee 100644 --- a/internal/api/s2s/user/outboxget_test.go +++ b/internal/api/s2s/user/outboxget_test.go @@ -46,15 +46,6 @@ func (suite *OutboxGetTestSuite) TestGetOutbox() { signedRequest := derefRequests["foss_satan_dereference_zork_outbox"] targetAccount := suite.testAccounts["local_account_1"] - clientWorker := concurrency.NewWorkerPool[messages.FromClientAPI](-1, -1) - fedWorker := concurrency.NewWorkerPool[messages.FromFederator](-1, -1) - - tc := testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil, "../../../../testrig/media"), suite.db, fedWorker) - federator := testrig.NewTestFederator(suite.db, tc, suite.storage, suite.mediaManager, fedWorker) - emailSender := testrig.NewEmailSender("../../../../web/template/", nil) - processor := testrig.NewTestProcessor(suite.db, suite.storage, federator, emailSender, suite.mediaManager, clientWorker, fedWorker) - userModule := user.New(processor).(*user.Module) - // setup request recorder := httptest.NewRecorder() ctx, _ := testrig.CreateGinTestContext(recorder, nil) @@ -76,7 +67,7 @@ func (suite *OutboxGetTestSuite) TestGetOutbox() { } // trigger the function being tested - userModule.OutboxGETHandler(ctx) + suite.userModule.OutboxGETHandler(ctx) // check response suite.EqualValues(http.StatusOK, recorder.Code) diff --git a/internal/api/s2s/user/repliesget_test.go b/internal/api/s2s/user/repliesget_test.go index 07a3f2788..7ec9ae54c 100644 --- a/internal/api/s2s/user/repliesget_test.go +++ b/internal/api/s2s/user/repliesget_test.go @@ -49,15 +49,6 @@ func (suite *RepliesGetTestSuite) TestGetReplies() { targetAccount := suite.testAccounts["local_account_1"] targetStatus := suite.testStatuses["local_account_1_status_1"] - clientWorker := concurrency.NewWorkerPool[messages.FromClientAPI](-1, -1) - fedWorker := concurrency.NewWorkerPool[messages.FromFederator](-1, -1) - - tc := testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil, "../../../../testrig/media"), suite.db, fedWorker) - federator := testrig.NewTestFederator(suite.db, tc, suite.storage, suite.mediaManager, fedWorker) - emailSender := testrig.NewEmailSender("../../../../web/template/", nil) - processor := testrig.NewTestProcessor(suite.db, suite.storage, federator, emailSender, suite.mediaManager, clientWorker, fedWorker) - userModule := user.New(processor).(*user.Module) - // setup request recorder := httptest.NewRecorder() ctx, _ := testrig.CreateGinTestContext(recorder, nil) @@ -83,7 +74,7 @@ func (suite *RepliesGetTestSuite) TestGetReplies() { } // trigger the function being tested - userModule.StatusRepliesGETHandler(ctx) + suite.userModule.StatusRepliesGETHandler(ctx) // check response suite.EqualValues(http.StatusOK, recorder.Code) diff --git a/internal/api/s2s/user/statusget_test.go b/internal/api/s2s/user/statusget_test.go index 26fb71818..42f7dbb1b 100644 --- a/internal/api/s2s/user/statusget_test.go +++ b/internal/api/s2s/user/statusget_test.go @@ -32,8 +32,6 @@ import ( "github.com/superseriousbusiness/activity/streams" "github.com/superseriousbusiness/activity/streams/vocab" "github.com/superseriousbusiness/gotosocial/internal/api/s2s/user" - "github.com/superseriousbusiness/gotosocial/internal/concurrency" - "github.com/superseriousbusiness/gotosocial/internal/messages" "github.com/superseriousbusiness/gotosocial/testrig" ) @@ -48,15 +46,6 @@ func (suite *StatusGetTestSuite) TestGetStatus() { targetAccount := suite.testAccounts["local_account_1"] targetStatus := suite.testStatuses["local_account_1_status_1"] - clientWorker := concurrency.NewWorkerPool[messages.FromClientAPI](-1, -1) - fedWorker := concurrency.NewWorkerPool[messages.FromFederator](-1, -1) - - tc := testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil, "../../../../testrig/media"), suite.db, fedWorker) - federator := testrig.NewTestFederator(suite.db, tc, suite.storage, suite.mediaManager, fedWorker) - emailSender := testrig.NewEmailSender("../../../../web/template/", nil) - processor := testrig.NewTestProcessor(suite.db, suite.storage, federator, emailSender, suite.mediaManager, clientWorker, fedWorker) - userModule := user.New(processor).(*user.Module) - // setup request recorder := httptest.NewRecorder() ctx, _ := testrig.CreateGinTestContext(recorder, nil) @@ -82,7 +71,7 @@ func (suite *StatusGetTestSuite) TestGetStatus() { } // trigger the function being tested - userModule.StatusGETHandler(ctx) + suite.userModule.StatusGETHandler(ctx) // check response suite.EqualValues(http.StatusOK, recorder.Code) @@ -116,15 +105,6 @@ func (suite *StatusGetTestSuite) TestGetStatusLowercase() { targetAccount := suite.testAccounts["local_account_1"] targetStatus := suite.testStatuses["local_account_1_status_1"] - clientWorker := concurrency.NewWorkerPool[messages.FromClientAPI](-1, -1) - fedWorker := concurrency.NewWorkerPool[messages.FromFederator](-1, -1) - - tc := testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil, "../../../../testrig/media"), suite.db, fedWorker) - federator := testrig.NewTestFederator(suite.db, tc, suite.storage, suite.mediaManager, fedWorker) - emailSender := testrig.NewEmailSender("../../../../web/template/", nil) - processor := testrig.NewTestProcessor(suite.db, suite.storage, federator, emailSender, suite.mediaManager, clientWorker, fedWorker) - userModule := user.New(processor).(*user.Module) - // setup request recorder := httptest.NewRecorder() ctx, _ := testrig.CreateGinTestContext(recorder, nil) @@ -150,7 +130,7 @@ func (suite *StatusGetTestSuite) TestGetStatusLowercase() { } // trigger the function being tested - userModule.StatusGETHandler(ctx) + suite.userModule.StatusGETHandler(ctx) // check response suite.EqualValues(http.StatusOK, recorder.Code) diff --git a/internal/api/s2s/user/userget_test.go b/internal/api/s2s/user/userget_test.go index 0b242432c..9e0fd33ad 100644 --- a/internal/api/s2s/user/userget_test.go +++ b/internal/api/s2s/user/userget_test.go @@ -25,7 +25,6 @@ import ( "net/http" "net/http/httptest" "testing" - "time" "github.com/gin-gonic/gin" "github.com/stretchr/testify/suite" @@ -33,8 +32,6 @@ import ( "github.com/superseriousbusiness/activity/streams/vocab" apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" "github.com/superseriousbusiness/gotosocial/internal/api/s2s/user" - "github.com/superseriousbusiness/gotosocial/internal/concurrency" - "github.com/superseriousbusiness/gotosocial/internal/messages" "github.com/superseriousbusiness/gotosocial/internal/oauth" "github.com/superseriousbusiness/gotosocial/testrig" ) @@ -49,15 +46,6 @@ func (suite *UserGetTestSuite) TestGetUser() { signedRequest := derefRequests["foss_satan_dereference_zork"] targetAccount := suite.testAccounts["local_account_1"] - clientWorker := concurrency.NewWorkerPool[messages.FromClientAPI](-1, -1) - fedWorker := concurrency.NewWorkerPool[messages.FromFederator](-1, -1) - - tc := testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil, "../../../../testrig/media"), suite.db, fedWorker) - federator := testrig.NewTestFederator(suite.db, tc, suite.storage, suite.mediaManager, fedWorker) - emailSender := testrig.NewEmailSender("../../../../web/template/", nil) - processor := testrig.NewTestProcessor(suite.db, suite.storage, federator, emailSender, suite.mediaManager, clientWorker, fedWorker) - userModule := user.New(processor).(*user.Module) - // setup request recorder := httptest.NewRecorder() ctx, _ := testrig.CreateGinTestContext(recorder, nil) @@ -79,7 +67,7 @@ func (suite *UserGetTestSuite) TestGetUser() { } // trigger the function being tested - userModule.UsersGETHandler(ctx) + suite.userModule.UsersGETHandler(ctx) // check response suite.EqualValues(http.StatusOK, recorder.Code) @@ -110,6 +98,12 @@ func (suite *UserGetTestSuite) TestGetUser() { // TestGetUserPublicKeyDeleted checks whether the public key of a deleted account can still be dereferenced. // This is needed by remote instances for authenticating delete requests and stuff like that. func (suite *UserGetTestSuite) TestGetUserPublicKeyDeleted() { + if err := suite.processor.Start(); err != nil { + suite.FailNow(err.Error()) + } + defer suite.processor.Stop() + + userModule := user.New(suite.processor).(*user.Module) targetAccount := suite.testAccounts["local_account_1"] // first delete the account, as though zork had deleted himself @@ -123,22 +117,18 @@ func (suite *UserGetTestSuite) TestGetUserPublicKeyDeleted() { DeleteOriginID: targetAccount.ID, }) - // now wait just a sec for it to go through.... - time.Sleep(1 * time.Second) + // wait for the account delete to be processed + if !testrig.WaitFor(func() bool { + a, _ := suite.db.GetAccountByID(context.Background(), targetAccount.ID) + return !a.SuspendedAt.IsZero() + }) { + suite.FailNow("delete of account timed out") + } // the dereference we're gonna use derefRequests := testrig.NewTestDereferenceRequests(suite.testAccounts) signedRequest := derefRequests["foss_satan_dereference_zork_public_key"] - clientWorker := concurrency.NewWorkerPool[messages.FromClientAPI](-1, -1) - fedWorker := concurrency.NewWorkerPool[messages.FromFederator](-1, -1) - - tc := testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil, "../../../../testrig/media"), suite.db, fedWorker) - federator := testrig.NewTestFederator(suite.db, tc, suite.storage, suite.mediaManager, fedWorker) - emailSender := testrig.NewEmailSender("../../../../web/template/", nil) - processor := testrig.NewTestProcessor(suite.db, suite.storage, federator, emailSender, suite.mediaManager, clientWorker, fedWorker) - userModule := user.New(processor).(*user.Module) - // setup request recorder := httptest.NewRecorder() ctx, _ := testrig.CreateGinTestContext(recorder, nil) diff --git a/internal/federation/dereferencing/media_test.go b/internal/federation/dereferencing/media_test.go index 26d5c0c49..1c460b69e 100644 --- a/internal/federation/dereferencing/media_test.go +++ b/internal/federation/dereferencing/media_test.go @@ -20,13 +20,12 @@ package dereferencing_test import ( "context" - "fmt" "testing" - "time" "github.com/stretchr/testify/suite" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/media" + "github.com/superseriousbusiness/gotosocial/testrig" ) type AttachmentTestSuite struct { @@ -128,12 +127,11 @@ func (suite *AttachmentTestSuite) TestDereferenceAttachmentAsync() { suite.NoError(err) attachmentID := processingMedia.AttachmentID() - // wait for the media to finish processing - for finished := processingMedia.Finished(); !finished; finished = processingMedia.Finished() { - time.Sleep(10 * time.Millisecond) - fmt.Printf("\n\nnot finished yet...\n\n") + if !testrig.WaitFor(func() bool { + return processingMedia.Finished() + }) { + suite.FailNow("timed out waiting for media to be processed") } - fmt.Printf("\n\nfinished!\n\n") // now get the attachment from the database attachment, err := suite.db.GetAttachmentByID(ctx, attachmentID) diff --git a/internal/federation/federatingactor_test.go b/internal/federation/federatingactor_test.go index 59687345a..bab38abe2 100644 --- a/internal/federation/federatingactor_test.go +++ b/internal/federation/federatingactor_test.go @@ -115,10 +115,22 @@ func (suite *FederatingActorTestSuite) TestSendRemoteFollower() { suite.NotNil(activity) // because we added 1 remote follower for zork, there should be a url in sentMessage - suite.Len(httpClient.SentMessages, 1) - msg, ok := httpClient.SentMessages[testRemoteAccount.InboxURI] - suite.True(ok) - suite.Equal(`{"@context":"https://www.w3.org/ns/activitystreams","actor":"http://localhost:8080/users/the_mighty_zork","id":"http://localhost:8080/whatever_some_create","object":{"attributedTo":"http://localhost:8080/users/the_mighty_zork","content":"boobies","id":"http://localhost:8080/users/the_mighty_zork/statuses/01G1TR6BADACCZWQMNF9X21TV5","published":"2022-06-02T12:22:21+02:00","tag":[],"to":"http://localhost:8080/users/the_mighty_zork/followers","type":"Note","url":"http://localhost:8080/@the_mighty_zork/statuses/01G1TR6BADACCZWQMNF9X21TV5"},"published":"2022-06-02T12:22:21+02:00","to":"http://localhost:8080/users/the_mighty_zork/followers","type":"Create"}`, string(msg)) + var sent [][]byte + if !testrig.WaitFor(func() bool { + sentI, ok := httpClient.SentMessages.Load(testRemoteAccount.InboxURI) + if ok { + sent, ok = sentI.([][]byte) + if !ok { + panic("SentMessages entry was not []byte") + } + return true + } + return false + }) { + suite.FailNow("timed out waiting for message") + } + + suite.Equal(`{"@context":"https://www.w3.org/ns/activitystreams","actor":"http://localhost:8080/users/the_mighty_zork","id":"http://localhost:8080/whatever_some_create","object":{"attributedTo":"http://localhost:8080/users/the_mighty_zork","content":"boobies","id":"http://localhost:8080/users/the_mighty_zork/statuses/01G1TR6BADACCZWQMNF9X21TV5","published":"2022-06-02T12:22:21+02:00","tag":[],"to":"http://localhost:8080/users/the_mighty_zork/followers","type":"Note","url":"http://localhost:8080/@the_mighty_zork/statuses/01G1TR6BADACCZWQMNF9X21TV5"},"published":"2022-06-02T12:22:21+02:00","to":"http://localhost:8080/users/the_mighty_zork/followers","type":"Create"}`, string(sent[0])) } func TestFederatingActorTestSuite(t *testing.T) { diff --git a/internal/media/manager_test.go b/internal/media/manager_test.go index 396df1523..67f0184bd 100644 --- a/internal/media/manager_test.go +++ b/internal/media/manager_test.go @@ -26,7 +26,6 @@ import ( "os" "path" "testing" - "time" "codeberg.org/gruf/go-store/kv" "codeberg.org/gruf/go-store/storage" @@ -34,6 +33,7 @@ import ( gtsmodel "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/media" gtsstorage "github.com/superseriousbusiness/gotosocial/internal/storage" + "github.com/superseriousbusiness/gotosocial/testrig" ) type ManagerTestSuite struct { @@ -360,11 +360,11 @@ func (suite *ManagerTestSuite) TestSimpleJpegProcessAsync() { attachmentID := processingMedia.AttachmentID() // wait for the media to finish processing - for finished := processingMedia.Finished(); !finished; finished = processingMedia.Finished() { - time.Sleep(10 * time.Millisecond) - fmt.Printf("\n\nnot finished yet...\n\n") + if !testrig.WaitFor(func() bool { + return processingMedia.Finished() + }) { + suite.FailNow("timed out waiting for media to be processed") } - fmt.Printf("\n\nfinished!\n\n") // fetch the attachment from the database attachment, err := suite.db.GetAttachmentByID(ctx, attachmentID) diff --git a/internal/processing/account_test.go b/internal/processing/account_test.go index b34358ba1..a9b492d06 100644 --- a/internal/processing/account_test.go +++ b/internal/processing/account_test.go @@ -29,6 +29,7 @@ import ( "github.com/superseriousbusiness/activity/pub" apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/testrig" ) type AccountTestSuite struct { @@ -59,23 +60,30 @@ func (suite *AccountTestSuite) TestAccountDeleteLocal() { suite.NoError(errWithCode) // the delete should be federated outwards to the following account's inbox - var sent []byte - var ok bool - for !ok { - sent, ok = suite.httpClient.SentMessages[followingAccount.InboxURI] - } - - suite.True(ok) - delete := &struct { + var sent [][]byte + delete := new(struct { Actor string `json:"actor"` ID string `json:"id"` Object string `json:"object"` To string `json:"to"` CC string `json:"cc"` Type string `json:"type"` - }{} - err = json.Unmarshal(sent, delete) - suite.NoError(err) + }) + + if !testrig.WaitFor(func() bool { + sentI, ok := suite.httpClient.SentMessages.Load(followingAccount.InboxURI) + if ok { + sent, ok = sentI.([][]byte) + if !ok { + panic("SentMessages entry was not [][]byte") + } + err = json.Unmarshal(sent[0], delete) + return err == nil + } + return false + }) { + suite.FailNow("timed out waiting for message") + } suite.Equal(deletingAccount.URI, delete.Actor) suite.Equal(deletingAccount.URI, delete.Object) @@ -83,13 +91,12 @@ func (suite *AccountTestSuite) TestAccountDeleteLocal() { suite.Equal(pub.PublicActivityPubIRI, delete.CC) suite.Equal("Delete", delete.Type) - // wait for the delete to go through - time.Sleep(1 * time.Second) - - // the deleted account should be deleted - dbAccount, err := suite.db.GetAccountByID(ctx, deletingAccount.ID) - suite.NoError(err) - suite.WithinDuration(dbAccount.SuspendedAt, time.Now(), 30*time.Second) + if !testrig.WaitFor(func() bool { + dbAccount, _ := suite.db.GetAccountByID(ctx, deletingAccount.ID) + return suite.WithinDuration(dbAccount.SuspendedAt, time.Now(), 30*time.Second) + }) { + suite.FailNow("timed out waiting for account to be deleted") + } } func TestAccountTestSuite(t *testing.T) { diff --git a/internal/processing/followrequest_test.go b/internal/processing/followrequest_test.go index 41e4f686e..5be615ab5 100644 --- a/internal/processing/followrequest_test.go +++ b/internal/processing/followrequest_test.go @@ -28,6 +28,7 @@ import ( "github.com/stretchr/testify/suite" apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/testrig" ) type FollowRequestTestSuite struct { @@ -54,11 +55,22 @@ func (suite *FollowRequestTestSuite) TestFollowRequestAccept() { relationship, errWithCode := suite.processor.FollowRequestAccept(context.Background(), suite.testAutheds["local_account_1"], requestingAccount.ID) suite.NoError(errWithCode) suite.EqualValues(&apimodel.Relationship{ID: "01FHMQX3GAABWSM0S2VZEC2SWC", Following: false, ShowingReblogs: false, Notifying: false, FollowedBy: true, Blocking: false, BlockedBy: false, Muting: false, MutingNotifications: false, Requested: false, DomainBlocking: false, Endorsed: false, Note: ""}, relationship) - time.Sleep(1 * time.Second) // accept should be sent to some_user - sent, ok := suite.httpClient.SentMessages[requestingAccount.InboxURI] - suite.True(ok) + var sent [][]byte + if !testrig.WaitFor(func() bool { + sentI, ok := suite.httpClient.SentMessages.Load(requestingAccount.InboxURI) + if ok { + sent, ok = sentI.([][]byte) + if !ok { + panic("SentMessages entry was not []byte") + } + return true + } + return false + }) { + suite.FailNow("timed out waiting for message") + } accept := &struct { Actor string `json:"actor"` @@ -73,7 +85,7 @@ func (suite *FollowRequestTestSuite) TestFollowRequestAccept() { To string `json:"to"` Type string `json:"type"` }{} - err = json.Unmarshal(sent, accept) + err = json.Unmarshal(sent[0], accept) suite.NoError(err) suite.Equal(targetAccount.URI, accept.Actor) @@ -106,11 +118,22 @@ func (suite *FollowRequestTestSuite) TestFollowRequestReject() { relationship, errWithCode := suite.processor.FollowRequestReject(context.Background(), suite.testAutheds["local_account_1"], requestingAccount.ID) suite.NoError(errWithCode) suite.EqualValues(&apimodel.Relationship{ID: "01FHMQX3GAABWSM0S2VZEC2SWC", Following: false, ShowingReblogs: false, Notifying: false, FollowedBy: false, Blocking: false, BlockedBy: false, Muting: false, MutingNotifications: false, Requested: false, DomainBlocking: false, Endorsed: false, Note: ""}, relationship) - time.Sleep(1 * time.Second) // reject should be sent to some_user - sent, ok := suite.httpClient.SentMessages[requestingAccount.InboxURI] - suite.True(ok) + var sent [][]byte + if !testrig.WaitFor(func() bool { + sentI, ok := suite.httpClient.SentMessages.Load(requestingAccount.InboxURI) + if ok { + sent, ok = sentI.([][]byte) + if !ok { + panic("SentMessages entry was not []byte") + } + return true + } + return false + }) { + suite.FailNow("timed out waiting for message") + } reject := &struct { Actor string `json:"actor"` @@ -125,7 +148,7 @@ func (suite *FollowRequestTestSuite) TestFollowRequestReject() { To string `json:"to"` Type string `json:"type"` }{} - err = json.Unmarshal(sent, reject) + err = json.Unmarshal(sent[0], reject) suite.NoError(err) suite.Equal(targetAccount.URI, reject.Actor) diff --git a/internal/processing/fromfederator_test.go b/internal/processing/fromfederator_test.go index 86b63dade..8489303e8 100644 --- a/internal/processing/fromfederator_test.go +++ b/internal/processing/fromfederator_test.go @@ -482,26 +482,22 @@ func (suite *FromFederatorTestSuite) TestProcessFollowRequestUnlocked() { }) suite.NoError(err) - // a notification should be streamed - var msg *stream.Message - select { - case msg = <-wssStream.Messages: - // fine - case <-time.After(5 * time.Second): - suite.FailNow("no message from wssStream") + // an accept message should be sent to satan's inbox + var sent [][]byte + if !testrig.WaitFor(func() bool { + sentI, ok := suite.httpClient.SentMessages.Load(originAccount.InboxURI) + if ok { + sent, ok = sentI.([][]byte) + if !ok { + panic("SentMessages entry was not []byte") + } + return true + } + return false + }) { + suite.FailNow("timed out waiting for message") } - suite.Equal(stream.EventTypeNotification, msg.Event) - suite.NotEmpty(msg.Payload) - suite.EqualValues([]string{stream.TimelineHome}, msg.Stream) - notif := &model.Notification{} - err = json.Unmarshal([]byte(msg.Payload), notif) - suite.NoError(err) - suite.Equal("follow", notif.Type) - suite.Equal(originAccount.ID, notif.Account.ID) - // an accept message should be sent to satan's inbox - suite.Len(suite.httpClient.SentMessages, 1) - acceptBytes := suite.httpClient.SentMessages[originAccount.InboxURI] accept := &struct { Actor string `json:"actor"` ID string `json:"id"` @@ -515,7 +511,7 @@ func (suite *FromFederatorTestSuite) TestProcessFollowRequestUnlocked() { To string `json:"to"` Type string `json:"type"` }{} - err = json.Unmarshal(acceptBytes, accept) + err = json.Unmarshal(sent[0], accept) suite.NoError(err) suite.Equal(targetAccount.URI, accept.Actor) @@ -526,6 +522,23 @@ func (suite *FromFederatorTestSuite) TestProcessFollowRequestUnlocked() { suite.Equal("Follow", accept.Object.Type) suite.Equal(originAccount.URI, accept.To) suite.Equal("Accept", accept.Type) + + // a notification should be streamed + var msg *stream.Message + select { + case msg = <-wssStream.Messages: + // fine + case <-time.After(5 * time.Second): + suite.FailNow("no message from wssStream") + } + suite.Equal(stream.EventTypeNotification, msg.Event) + suite.NotEmpty(msg.Payload) + suite.EqualValues([]string{stream.TimelineHome}, msg.Stream) + notif := &model.Notification{} + err = json.Unmarshal([]byte(msg.Payload), notif) + suite.NoError(err) + suite.Equal("follow", notif.Type) + suite.Equal(originAccount.ID, notif.Account.ID) } // TestCreateStatusFromIRI checks if a forwarded status can be dereferenced by the processor. diff --git a/internal/processing/media/getfile_test.go b/internal/processing/media/getfile_test.go index 6ba06426f..a8a3568e7 100644 --- a/internal/processing/media/getfile_test.go +++ b/internal/processing/media/getfile_test.go @@ -23,10 +23,10 @@ import ( "io" "path" "testing" - "time" "github.com/stretchr/testify/suite" apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/media" "github.com/superseriousbusiness/gotosocial/testrig" ) @@ -99,10 +99,16 @@ func (suite *GetFileTestSuite) TestGetRemoteFileUncached() { suite.Equal(suite.testRemoteAttachments[testAttachment.RemoteURL].Data, b) suite.Equal(suite.testRemoteAttachments[testAttachment.RemoteURL].ContentType, content.ContentType) suite.EqualValues(len(suite.testRemoteAttachments[testAttachment.RemoteURL].Data), content.ContentLength) - time.Sleep(2 * time.Second) // wait a few seconds for the media manager to finish doing stuff // the attachment should be updated in the database - dbAttachment, err := suite.db.GetAttachmentByID(ctx, testAttachment.ID) + var dbAttachment *gtsmodel.MediaAttachment + if !testrig.WaitFor(func() bool { + dbAttachment, err = suite.db.GetAttachmentByID(ctx, testAttachment.ID) + return dbAttachment != nil + }) { + suite.FailNow("timed out waiting for updated attachment") + } + suite.NoError(err) suite.True(*dbAttachment.Cached) @@ -149,12 +155,13 @@ func (suite *GetFileTestSuite) TestGetRemoteFileUncachedInterrupted() { suite.NoError(closer.Close()) } - time.Sleep(2 * time.Second) // wait a few seconds for the media manager to finish doing stuff - // the attachment should still be updated in the database even though the caller hung up - dbAttachment, err := suite.db.GetAttachmentByID(ctx, testAttachment.ID) - suite.NoError(err) - suite.True(*dbAttachment.Cached) + if !testrig.WaitFor(func() bool { + dbAttachment, _ := suite.db.GetAttachmentByID(ctx, testAttachment.ID) + return *dbAttachment.Cached + }) { + suite.FailNow("timed out waiting for attachment to be updated") + } // the file should be back in storage at the same path as before refreshedBytes, err := suite.storage.Get(ctx, testAttachment.File.Path) diff --git a/internal/trans/trans_test.go b/internal/trans/trans_test.go index 4eedf5703..7f3954989 100644 --- a/internal/trans/trans_test.go +++ b/internal/trans/trans_test.go @@ -33,9 +33,9 @@ type TransTestSuite struct { func (suite *TransTestSuite) SetupTest() { testrig.InitTestConfig() - testrig.InitTestLog() + testrig.InitTestLog() - suite.testAccounts = testrig.NewTestAccounts() + suite.testAccounts = testrig.NewTestAccounts() suite.db = testrig.NewTestDB() testrig.StandardDBSetup(suite.db, nil) |