public void TestDeserializeCacheFail() { string id = "DeserializeCacheFail"; string uniqueName = CacheEntry.BuildUniqueName(typeof(ShortCacheObject), new LoadContext(id)); var cii = new CacheItemInfo(uniqueName, DateTime.Now, DateTime.Now.AddSeconds(10)); MemoryStream ms = new MemoryStream(); var data = Encoding.UTF8.GetBytes("garbage"); ms.Write(data, 0, data.Length); DataManager.StoreProvider.Write(cii, data); var val = (int)DateTime.Now.Ticks; ShortCacheObject.DefaultIntValue = val; DataManager.Current.Load <ShortCacheObject>(id, (sco) => { ShortCacheObject.DefaultIntValue = 1234; Assert.AreEqual(val, sco.IntProp); TestComplete(); }, (ex) => { Assert.Fail(); TestComplete(); } ); }
public void TestLoadWithExpiredCache() { string uniqueName = CacheEntry.BuildUniqueName(typeof(ShortCacheObject), new LoadContext("ExpiredItem")); var cii = new CacheItemInfo(uniqueName, DateTime.Now, DateTime.Now.AddSeconds(-10)); DataManager.StoreProvider.Write(cii, ShortCacheObject.SCOLoadRequest.WriteToStream("ExpiredItemValue", -1).GetBuffer()); Thread.Sleep(100); // let the write happen; // slow down the load so we get the cache value. // var oldTime = ShortCacheObject.SCOLoadRequest.LoadTimeMs; ShortCacheObject.SCOLoadRequest.LoadTimeMs = 1000; DataManager.Current.Load <ShortCacheObject>("ExpiredItem", (v) => { ShortCacheObject.SCOLoadRequest.LoadTimeMs = oldTime; Assert.IsNotNull(v); Assert.AreEqual("ExpiredItemValue", v.StringProp); TestComplete(); }, (ex) => { Assert.Fail(ex.Message); TestComplete(); }); }
public void TestRefreshWithValidCache() { IUpdatable val = null; LoadContext lc = new LoadContext(ShortCacheObject.DefaultIdentifier); DataManager.Current.Clear <ShortCacheObject>(lc); // write the cache entry // string uniqueName = CacheEntry.BuildUniqueName(typeof(ShortCacheObject), lc); var cii = new CacheItemInfo(uniqueName, DateTime.Now, DateTime.Now.AddMinutes(1)); var t = DateTime.Now.ToString(); DataManager.StoreProvider.Write(cii, ShortCacheObject.SCOLoadRequest.WriteToStream(t, -1).GetBuffer()); string oldDefault = ShortCacheObject.DefaultStringValue; ShortCacheObject.DefaultStringValue = DateTime.Now.Ticks.ToString(); val = DataManager.Current.Refresh <ShortCacheObject>(lc, (v) => { // we've got a value Assert.AreEqual(ShortCacheObject.DefaultStringValue, v.StringProp); ShortCacheObject.DefaultStringValue = oldDefault; TestComplete(); }, (ex) => { Assert.Fail(ex.Message); TestComplete(); }); }
public void TestLoadFromCache() { // populate the cache with an item. // string uniqueName = CacheEntry.BuildUniqueName(typeof(ShortCacheObject), new LoadContext("LFC")); var cii = new CacheItemInfo(uniqueName, DateTime.Now, DateTime.Now.AddHours(1)); DataManager.StoreProvider.Write(cii, ShortCacheObject.SCOLoadRequest.WriteToStream("LoadFromCache", -1).GetBuffer()); Thread.Sleep(250); // let the write happen; // sync load that value. // var value = DataManager.Current.LoadFromCache <ShortCacheObject>("LFC"); Assert.AreEqual("LoadFromCache", value.StringProp); Assert.AreEqual(-1, value.IntProp); }
public void TestInvalidateFromCache() { LoadContext lc = new LoadContext("InvalidateFromCache"); var time = DateTime.Now.ToString(); // write the cache entry // string uniqueName = CacheEntry.BuildUniqueName(typeof(ShortCacheObject), lc); var cii = new CacheItemInfo(uniqueName, DateTime.Now, DateTime.Now.AddHours(1)); DataManager.StoreProvider.Write(cii, ShortCacheObject.SCOLoadRequest.WriteToStream(time, -1).GetBuffer()); Thread.Sleep(250); TestInvalidateCore(lc, time); }
public void TestCleanup() { var dval = DateTime.Now.ToString(); string uniqueName = CacheEntry.BuildUniqueName(typeof(TestPoco), new LoadContext("foo")); DateTime timestamp = DateTime.Now.AddDays(-2); var cii = new CacheItemInfo(uniqueName, timestamp, timestamp); DataManager.StoreProvider.Write(cii, UTF8Encoding.UTF8.GetBytes(dval)); DataManager.Current.Cleanup(DateTime.Now.AddDays(-1), () => { var item = DataManager.StoreProvider.GetLastestExpiringItem(uniqueName); Assert.IsNull(item); TestComplete(); } ); }
public void TestRefreshBeforeCacheExpires() { IUpdatable val = null; LoadContext lc = new LoadContext(ShortCacheObject.DefaultIdentifier); // write the cache entry // string uniqueName = CacheEntry.BuildUniqueName(typeof(ShortCacheObject), lc); var cii = new CacheItemInfo(uniqueName, DateTime.Now, DateTime.Now.AddHours(1)); var t = DateTime.Now.ToString(); DataManager.StoreProvider.Write(cii, ShortCacheObject.SCOLoadRequest.WriteToStream(t, -1).GetBuffer()); val = DataManager.Current.Load <ShortCacheObject>(lc, (v) => { string oldDefault = ShortCacheObject.DefaultStringValue; // we've got a value Assert.AreEqual(t, v.StringProp); ShortCacheObject.DefaultStringValue = DateTime.Now.ToString(); // now request a new value via refresh. // DataManager.Current.Refresh <ShortCacheObject>(ShortCacheObject.DefaultIdentifier, (v2) => { Assert.AreEqual(v2.StringProp, ShortCacheObject.DefaultStringValue); ShortCacheObject.DefaultStringValue = oldDefault; TestComplete(); }, (ex2) => { Assert.Fail(ex2.Message); TestComplete(); } ); }, (ex) => { Assert.Fail(ex.Message); TestComplete(); }); }
public void TestDataLoaderCacheLoad() { var id = "TestPocoCache"; var dval = DateTime.Now.ToString(); string uniqueName = CacheEntry.BuildUniqueName(typeof(TestPoco), new LoadContext(id)); var cii = new CacheItemInfo(uniqueName, DateTime.Now, DateTime.Now.AddSeconds(10)); DataManager.StoreProvider.Write(cii, UTF8Encoding.UTF8.GetBytes(dval)); Thread.Sleep(250); // let the write happen; DataManager.Current.Load <TestPoco>(id, (tp2) => { Assert.AreEqual(dval, tp2.Value); TestComplete(); }, (ex) => { Assert.Fail(); TestComplete(); } ); }
private void TestValidCacheCore(string cachedValue, string newValue, string expectedValue, int secondsUntilCacheExpires) { string uniqueName = CacheEntry.BuildUniqueName(typeof(ValidCacheOnlyObject), new LoadContext("VCO")); var cii = new CacheItemInfo(uniqueName, DateTime.Now, DateTime.Now.AddSeconds(secondsUntilCacheExpires)); DataManager.StoreProvider.Write(cii, ShortCacheObject.SCOLoadRequest.WriteToStream(cachedValue, -1).GetBuffer()); Thread.Sleep(100); // sleep to let the write happen; ShortCacheObject.DefaultStringValue = newValue; DataManager.Current.Load <ValidCacheOnlyObject>("VCO", (v1) => { Assert.AreEqual(expectedValue, v1.StringProp); TestComplete(); }, (ex) => { Assert.Fail(); TestComplete(); }); }