Example #1
0
        public async Task NonBlockingGetValueWorkWithAsyncFactory()
        {
            var lazy  = new AsyncLazy.Lazy <int>(async() => { await Task.Delay(10); return(42); });
            var value = await lazy.GetValueAsync();

            value.Should().Be(42);
        }
Example #2
0
        public async Task NonBlockingGetValueWorkWithNormalFactory()
        {
            var lazy  = new AsyncLazy.Lazy <int>(() => 42);
            var value = await lazy.GetValueAsync();

            value.Should().Be(42);
        }
Example #3
0
        public async Task FactoryOnlyCalledOnceInMultiTaskContext()
        {
            int         counter = 0;
            var         lazy    = new AsyncLazy.Lazy <int>(() => ++ counter);
            List <Task> tasks   = new List <Task>();

            for (int i = 0; i < 10; i++)
            {
                tasks.Add(Task.Run(async() =>
                {
                    await Task.Delay(10);
                    return((await lazy.GetValueAsync()).Should().Be(1));
                }));
            }
            await Task.WhenAll(tasks);
        }