public void ItemAddedPreviousToFailedAddIsRemovedCompletelyIfSecondAddFails()
        {
            TestConfigurationContext context = new TestConfigurationContext();

            using (IsolatedStorageBackingStore backingStore = new IsolatedStorageBackingStore("foo"))
            {
                CacheCapacityScavengingPolicy scavengingPolicy = new CacheCapacityScavengingPolicy("test", new CachingConfigurationView(context));

                Cache cache = new Cache(backingStore, scavengingPolicy);
                cache.Initialize(this);

                cache.Add("my", new SerializableClass());

                try
                {
                    cache.Add("my", new NonSerializableClass());
                    Assert.Fail("Should have thrown exception internally to Cache.Add");
                }
                catch (Exception)
                {
                    Assert.IsFalse(cache.Contains("my"));
                    Assert.AreEqual(0, backingStore.Count);

                    Hashtable isolatedStorageContents = backingStore.Load();
                    Assert.AreEqual(0, isolatedStorageContents.Count);
                }
            }
        }
Example #2
0
        public void WillScavenge()
        {
            CacheItem item1 = new CacheItem("key1", "value1", CacheItemPriority.NotRemovable, null);
            CacheItem item2 = new CacheItem("key2", "value2", CacheItemPriority.High, null);
            CacheItem item3 = new CacheItem("key3", "value3", CacheItemPriority.Normal, null);
            CacheItem item4 = new CacheItem("key4", "value4", CacheItemPriority.Low, null);

            AddCacheItem("key1", item1);
            AddCacheItem("key2", item2);
            AddCacheItem("key3", item3);
            AddCacheItem("key4", item4);

            TestConfigurationContext context = new TestConfigurationContext();
            CachingConfigurationView view    = new CachingConfigurationView(context);

            view.GetCacheManagerSettings().CacheManagers["test"].MaximumElementsInCacheBeforeScavenging = 1;
            view.GetCacheManagerSettings().CacheManagers["test"].NumberToRemoveWhenScavenging           = 2;

            CacheCapacityScavengingPolicy scavengingPolicy = new CacheCapacityScavengingPolicy("test", view);
            ScavengerTask scavenger = new ScavengerTask("test", view, scavengingPolicy, this);

            scavenger.DoScavenging();

            Assert.AreEqual("key4key3", scavengedKeys);
        }
Example #3
0
        public void WillRemoveMultipleEligibleForScavenging()
        {
            TestConfigurationContext context = new TestConfigurationContext();
            CachingConfigurationView view    = new CachingConfigurationView(context);

            view.GetCacheManagerSettings().CacheManagers["test"].MaximumElementsInCacheBeforeScavenging = 2;
            view.GetCacheManagerSettings().CacheManagers["test"].NumberToRemoveWhenScavenging           = 3;

            CacheCapacityScavengingPolicy scavengingPolicy = new CacheCapacityScavengingPolicy("test", view);
            ScavengerTask scavenger        = new ScavengerTask("test", view, scavengingPolicy, this);
            CacheItem     itemToRemove     = new CacheItem("key1", "value", CacheItemPriority.High, null);
            CacheItem     itemToRemain     = new CacheItem("key2", "value", CacheItemPriority.Low, null);
            CacheItem     itemToRemoveAlso = new CacheItem("key3", "value", CacheItemPriority.Normal, null);

            itemToRemove.MakeEligibleForScavenging();
            itemToRemain.MakeEligibleForScavenging();
            itemToRemoveAlso.MakeEligibleForScavenging();

            AddCacheItem("key1", itemToRemove);
            AddCacheItem("key2", itemToRemain);
            AddCacheItem("key3", itemToRemoveAlso);

            scavenger.DoScavenging();

            Assert.AreEqual("key2key3key1", scavengedKeys);
        }
Example #4
0
        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);
        }
Example #5
0
        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);
        }
