public void BasicCacheActionElementCount_Ok()
        {
            //Arrange
            string cacheName = "CacheForTest";
            ICacheManager cacheManager = new CacheManager(cacheName);

            //Act
            var result = cacheManager.ElementsCount();

            //Assert
            Assert.IsTrue(result == 0, "Just created cache should have 0 elements");
        }
        public void BasicCacheActionGetExisting_Ok()
        {
            //Arrange
            string cacheName = "CacheForTest";
            ICacheManager cacheManager = new CacheManager(cacheName);
            var dummyValue = DummyObject.GetAnInstance();

            //Act
            cacheManager.Set(dummyValue);
            var result = cacheManager.Get(dummyValue.GetUniqueHash());

            //Assert
            Assert.IsNotNull(result, "Existing key should not return null value");
            Assert.IsTrue(dummyValue.Equals(result), "Objects are not the same");
        }
        public void DeleteWithSyncOn2LevelsCache_Ok()
        {
            //Arrange
            var level2CacheMock = new Mock<ICacheManager>();
            ICacheManager cacheManager = new CacheManager("CacheForTest", masterCache: level2CacheMock.Object);

            //Act
            cacheManager.Delete(string.Empty, SyncMode.Sync);

            //Assert
            level2CacheMock.Verify(x => x.Set(It.IsAny<CacheableEntity>(), It.IsAny<SyncMode>()), Times.Never);
            level2CacheMock.Verify(x => x.Get(It.IsAny<string>(), It.IsAny<SyncMode>()), Times.Never);
            level2CacheMock.Verify(x => x.Delete(It.IsAny<string>(), It.IsAny<SyncMode>()), Times.Once);
            level2CacheMock.Verify(x => x.ClearCache(), Times.Never);
            level2CacheMock.Verify(x => x.ElementsCount(), Times.Never);
        }
        public void BasicCacheActionDelete_Ok()
        {
            //Arrange
            string cacheName = "CacheForTest";
            ICacheManager cacheManager = new CacheManager(cacheName);
            var dummyValue = DummyObject.GetAnInstance();

            //Act
            cacheManager.Set(dummyValue);
            var totalElementsBeforeDelete = cacheManager.ElementsCount();

            cacheManager.Delete(dummyValue.GetUniqueHash());
            var totalElementsAfterDelete = cacheManager.ElementsCount();

            //Assert
            Assert.IsTrue(totalElementsBeforeDelete == 1, "We should have just one element within the cache");
            Assert.IsTrue(totalElementsAfterDelete == 0, "We should have zero elements within the cache");
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            ChannelFactory<ICacheManager> channelFactory = new ChannelFactory<ICacheManager>("*");
            ICacheManager masterCache = channelFactory.CreateChannel();

            IPersistenceEngine persistenceEngine = new XmlFilePersistenceEngine(@".\CacheContent\");

            ICacheManager cacheManager = new CacheManager("LocalCache", persistenceEngine, masterCache);

            var engine_A = new Engine() { Id = 1, Description = "This is the engine A" };

            cacheManager.Set(engine_A, SyncMode.SyncParent);
            var getResult_1 = cacheManager.Get(engine_A.GetUniqueHash());

            cacheManager.Delete(engine_A.GetUniqueHash());
            var getResult_3 = cacheManager.Get(engine_A.GetUniqueHash());

            var getResult_5 = cacheManager.Get(engine_A.GetUniqueHash(), SyncMode.SyncParent);
            var getResult_6 = cacheManager.Get(engine_A.GetUniqueHash());
        }
        public void DeleteWithParentSyncOn3LevelsCache_Ok()
        {
            //Arrange
            var level3CacheMock = new Mock<ICacheManager>();
            ICacheManager level2Cache = new CacheManager("CacheForTestLevel2", masterCache: level3CacheMock.Object);
            ICacheManager level1Cache = new CacheManager("CacheForTestLevel2", masterCache: level2Cache);
            var dummyValue = DummyObject.GetAnInstance();
            level1Cache.Set(dummyValue);
            level2Cache.Set(dummyValue);

            //Act
            level1Cache.Delete(dummyValue.GetUniqueHash(), SyncMode.SyncParent);

            //Assert
            Assert.IsTrue(level2Cache.ElementsCount() == 0, "Parent sync should delete the value from level 2 cache");
            Assert.IsTrue(level1Cache.ElementsCount() == 0, "Parent sync should delte the value from level 1 cache");

            level3CacheMock.Verify(x => x.Set(It.IsAny<CacheableEntity>(), It.IsAny<SyncMode>()), Times.Never);
            level3CacheMock.Verify(x => x.Get(It.IsAny<string>(), It.IsAny<SyncMode>()), Times.Never);
            level3CacheMock.Verify(x => x.Delete(It.IsAny<string>(), It.IsAny<SyncMode>()), Times.Never);
            level3CacheMock.Verify(x => x.ClearCache(), Times.Never);
            level3CacheMock.Verify(x => x.ElementsCount(), Times.Never);
        }
        public void BasicCacheActionGetNonExisting_Ok()
        {
            //Arrange
            string cacheName = "CacheForTest";
            ICacheManager cacheManager = new CacheManager(cacheName);

            //Act
            var result = cacheManager.Get("NonExisitingKey");

            //Assert
            Assert.IsNull(result, "Non existing key should return null value");
        }
        public void BasicCacheCreation_Ok()
        {
            //Arrange
            string cacheName = "CacheForTest";
            ICacheManager cacheManager = new CacheManager(cacheName);

            //Act

            //Assert
            Assert.IsTrue((cacheManager as CacheManager).CacheName == cacheName, "Name was not properly set");
            Assert.IsFalse((cacheManager as CacheManager).IsSlaveCache, "Cache was not marked as expected (should not be a slave cache since we didn't provide a master cache)");
        }
        public void BasicCacheActionSet_Ok()
        {
            //Arrange
            string cacheName = "CacheForTest";
            ICacheManager cacheManager = new CacheManager(cacheName);
            var dummyValue = DummyObject.GetAnInstance();

            //Act
            cacheManager.Set(dummyValue);
            var totalElements = cacheManager.ElementsCount();

            //Assert
            Assert.IsTrue(totalElements == 1, "We should have just one element within the cache");
        }
        public void SetWithoutSyncOn2LevelsCache_Ok()
        {
            //Arrange
            var level2CacheMock = new Mock<ICacheManager>();
            ICacheManager cacheManager = new CacheManager("CacheForTest", masterCache: level2CacheMock.Object);
            var dummyValue = DummyObject.GetAnInstance();

            //Act
            cacheManager.Set(dummyValue, SyncMode.NoSync);

            //Assert
            level2CacheMock.Verify(x => x.Get(It.IsAny<string>(), It.IsAny<SyncMode>()), Times.Never);
            level2CacheMock.Verify(x => x.Set(It.IsAny<CacheableEntity>(), It.IsAny<SyncMode>()), Times.Never);
            level2CacheMock.Verify(x => x.Delete(It.IsAny<string>(), It.IsAny<SyncMode>()), Times.Never);
            level2CacheMock.Verify(x => x.ClearCache(), Times.Never);
            level2CacheMock.Verify(x => x.ElementsCount(), Times.Never);
        }
        public void GetWithParentSyncOn3LevelsCache_Ok()
        {
            //Arrange
            var level3CacheMock = new Mock<ICacheManager>();
            ICacheManager level2Cache = new CacheManager("CacheForTestLevel2", masterCache: level3CacheMock.Object);
            ICacheManager level1Cache = new CacheManager("CacheForTestLevel2", masterCache: level2Cache);
            var dummyValue = DummyObject.GetAnInstance();
            level2Cache.Set(dummyValue);

            //Act
            var result = level1Cache.Get(dummyValue.GetUniqueHash(), SyncMode.SyncParent);

            //Assert
            Assert.IsTrue(level2Cache.ElementsCount() == 1, "Parent sync should should keep value unchanged");
            Assert.IsNotNull(result, "REsult should not be null");
            Assert.IsTrue(dummyValue.Equals(dummyValue), "Retrieved value should be the same one we set on lvl 2 cache");

            level3CacheMock.Verify(x => x.Set(It.IsAny<CacheableEntity>(), It.IsAny<SyncMode>()), Times.Never);
            level3CacheMock.Verify(x => x.Get(It.IsAny<string>(), It.IsAny<SyncMode>()), Times.Never);
            level3CacheMock.Verify(x => x.Delete(It.IsAny<string>(), It.IsAny<SyncMode>()), Times.Never);
            level3CacheMock.Verify(x => x.ClearCache(), Times.Never);
            level3CacheMock.Verify(x => x.ElementsCount(), Times.Never);
        }