static void NewSettingRegistered(AmbientSettingsOverride settingsSet, object sender, IAmbientSettingInfo setting) { // is there a value for this setting? string value; if (settingsSet._overrideRawSettings.TryGetValue(setting.Key, out value)) { // get the typed value settingsSet._overrideTypedSettings[setting.Key] = setting.Convert(settingsSet, value ?? ""); } }
public void SettingGlobalSettingsSetChangeNotification() { AmbientService <IAmbientSettingsSet> pretendGlobalSettings = new AmbientService <IAmbientSettingsSet>(); string testSettingKey = nameof(SettingGlobalSettingsSetChangeNotification); string defaultValue = "defaultValue"; string overrideValue = "overrideSettingsSetValue"; Dictionary <string, string> overrides = new Dictionary <string, string>() { { testSettingKey, overrideValue } }; string notificationNewValue = ""; AmbientSetting <string> testSetting = new AmbientSetting <string>(pretendGlobalSettings, testSettingKey, "", s => { notificationNewValue = s; return(s); }, defaultValue); Assert.AreEqual(defaultValue, testSetting.Value); AmbientSettingsOverride pretendGlobalSettingsImplementation = new AmbientSettingsOverride(overrides, nameof(SettingGlobalSettingsSetChangeNotification), null, pretendGlobalSettings); pretendGlobalSettings.Global = pretendGlobalSettingsImplementation; Assert.AreEqual(overrideValue, testSetting.Value); Assert.AreEqual(overrideValue, notificationNewValue); IMutableAmbientSettingsSet globalSettingsSet = pretendGlobalSettings.Global as IMutableAmbientSettingsSet; if (globalSettingsSet != null) { string valueChangeValue = "valueChange"; globalSettingsSet.ChangeSetting(testSettingKey, valueChangeValue); Assert.AreEqual(valueChangeValue, testSetting.Value); Assert.AreEqual(valueChangeValue, notificationNewValue); } overrides = new Dictionary <string, string>() { { testSettingKey, overrideValue } }; AmbientSettingsOverride pretendGlobalSettingsSet2 = new AmbientSettingsOverride(overrides, nameof(SettingGlobalSettingsSetChangeNotification), null, pretendGlobalSettings); pretendGlobalSettings.Global = pretendGlobalSettingsSet2; Assert.AreEqual(overrideValue, testSetting.Value); overrides[testSettingKey] = null; AmbientSettingsOverride pretendGlobalSettingsSet3 = new AmbientSettingsOverride(overrides, nameof(SettingGlobalSettingsSetChangeNotification), null, pretendGlobalSettings); pretendGlobalSettings.Global = pretendGlobalSettingsSet3; Assert.AreEqual(defaultValue, testSetting.Value); }
public async Task CacheRefresh() { AmbientSettingsOverride localSettingsSet = new AmbientSettingsOverride(TestCacheSettingsDictionary, nameof(CacheRefresh)); using (AmbientClock.Pause()) using (new ScopedLocalServiceOverride <IAmbientSettingsSet>(localSettingsSet)) { TestCache ret; IAmbientCache cache = new BasicAmbientCache(localSettingsSet); await cache.Store <TestCache>(true, "CacheRefresh1", this, TimeSpan.FromSeconds(1)); AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(1100)); ret = await cache.Retrieve <TestCache>("CacheRefresh1", null); Assert.IsNull(ret); await cache.Store <TestCache>(true, "CacheRefresh1", this, TimeSpan.FromMinutes(10)); ret = await cache.Retrieve <TestCache>("CacheRefresh1", null); Assert.AreEqual(this, ret); await Eject(cache, 1); await cache.Store <TestCache>(true, "CacheRefresh2", this); ret = await cache.Retrieve <TestCache>("CacheRefresh2", null); Assert.AreEqual(this, ret); await cache.Store <TestCache>(true, "CacheRefresh3", this); ret = await cache.Retrieve <TestCache>("CacheRefresh3", null); Assert.AreEqual(this, ret); await cache.Remove <TestCache>(true, "CacheRefresh3"); ret = await cache.Retrieve <TestCache>("CacheRefresh3", null); Assert.IsNull(ret); await Eject(cache, 1); } }
public async Task CacheSpecifiedImplementation() { AmbientSettingsOverride localSettingsSet = new AmbientSettingsOverride(TestCacheSettingsDictionary, nameof(CacheSpecifiedImplementation)); using (AmbientClock.Pause()) using (new ScopedLocalServiceOverride <IAmbientSettingsSet>(localSettingsSet)) { TestCache ret; IAmbientCache cacheService = new BasicAmbientCache(localSettingsSet); AmbientCache <TestCache> cache = new AmbientCache <TestCache>(cacheService, "prefix"); await cache.Store <TestCache>(true, "Test1", this); ret = await cache.Retrieve <TestCache>("Test1", null); Assert.AreEqual(this, ret); await cache.Remove <TestCache>(true, "Test1"); ret = await cache.Retrieve <TestCache>("Test1", null); Assert.IsNull(ret); await cache.Store <TestCache>(true, "Test2", this, null, DateTime.MinValue); ret = await cache.Retrieve <TestCache>("Test2", null); Assert.AreEqual(this, ret); await Eject(cache, 1); ret = await cache.Retrieve <TestCache>("Test2", null); Assert.IsNull(ret); await cache.Store <TestCache>(true, "Test3", this, TimeSpan.FromMinutes(-1)); ret = await cache.Retrieve <TestCache>("Test3", null); Assert.IsNull(ret); await cache.Store <TestCache>(true, "Test4", this, TimeSpan.FromMinutes(10), AmbientClock.UtcNow.AddMinutes(11)); ret = await cache.Retrieve <TestCache>("Test4", null); Assert.AreEqual(this, ret); await cache.Store <TestCache>(true, "Test5", this, TimeSpan.FromMinutes(10), AmbientClock.Now.AddMinutes(11)); ret = await cache.Retrieve <TestCache>("Test5", null); Assert.AreEqual(this, ret); await cache.Store <TestCache>(true, "Test6", this, TimeSpan.FromMinutes(60), AmbientClock.UtcNow.AddMinutes(10)); ret = await cache.Retrieve <TestCache>("Test6", null); Assert.AreEqual(this, ret); ret = await cache.Retrieve <TestCache>("Test6", TimeSpan.FromMinutes(10)); Assert.AreEqual(this, ret); await Eject(cache, 50); await cache.Clear(); ret = await cache.Retrieve <TestCache>("Test6", null); Assert.IsNull(ret); } }
public async Task CacheAmbient() { AmbientSettingsOverride localSettingsSet = new AmbientSettingsOverride(TestCacheSettingsDictionary, nameof(CacheAmbient)); using (new ScopedLocalServiceOverride <IAmbientSettingsSet>(localSettingsSet)) { IAmbientCache localOverride = new BasicAmbientCache(); using (ScopedLocalServiceOverride <IAmbientCache> localCache = new ScopedLocalServiceOverride <IAmbientCache>(localOverride)) { TestCache ret; AmbientCache <TestCache> cache = new AmbientCache <TestCache>(); await cache.Store(true, "Test1", this); await cache.Store(true, "Test1", this); ret = await cache.Retrieve <TestCache>("Test1", null); Assert.AreEqual(this, ret); await cache.Remove <TestCache>(true, "Test1"); ret = await cache.Retrieve <TestCache>("Test1", null); Assert.IsNull(ret); await cache.Store(true, "Test2", this, null, DateTime.MinValue); ret = await cache.Retrieve <TestCache>("Test2", null); Assert.AreEqual(this, ret); await Eject(cache, 2); ret = await cache.Retrieve <TestCache>("Test2", null); Assert.IsNull(ret); await cache.Store(true, "Test3", this, TimeSpan.FromMinutes(-1)); ret = await cache.Retrieve <TestCache>("Test3", null); Assert.IsNull(ret); await cache.Store(true, "Test4", this, TimeSpan.FromMinutes(10), DateTime.UtcNow.AddMinutes(11)); ret = await cache.Retrieve <TestCache>("Test4", null); Assert.AreEqual(this, ret); await cache.Store(true, "Test5", this, TimeSpan.FromMinutes(10), DateTime.Now.AddMinutes(11)); ret = await cache.Retrieve <TestCache>("Test5", null); Assert.AreEqual(this, ret); await cache.Store(true, "Test6", this, TimeSpan.FromMinutes(60), DateTime.UtcNow.AddMinutes(10)); ret = await cache.Retrieve <TestCache>("Test6", null); Assert.AreEqual(this, ret); ret = await cache.Retrieve <TestCache>("Test6", TimeSpan.FromMinutes(10)); Assert.AreEqual(this, ret); await Eject(cache, 50); await cache.Clear(); ret = await cache.Retrieve <TestCache>("Test6", null); Assert.IsNull(ret); } } }
public async Task CacheSkipAndEmptyEject() { AmbientSettingsOverride localSettingsSet = new AmbientSettingsOverride(AllowEmptyCacheSettingsDictionary, nameof(CacheAmbient)); using (AmbientClock.Pause()) using (new ScopedLocalServiceOverride <IAmbientSettingsSet>(localSettingsSet)) { IAmbientCache localOverride = new BasicAmbientCache(); using (ScopedLocalServiceOverride <IAmbientCache> localCache = new ScopedLocalServiceOverride <IAmbientCache>(localOverride)) { string keyName1 = nameof(CacheExpiration) + "1"; string keyName2 = nameof(CacheExpiration) + "2"; string keyName3 = nameof(CacheExpiration) + "3"; //string keyName4 = nameof(CacheExpiration) + "4"; //string keyName5 = nameof(CacheExpiration) + "5"; //string keyName6 = nameof(CacheExpiration) + "6"; //string keyName7 = nameof(CacheExpiration) + "7"; TestCache ret; AmbientCache <TestCache> cache = new AmbientCache <TestCache>(); await cache.Store(true, keyName1, this, TimeSpan.FromMilliseconds(100)); await cache.Store(true, keyName2, this, TimeSpan.FromMilliseconds(50)); await cache.Store(true, keyName3, this, TimeSpan.FromMilliseconds(100)); ret = await cache.Retrieve <TestCache>(keyName1); Assert.IsNotNull(ret); ret = await cache.Retrieve <TestCache>(keyName2, TimeSpan.FromMilliseconds(100)); Assert.IsNotNull(ret); ret = await cache.Retrieve <TestCache>(keyName3); Assert.IsNotNull(ret); AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(50)); await Eject(cache, 1); // this should eject 1 because it's the LRU timed, and the first timed entry for 2 because that's expired, but 2 should remain with a refreshed entry ret = await cache.Retrieve <TestCache>(keyName1); Assert.IsNull(ret); ret = await cache.Retrieve <TestCache>(keyName2, TimeSpan.FromMilliseconds(100)); Assert.IsNotNull(ret); ret = await cache.Retrieve <TestCache>(keyName3); Assert.IsNotNull(ret); await Eject(cache, 1); // this should skip 2 because it's bee refershed again and eject 3 because it's the LRU timed ret = await cache.Retrieve <TestCache>(keyName1); Assert.IsNull(ret); ret = await cache.Retrieve <TestCache>(keyName2); Assert.IsNotNull(ret); ret = await cache.Retrieve <TestCache>(keyName3); Assert.IsNull(ret); // change key2 to be untimed await cache.Store(true, keyName2, this); await Eject(cache, 1); // this should skip over the timed entry for 2 but then eject it because it is untimed } } }