Files
mostlymatter/server/channels/store/retrylayer/retrylayer_test.go
Miguel de la Cruz ecdce71fc4 Adds the main Property System Architecture components (#29644)
* Adds the main Property System Architecture components

This change adds the necessary migrations for the Property Groups,
Fields and Values tables to be created, the store layer and a Property
Service that can be used from the app layer.

* Update property field type to use user instead of person

* Update PropertyFields to allow for unique nondeleted fields and remove redundant indexes

* Update PropertyValues to allow for unique nondeleted fields and remove redundant indexes

* Use StringMap instead of the map[string]any on property fields

* Add i18n strings

* Revert "Use StringMap instead of the map[string]any on property fields"

This reverts commit e2735ab0f8589d2524d636419ca0cb144575c4d6.

* Cast JSON binary data to string and add todo note for StringMap use

* Add mocks to the retrylayer tests

* Cast JSON binary data to string in property value store

* Check for binary parameter instead of casting to string for JSON data

* Check property field type is one of the allowed ones

* Avoid reusing err variable to be explicit about the returned value

* Merge Property System Migrations into one file

* Adds NOT NULL to timestamps at the DB level

* Update stores to use tableSelectQuery instead of a slice var

* Update PropertyField model translations to be more explicit and avoid repetition

* Update PropertyValue model translations to be more explicit and avoid repetition

* Use ExecBuilder instead of ToSql&Exec

* Update property field errors to add context

* Ensure PerPage is greater than zero

* Update store errors to give more context

* Use ExecBuilder in the property stores where possible

* Add an on conflict suffix to the group register to avoid race conditions

* Remove badly used translation string

* Remove unused get in register group method

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
2025-01-13 11:41:44 +00:00

138 строки
5.7 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package retrylayer
import (
"testing"
"github.com/go-sql-driver/mysql"
"github.com/lib/pq"
"github.com/pkg/errors"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/v8/channels/store/storetest/mocks"
)
func genStore() *mocks.Store {
mock := &mocks.Store{}
mock.On("Audit").Return(&mocks.AuditStore{})
mock.On("Bot").Return(&mocks.BotStore{})
mock.On("Channel").Return(&mocks.ChannelStore{})
mock.On("ChannelMemberHistory").Return(&mocks.ChannelMemberHistoryStore{})
mock.On("ChannelBookmark").Return(&mocks.ChannelBookmarkStore{})
mock.On("ClusterDiscovery").Return(&mocks.ClusterDiscoveryStore{})
mock.On("RemoteCluster").Return(&mocks.RemoteClusterStore{})
mock.On("Command").Return(&mocks.CommandStore{})
mock.On("CommandWebhook").Return(&mocks.CommandWebhookStore{})
mock.On("Compliance").Return(&mocks.ComplianceStore{})
mock.On("Emoji").Return(&mocks.EmojiStore{})
mock.On("FileInfo").Return(&mocks.FileInfoStore{})
mock.On("UploadSession").Return(&mocks.UploadSessionStore{})
mock.On("Group").Return(&mocks.GroupStore{})
mock.On("Job").Return(&mocks.JobStore{})
mock.On("License").Return(&mocks.LicenseStore{})
mock.On("LinkMetadata").Return(&mocks.LinkMetadataStore{})
mock.On("SharedChannel").Return(&mocks.SharedChannelStore{})
mock.On("OAuth").Return(&mocks.OAuthStore{})
mock.On("OutgoingOAuthConnection").Return(&mocks.OutgoingOAuthConnectionStore{})
mock.On("Plugin").Return(&mocks.PluginStore{})
mock.On("Post").Return(&mocks.PostStore{})
mock.On("Thread").Return(&mocks.ThreadStore{})
mock.On("Preference").Return(&mocks.PreferenceStore{})
mock.On("ProductNotices").Return(&mocks.ProductNoticesStore{})
mock.On("Reaction").Return(&mocks.ReactionStore{})
mock.On("RetentionPolicy").Return(&mocks.RetentionPolicyStore{})
mock.On("Role").Return(&mocks.RoleStore{})
mock.On("Scheme").Return(&mocks.SchemeStore{})
mock.On("Session").Return(&mocks.SessionStore{})
mock.On("Status").Return(&mocks.StatusStore{})
mock.On("System").Return(&mocks.SystemStore{})
mock.On("Team").Return(&mocks.TeamStore{})
mock.On("TermsOfService").Return(&mocks.TermsOfServiceStore{})
mock.On("Token").Return(&mocks.TokenStore{})
mock.On("User").Return(&mocks.UserStore{})
mock.On("UserAccessToken").Return(&mocks.UserAccessTokenStore{})
mock.On("UserTermsOfService").Return(&mocks.UserTermsOfServiceStore{})
mock.On("Webhook").Return(&mocks.WebhookStore{})
mock.On("NotifyAdmin").Return(&mocks.NotifyAdminStore{})
mock.On("Draft").Return(&mocks.DraftStore{})
mock.On("PostPriority").Return(&mocks.PostPriorityStore{})
mock.On("PostAcknowledgement").Return(&mocks.PostAcknowledgementStore{})
mock.On("PostPersistentNotification").Return(&mocks.PostPersistentNotificationStore{})
mock.On("DesktopTokens").Return(&mocks.DesktopTokensStore{})
mock.On("ChannelBookmark").Return(&mocks.ChannelBookmarkStore{})
mock.On("ScheduledPost").Return(&mocks.ScheduledPostStore{})
mock.On("PropertyField").Return(&mocks.PropertyFieldStore{})
mock.On("PropertyGroup").Return(&mocks.PropertyGroupStore{})
mock.On("PropertyValue").Return(&mocks.PropertyValueStore{})
return mock
}
func TestRetry(t *testing.T) {
t.Run("on regular error should not retry", func(t *testing.T) {
mock := genStore()
mockBotStore := mock.Bot().(*mocks.BotStore)
mockBotStore.On("Get", "test", false).Return(nil, errors.New("regular error")).Times(1)
mock.On("Bot").Return(&mockBotStore)
layer := New(mock)
layer.Bot().Get("test", false)
mockBotStore.AssertExpectations(t)
})
t.Run("on success should not retry", func(t *testing.T) {
mock := genStore()
mockBotStore := mock.Bot().(*mocks.BotStore)
mockBotStore.On("Get", "test", false).Return(&model.Bot{}, nil).Times(1)
mock.On("Bot").Return(&mockBotStore)
layer := New(mock)
layer.Bot().Get("test", false)
mockBotStore.AssertExpectations(t)
})
t.Run("on mysql repeatable error should retry", func(t *testing.T) {
mock := genStore()
mockBotStore := mock.Bot().(*mocks.BotStore)
mysqlErr := mysql.MySQLError{Number: uint16(1213), Message: "Deadlock"}
mockBotStore.On("Get", "test", false).Return(nil, errors.Wrap(&mysqlErr, "test-error")).Times(3)
mock.On("Bot").Return(&mockBotStore)
layer := New(mock)
layer.Bot().Get("test", false)
mockBotStore.AssertExpectations(t)
})
t.Run("on mysql not repeatable error should not retry", func(t *testing.T) {
mock := genStore()
mockBotStore := mock.Bot().(*mocks.BotStore)
mysqlErr := mysql.MySQLError{Number: uint16(1000), Message: "Not repeatable error"}
mockBotStore.On("Get", "test", false).Return(nil, errors.Wrap(&mysqlErr, "test-error")).Times(1)
mock.On("Bot").Return(&mockBotStore)
layer := New(mock)
layer.Bot().Get("test", false)
mockBotStore.AssertExpectations(t)
})
t.Run("on postgres repeatable error should retry", func(t *testing.T) {
for _, errCode := range []string{"40001", "40P01"} {
t.Run("error "+errCode, func(t *testing.T) {
mock := genStore()
mockBotStore := mock.Bot().(*mocks.BotStore)
pqErr := pq.Error{Code: pq.ErrorCode(errCode)}
mockBotStore.On("Get", "test", false).Return(nil, errors.Wrap(&pqErr, "test-error")).Times(3)
mock.On("Bot").Return(&mockBotStore)
layer := New(mock)
layer.Bot().Get("test", false)
mockBotStore.AssertExpectations(t)
})
}
})
t.Run("on postgres not repeatable error should not retry", func(t *testing.T) {
mock := genStore()
mockBotStore := mock.Bot().(*mocks.BotStore)
pqErr := pq.Error{Code: "20000"}
mockBotStore.On("Get", "test", false).Return(nil, errors.Wrap(&pqErr, "test-error")).Times(1)
mock.On("Bot").Return(&mockBotStore)
layer := New(mock)
layer.Bot().Get("test", false)
mockBotStore.AssertExpectations(t)
})
}