* Refactor to use structured logging * Properly formatted with gofmt * created interface Cache, but construction of cache is still coupled. * Implementing cache factories to build caches * Simple redis implementation without error handling. Keys and values by default are string * refactor NewLocalCacheLayer to inject cache factory * Removed redis impl to focus on cache abstraction * CacheFactory injected on sqlsupplier and saved in Store struct * remove useless private method * replace concrete declaration of lru cache to cache abstraction * discard spaces * Renamed factory to provider because concrete implementations of factories may hold an state (such as a redis client for example) * refactor to include all caches in the same package cache and subpackages * method close cache. This method will be used for concrete implementations that need to release resources (close a connection, etc) * closing cacheprovider and releasing resources while closing sql store * fixed merge conflict fail * gofmt files * remove unused property from post_store * naming refactor to avoid stutter. Added godocs on interface * Store doesnt know anything about the cache and provider. Cache provider will be built after loading config and injected in localCacheLayer * fixed broken test * cache provider initialized before RunOldAppInitialization which initializes the localcachelayer * move statusCache to server to initialize it with the new cache provider * update terms_service and channel_layer to have new cacheProvider * gofmt * Add Connect method to the cache provider * mock cacheprovider in user_layer_test Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com> Co-authored-by: mattermod <mattermod@users.noreply.github.com>
184 строки
7.2 KiB
Go
184 строки
7.2 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package localcachelayer
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/mattermost/mattermost-server/v5/model"
|
|
"github.com/mattermost/mattermost-server/v5/store/storetest"
|
|
"github.com/mattermost/mattermost-server/v5/store/storetest/mocks"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestPostStore(t *testing.T) {
|
|
StoreTestWithSqlSupplier(t, storetest.TestPostStore)
|
|
}
|
|
|
|
func TestPostStoreLastPostTimeCache(t *testing.T) {
|
|
var fakeLastTime int64 = 1
|
|
channelId := "channelId"
|
|
fakeOptions := model.GetPostsSinceOptions{
|
|
ChannelId: channelId,
|
|
Time: fakeLastTime,
|
|
SkipFetchThreads: false,
|
|
}
|
|
|
|
t.Run("GetEtag: first call not cached, second cached and returning same data", func(t *testing.T) {
|
|
mockStore := getMockStore()
|
|
mockCacheProvider := getMockCacheProvider()
|
|
cachedStore := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider)
|
|
|
|
expectedResult := fmt.Sprintf("%v.%v", model.CurrentVersion, fakeLastTime)
|
|
|
|
etag := cachedStore.Post().GetEtag(channelId, true)
|
|
assert.Equal(t, etag, expectedResult)
|
|
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetEtag", 1)
|
|
|
|
etag = cachedStore.Post().GetEtag(channelId, true)
|
|
assert.Equal(t, etag, expectedResult)
|
|
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetEtag", 1)
|
|
})
|
|
|
|
t.Run("GetEtag: first call not cached, second force no cached", func(t *testing.T) {
|
|
mockStore := getMockStore()
|
|
mockCacheProvider := getMockCacheProvider()
|
|
cachedStore := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider)
|
|
|
|
cachedStore.Post().GetEtag(channelId, true)
|
|
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetEtag", 1)
|
|
cachedStore.Post().GetEtag(channelId, false)
|
|
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetEtag", 2)
|
|
})
|
|
|
|
t.Run("GetEtag: first call not cached, invalidate, and then not cached again", func(t *testing.T) {
|
|
mockStore := getMockStore()
|
|
mockCacheProvider := getMockCacheProvider()
|
|
cachedStore := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider)
|
|
|
|
cachedStore.Post().GetEtag(channelId, true)
|
|
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetEtag", 1)
|
|
cachedStore.Post().InvalidateLastPostTimeCache(channelId)
|
|
cachedStore.Post().GetEtag(channelId, true)
|
|
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetEtag", 2)
|
|
})
|
|
|
|
t.Run("GetEtag: first call not cached, clear caches, and then not cached again", func(t *testing.T) {
|
|
mockStore := getMockStore()
|
|
mockCacheProvider := getMockCacheProvider()
|
|
cachedStore := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider)
|
|
|
|
cachedStore.Post().GetEtag(channelId, true)
|
|
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetEtag", 1)
|
|
cachedStore.Post().ClearCaches()
|
|
cachedStore.Post().GetEtag(channelId, true)
|
|
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetEtag", 2)
|
|
})
|
|
|
|
t.Run("GetPostsSince: first call not cached, second cached and returning same data", func(t *testing.T) {
|
|
mockStore := getMockStore()
|
|
mockCacheProvider := getMockCacheProvider()
|
|
cachedStore := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider)
|
|
|
|
expectedResult := model.NewPostList()
|
|
|
|
list, err := cachedStore.Post().GetPostsSince(fakeOptions, true)
|
|
require.Nil(t, err)
|
|
assert.Equal(t, list, expectedResult)
|
|
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPostsSince", 1)
|
|
|
|
list, err = cachedStore.Post().GetPostsSince(fakeOptions, true)
|
|
require.Nil(t, err)
|
|
assert.Equal(t, list, expectedResult)
|
|
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPostsSince", 1)
|
|
})
|
|
|
|
t.Run("GetPostsSince: first call not cached, second force no cached", func(t *testing.T) {
|
|
mockStore := getMockStore()
|
|
mockCacheProvider := getMockCacheProvider()
|
|
cachedStore := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider)
|
|
|
|
cachedStore.Post().GetPostsSince(fakeOptions, true)
|
|
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPostsSince", 1)
|
|
cachedStore.Post().GetPostsSince(fakeOptions, false)
|
|
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPostsSince", 2)
|
|
})
|
|
|
|
t.Run("GetPostsSince: first call not cached, invalidate, and then not cached again", func(t *testing.T) {
|
|
mockStore := getMockStore()
|
|
mockCacheProvider := getMockCacheProvider()
|
|
cachedStore := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider)
|
|
|
|
cachedStore.Post().GetPostsSince(fakeOptions, true)
|
|
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPostsSince", 1)
|
|
cachedStore.Post().InvalidateLastPostTimeCache(channelId)
|
|
cachedStore.Post().GetPostsSince(fakeOptions, true)
|
|
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPostsSince", 2)
|
|
})
|
|
|
|
t.Run("GetPostsSince: first call not cached, clear caches, and then not cached again", func(t *testing.T) {
|
|
mockStore := getMockStore()
|
|
mockCacheProvider := getMockCacheProvider()
|
|
cachedStore := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider)
|
|
|
|
cachedStore.Post().GetPostsSince(fakeOptions, true)
|
|
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPostsSince", 1)
|
|
cachedStore.Post().ClearCaches()
|
|
cachedStore.Post().GetPostsSince(fakeOptions, true)
|
|
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPostsSince", 2)
|
|
})
|
|
}
|
|
|
|
func TestPostStoreCache(t *testing.T) {
|
|
fakePosts := &model.PostList{}
|
|
fakeOptions := model.GetPostsOptions{ChannelId: "123", PerPage: 30}
|
|
|
|
t.Run("first call not cached, second cached and returning same data", func(t *testing.T) {
|
|
mockStore := getMockStore()
|
|
mockCacheProvider := getMockCacheProvider()
|
|
cachedStore := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider)
|
|
|
|
gotPosts, err := cachedStore.Post().GetPosts(fakeOptions, true)
|
|
require.Nil(t, err)
|
|
assert.Equal(t, fakePosts, gotPosts)
|
|
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPosts", 1)
|
|
|
|
_, _ = cachedStore.Post().GetPosts(fakeOptions, true)
|
|
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPosts", 1)
|
|
})
|
|
|
|
t.Run("first call not cached, second force not cached", func(t *testing.T) {
|
|
mockStore := getMockStore()
|
|
mockCacheProvider := getMockCacheProvider()
|
|
cachedStore := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider)
|
|
|
|
gotPosts, err := cachedStore.Post().GetPosts(fakeOptions, true)
|
|
require.Nil(t, err)
|
|
assert.Equal(t, fakePosts, gotPosts)
|
|
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPosts", 1)
|
|
|
|
_, _ = cachedStore.Post().GetPosts(fakeOptions, false)
|
|
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPosts", 2)
|
|
})
|
|
|
|
t.Run("first call not cached, invalidate, and then not cached again", func(t *testing.T) {
|
|
mockStore := getMockStore()
|
|
mockCacheProvider := getMockCacheProvider()
|
|
cachedStore := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider)
|
|
|
|
gotPosts, err := cachedStore.Post().GetPosts(fakeOptions, true)
|
|
require.Nil(t, err)
|
|
assert.Equal(t, fakePosts, gotPosts)
|
|
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPosts", 1)
|
|
|
|
cachedStore.Post().InvalidateLastPostTimeCache("12360")
|
|
|
|
_, _ = cachedStore.Post().GetPosts(fakeOptions, true)
|
|
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPosts", 1)
|
|
|
|
})
|
|
}
|