public async Task LazyToAsyncLazy() { var lazy = new AtomicLazy <string>(() => "hello"); var result = await lazy.ToAsync().Value; result.ShouldBe("hello"); }
public void AsyncLazyToLazy() { var lazy = new AtomicLazy <Task <string> >(() => Task.FromResult("hello")); var result = lazy.FromAsync().Value; result.ShouldBe("hello"); }
public async Task TestWithSlowFactory() { var counter = 0; var lazy = new AtomicLazy <string>(() => { Interlocked.Increment(ref counter); return(Delay("result")); }); await Simultaneously.Run(100, () => lazy.Value); lazy.Value.ShouldBe("result"); counter.ShouldBe(1); }
public Task SaveAllContentItemsAsync(string cultureCode, IEnumerable <ContentItem> items) { if (items is null) { throw new ArgumentNullException(nameof(items)); } //this updates the dictionary with an already initialized lazy loader var dict = new ConcurrentDictionary <string, ContentItem>(); foreach (var item in items) { dict.TryAdd(item.Name, item); } var val = new AtomicLazy <ConcurrentDictionary <string, ContentItem> >(dict); _cache.AddOrUpdate(cultureCode, val, (key, oldValue) => val); return(Task.CompletedTask); }
public void TestWithThrow() { var failedOnce = false; var lazy = new AtomicLazy <string>(() => { if (failedOnce) { return("success"); } else { failedOnce = true; throw new Exception("Failed once"); } }); Should.Throw <Exception>(() => lazy.Value); lazy.Value.ShouldBe("success"); }
public void TestWithFactory() { var lazy = new AtomicLazy <string>(() => "result"); lazy.Value.ShouldBe("result"); }
public void TestWithInitialValue() { var lazy = new AtomicLazy <string>("result"); lazy.Value.ShouldBe("result"); }