public void SetUp()
 {
     inMemoryCache   = new Hashtable();
     expirer         = new ExpirationTask(this);
     expiredItemKeys = "";
     callbackCount   = 0;
     callbackReason  = CacheItemRemovedReason.Unknown;
 }
示例#2
0
 public void TestInitialize()
 {
     inMemoryCache           = new Hashtable();
     instrumentationProvider = new NullCachingInstrumentationProvider();
     expirer         = new ExpirationTask(this, instrumentationProvider);
     expiredItemKeys = "";
     callbackCount   = 0;
     callbackReason  = CacheItemRemovedReason.Unknown;
 }
		public void TestInitialize()
		{
			inMemoryCache = new Hashtable();
			instrumentationProvider = new CachingInstrumentationProvider();
			expirer = new ExpirationTask(this, instrumentationProvider);
			expiredItemKeys = "";
			callbackCount = 0;
			callbackReason = CacheItemRemovedReason.Unknown;
		}
示例#4
0
        public void DoesNotCallbackIfRemoveFromCacheThrowsException()
        {
            CacheItem exceptionThrowingCacheItem = new CacheItem("ThrowException", "value", CacheItemPriority.Normal, new MockRefreshAction(), null);
            exceptionThrowingCacheItem.WillBeExpired = true;
            AddCacheItem("ThrowException", exceptionThrowingCacheItem);

            ExpirationTask expirer = new ExpirationTask(this);
            expirer.SweepExpiredItemsFromCache(inMemoryCache);

            Thread.Sleep(250);

            Assert.AreEqual(0, callbackCount);
        }
示例#5
0
        public void DoesNotCallbackIfRemoveFromCacheThrowsException()
        {
            CacheItem exceptionThrowingCacheItem = new CacheItem("ThrowException", "value", CacheItemPriority.Normal, new MockRefreshAction(), null);

            exceptionThrowingCacheItem.WillBeExpired = true;
            AddCacheItem("ThrowException", exceptionThrowingCacheItem);

            ExpirationTask expirer           = new ExpirationTask(this, instrumentationProvider);
            int            expiredItemsCount = expirer.SweepExpiredItemsFromCache(inMemoryCache);

            Thread.Sleep(250);

            Assert.AreEqual(0, callbackCount);
            Assert.AreEqual(1, expiredItemsCount);
        }
        public void RemovalFailureDuringCacheExpirationDoesFireEvents()
        {
            ICacheOperations cacheOperations = new FailingOnRemovalCacheOperations();
            ExpirationTask   expiration      = new ExpirationTask(cacheOperations, instrumentationProvider);

            CacheItem expiredItem1 = new CacheItem(key1, new object(), CacheItemPriority.NotRemovable, new ExceptionThrowingCallback(), new AlwaysExpired());

            cacheOperations.CurrentCacheState.Add(key1, expiredItem1);
            expiredItem1.WillBeExpired = true;

            expiration.SweepExpiredItemsFromCache(cacheOperations.CurrentCacheState);

            Assert.AreEqual(1, instrumentationListener.eventArgs.Count);
            Assert.AreSame(typeof(CacheFailureEventArgs), instrumentationListener.eventArgs[0].GetType());
        }
示例#7
0
        public void RemovalAfterMarkingDoesNotTriggerSweepForItem()
        {
            CacheItem removedItem = CreateNeverExpiredCacheItem("key", "value");

            removedItem.WillBeExpired = true;
            removedItem.TouchedByUserAction(true);
            AddCacheItem("key", removedItem);

            ExpirationTask expirer           = new ExpirationTask(this, instrumentationProvider);
            int            expiredItemsCount = expirer.SweepExpiredItemsFromCache(inMemoryCache);

            Assert.AreEqual("", expiredItemKeys, "Items removed through user action during expiration should not be further expired.");
            Assert.IsFalse(removedItem.WillBeExpired, "User removal of this object should reset its expiration flag");
            Assert.AreEqual(0, expiredItemsCount);
        }
        public void CacheExpirationUnsuccessfulDoesNotFireEvents()
        {
            ICacheOperations cacheOperations = new MockCacheOperations();
            ExpirationTask   expiration      = new ExpirationTask(cacheOperations, instrumentationProvider);

            CacheItem expiredItem1 = new CacheItem(key1, new object(), CacheItemPriority.NotRemovable, null, new NeverExpired());

            cacheOperations.CurrentCacheState.Add(key1, expiredItem1);
            CacheItem expiredItem2 = new CacheItem(key2, new object(), CacheItemPriority.NotRemovable, null, new NeverExpired());

            cacheOperations.CurrentCacheState.Add(key2, expiredItem2);

            expiration.DoExpirations();

            Assert.AreEqual(0, instrumentationListener.eventArgs.Count);
        }
