예제 #1
0
        public void TestMultiCacheWith3ItemsThen5()
        {
            Assert.AreEqual(0, cache1.Hits);
            Assert.AreEqual(0, cache1.Misses);

            var service  = new MultiService();
            var results1 = service.GetItems(new[] { "k1", "k2", "k3" });

            Assert.AreEqual(0, cache1.Hits);
            Assert.AreEqual(3, cache1.Misses);
            LinqAssert.Count(results1, 3);
            LinqAssert.Any(results1, r => r.Id == "k1");
            LinqAssert.Any(results1, r => r.Id == "k2");
            LinqAssert.Any(results1, r => r.Id == "k3");

            var results2 = service.GetItems(new[] { "k5", "k2", "k1", "k4", "k3" });

            Assert.AreEqual(3, cache1.Hits);
            Assert.AreEqual(5, cache1.Misses);
            LinqAssert.Count(results2, 5);
            LinqAssert.Any(results2, r => r.Id == "k1");
            LinqAssert.Any(results2, r => r.Id == "k2");
            LinqAssert.Any(results2, r => r.Id == "k3");
            LinqAssert.Any(results2, r => r.Id == "k4");
            LinqAssert.Any(results2, r => r.Id == "k5");
        }
예제 #2
0
        public void TestMongoDbCacheObjectMutated()
        {
            var cache = new MongoDbCache(
                "cache1",
                new MongoDbCachePolicy
            {
                DatabaseName = "TestCacheDb",
            });

            cache.ClearAll();

            List <string> value = new List <string> {
                "1"
            };

            Func <IEnumerable <object> > getter = () => { return(value); };

            IEnumerable <object> result;

            result = cache.Get("key", getter);
            LinqAssert.AreSame(new object[] { "1" }, result);

            value.Add("2");

            result = cache.Get("key", getter);
            LinqAssert.AreSame(new object[] { "1" }, result);
        }
예제 #3
0
        public void TestNamedCache3()
        {
            Assert.AreEqual(0, cache2.Hits);
            Assert.AreEqual(0, cache2.Misses);

            var service = new Service2();
            IEnumerable <string> result;

            result = service.MethodToCache1(2.0);
            Assert.AreEqual(0, cache2.Hits);
            Assert.AreEqual(2, cache2.Misses);
            LinqAssert.AreSame(new[] { "0.9", "1.9" }, result);

            result = service.MethodToCache1(2.0);
            Assert.AreEqual(1, cache2.Hits);
            Assert.AreEqual(2, cache2.Misses);
            LinqAssert.AreSame(new[] { "0.9", "1.9" }, result);

            result = service.MethodToCache1(2);
            Assert.AreEqual(1, cache2.Hits);
            Assert.AreEqual(4, cache2.Misses);
            LinqAssert.AreSame(new[] { "1", "2" }, result);

            result = service.MethodToCache1(2);
            Assert.AreEqual(2, cache2.Hits);
            Assert.AreEqual(4, cache2.Misses);
            LinqAssert.AreSame(new[] { "1", "2" }, result);
        }
예제 #4
0
        public void TestMultiCacheWith3ItemsGuidKey()
        {
            Assert.AreEqual(0, cache1.Hits);
            Assert.AreEqual(0, cache1.Misses);

            var id1 = Guid.NewGuid();
            var id2 = Guid.NewGuid();
            var id3 = Guid.NewGuid();

            var service = new MultiService2();
            var results = service.GetItems(new[] { id1, id2, id3 });

            Assert.AreEqual(0, cache1.Hits);
            Assert.AreEqual(3, cache1.Misses);
            LinqAssert.Count(results, 3);
            LinqAssert.Any(results, r => r.Id == id1);
            LinqAssert.Any(results, r => r.Id == id2);
            LinqAssert.Any(results, r => r.Id == id3);

            results = service.GetItems(new[] { id1, id2, id3 });

            Assert.AreEqual(3, cache1.Hits);
            Assert.AreEqual(3, cache1.Misses);
            LinqAssert.Count(results, 3);
            LinqAssert.Any(results, r => r.Id == id1);
            LinqAssert.Any(results, r => r.Id == id2);
            LinqAssert.Any(results, r => r.Id == id3);
        }
