(t *testing.T)
| 162 | } |
| 163 | |
| 164 | func TestChannelCacheCompactInactiveChannels(t *testing.T) { |
| 165 | |
| 166 | base.SetUpTestLogging(t, base.LevelInfo, base.KeyCache) |
| 167 | |
| 168 | // Define cache with max channels 20, watermarks 50/90 |
| 169 | options := DefaultCacheOptions().ChannelCacheOptions |
| 170 | options.MaxNumChannels = 20 |
| 171 | options.CompactHighWatermarkPercent = 90 |
| 172 | options.CompactLowWatermarkPercent = 50 |
| 173 | |
| 174 | stats, err := base.NewSyncGatewayStats() |
| 175 | require.NoError(t, err) |
| 176 | dbstats, err := stats.NewDBStats("", false, false, false, nil, nil) |
| 177 | require.NoError(t, err) |
| 178 | testStats := dbstats.Cache() |
| 179 | activeChannelStat := &base.SgwIntStat{} |
| 180 | activeChannels := channels.NewActiveChannels(activeChannelStat) |
| 181 | |
| 182 | ctx := base.TestCtx(t) |
| 183 | cache, err := newChannelCache(base.TestCtx(t), "testDb", options, testQueryHandlerFactory, activeChannels, testStats) |
| 184 | require.NoError(t, err, "Background task error whilst creating channel cache") |
| 185 | defer cache.Stop(ctx) |
| 186 | |
| 187 | // Add 16 channels to the cache. Mark odd channels as active, even channels as inactive. |
| 188 | // Shouldn't trigger compaction (hwm is not exceeded) |
| 189 | for i := 1; i <= 18; i++ { |
| 190 | channel := channels.NewID(fmt.Sprintf("chan_%d", i), base.DefaultCollectionID) |
| 191 | cache.addChannelCache(ctx, channel) |
| 192 | if i%2 == 1 { |
| 193 | log.Printf("Marking channel %q as active", channel) |
| 194 | activeChannels.IncrChannel(channel) |
| 195 | } |
| 196 | } |
| 197 | // Validate cache size |
| 198 | assert.Equal(t, 18, cache.channelCaches.Length()) |
| 199 | |
| 200 | log.Printf("adding 19th element to cache...") |
| 201 | // Add another channel to cache, should trigger compaction |
| 202 | cache.addChannelCache(ctx, channels.NewID("chan_19", base.DefaultCollectionID)) |
| 203 | |
| 204 | assert.True(t, waitForCompaction(cache), "Compaction didn't complete in expected time") |
| 205 | |
| 206 | // Validate cache size |
| 207 | assert.Equal(t, 10, cache.channelCaches.Length()) |
| 208 | |
| 209 | // Validate active channels have been retained in cache |
| 210 | for i := 1; i <= 19; i++ { |
| 211 | channel := channels.NewID(fmt.Sprintf("chan_%d", i), base.DefaultCollectionID) |
| 212 | _, isCached := cache.channelCaches.Get(channel) |
| 213 | if i%2 == 1 { |
| 214 | assert.True(t, isCached, fmt.Sprintf("Channel %q was active, should be retained in cache", channel)) |
| 215 | } else { |
| 216 | assert.False(t, isCached, fmt.Sprintf("Channel %q was inactive, should be evicted from cache", channel)) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | } |
| 221 |
nothing calls this directly
no test coverage detected