示例#9
0
        public void WillDoCallbackAtExpiration()
        {
            CacheItem removedItem = new CacheItem("1", "value", CacheItemPriority.Normal, new MockRefreshAction(), new AlwaysExpired());

            removedItem.WillBeExpired = true;
            AddCacheItem("1", removedItem);

            ExpirationTask expirer           = new ExpirationTask(this, instrumentationProvider);
            int            expiredItemsCount = expirer.SweepExpiredItemsFromCache(inMemoryCache);

            Thread.Sleep(250);

            Assert.AreEqual(1, callbackCount, "Refresh action should be taken for expired items if it exists");
            Assert.AreEqual(CacheItemRemovedReason.Expired, callbackReason);
            Assert.AreEqual(1, expiredItemsCount);
        }
示例#10
0
        public void CanDoMarkAndSweepInBackground()
        {
            CacheItem itemOne = new CacheItem("1", "value", CacheItemPriority.Normal, null, new AbsoluteTime(TimeSpan.FromSeconds(0.5)));

            AddCacheItem("1", itemOne);

            ExpirationTask      expirer   = new ExpirationTask(this, instrumentationProvider);
            BackgroundScheduler scheduler = new BackgroundScheduler(expirer, null, null);

            Thread.Sleep(1000);

            scheduler.ExpirationTimeoutExpired(null);

            Thread.Sleep(100);

            Assert.AreEqual("1", expiredItemKeys, "Item should have been expired in the background");
        }
示例#11
0
        public void ExceptionThrownInCallbackDoesNotStopOtherCallbacksFromExecuting()
        {
            CacheItem exceptionThrowingCacheItem = new CacheItem("ThrowException", "value", CacheItemPriority.Normal, new ExceptionThrowingMockRefreshAction(), null);
            CacheItem normalCacheItem            = new CacheItem("normal", "value2", CacheItemPriority.Normal, new MockRefreshAction(), null);

            exceptionThrowingCacheItem.WillBeExpired = true;
            normalCacheItem.WillBeExpired            = true;
            AddCacheItem("ThrowException", exceptionThrowingCacheItem);
            AddCacheItem("normal", normalCacheItem);

            ExpirationTask expirer           = new ExpirationTask(this, instrumentationProvider);
            int            expiredItemsCount = expirer.SweepExpiredItemsFromCache(inMemoryCache);

            Thread.Sleep(250);

            Assert.AreEqual(1, callbackCount, "Second item was still refreshed despite first refresh action throwing exception");
            Assert.AreEqual(CacheItemRemovedReason.Expired, callbackReason);
            Assert.AreEqual(2, expiredItemsCount);
        }
示例#12
0
        public void CanDoMarkAndSweepInBackground()
        {
            CacheItem itemOne = new CacheItem("1", "value", CacheItemPriority.Normal, null, new AbsoluteTime(TimeSpan.FromSeconds(0.5)));
            AddCacheItem("1", itemOne);

            ExpirationTask expirer = new ExpirationTask(this);
            BackgroundScheduler scheduler =
                new BackgroundScheduler(expirer, null);
            scheduler.Start();

            Thread.Sleep(1000);

            scheduler.ExpirationTimeoutExpired(null);

            Thread.Sleep(100);
            scheduler.Stop();

            Assert.AreEqual("1", expiredItemKeys, "Item should have been expired in the background");
        }
        public void CacheExpirationDoesFireEvents()
        {
            ICacheOperations cacheOperations = new MockCacheOperations();
            ExpirationTask   expiration      = new ExpirationTask(cacheOperations, instrumentationProvider);

            CacheItem expiredItem1 = new CacheItem(key1, new object(), CacheItemPriority.NotRemovable, null, new AlwaysExpired());

            cacheOperations.CurrentCacheState.Add(key1, expiredItem1);
            CacheItem expiredItem2 = new CacheItem(key2, new object(), CacheItemPriority.NotRemovable, null, new AlwaysExpired());

            cacheOperations.CurrentCacheState.Add(key2, expiredItem2);

            expiration.DoExpirations();

            Assert.AreEqual(1, instrumentationListener.eventArgs.Count);
            IList <CacheExpiredEventArgs> expiries = FilterEventArgs <CacheExpiredEventArgs>(instrumentationListener.eventArgs);

            Assert.AreEqual(1, expiries.Count);
            Assert.AreEqual(2L, expiries[0].ItemsExpired);
        }
