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