TestChannelCacheCompactNRU tests compaction where a subset of the channels are marked as recently used between compact triggers. In the second compact, NRU channels should have eviction priority.
(t *testing.T)
| 222 | // TestChannelCacheCompactNRU tests compaction where a subset of the channels are marked as recently used |
| 223 | // between compact triggers. In the second compact, NRU channels should have eviction priority. |
| 224 | func TestChannelCacheCompactNRU(t *testing.T) { |
| 225 | |
| 226 | base.SetUpTestLogging(t, base.LevelInfo, base.KeyCache) |
| 227 | |
| 228 | // Define cache with max channels 20, watermarks 50/90 |
| 229 | options := DefaultCacheOptions().ChannelCacheOptions |
| 230 | options.MaxNumChannels = 20 |
| 231 | options.CompactHighWatermarkPercent = 90 |
| 232 | options.CompactLowWatermarkPercent = 70 |
| 233 | |
| 234 | stats, err := base.NewSyncGatewayStats() |
| 235 | require.NoError(t, err) |
| 236 | dbstats, err := stats.NewDBStats("", false, false, false, nil, nil) |
| 237 | require.NoError(t, err) |
| 238 | testStats := dbstats.Cache() |
| 239 | activeChannelStat := &base.SgwIntStat{} |
| 240 | activeChannels := channels.NewActiveChannels(activeChannelStat) |
| 241 | ctx := base.TestCtx(t) |
| 242 | cache, err := newChannelCache(base.TestCtx(t), "testDb", options, testQueryHandlerFactory, activeChannels, testStats) |
| 243 | require.NoError(t, err, "Background task error whilst creating channel cache") |
| 244 | defer cache.Stop(ctx) |
| 245 | |
| 246 | // Add 18 channels to the cache. Mark channels 1-10 as active |
| 247 | // Shouldn't trigger compaction (hwm is not exceeded) |
| 248 | for i := 1; i <= 18; i++ { |
| 249 | channel := channels.NewID(fmt.Sprintf("chan_%d", i), base.DefaultCollectionID) |
| 250 | cache.addChannelCache(ctx, channel) |
| 251 | if i <= 10 { |
| 252 | log.Printf("Marking channel %q as active", channel) |
| 253 | activeChannels.IncrChannel(channel) |
| 254 | } |
| 255 | } |
| 256 | // Validate cache size |
| 257 | assert.Equal(t, 18, cache.channelCaches.Length()) |
| 258 | |
| 259 | // Add another channel to cache, should trigger compaction |
| 260 | cache.addChannelCache(ctx, channels.NewID("chan_19", base.DefaultCollectionID)) |
| 261 | assert.True(t, waitForCompaction(cache), "Compaction didn't complete in expected time") |
| 262 | |
| 263 | // Expect channels 1-10, 11-15 to be evicted, and all to be marked as NRU during compaction |
| 264 | assert.Equal(t, 14, cache.channelCaches.Length()) |
| 265 | |
| 266 | // Validate recently used channels have been retained in cache |
| 267 | for i := 1; i <= 19; i++ { |
| 268 | channel := channels.NewID(fmt.Sprintf("chan_%d", i), base.DefaultCollectionID) |
| 269 | _, isCached := cache.channelCaches.Get(channel) |
| 270 | if i <= 10 || i > 15 { |
| 271 | assert.True(t, isCached, fmt.Sprintf("Expected %q to be cached", channel)) |
| 272 | } else { |
| 273 | assert.False(t, isCached, fmt.Sprintf("Expected %q to not be cached", channel)) |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | // Mark channels 1-5 as recently used |
| 278 | for i := 1; i <= 5; i++ { |
| 279 | channel := channels.NewID(fmt.Sprintf("chan_%d", i), base.DefaultCollectionID) |
| 280 | cacheElement, isCached := cache.channelCaches.Get(channel) |
| 281 | assert.True(t, isCached, fmt.Sprintf("Expected %s to be cached during recently used update", channel)) |
nothing calls this directly
no test coverage detected