예제 #1
0
 public void GetOrAddWithDifferentInstance()
 {
     _itemPolicyMock.Setup(x => x.Create<One>()).Returns(new CacheItemPolicy()).Verifiable();
     var cacher1 = new MemoryCacher<One>(_itemPolicyMock.Object, _objectCacheFactory.Object, typeof (One).Name);
     var cacher2 = new MemoryCacher<One>(_itemPolicyMock.Object, _objectCacheFactory.Object, typeof (One).Name);
     var exp = new One();
     var key = "1sdrsdhsryjtujuw54hrt5nfgmdrhsdr";
     Func<string, One> valueFactory = s =>
     {
         Assert.AreEqual(key, s);
         return exp;
     };
     _objectCacheFactory.Setup(x => x.Create()).Returns(MemoryCache.Default).Verifiable();
     var actual1 = cacher1.GetOrAdd(key, valueFactory);
     var actual2 = cacher2.GetOrAdd(key, s =>
     {
         Assert.Fail("Дважды вызвана фабрика");
         return null;
     });
     Assert.AreEqual(exp, actual1);
     Assert.AreEqual(exp, actual2);
     Assert.AreEqual(actual1, actual2);
 }
예제 #2
0
        public void TryGetFoundResultTest()
        {
            const string prefix = "123123aawdawdsegsegsegawdf";
            const string key = "321awdawawdasefgsfgsegsegwd";

            var mCache = MemoryCache.Default;
            _objectCacheFactory.Setup(x => x.Create()).Returns(mCache).Verifiable();
            var fullKey = FullKey(prefix, key);
            var exp = new One();
            mCache.Add(fullKey, exp, DateTimeOffset.MaxValue);
            var cacher = new MemoryCacher<One>(_itemPolicyMock.Object, _objectCacheFactory.Object, prefix);
            One actual;
            var tryGetResult = cacher.TryGet(key, out actual);
            Assert.AreEqual(CacheTryGetResult.Found, tryGetResult);
            Assert.AreEqual(exp, actual);
        }
예제 #3
0
        public void TryGetNullResultTest()
        {
            const string prefix = "123123aawdawdawdf";
            const string key = "321awdawawdawd";

            var mCache = MemoryCache.Default;
            _objectCacheFactory.Setup(x => x.Create()).Returns(mCache).Verifiable();

            var cacher = new MemoryCacher<One>(_itemPolicyMock.Object, _objectCacheFactory.Object, prefix);
            One actual;
            var tryGetResult = cacher.TryGet(key, out actual);
            Assert.AreEqual(CacheTryGetResult.NotFound, tryGetResult);
            Assert.IsNull(actual);
        }
예제 #4
0
        public void RemoveTest()
        {
            const string prefix = "123123af";
            const string key = "321awdaw";

            var mCache = MemoryCache.Default;
            _objectCacheFactory.Setup(x => x.Create()).Returns(mCache).Verifiable();
            var fullKey = FullKey(prefix, key);
            var exp = new One();
            var resExp = mCache.AddOrGetExisting(fullKey, exp, new CacheItemPolicy());
            var actual = mCache.Get(fullKey);
            Assert.IsNull(resExp);
            Assert.AreEqual(exp, actual);

            var cacher = new MemoryCacher<One>(_itemPolicyMock.Object, _objectCacheFactory.Object, prefix);
            cacher.Remove(key);
            var after = mCache.Get(fullKey);
            Assert.IsNull(after);
        }
예제 #5
0
        public void GetOrAddWithNullTest()
        {
            _itemPolicyMock.Setup(x => x.Create<One>()).Returns(new CacheItemPolicy()).Verifiable();
            var cacher = new MemoryCacher<One>(_itemPolicyMock.Object, _objectCacheFactory.Object, typeof (One).Name);
            Func<string, One> valueFactory = s => null;
            const string key = "100sfgsdgrsdg";
            _objectCacheFactory.Setup(x => x.Create()).Returns(MemoryCache.Default).Verifiable();
            var actual1 = cacher.GetOrAdd(key, valueFactory);
            var actual2 = cacher.GetOrAdd(key, s =>
            {
                Assert.Fail("Дважды вызвана фабрика");
                return null;
            });

            Assert.IsNull(actual1);
            Assert.IsNull(actual2);
        }
예제 #6
0
        public void GetOrAddWithCastExceptionTest()
        {
            const string prefix = "123";
            const string key = "321";

            var cacher = new MemoryCacher<One>(_itemPolicyMock.Object, _objectCacheFactory.Object, prefix);

            var mCache = MemoryCache.Default;
            _objectCacheFactory.Setup(x => x.Create()).Returns(mCache).Verifiable();
            var fullKey = FullKey(prefix, key);
            mCache.Add(fullKey, "123", new CacheItemPolicy());
            cacher.GetOrAdd(key, s => new One());
        }