예제 #5
0
        public void TestNamedCache2()
        {
            var service = new Service2();
            var result  = service.MethodToCache1(2);

            LinqAssert.AreSame(new [] { "1", "2" }, result);
            LinqAssert.AreSame(new[] { "1", "2" }, result);
        }
예제 #6
0
        public void TestMultiCacheWith1Items()
        {
            Assert.AreEqual(0, cache1.Hits);
            Assert.AreEqual(0, cache1.Misses);

            var service = new MultiService();
            var results = service.GetItems(new[] { "k1" });

            Assert.AreEqual(0, cache1.Hits);
            Assert.AreEqual(1, cache1.Misses);
            LinqAssert.Count(results, 1);
        }
예제 #7
0
        public void TestReadAppConfig()
        {
            var config = ConfigurationManager.GetSection("PubComp/CacheConfig") as IList <ConfigNode>;

            Assert.IsNotNull(config);

            Assert.AreEqual(15, config.Count);
            LinqAssert.Count(config.OfType <CacheConfig>(), 4);
            LinqAssert.Count(config.OfType <RemoveConfig>(), 5);
            LinqAssert.Count(config.OfType <ConnectionStringConfig>(), 4);
            LinqAssert.Count(config.OfType <NotifierConfig>(), 2);

            LinqAssert.Any(config, c =>
                           c.Action == ConfigAction.Add &&
                           c.Name == "cacheFromConfig1" &&
                           c is CacheConfig);

            LinqAssert.Any(config, c =>
                           c.Action == ConfigAction.Add &&
                           c.Name == "cacheFromConfig2" &&
                           c is NoCacheConfig);

            LinqAssert.Any(config, c =>
                           c.Action == ConfigAction.Add &&
                           c.Name == "cacheFromConfig2" &&
                           c is MockNoCacheConfig &&
                           ((MockNoCacheConfig)c).Policy != null &&
                           ((MockNoCacheConfig)c).Policy.SlidingExpiration.HasValue &&
                           ((MockNoCacheConfig)c).Policy.SlidingExpiration?.Minutes == 15 &&
                           ((MockNoCacheConfig)c).Policy.AbsoluteExpiration.HasValue == false &&
                           ((MockNoCacheConfig)c).Policy.ExpirationFromAdd.HasValue == false);

            LinqAssert.Any(config, c =>
                           c.Action == ConfigAction.Remove &&
                           c.Name == "cacheFromConfig2" &&
                           c is RemoveConfig);

            LinqAssert.Any(config, c =>
                           c.Action == ConfigAction.Add &&
                           c.Name == "cacheFromConfig3" &&
                           c is CacheConfig &&
                           ((CacheConfig)c).CreateCache() is NoCache);

            LinqAssert.Any(config, c =>
                           c.Action == ConfigAction.Remove &&
                           c.Name == "cacheFromConfig3" &&
                           c is RemoveConfig);

            LinqAssert.Any(config, c =>
                           c.Action == ConfigAction.Remove &&
                           c.Name == "cacheFromConfig4" &&
                           c is RemoveConfig);
        }
