public void HydratingFromMultipleThreads_IsSafe() { var numberOfHydrations = 0; var cache = new CachedValue<int>(() => ++numberOfHydrations); Assert.That(cache.Value, Is.EqualTo(1)); using (var countdownEvent = new CountdownEvent(2)) { Action threadAction = () => { cache.Invalidate(); countdownEvent.Signal(); countdownEvent.Wait(); Assert.That(cache.Value, Is.EqualTo(2)); }; var t1 = threadAction.BeginInvoke(threadAction.EndInvoke, null); var t2 = threadAction.BeginInvoke(threadAction.EndInvoke, null); WaitHandle.WaitAll(new[]{t1.AsyncWaitHandle, t2.AsyncWaitHandle}); } Assert.That(numberOfHydrations, Is.EqualTo(2)); }
public IContent Load(IPath novelPath) { var item = Items.FirstOrDefault(x => x.Source.EqualsTo(novelPath)); if (item == null) { _content.Invalidate(); } return(new EqualityNovel(new Novel(new ContentFolder(novelPath)))); }
public void Value_WhenInvalidatedAfterHydrating_HydratesAgain() { var numberOfHydrations = 0; var cache = new CachedValue<int>(() => ++numberOfHydrations); Assert.That(cache.Value, Is.EqualTo(1)); cache.Invalidate(); Assert.That(cache.Value, Is.EqualTo(2)); Assert.That(numberOfHydrations, Is.EqualTo(2)); }
public void Save(string content) { _file.Save(content); _content.Invalidate(); }