github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/store/localcachelayer/post_layer_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package localcachelayer 5 6 import ( 7 "fmt" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 13 "github.com/mattermost/mattermost-server/v5/model" 14 "github.com/mattermost/mattermost-server/v5/store/storetest" 15 "github.com/mattermost/mattermost-server/v5/store/storetest/mocks" 16 ) 17 18 func TestPostStore(t *testing.T) { 19 StoreTestWithSqlStore(t, storetest.TestPostStore) 20 } 21 22 func TestPostStoreLastPostTimeCache(t *testing.T) { 23 var fakeLastTime int64 = 1 24 channelId := "channelId" 25 fakeOptions := model.GetPostsSinceOptions{ 26 ChannelId: channelId, 27 Time: fakeLastTime, 28 SkipFetchThreads: false, 29 } 30 31 t.Run("GetEtag: first call not cached, second cached and returning same data", func(t *testing.T) { 32 mockStore := getMockStore() 33 mockCacheProvider := getMockCacheProvider() 34 cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider) 35 require.NoError(t, err) 36 37 expectedResult := fmt.Sprintf("%v.%v", model.CurrentVersion, fakeLastTime) 38 39 etag := cachedStore.Post().GetEtag(channelId, true, false) 40 assert.Equal(t, etag, expectedResult) 41 mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetEtag", 1) 42 43 etag = cachedStore.Post().GetEtag(channelId, true, false) 44 assert.Equal(t, etag, expectedResult) 45 mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetEtag", 1) 46 }) 47 48 t.Run("GetEtag: first call not cached, second force no cached", func(t *testing.T) { 49 mockStore := getMockStore() 50 mockCacheProvider := getMockCacheProvider() 51 cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider) 52 require.NoError(t, err) 53 54 cachedStore.Post().GetEtag(channelId, true, false) 55 mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetEtag", 1) 56 cachedStore.Post().GetEtag(channelId, false, false) 57 mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetEtag", 2) 58 }) 59 60 t.Run("GetEtag: first call not cached, invalidate, and then not cached again", func(t *testing.T) { 61 mockStore := getMockStore() 62 mockCacheProvider := getMockCacheProvider() 63 cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider) 64 require.NoError(t, err) 65 66 cachedStore.Post().GetEtag(channelId, true, false) 67 mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetEtag", 1) 68 cachedStore.Post().InvalidateLastPostTimeCache(channelId) 69 cachedStore.Post().GetEtag(channelId, true, false) 70 mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetEtag", 2) 71 }) 72 73 t.Run("GetEtag: first call not cached, clear caches, and then not cached again", func(t *testing.T) { 74 mockStore := getMockStore() 75 mockCacheProvider := getMockCacheProvider() 76 cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider) 77 require.NoError(t, err) 78 79 cachedStore.Post().GetEtag(channelId, true, false) 80 mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetEtag", 1) 81 cachedStore.Post().ClearCaches() 82 cachedStore.Post().GetEtag(channelId, true, false) 83 mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetEtag", 2) 84 }) 85 86 t.Run("GetPostsSince: first call not cached, second cached and returning same data", func(t *testing.T) { 87 mockStore := getMockStore() 88 mockCacheProvider := getMockCacheProvider() 89 cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider) 90 require.NoError(t, err) 91 92 expectedResult := model.NewPostList() 93 94 list, err := cachedStore.Post().GetPostsSince(fakeOptions, true) 95 require.NoError(t, err) 96 assert.Equal(t, list, expectedResult) 97 mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPostsSince", 1) 98 99 list, err = cachedStore.Post().GetPostsSince(fakeOptions, true) 100 require.NoError(t, err) 101 assert.Equal(t, list, expectedResult) 102 mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPostsSince", 1) 103 }) 104 105 t.Run("GetPostsSince: first call not cached, second force no cached", func(t *testing.T) { 106 mockStore := getMockStore() 107 mockCacheProvider := getMockCacheProvider() 108 cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider) 109 require.NoError(t, err) 110 111 cachedStore.Post().GetPostsSince(fakeOptions, true) 112 mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPostsSince", 1) 113 cachedStore.Post().GetPostsSince(fakeOptions, false) 114 mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPostsSince", 2) 115 }) 116 117 t.Run("GetPostsSince: first call not cached, invalidate, and then not cached again", func(t *testing.T) { 118 mockStore := getMockStore() 119 mockCacheProvider := getMockCacheProvider() 120 cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider) 121 require.NoError(t, err) 122 123 cachedStore.Post().GetPostsSince(fakeOptions, true) 124 mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPostsSince", 1) 125 cachedStore.Post().InvalidateLastPostTimeCache(channelId) 126 cachedStore.Post().GetPostsSince(fakeOptions, true) 127 mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPostsSince", 2) 128 }) 129 130 t.Run("GetPostsSince: first call not cached, clear caches, and then not cached again", func(t *testing.T) { 131 mockStore := getMockStore() 132 mockCacheProvider := getMockCacheProvider() 133 cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider) 134 require.NoError(t, err) 135 136 cachedStore.Post().GetPostsSince(fakeOptions, true) 137 mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPostsSince", 1) 138 cachedStore.Post().ClearCaches() 139 cachedStore.Post().GetPostsSince(fakeOptions, true) 140 mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPostsSince", 2) 141 }) 142 } 143 144 func TestPostStoreCache(t *testing.T) { 145 fakePosts := &model.PostList{} 146 fakeOptions := model.GetPostsOptions{ChannelId: "123", PerPage: 30} 147 148 t.Run("first call not cached, second cached and returning same data", func(t *testing.T) { 149 mockStore := getMockStore() 150 mockCacheProvider := getMockCacheProvider() 151 cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider) 152 require.NoError(t, err) 153 154 gotPosts, err := cachedStore.Post().GetPosts(fakeOptions, true) 155 require.NoError(t, err) 156 assert.Equal(t, fakePosts, gotPosts) 157 mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPosts", 1) 158 159 _, _ = cachedStore.Post().GetPosts(fakeOptions, true) 160 mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPosts", 1) 161 }) 162 163 t.Run("first call not cached, second force not cached", func(t *testing.T) { 164 mockStore := getMockStore() 165 mockCacheProvider := getMockCacheProvider() 166 cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider) 167 require.NoError(t, err) 168 169 gotPosts, err := cachedStore.Post().GetPosts(fakeOptions, true) 170 require.NoError(t, err) 171 assert.Equal(t, fakePosts, gotPosts) 172 mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPosts", 1) 173 174 _, _ = cachedStore.Post().GetPosts(fakeOptions, false) 175 mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPosts", 2) 176 }) 177 178 t.Run("first call not cached, invalidate, and then not cached again", func(t *testing.T) { 179 mockStore := getMockStore() 180 mockCacheProvider := getMockCacheProvider() 181 cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider) 182 require.NoError(t, err) 183 184 gotPosts, err := cachedStore.Post().GetPosts(fakeOptions, true) 185 require.NoError(t, err) 186 assert.Equal(t, fakePosts, gotPosts) 187 mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPosts", 1) 188 189 cachedStore.Post().InvalidateLastPostTimeCache("12360") 190 191 _, _ = cachedStore.Post().GetPosts(fakeOptions, true) 192 mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPosts", 1) 193 194 }) 195 }