github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/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("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("OAuth").Return(&mocks.OAuthStore{})
    35  	mock.On("Plugin").Return(&mocks.PluginStore{})
    36  	mock.On("Post").Return(&mocks.PostStore{})
    37  	mock.On("Thread").Return(&mocks.ThreadStore{})
    38  	mock.On("Preference").Return(&mocks.PreferenceStore{})
    39  	mock.On("ProductNotices").Return(&mocks.ProductNoticesStore{})
    40  	mock.On("Reaction").Return(&mocks.ReactionStore{})
    41  	mock.On("Role").Return(&mocks.RoleStore{})
    42  	mock.On("Scheme").Return(&mocks.SchemeStore{})
    43  	mock.On("Session").Return(&mocks.SessionStore{})
    44  	mock.On("Status").Return(&mocks.StatusStore{})
    45  	mock.On("System").Return(&mocks.SystemStore{})
    46  	mock.On("Team").Return(&mocks.TeamStore{})
    47  	mock.On("TermsOfService").Return(&mocks.TermsOfServiceStore{})
    48  	mock.On("Token").Return(&mocks.TokenStore{})
    49  	mock.On("User").Return(&mocks.UserStore{})
    50  	mock.On("UserAccessToken").Return(&mocks.UserAccessTokenStore{})
    51  	mock.On("UserTermsOfService").Return(&mocks.UserTermsOfServiceStore{})
    52  	mock.On("Webhook").Return(&mocks.WebhookStore{})
    53  	return mock
    54  }
    55  
    56  func TestRetry(t *testing.T) {
    57  	t.Run("on regular error should not retry", func(t *testing.T) {
    58  		mock := genStore()
    59  		mockBotStore := mock.Bot().(*mocks.BotStore)
    60  		mockBotStore.On("Get", "test", false).Return(nil, errors.New("regular error")).Times(1)
    61  		mock.On("Bot").Return(&mockBotStore)
    62  		layer := New(mock)
    63  		layer.Bot().Get("test", false)
    64  		mockBotStore.AssertExpectations(t)
    65  	})
    66  	t.Run("on success should not retry", func(t *testing.T) {
    67  		mock := genStore()
    68  		mockBotStore := mock.Bot().(*mocks.BotStore)
    69  		mockBotStore.On("Get", "test", false).Return(&model.Bot{}, nil).Times(1)
    70  		mock.On("Bot").Return(&mockBotStore)
    71  		layer := New(mock)
    72  		layer.Bot().Get("test", false)
    73  		mockBotStore.AssertExpectations(t)
    74  	})
    75  	t.Run("on mysql repeatable error should retry", func(t *testing.T) {
    76  		mock := genStore()
    77  		mockBotStore := mock.Bot().(*mocks.BotStore)
    78  		mysqlErr := mysql.MySQLError{Number: uint16(1213), Message: "Deadlock"}
    79  		mockBotStore.On("Get", "test", false).Return(nil, errors.Wrap(&mysqlErr, "test-error")).Times(3)
    80  		mock.On("Bot").Return(&mockBotStore)
    81  		layer := New(mock)
    82  		layer.Bot().Get("test", false)
    83  		mockBotStore.AssertExpectations(t)
    84  	})
    85  	t.Run("on mysql not repeatable error should not retry", func(t *testing.T) {
    86  		mock := genStore()
    87  		mockBotStore := mock.Bot().(*mocks.BotStore)
    88  		mysqlErr := mysql.MySQLError{Number: uint16(1000), Message: "Not repeatable error"}
    89  		mockBotStore.On("Get", "test", false).Return(nil, errors.Wrap(&mysqlErr, "test-error")).Times(1)
    90  		mock.On("Bot").Return(&mockBotStore)
    91  		layer := New(mock)
    92  		layer.Bot().Get("test", false)
    93  		mockBotStore.AssertExpectations(t)
    94  	})
    95  
    96  	t.Run("on postgres repeatable error should retry", func(t *testing.T) {
    97  		for _, errCode := range []string{"40001", "40P01"} {
    98  			t.Run("error "+errCode, func(t *testing.T) {
    99  				mock := genStore()
   100  				mockBotStore := mock.Bot().(*mocks.BotStore)
   101  				pqErr := pq.Error{Code: pq.ErrorCode(errCode)}
   102  				mockBotStore.On("Get", "test", false).Return(nil, errors.Wrap(&pqErr, "test-error")).Times(3)
   103  				mock.On("Bot").Return(&mockBotStore)
   104  				layer := New(mock)
   105  				layer.Bot().Get("test", false)
   106  				mockBotStore.AssertExpectations(t)
   107  			})
   108  		}
   109  	})
   110  
   111  	t.Run("on postgres not repeatable error should not retry", func(t *testing.T) {
   112  		mock := genStore()
   113  		mockBotStore := mock.Bot().(*mocks.BotStore)
   114  		pqErr := pq.Error{Code: "20000"}
   115  		mockBotStore.On("Get", "test", false).Return(nil, errors.Wrap(&pqErr, "test-error")).Times(1)
   116  		mock.On("Bot").Return(&mockBotStore)
   117  		layer := New(mock)
   118  		layer.Bot().Get("test", false)
   119  		mockBotStore.AssertExpectations(t)
   120  	})
   121  }