Example #6
0
        public void WillNotDieIfNotEnoughItemsToScavenge()
        {
            TestConfigurationContext context = new TestConfigurationContext();
            CachingConfigurationView view = new CachingConfigurationView(context);
            view.GetCacheManagerSettings().CacheManagers["test"].MaximumElementsInCacheBeforeScavenging = 2;
            view.GetCacheManagerSettings().CacheManagers["test"].NumberToRemoveWhenScavenging = 4;

            CacheCapacityScavengingPolicy scavengingPolicy = new CacheCapacityScavengingPolicy("test", view);
            ScavengerTask scavenger = new ScavengerTask("test", view, scavengingPolicy, this);
            CacheItem itemToRemove = new CacheItem("key1", "value", CacheItemPriority.High, null);
            CacheItem itemToRemain = new CacheItem("key2", "value", CacheItemPriority.Low, null);
            CacheItem itemToRemoveAlso = new CacheItem("key3", "value", CacheItemPriority.Normal, null);

            itemToRemove.MakeEligibleForScavenging();
            itemToRemain.MakeEligibleForScavenging();
            itemToRemoveAlso.MakeEligibleForScavenging();

            AddCacheItem("key1", itemToRemove);
            AddCacheItem("key2", itemToRemain);
            AddCacheItem("key3", itemToRemoveAlso);

            scavenger.DoScavenging();

            Assert.AreEqual("key2key3key1", scavengedKeys);
        }
        public void ItemAddedPreviousToFailedAddIsRemovedCompletelyIfSecondAddFails()
        {
            TestConfigurationContext context = new TestConfigurationContext();
            using (IsolatedStorageBackingStore backingStore = new IsolatedStorageBackingStore("foo"))
            {
                CacheCapacityScavengingPolicy scavengingPolicy = new CacheCapacityScavengingPolicy("test", new CachingConfigurationView(context));

                Cache cache = new Cache(backingStore, scavengingPolicy);
                cache.Initialize(this);

                cache.Add("my", new SerializableClass());

                try
                {
                    cache.Add("my", new NonSerializableClass());
                    Assert.Fail("Should have thrown exception internally to Cache.Add");
                }
                catch (Exception)
                {
                    Assert.IsFalse(cache.Contains("my"));
                    Assert.AreEqual(0, backingStore.Count);

                    Hashtable isolatedStorageContents = backingStore.Load();
                    Assert.AreEqual(0, isolatedStorageContents.Count);
                }
            }
        }
Example #8
0
        public void WillRemoveSingleItemFromCache()
        {
            TestConfigurationContext context = new TestConfigurationContext();
            CachingConfigurationView view    = new CachingConfigurationView(context);

            view.GetCacheManagerSettings().CacheManagers["test"].MaximumElementsInCacheBeforeScavenging = 0;

            CacheCapacityScavengingPolicy scavengingPolicy = new CacheCapacityScavengingPolicy("test", view);
            ScavengerTask scavenger    = new ScavengerTask("test", view, scavengingPolicy, this);
            CacheItem     itemToRemove = new CacheItem("key", "value", CacheItemPriority.Low, null);

            itemToRemove.MakeEligibleForScavenging();
            AddCacheItem("key", itemToRemove);

            scavenger.DoScavenging();

            Assert.AreEqual("key", scavengedKeys);
        }
        public void ExceptionThrownDuringAddResultsInObjectBeingRemovedFromCacheCompletely()
        {
            TestConfigurationContext context = new TestConfigurationContext();
            MockBackingStore backingStore = new MockBackingStore();
            CacheCapacityScavengingPolicy scavengingPolicy = new CacheCapacityScavengingPolicy("test", new CachingConfigurationView(context));

            Cache cache = new Cache(backingStore, scavengingPolicy);
            cache.Initialize(this);

            try
            {
                cache.Add("foo", "bar");
                Assert.Fail("Should have thrown exception thrown internally to Cache.Add");
            }
            catch (Exception)
            {
                Assert.IsFalse(cache.Contains("foo"));
                Assert.AreEqual(1, backingStore.removalCount);
            }
        }
        public void ExceptionThrownDuringAddResultsInObjectBeingRemovedFromCacheCompletely()
        {
            TestConfigurationContext      context          = new TestConfigurationContext();
            MockBackingStore              backingStore     = new MockBackingStore();
            CacheCapacityScavengingPolicy scavengingPolicy = new CacheCapacityScavengingPolicy("test", new CachingConfigurationView(context));

            Cache cache = new Cache(backingStore, scavengingPolicy);

            cache.Initialize(this);

            try
            {
                cache.Add("foo", "bar");
                Assert.Fail("Should have thrown exception thrown internally to Cache.Add");
            }
            catch (Exception)
            {
                Assert.IsFalse(cache.Contains("foo"));
                Assert.AreEqual(1, backingStore.removalCount);
            }
        }
        public void ExceptionThrownDuringAddIntoIsolatedStorageAllowsItemToBeReaddedLater()
        {
            TestConfigurationContext context = new TestConfigurationContext();
            using (IsolatedStorageBackingStore backingStore = new IsolatedStorageBackingStore("foo"))
            {
                CacheCapacityScavengingPolicy scavengingPolicy = new CacheCapacityScavengingPolicy("test", new CachingConfigurationView(context));

                Cache cache = new Cache(backingStore, scavengingPolicy);
                cache.Initialize(this);

                try
                {
                    cache.Add("my", new NonSerializableClass());
                    Assert.Fail("Should have thrown exception internally to Cache.Add");
                }
                catch (Exception)
                {
                    cache.Add("my", new SerializableClass());
                    Assert.IsTrue(cache.Contains("my"));
                }
            }
        }
        public void ExceptionThrownDuringAddIntoIsolatedStorageAllowsItemToBeReaddedLater()
        {
            TestConfigurationContext context = new TestConfigurationContext();

            using (IsolatedStorageBackingStore backingStore = new IsolatedStorageBackingStore("foo"))
            {
                CacheCapacityScavengingPolicy scavengingPolicy = new CacheCapacityScavengingPolicy("test", new CachingConfigurationView(context));

                Cache cache = new Cache(backingStore, scavengingPolicy);
                cache.Initialize(this);

                try
                {
                    cache.Add("my", new NonSerializableClass());
                    Assert.Fail("Should have thrown exception internally to Cache.Add");
                }
                catch (Exception)
                {
                    cache.Add("my", new SerializableClass());
                    Assert.IsTrue(cache.Contains("my"));
                }
            }
        }