예제 #8
0
        public void TestKeyGeneration_CacheList()
        {
            Assert.AreEqual(0, cache1.Hits);
            Assert.AreEqual(0, cache1.Misses);

            var service = new MultiService();
            var results = service.GetItems(new[] { "k1", "k2", "k3" });

            Assert.AreEqual(0, cache1.Hits);
            Assert.AreEqual(3, cache1.Misses);
            LinqAssert.Count(results, 3);
            LinqAssert.Any(results, r => r.Id == "k1");
            LinqAssert.Any(results, r => r.Id == "k2");
            LinqAssert.Any(results, r => r.Id == "k3");

            results = service.GetItems(new[] { "k1", "k2", "k3" });

            Assert.AreEqual(3, cache1.Hits);
            Assert.AreEqual(3, cache1.Misses);
            LinqAssert.Count(results, 3);
            LinqAssert.Any(results, r => r.Id == "k1");
            LinqAssert.Any(results, r => r.Id == "k2");
            LinqAssert.Any(results, r => r.Id == "k3");

            // Note: must use List<string>, not string[] here in order to get correct key
            var key = CacheKey.GetKey(() => service.GetItems(new List <string> {
                "k2"
            }));

            cache1.Clear(key);

            results = service.GetItems(new[] { "k1" });
            Assert.AreEqual(4, cache1.Hits);
            Assert.AreEqual(3, cache1.Misses);
            LinqAssert.Count(results, 1);
            LinqAssert.Any(results, r => r.Id == "k1");

            results = service.GetItems(new[] { "k2" });
            Assert.AreEqual(4, cache1.Hits);
            Assert.AreEqual(4, cache1.Misses);
            LinqAssert.Count(results, 1);
            LinqAssert.Any(results, r => r.Id == "k2");

            results = service.GetItems(new[] { "k1", "k2", "k3" });

            Assert.AreEqual(7, cache1.Hits);
            Assert.AreEqual(4, cache1.Misses);
            LinqAssert.Count(results, 3);
            LinqAssert.Any(results, r => r.Id == "k1");
            LinqAssert.Any(results, r => r.Id == "k2");
            LinqAssert.Any(results, r => r.Id == "k3");
        }
예제 #9
0
        public void TestReadAppConfig()
        {
            var config = ConfigurationManager.GetSection("PubComp/CacheConfig") as IList <CacheConfig>;

            Assert.IsNotNull(config);
            Assert.AreEqual(7, config.Count);

            LinqAssert.Any(config, c =>
                           c.Action == ConfigAction.Add &&
                           c.Name == "cacheFromConfig1" &&
                           c is NoCacheConfig);

            LinqAssert.Any(config, c =>
                           c.Action == ConfigAction.Add &&
                           c.Name == "cacheFromConfig2" &&
                           c is NoCacheConfig);

            LinqAssert.Any(config, c =>
                           c.Action == ConfigAction.Remove &&
                           c.Name == "cacheFromConfig2" &&
                           c is NoCacheConfig);

            LinqAssert.Any(config, c =>
                           c.Action == ConfigAction.Add &&
                           c.Name == "cacheFromConfig2" &&
                           c is MockNoCacheConfig &&
                           ((MockNoCacheConfig)c).Policy != null &&
                           ((MockNoCacheConfig)c).Policy.SlidingExpiration.HasValue &&
                           ((MockNoCacheConfig)c).Policy.SlidingExpiration.Value.Minutes == 15 &&
                           ((MockNoCacheConfig)c).Policy.AbsoluteExpiration.HasValue == false &&
                           ((MockNoCacheConfig)c).Policy.ExpirationFromAdd.HasValue == false);

            LinqAssert.Any(config, c =>
                           c.Action == ConfigAction.Add &&
                           c.Name == "cacheFromConfig3" &&
                           c is NoCacheConfig);

            LinqAssert.Any(config, c =>
                           c.Action == ConfigAction.Remove &&
                           c.Name == "cacheFromConfig3" &&
                           c is NoCacheConfig);

            LinqAssert.Any(config, c =>
                           c.Action == ConfigAction.Remove &&
                           c.Name == "cacheFromConfig4" &&
                           c is NoCacheConfig);
        }
        public void TestInMemorySerializedCacheObjectMutated()
        {
            var cache = new InMemorySerializedCache("cache1", new TimeSpan(0, 2, 0));

            List <string> value = new List <string> {
                "1"
            };

            Func <IEnumerable <object> > getter = () => { return(value); };

            IEnumerable <object> result;

            result = cache.Get("key", getter);
            LinqAssert.AreSame(new object[] { "1" }, result);

            value.Add("2");

            result = cache.Get("key", getter);
            LinqAssert.AreSame(new object[] { "1" }, result);
        }