public void CanScavengeInBackground() { CacheItem item1 = new CacheItem("key1", "value1", CacheItemPriority.Low, null); CacheItem item2 = new CacheItem("key2", "value2", CacheItemPriority.Normal, null); CacheItem item3 = new CacheItem("key3", "value3", CacheItemPriority.High, null); AddCacheItem("key1", item1); AddCacheItem("key2", item2); AddCacheItem("key3", item3); TestConfigurationContext context = new TestConfigurationContext(); CachingConfigurationView view = new CachingConfigurationView(context); view.GetCacheManagerSettings().CacheManagers["test"].MaximumElementsInCacheBeforeScavenging = 2; view.GetCacheManagerSettings().CacheManagers["test"].NumberToRemoveWhenScavenging = 1; CacheCapacityScavengingPolicy scavengingPolicy = new CacheCapacityScavengingPolicy("test", view); ScavengerTask scavenger = new ScavengerTask("test", view, scavengingPolicy, this); BackgroundScheduler scheduler = new BackgroundScheduler(null, scavenger); scheduler.Start(); Thread.Sleep(500); scheduler.StartScavenging(); Thread.Sleep(250); scheduler.Stop(); Thread.Sleep(250); Assert.AreEqual("key1", scavengedKeys); }
public void CanScavengeInBackground() { CacheItem item1 = new CacheItem("key1", "value1", CacheItemPriority.Low, null); CacheItem item2 = new CacheItem("key2", "value2", CacheItemPriority.Normal, null); CacheItem item3 = new CacheItem("key3", "value3", CacheItemPriority.High, null); AddCacheItem("key1", item1); AddCacheItem("key2", item2); AddCacheItem("key3", item3); CacheCapacityScavengingPolicy scavengingPolicy = new CacheCapacityScavengingPolicy(2); ScavengerTask scavenger = new ScavengerTask(1, scavengingPolicy, this, instrumentationProvider); BackgroundScheduler scheduler = new BackgroundScheduler(null, scavenger, instrumentationProvider); scheduler.Start(); Thread.Sleep(500); scheduler.StartScavenging(); Thread.Sleep(250); scheduler.Stop(); Thread.Sleep(250); Assert.AreEqual("key1", scavengedKeys); }
public void WillNotScheduleNewScavengeTaskIfOneIsAlreadyScheduled() { inMemoryCacheRequestSemaphore = new EventWaitHandle(false, EventResetMode.ManualReset); CacheItem item1 = new CacheItem("key1", "value1", CacheItemPriority.Low, null); CacheItem item2 = new CacheItem("key2", "value2", CacheItemPriority.Normal, null); CacheItem item3 = new CacheItem("key3", "value3", CacheItemPriority.Normal, null); CacheItem item4 = new CacheItem("key4", "value4", CacheItemPriority.Normal, null); CacheItem item5 = new CacheItem("key5", "value5", CacheItemPriority.High, null); CacheItem item6 = new CacheItem("key6", "value6", CacheItemPriority.High, null); CacheItem item7 = new CacheItem("key7", "value7", CacheItemPriority.High, null); AddCacheItem("key1", item1); AddCacheItem("key2", item2); AddCacheItem("key3", item3); ScavengerTask scavenger = new ScavengerTask(2, 3, this, instrumentationProvider); BackgroundScheduler scheduler = new BackgroundScheduler(null, scavenger, instrumentationProvider); AddCacheItem("key4", item4); // this new scavenge request will be scheduled, it's the first one scheduler.StartScavenging(); // the scavenge request scheduled above would be processed here and will be blocked by the event Thread.Sleep(500); AddCacheItem("key5", item5); // this new scavenge request will be scheduled, because the previously scheduled one will have started scheduler.StartScavenging(); Thread.Sleep(250); AddCacheItem("key6", item6); // this new scavenge request will be ignored scheduler.StartScavenging(); Thread.Sleep(250); AddCacheItem("key7", item7); // this new scavenge request will be ignored scheduler.StartScavenging(); Thread.Sleep(250); bool value = inMemoryCacheRequestSemaphore.Set(); Thread.Sleep(250); // 3 cache requests should be issued: one for "key4" for one element and two for the remaining elements // since at most 2 elements are removed per scavenge Assert.AreEqual(3, inMemoryCacheRequests); }