public void GetAsyncCacheStore_should_return_default_store_if_cannot_get_store_adaptor() { var invocation = Substitute.For <_IInvocation>(); var context = new Dictionary <string, object> { [Global.__flatwhite_outputcache_attribute] = new OutputCacheAttribute { CacheStoreType = Substitute.For <ICacheStore>().GetType() } }; var cacheStoreProvider = Substitute.For <ICacheStoreProvider>(); cacheStoreProvider.When(x => x.GetAsyncCacheStore(Arg.Is <Type>(t => typeof(IAsyncCacheStore).IsAssignableFrom(t)))).Do(x => { throw new KeyNotFoundException(); }); cacheStoreProvider.When(x => x.GetCacheStore(Arg.Is <Type>(t => typeof(ICacheStore).IsAssignableFrom(t)))).Do(x => { throw new KeyNotFoundException(); }); Global.CacheStoreProvider = cacheStoreProvider; var stg = new DefaultCacheStrategy(); // Action var store = stg.GetAsyncCacheStore(invocation, context); // Assert cacheStoreProvider.Received(1).GetCacheStore(Arg.Is <Type>(t => t == Substitute.For <ICacheStore>().GetType())); cacheStoreProvider.Received(1).GetAsyncCacheStore(0); Assert.IsFalse(store is CacheStoreAdaptor); }
public void GetCacheStore_should_return_default_cache_store_if_not_found() { var invocation = Substitute.For <_IInvocation>(); var context = new Dictionary <string, object> { [Global.__flatwhite_outputcache_attribute] = new OutputCacheAttribute { CacheStoreId = 100, CacheStoreType = Substitute.For <ICacheStore>().GetType() } }; var cacheStoreProvider = Substitute.For <ICacheStoreProvider>(); cacheStoreProvider.GetCacheStore(Arg.Is <int>(i => i > 0)).Returns((ICacheStore)null); cacheStoreProvider.GetCacheStore(Arg.Any <Type>()).Returns((ICacheStore)null); Global.CacheStoreProvider = cacheStoreProvider; var stg = new DefaultCacheStrategy(); // Action var store = stg.GetCacheStore(invocation, context); // Assert cacheStoreProvider.Received(1).GetCacheStore(0); }
public void GetAsyncCacheStore_by_id_should_handle_Exception() { var invocation = Substitute.For <_IInvocation>(); var context = new Dictionary <string, object> { [Global.__flatwhite_outputcache_attribute] = new OutputCacheAttribute { CacheStoreId = 101, CacheStoreType = Substitute.For <IAsyncCacheStore>().GetType() } }; var cacheStoreProvider = Substitute.For <ICacheStoreProvider>(); cacheStoreProvider.When(x => x.GetAsyncCacheStore(101)).Do(x => { throw new Exception(); }); cacheStoreProvider.GetAsyncCacheStore(Arg.Any <Type>()).Returns(Substitute.For <IAsyncCacheStore>()); Global.CacheStoreProvider = cacheStoreProvider; var stg = new DefaultCacheStrategy(); // Action var store = stg.GetAsyncCacheStore(invocation, context); // Assert cacheStoreProvider.DidNotReceive().GetAsyncCacheStore(0); }
public void GetAsyncCacheStore_should_try_to_get_by_type() { var invocation = Substitute.For <_IInvocation>(); var context = new Dictionary <string, object> { [Global.__flatwhite_outputcache_attribute] = new OutputCacheAttribute { CacheStoreId = 101, CacheStoreType = Substitute.For <IAsyncCacheStore>().GetType() } }; var cacheStoreProvider = Substitute.For <ICacheStoreProvider>(); cacheStoreProvider.GetAsyncCacheStore(Arg.Any <int>()).Returns((IAsyncCacheStore)null); cacheStoreProvider.GetAsyncCacheStore(Arg.Any <Type>()).Returns(Substitute.For <IAsyncCacheStore>()); Global.CacheStoreProvider = cacheStoreProvider; var stg = new DefaultCacheStrategy(); // Action var store = stg.GetAsyncCacheStore(invocation, context); // Assert cacheStoreProvider.DidNotReceive().GetAsyncCacheStore(0); }
/// <summary> /// 获取配置文件中配置项的值 /// </summary> /// <param name="configName">配置项名称</param> /// <returns></returns> private static object GetConfig(string configName) { string objId = string.Format("{0}.Config", CacheObjectPrefix_Config); DataTable dt = null; ICacheStrategy cache = new DefaultCacheStrategy(); object obj = cache.RetrieveObject(objId); if (obj != null) { dt = obj as DataTable; } if (dt == null) { dt = LoadConfig(); if (dt.Rows[0]["TurnOnCache"].ToBoolean(true)) { cache.AddObject(objId, dt);// 写入缓存 } } return(dt.Rows[0][configName]); }
public void GetCacheStore_should_return_default_cache_store_if_id_not_found() { var invocation = Substitute.For <_IInvocation>(); var context = new Dictionary <string, object> { [Global.__flatwhite_outputcache_attribute] = new OutputCacheAttribute { CacheStoreId = 100 } }; var cacheStoreProvider = Substitute.For <ICacheStoreProvider>(); cacheStoreProvider.When(x => x.GetCacheStore(100)).Do(x => { throw new KeyNotFoundException(); }); Global.CacheStoreProvider = cacheStoreProvider; var stg = new DefaultCacheStrategy(); // Action var store = stg.GetCacheStore(invocation, context); // Assert cacheStoreProvider.Received(1).GetCacheStore(0); }
public void GetAsyncCacheStore_should_return_found_async_store_by_id() { var invocation = Substitute.For <_IInvocation>(); var context = new Dictionary <string, object> { [Global.__flatwhite_outputcache_attribute] = new OutputCacheAttribute { CacheStoreId = 100 } }; var cacheStoreProvider = Substitute.For <ICacheStoreProvider>(); cacheStoreProvider.GetAsyncCacheStore(100).Returns(Substitute.For <IAsyncCacheStore>()); Global.CacheStoreProvider = cacheStoreProvider; var stg = new DefaultCacheStrategy(); // Action var store = stg.GetAsyncCacheStore(invocation, context); // Assert cacheStoreProvider.Received(1).GetAsyncCacheStore(100); cacheStoreProvider.DidNotReceive().GetAsyncCacheStore(0); }
public void GetCacheStore_should_try_to_get_cache_store_by_provided_type() { var invocation = Substitute.For <_IInvocation>(); var context = new Dictionary <string, object> { [Global.__flatwhite_outputcache_attribute] = new OutputCacheAttribute { CacheStoreId = 100, CacheStoreType = Substitute.For <ICacheStore>().GetType() } }; var cacheStoreProvider = Substitute.For <ICacheStoreProvider>(); cacheStoreProvider.GetCacheStore(100).Returns((ICacheStore)null); Global.CacheStoreProvider = cacheStoreProvider; var stg = new DefaultCacheStrategy(); // Action var store = stg.GetCacheStore(invocation, context); // Assert cacheStoreProvider.Received(1).GetCacheStore(Arg.Is <Type>(t => t == Substitute.For <ICacheStore>().GetType())); }