public async Task Get_New_Value_When_Delay_Is_Not_Expired_And_Cache_Was_Flushed() { var name = DateTime.UtcNow.ToShortDateString(); var cache = new InMemoryCache(name, CacheDurationInSeconds); cache.Size.Should().Be(0); var xx = await cache.AddOrGetAsync("foo", async() => { await Task.Delay(RandomDelay()); return(10); }).ConfigureAwait(false); cache.Size.Should().Be(1); xx.Should().Be(10); await Task.Delay(RandomDelay()); await cache.ClearCacheEntriesAsync(); cache.Size.Should().Be(0); var yy = await cache.AddOrGetAsync("foo", async() => { await Task.Delay(RandomDelay()); return(20); }).ConfigureAwait(false); cache.Size.Should().Be(1); yy.Should().Be(20); }
public async Task Should_Not_Throw_Exception_When_Cache_Is_Flushed_While_Is_Used_V3() { var name = DateTime.UtcNow.ToShortDateString(); var cache = new InMemoryCache(name, CacheDurationInSeconds); var key = Guid.NewGuid().ToString(); Task <int> Func1() => cache.AddOrGetAsync(key, async() => { await Task.Delay(RandomDelay()); return(10); }); var task1 = Task.Factory.StartNew(Func1, TaskCreationOptions.LongRunning); Task <int> Func2() => cache.AddOrGetAsync(key, async() => { await Task.Delay(RandomDelay()); return(20); }); var task2 = Task.Factory.StartNew(Func2, TaskCreationOptions.LongRunning); Task <int> Func3() => cache.AddOrGetAsync(key, async() => { await Task.Delay(RandomDelay()); return(30); }); var task3 = Task.Factory.StartNew(Func3, TaskCreationOptions.LongRunning); await Task.WhenAll(task1, task2, task3); cache.Size.Should().Be(1); }
private static Task[] RandomCacheTasks() { var name = DateTime.UtcNow.ToShortDateString(); var cache = new InMemoryCache(name, CacheDurationInSeconds); var tasks1 = Enumerable.Range(1, 10) .Select(x => cache.AddOrGetAsync($"foo-{x}", async() => { await Task.Delay(RandomDelay()); return(x); })); var tasks2 = Enumerable.Range(1, 10) .Select(x => cache.AddOrGetAsync($"bar-{x}", async() => { await Task.Delay(RandomDelay()); return(x); })); var tasks3 = Enumerable.Range(1, 10) .Select(_ => Task.Run(async() => await cache.ClearCacheEntriesAsync())); var tasks4 = Enumerable.Range(1, 10) .Select(_ => Task.Delay(RandomDelay())); var array = tasks1.Union(tasks2).Union(tasks3).Union(tasks4); return(array.OrderBy(_ => Random.Next()).ToArray()); }
public async Task Should_Not_Throw_Exception_When_Cache_Is_Flushed_While_Is_Used_V2() { var name = DateTime.UtcNow.ToShortDateString(); var cache = new InMemoryCache(name, CacheDurationInSeconds); async Task FuncBody(int index) { var key = Guid.NewGuid().ToString(); var xx = await cache.AddOrGetAsync(key, async() => { await Task.Delay(RandomDelay()); return(10); }); xx.Should().Be(10); await Task.Run(async() => { await Task.Delay(RandomDelay()); await cache.ClearCacheEntriesAsync(); }); var yy = await cache.AddOrGetAsync(key, async() => { await Task.Delay(RandomDelay()); return(20); }); yy.Should().Be(20); } var source = Enumerable.Range(1, 30); await ParallelForEachAsync(source, FuncBody, 5); cache.Size.Should().BeGreaterThan(0); }