Пример #1
0
        public void Cache_KeyShouldBeRemovedFromTheCollectionToAvoidMemoryLeak_TwoThreadsOnTheSameKey()
        {
            var locksCollection = new TestLocksCollection <string, InMemoryCache.CachedObjectLock>();

            ICache cache = new InMemoryCache(locksCollection);

            var thread1CacheController = new UnitTestThreadCacheController();

            thread1CacheController.hold = true;
            var resultFromThread1 = new object();

            var thread2CacheController = new UnitTestThreadCacheController();

            thread2CacheController.cacheKey = thread1CacheController.cacheKey;
            var resultFromThread2 = new object();

            FireReadingFromCache(cache, thread1CacheController, resultFromThread1);

            WaitUntilThreadStartsPopulating(thread1CacheController);

            FireReadingFromCache(cache, thread2CacheController, resultFromThread2);

            WaitUntilTwoThreadsPickedLockObjectsUp(locksCollection);

            Assert.True(locksCollection.ContainsKey(thread1CacheController.cacheKey));

            thread1CacheController.hold = false;

            EnsureReadingFinished(thread1CacheController);
            EnsureReadingFinished(thread2CacheController);

            Assert.False(locksCollection.ContainsKey(thread1CacheController.cacheKey));
            Assert.False(thread1CacheController.exceptionThrown);
            Assert.False(thread2CacheController.exceptionThrown);
        }
Пример #2
0
        public void Cache_KeyShouldBeRemovedFromTheCollectionToAvoidMemoryLeak()
        {
            ILocksCollection <string, InMemoryCache.CachedObjectLock> testLocksCollection = new TestLocksCollection <string, InMemoryCache.CachedObjectLock>();

            ICache cache = new InMemoryCache(testLocksCollection);

            var thread1CacheController = new UnitTestThreadCacheController();

            thread1CacheController.hold = true;
            var resultFromThread1 = new object();

            FireReadingFromCache(cache, thread1CacheController, resultFromThread1);

            WaitUntilThreadStartsPopulating(thread1CacheController);

            Assert.True(testLocksCollection.ContainsKey(thread1CacheController.cacheKey));

            thread1CacheController.hold = false;

            EnsureReadingFinished(thread1CacheController);

            Assert.False(testLocksCollection.ContainsKey(thread1CacheController.cacheKey));
        }