github.com/mattermost/mattermost-server/v5@v5.39.3/store/retrylayer/retrylayer_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package retrylayer 5 6 import ( 7 "testing" 8 9 "github.com/go-sql-driver/mysql" 10 "github.com/lib/pq" 11 "github.com/pkg/errors" 12 13 "github.com/mattermost/mattermost-server/v5/model" 14 "github.com/mattermost/mattermost-server/v5/store/storetest/mocks" 15 ) 16 17 func genStore() *mocks.Store { 18 mock := &mocks.Store{} 19 mock.On("Audit").Return(&mocks.AuditStore{}) 20 mock.On("Bot").Return(&mocks.BotStore{}) 21 mock.On("Channel").Return(&mocks.ChannelStore{}) 22 mock.On("ChannelMemberHistory").Return(&mocks.ChannelMemberHistoryStore{}) 23 mock.On("ClusterDiscovery").Return(&mocks.ClusterDiscoveryStore{}) 24 mock.On("RemoteCluster").Return(&mocks.RemoteClusterStore{}) 25 mock.On("Command").Return(&mocks.CommandStore{}) 26 mock.On("CommandWebhook").Return(&mocks.CommandWebhookStore{}) 27 mock.On("Compliance").Return(&mocks.ComplianceStore{}) 28 mock.On("Emoji").Return(&mocks.EmojiStore{}) 29 mock.On("FileInfo").Return(&mocks.FileInfoStore{}) 30 mock.On("UploadSession").Return(&mocks.UploadSessionStore{}) 31 mock.On("Group").Return(&mocks.GroupStore{}) 32 mock.On("Job").Return(&mocks.JobStore{}) 33 mock.On("License").Return(&mocks.LicenseStore{}) 34 mock.On("LinkMetadata").Return(&mocks.LinkMetadataStore{}) 35 mock.On("SharedChannel").Return(&mocks.SharedChannelStore{}) 36 mock.On("OAuth").Return(&mocks.OAuthStore{}) 37 mock.On("Plugin").Return(&mocks.PluginStore{}) 38 mock.On("Post").Return(&mocks.PostStore{}) 39 mock.On("Thread").Return(&mocks.ThreadStore{}) 40 mock.On("Preference").Return(&mocks.PreferenceStore{}) 41 mock.On("ProductNotices").Return(&mocks.ProductNoticesStore{}) 42 mock.On("Reaction").Return(&mocks.ReactionStore{}) 43 mock.On("RetentionPolicy").Return(&mocks.RetentionPolicyStore{}) 44 mock.On("Role").Return(&mocks.RoleStore{}) 45 mock.On("Scheme").Return(&mocks.SchemeStore{}) 46 mock.On("Session").Return(&mocks.SessionStore{}) 47 mock.On("Status").Return(&mocks.StatusStore{}) 48 mock.On("System").Return(&mocks.SystemStore{}) 49 mock.On("Team").Return(&mocks.TeamStore{}) 50 mock.On("TermsOfService").Return(&mocks.TermsOfServiceStore{}) 51 mock.On("Token").Return(&mocks.TokenStore{}) 52 mock.On("User").Return(&mocks.UserStore{}) 53 mock.On("UserAccessToken").Return(&mocks.UserAccessTokenStore{}) 54 mock.On("UserTermsOfService").Return(&mocks.UserTermsOfServiceStore{}) 55 mock.On("Webhook").Return(&mocks.WebhookStore{}) 56 return mock 57 } 58 59 func TestRetry(t *testing.T) { 60 t.Run("on regular error should not retry", func(t *testing.T) { 61 mock := genStore() 62 mockBotStore := mock.Bot().(*mocks.BotStore) 63 mockBotStore.On("Get", "test", false).Return(nil, errors.New("regular error")).Times(1) 64 mock.On("Bot").Return(&mockBotStore) 65 layer := New(mock) 66 layer.Bot().Get("test", false) 67 mockBotStore.AssertExpectations(t) 68 }) 69 t.Run("on success should not retry", func(t *testing.T) { 70 mock := genStore() 71 mockBotStore := mock.Bot().(*mocks.BotStore) 72 mockBotStore.On("Get", "test", false).Return(&model.Bot{}, nil).Times(1) 73 mock.On("Bot").Return(&mockBotStore) 74 layer := New(mock) 75 layer.Bot().Get("test", false) 76 mockBotStore.AssertExpectations(t) 77 }) 78 t.Run("on mysql repeatable error should retry", func(t *testing.T) { 79 mock := genStore() 80 mockBotStore := mock.Bot().(*mocks.BotStore) 81 mysqlErr := mysql.MySQLError{Number: uint16(1213), Message: "Deadlock"} 82 mockBotStore.On("Get", "test", false).Return(nil, errors.Wrap(&mysqlErr, "test-error")).Times(3) 83 mock.On("Bot").Return(&mockBotStore) 84 layer := New(mock) 85 layer.Bot().Get("test", false) 86 mockBotStore.AssertExpectations(t) 87 }) 88 t.Run("on mysql not repeatable error should not retry", func(t *testing.T) { 89 mock := genStore() 90 mockBotStore := mock.Bot().(*mocks.BotStore) 91 mysqlErr := mysql.MySQLError{Number: uint16(1000), Message: "Not repeatable error"} 92 mockBotStore.On("Get", "test", false).Return(nil, errors.Wrap(&mysqlErr, "test-error")).Times(1) 93 mock.On("Bot").Return(&mockBotStore) 94 layer := New(mock) 95 layer.Bot().Get("test", false) 96 mockBotStore.AssertExpectations(t) 97 }) 98 99 t.Run("on postgres repeatable error should retry", func(t *testing.T) { 100 for _, errCode := range []string{"40001", "40P01"} { 101 t.Run("error "+errCode, func(t *testing.T) { 102 mock := genStore() 103 mockBotStore := mock.Bot().(*mocks.BotStore) 104 pqErr := pq.Error{Code: pq.ErrorCode(errCode)} 105 mockBotStore.On("Get", "test", false).Return(nil, errors.Wrap(&pqErr, "test-error")).Times(3) 106 mock.On("Bot").Return(&mockBotStore) 107 layer := New(mock) 108 layer.Bot().Get("test", false) 109 mockBotStore.AssertExpectations(t) 110 }) 111 } 112 }) 113 114 t.Run("on postgres not repeatable error should not retry", func(t *testing.T) { 115 mock := genStore() 116 mockBotStore := mock.Bot().(*mocks.BotStore) 117 pqErr := pq.Error{Code: "20000"} 118 mockBotStore.On("Get", "test", false).Return(nil, errors.Wrap(&pqErr, "test-error")).Times(1) 119 mock.On("Bot").Return(&mockBotStore) 120 layer := New(mock) 121 layer.Bot().Get("test", false) 122 mockBotStore.AssertExpectations(t) 123 }) 124 }