示例#14
0
        public CacheManager BuildCacheManager(
            string cacheManagerName,
            IBackingStore backingStore,
            int maximumElementsInCacheBeforeScavenging,
            int numberToRemoveWhenScavenging,
            int expirationPollFrequencyInSeconds,
            CachingInstrumentationProvider instrumentationProvider)
        {
            CacheCapacityScavengingPolicy scavengingPolicy =
                new CacheCapacityScavengingPolicy(maximumElementsInCacheBeforeScavenging);
            Cache cache = new Cache(backingStore, scavengingPolicy, instrumentationProvider);
            ExpirationPollTimer timer          = new ExpirationPollTimer();
            ExpirationTask      expirationTask = CreateExpirationTask(cache, instrumentationProvider);
            ScavengerTask       scavengerTask  = new ScavengerTask(numberToRemoveWhenScavenging, scavengingPolicy, cache, instrumentationProvider);
            BackgroundScheduler scheduler      = new BackgroundScheduler(expirationTask, scavengerTask, instrumentationProvider);

            cache.Initialize(scheduler);
            scheduler.Start();
            timer.StartPolling(new TimerCallback(scheduler.ExpirationTimeoutExpired), expirationPollFrequencyInSeconds * 1000);
            return(new CacheManager(cache, scheduler, timer));
        }
		public void ExceptionThrownInCallbackDoesNotStopOtherCallbacksFromExecuting()
		{
			CacheItem exceptionThrowingCacheItem = new CacheItem("ThrowException", "value", CacheItemPriority.Normal, new ExceptionThrowingMockRefreshAction(), null);
			CacheItem normalCacheItem = new CacheItem("normal", "value2", CacheItemPriority.Normal, new MockRefreshAction(), null);
			exceptionThrowingCacheItem.WillBeExpired = true;
			normalCacheItem.WillBeExpired = true;
			AddCacheItem("ThrowException", exceptionThrowingCacheItem);
			AddCacheItem("normal", normalCacheItem);

			ExpirationTask expirer = new ExpirationTask(this, instrumentationProvider);
			int expiredItemsCount = expirer.SweepExpiredItemsFromCache(inMemoryCache);

			Thread.Sleep(250);

			Assert.AreEqual(1, callbackCount, "Second item was still refreshed despite first refresh action throwing exception");
			Assert.AreEqual(CacheItemRemovedReason.Expired, callbackReason);
			Assert.AreEqual(2, expiredItemsCount);
		}
		public void WillDoCallbackAtExpiration()
		{
			CacheItem removedItem = new CacheItem("1", "value", CacheItemPriority.Normal, new MockRefreshAction(), new AlwaysExpired());
			removedItem.WillBeExpired = true;
			AddCacheItem("1", removedItem);

			ExpirationTask expirer = new ExpirationTask(this, instrumentationProvider);
			int expiredItemsCount = expirer.SweepExpiredItemsFromCache(inMemoryCache);

			Thread.Sleep(250);

			Assert.AreEqual(1, callbackCount, "Refresh action should be taken for expired items if it exists");
			Assert.AreEqual(CacheItemRemovedReason.Expired, callbackReason);
			Assert.AreEqual(1, expiredItemsCount);
		}
		public void RemovalAfterMarkingDoesNotTriggerSweepForItem()
		{
			CacheItem removedItem = CreateNeverExpiredCacheItem("key", "value");
			removedItem.WillBeExpired = true;
			removedItem.TouchedByUserAction(true);
			AddCacheItem("key", removedItem);

			ExpirationTask expirer = new ExpirationTask(this, instrumentationProvider);
			int expiredItemsCount = expirer.SweepExpiredItemsFromCache(inMemoryCache);

			Assert.AreEqual("", expiredItemKeys, "Items removed through user action during expiration should not be further expired.");
			Assert.IsFalse(removedItem.WillBeExpired, "User removal of this object should reset its expiration flag");
			Assert.AreEqual(0, expiredItemsCount);
		}
示例#18
0
 public void SetUp()
 {
     inMemoryCache = new Hashtable();
     expirer = new ExpirationTask(this);
     expiredItemKeys = "";
     callbackCount = 0;
     callbackReason = CacheItemRemovedReason.Unknown;
 }
示例#19
0
 /// <param name="expirationTask">Задание для экспиратора</param>
 /// <param name="scavengerTask">Задание для сборщика</param>
 /// <param name="instrumentationProvider"></param>
 public BackgroundScheduler(ExpirationTask expirationTask, ScavengerTask scavengerTask, ICachingInstrumentationProvider instrumentationProvider)
 {
     this.expirationTask = expirationTask;
     this.scavengerTask = scavengerTask;
     this.instrumentationProvider = instrumentationProvider;
 }