snapshot of https://github.com/mattermost/mattermost.git (master, 2026-09-16; blobs > 131072B omitted)
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 1s
Server CI Nightly Race / Build Environment (push) Failing after 1s
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 1s
Server CI Nightly Race / Build Environment (push) Failing after 1s
This commit is contained in:
198
server/channels/store/localcachelayer/delivery_tracking_test.go
Normal file
198
server/channels/store/localcachelayer/delivery_tracking_test.go
Normal file
@@ -0,0 +1,198 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package localcachelayer
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
||||
"github.com/mattermost/mattermost/server/public/shared/request"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/store"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/store/storetest/mocks"
|
||||
)
|
||||
|
||||
func TestDeliveryTrackingStoreGetTrackedChannelIDsCache(t *testing.T) {
|
||||
logger := mlog.CreateConsoleTestLogger(t)
|
||||
rctx := request.TestContext(t)
|
||||
|
||||
setBasicMock := func(mockStore *mocks.Store, cachedStore LocalCacheStore) *mocks.DeliveryTrackingStore {
|
||||
mockDeliveryTrackingStore := mockStore.DeliveryTracking().(*mocks.DeliveryTrackingStore)
|
||||
mockDeliveryTrackingStore.On("GetTrackedChannelIDs", mock.Anything).Return([]string{"channel1", "channel2"}, nil)
|
||||
mockDeliveryTrackingStore.On("SaveTrackedChannelIDs", mock.Anything, mock.Anything).Return(nil)
|
||||
cachedStore.deliveryTracking.DeliveryTrackingStore = mockDeliveryTrackingStore
|
||||
return mockDeliveryTrackingStore
|
||||
}
|
||||
|
||||
t.Run("first call not cached, second cached and returning same data", func(t *testing.T) {
|
||||
mockStore := getMockStore(t)
|
||||
mockCacheProvider := getMockCacheProvider()
|
||||
cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider, logger)
|
||||
require.NoError(t, err)
|
||||
mockDeliveryTrackingStore := setBasicMock(mockStore, cachedStore)
|
||||
|
||||
channelIDs, err := cachedStore.DeliveryTracking().GetTrackedChannelIDs(rctx)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, []string{"channel1", "channel2"}, channelIDs)
|
||||
mockDeliveryTrackingStore.AssertNumberOfCalls(t, "GetTrackedChannelIDs", 1)
|
||||
|
||||
channelIDs, err = cachedStore.DeliveryTracking().GetTrackedChannelIDs(rctx)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, []string{"channel1", "channel2"}, channelIDs)
|
||||
mockDeliveryTrackingStore.AssertNumberOfCalls(t, "GetTrackedChannelIDs", 1)
|
||||
})
|
||||
|
||||
t.Run("first call not cached, clear cache, second call not cached", func(t *testing.T) {
|
||||
mockStore := getMockStore(t)
|
||||
mockCacheProvider := getMockCacheProvider()
|
||||
cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider, logger)
|
||||
require.NoError(t, err)
|
||||
mockDeliveryTrackingStore := setBasicMock(mockStore, cachedStore)
|
||||
|
||||
_, err = cachedStore.DeliveryTracking().GetTrackedChannelIDs(rctx)
|
||||
require.NoError(t, err)
|
||||
mockDeliveryTrackingStore.AssertNumberOfCalls(t, "GetTrackedChannelIDs", 1)
|
||||
|
||||
cachedStore.DeliveryTracking().ClearCaches()
|
||||
|
||||
_, err = cachedStore.DeliveryTracking().GetTrackedChannelIDs(rctx)
|
||||
require.NoError(t, err)
|
||||
mockDeliveryTrackingStore.AssertNumberOfCalls(t, "GetTrackedChannelIDs", 2)
|
||||
})
|
||||
|
||||
t.Run("saving the list invalidates the cache", func(t *testing.T) {
|
||||
mockStore := getMockStore(t)
|
||||
mockCacheProvider := getMockCacheProvider()
|
||||
cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider, logger)
|
||||
require.NoError(t, err)
|
||||
mockDeliveryTrackingStore := setBasicMock(mockStore, cachedStore)
|
||||
|
||||
_, err = cachedStore.DeliveryTracking().GetTrackedChannelIDs(rctx)
|
||||
require.NoError(t, err)
|
||||
mockDeliveryTrackingStore.AssertNumberOfCalls(t, "GetTrackedChannelIDs", 1)
|
||||
|
||||
require.NoError(t, cachedStore.DeliveryTracking().SaveTrackedChannelIDs(rctx, []string{"channel3"}))
|
||||
|
||||
_, err = cachedStore.DeliveryTracking().GetTrackedChannelIDs(rctx)
|
||||
require.NoError(t, err)
|
||||
mockDeliveryTrackingStore.AssertNumberOfCalls(t, "GetTrackedChannelIDs", 2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDeliveryTrackingStoreChannelEligibilityCache(t *testing.T) {
|
||||
logger := mlog.CreateConsoleTestLogger(t)
|
||||
rctx := request.TestContext(t)
|
||||
|
||||
openChannel := model.Channel{Id: "channel1", Type: model.ChannelTypeOpen}
|
||||
dmChannel := model.Channel{Id: "channel2", Type: model.ChannelTypeDirect}
|
||||
|
||||
newCachedStore := func(t *testing.T) (LocalCacheStore, *mocks.DeliveryTrackingStore, *mocks.ChannelStore) {
|
||||
mockStore := getMockStore(t)
|
||||
cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, getMockCacheProvider(), logger)
|
||||
require.NoError(t, err)
|
||||
|
||||
mockDeliveryTrackingStore := mockStore.DeliveryTracking().(*mocks.DeliveryTrackingStore)
|
||||
mockDeliveryTrackingStore.On("IsChannelTracked", mock.Anything, mock.Anything).Return(true, nil)
|
||||
mockDeliveryTrackingStore.On("SaveTrackedChannelIDs", mock.Anything, mock.Anything).Return(nil)
|
||||
cachedStore.deliveryTracking.DeliveryTrackingStore = mockDeliveryTrackingStore
|
||||
|
||||
mockChannelStore := mockStore.Channel().(*mocks.ChannelStore)
|
||||
mockChannelStore.On("Get", openChannel.Id, true).Return(&openChannel, nil)
|
||||
mockChannelStore.On("Get", dmChannel.Id, true).Return(&dmChannel, nil)
|
||||
mockChannelStore.On("Get", "missing", true).Return(nil, store.NewErrNotFound("Channel", "missing"))
|
||||
|
||||
return cachedStore, mockDeliveryTrackingStore, mockChannelStore
|
||||
}
|
||||
|
||||
t.Run("IsChannelTracked is resolved once per channel", func(t *testing.T) {
|
||||
cachedStore, mockDeliveryTrackingStore, _ := newCachedStore(t)
|
||||
|
||||
for range 3 {
|
||||
tracked, err := cachedStore.DeliveryTracking().IsChannelTracked(rctx, "channel1")
|
||||
require.NoError(t, err)
|
||||
require.True(t, tracked)
|
||||
}
|
||||
mockDeliveryTrackingStore.AssertNumberOfCalls(t, "IsChannelTracked", 1)
|
||||
|
||||
_, err := cachedStore.DeliveryTracking().IsChannelTracked(rctx, "channel2")
|
||||
require.NoError(t, err)
|
||||
mockDeliveryTrackingStore.AssertNumberOfCalls(t, "IsChannelTracked", 2)
|
||||
})
|
||||
|
||||
t.Run("IsChannelTrackable resolves through the channel store once per channel", func(t *testing.T) {
|
||||
cachedStore, mockDeliveryTrackingStore, mockChannelStore := newCachedStore(t)
|
||||
|
||||
for range 3 {
|
||||
trackable, err := cachedStore.DeliveryTracking().IsChannelTrackable(rctx, openChannel.Id)
|
||||
require.NoError(t, err)
|
||||
require.True(t, trackable)
|
||||
}
|
||||
mockChannelStore.AssertNumberOfCalls(t, "Get", 1)
|
||||
|
||||
// The dedicated store method is never consulted; the channel store is the source.
|
||||
mockDeliveryTrackingStore.AssertNumberOfCalls(t, "IsChannelTrackable", 0)
|
||||
})
|
||||
|
||||
t.Run("DMs are not trackable", func(t *testing.T) {
|
||||
cachedStore, _, _ := newCachedStore(t)
|
||||
|
||||
trackable, err := cachedStore.DeliveryTracking().IsChannelTrackable(rctx, dmChannel.Id)
|
||||
require.NoError(t, err)
|
||||
require.False(t, trackable)
|
||||
})
|
||||
|
||||
t.Run("unknown channels are not trackable and are re-resolved", func(t *testing.T) {
|
||||
cachedStore, _, mockChannelStore := newCachedStore(t)
|
||||
|
||||
for range 2 {
|
||||
trackable, err := cachedStore.DeliveryTracking().IsChannelTrackable(rctx, "missing")
|
||||
require.NoError(t, err)
|
||||
require.False(t, trackable)
|
||||
}
|
||||
// Not-found is never memoized: the channel may exist on a later read, and a transient
|
||||
// miss must not exclude it from auditing for the life of the process.
|
||||
mockChannelStore.AssertNumberOfCalls(t, "Get", 2)
|
||||
})
|
||||
|
||||
t.Run("saving the list re-resolves tracked but not trackable", func(t *testing.T) {
|
||||
cachedStore, mockDeliveryTrackingStore, mockChannelStore := newCachedStore(t)
|
||||
|
||||
_, err := cachedStore.DeliveryTracking().IsChannelTracked(rctx, openChannel.Id)
|
||||
require.NoError(t, err)
|
||||
_, err = cachedStore.DeliveryTracking().IsChannelTrackable(rctx, openChannel.Id)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NoError(t, cachedStore.DeliveryTracking().SaveTrackedChannelIDs(rctx, []string{"channel3"}))
|
||||
|
||||
_, err = cachedStore.DeliveryTracking().IsChannelTracked(rctx, openChannel.Id)
|
||||
require.NoError(t, err)
|
||||
mockDeliveryTrackingStore.AssertNumberOfCalls(t, "IsChannelTracked", 2)
|
||||
|
||||
// Saving the allow-list purges trackableChannels too, but channelByIdCache still holds
|
||||
// the channel, so no additional store read is needed to re-resolve it.
|
||||
_, err = cachedStore.DeliveryTracking().IsChannelTrackable(rctx, openChannel.Id)
|
||||
require.NoError(t, err)
|
||||
mockChannelStore.AssertNumberOfCalls(t, "Get", 1)
|
||||
})
|
||||
|
||||
t.Run("errors are not cached", func(t *testing.T) {
|
||||
mockStore := getMockStore(t)
|
||||
cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, getMockCacheProvider(), logger)
|
||||
require.NoError(t, err)
|
||||
|
||||
mockDeliveryTrackingStore := mockStore.DeliveryTracking().(*mocks.DeliveryTrackingStore)
|
||||
mockDeliveryTrackingStore.On("IsChannelTracked", mock.Anything, mock.Anything).Return(false, errors.New("boom"))
|
||||
cachedStore.deliveryTracking.DeliveryTrackingStore = mockDeliveryTrackingStore
|
||||
|
||||
for range 2 {
|
||||
_, err = cachedStore.DeliveryTracking().IsChannelTracked(rctx, "channel1")
|
||||
require.Error(t, err)
|
||||
}
|
||||
mockDeliveryTrackingStore.AssertNumberOfCalls(t, "IsChannelTracked", 2)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user