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); }
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); }
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); }
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); }
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); }
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()); }