Example #13
0
        public void WillScavenge()
        {
            CacheItem item1 = new CacheItem("key1", "value1", CacheItemPriority.NotRemovable, null);
            CacheItem item2 = new CacheItem("key2", "value2", CacheItemPriority.High, null);
            CacheItem item3 = new CacheItem("key3", "value3", CacheItemPriority.Normal, null);
            CacheItem item4 = new CacheItem("key4", "value4", CacheItemPriority.Low, null);

            AddCacheItem("key1", item1);
            AddCacheItem("key2", item2);
            AddCacheItem("key3", item3);
            AddCacheItem("key4", item4);

            TestConfigurationContext context = new TestConfigurationContext();
            CachingConfigurationView view = new CachingConfigurationView(context);
            view.GetCacheManagerSettings().CacheManagers["test"].MaximumElementsInCacheBeforeScavenging = 1;
            view.GetCacheManagerSettings().CacheManagers["test"].NumberToRemoveWhenScavenging = 2;

            CacheCapacityScavengingPolicy scavengingPolicy = new CacheCapacityScavengingPolicy("test", view);
            ScavengerTask scavenger = new ScavengerTask("test", view, scavengingPolicy, this);
            scavenger.DoScavenging();

            Assert.AreEqual("key4key3", scavengedKeys);
        }
Example #14
0
        public void WillRemoveSingleItemFromCache()
        {
            TestConfigurationContext context = new TestConfigurationContext();
            CachingConfigurationView view = new CachingConfigurationView(context);
            view.GetCacheManagerSettings().CacheManagers["test"].MaximumElementsInCacheBeforeScavenging = 0;

            CacheCapacityScavengingPolicy scavengingPolicy = new CacheCapacityScavengingPolicy("test", view);
            ScavengerTask scavenger = new ScavengerTask("test", view, scavengingPolicy, this);
            CacheItem itemToRemove = new CacheItem("key", "value", CacheItemPriority.Low, null);
            itemToRemove.MakeEligibleForScavenging();
            AddCacheItem("key", itemToRemove);

            scavenger.DoScavenging();

            Assert.AreEqual("key", scavengedKeys);
        }