Example #1
0
        public async Task LazyToAsyncLazy()
        {
            var lazy   = new AtomicLazy <string>(() => "hello");
            var result = await lazy.ToAsync().Value;

            result.ShouldBe("hello");
        }
Example #2
0
        public void AsyncLazyToLazy()
        {
            var lazy   = new AtomicLazy <Task <string> >(() => Task.FromResult("hello"));
            var result = lazy.FromAsync().Value;

            result.ShouldBe("hello");
        }
Example #3
0
        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);
        }
Example #4
0
        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);
        }
Example #5
0
        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");
        }
Example #6
0
        public void TestWithFactory()
        {
            var lazy = new AtomicLazy <string>(() => "result");

            lazy.Value.ShouldBe("result");
        }
Example #7
0
        public void TestWithInitialValue()
        {
            var lazy = new AtomicLazy <string>("result");

            lazy.Value.ShouldBe("result");
        }