public async Task LoadTest(int uniqueKeys, int maxRequests, int maxDelay, int maxBatchSize, bool caching, bool batching) { // arrange var random = new Random(); FetchDataDelegate <Guid, int> fetch = async(keys, cancellationToken) => { var values = new List <Result <int> >(keys.Count); foreach (Guid key in keys) { var value = random.Next(1, maxRequests); values.Add(value); } var delay = random.Next(maxDelay); await Task.Delay(delay); return(values); }; var options = new DataLoaderOptions <Guid> { Caching = caching, Batch = batching, MaxBatchSize = maxBatchSize }; var batchScheduler = new ManualBatchScheduler(); var loader = new DataLoader <Guid, int>(batchScheduler, fetch, options); var keyArray = new Guid[uniqueKeys]; for (var i = 0; i < keyArray.Length; i++) { keyArray[i] = Guid.NewGuid(); } var requests = new Task <int> [maxRequests]; // act for (var i = 0; i < maxRequests; i++) { requests[i] = Task.Factory.StartNew(async() => { var index = random.Next(uniqueKeys); var delay = random.Next(maxDelay); await Task.Delay(delay); return(await loader.LoadAsync(keyArray[index])); }, TaskCreationOptions.RunContinuationsAsynchronously) .Unwrap(); } while (requests.Any(task => !task.IsCompleted)) { await Task.Delay(25); batchScheduler.Dispatch(); } // assert var responses = await Task.WhenAll(requests); foreach (var response in responses) { Assert.True(response > 0); } }
public async Task VerifyEvents() { var listener = new TestListener(); var observer = new TestObserver(listener); using (DiagnosticListener.AllListeners.Subscribe(observer)) { // arrange var batchOptions = new DataLoaderOptions <string> { AutoDispatching = true, Batching = true, BatchRequestDelay = TimeSpan.FromMilliseconds(150), Caching = true }; var batchLoader = new DataLoader <string, string>( batchOptions, FetchDataAsync); var batchErrorLoader = new DataLoader <string, string>( batchOptions, (keys, canncellationToken) => throw new Exception("BatchError: Foo")); var singleOptions = new DataLoaderOptions <string> { AutoDispatching = true, Batching = false, Caching = true }; var singleLoader = new DataLoader <string, string>( singleOptions, FetchDataAsync); // act await Catch(() => batchLoader.LoadAsync("Foo")) .ConfigureAwait(false); await Task.Delay(400).ConfigureAwait(false); await Catch(() => batchLoader.LoadAsync("Foo", "Bar")) .ConfigureAwait(false); await Task.Delay(400).ConfigureAwait(false); await Catch(() => batchLoader.LoadAsync("Bar", "Baz")) .ConfigureAwait(false); await Task.Delay(400).ConfigureAwait(false); await Catch(() => batchLoader.LoadAsync("Qux")) .ConfigureAwait(false); await Task.Delay(400).ConfigureAwait(false); await Catch(() => batchErrorLoader.LoadAsync("Foo")) .ConfigureAwait(false); await Task.Delay(400).ConfigureAwait(false); await Catch(() => singleLoader.LoadAsync("Foo")) .ConfigureAwait(false); await Task.Delay(400).ConfigureAwait(false); await Catch(() => singleLoader.LoadAsync("Foo", "Bar")) .ConfigureAwait(false); await Task.Delay(400).ConfigureAwait(false); await Catch(() => singleLoader.LoadAsync("Bar", "Baz")) .ConfigureAwait(false); await Task.Delay(400).ConfigureAwait(false); await Catch(() => singleLoader.LoadAsync("Qux")) .ConfigureAwait(false); // assert listener.Snapshot(); } }
public async Task LoadTest(int uniqueKeys, int maxRequests, int maxDelay, int maxBatchSize, bool caching, bool batching, int slidingExpirationInMilliseconds) { // arrange var random = new Random(); FetchDataDelegate <Guid, int> fetch = async keys => { var values = new List <IResult <int> >(keys.Count); foreach (Guid key in keys) { var value = random.Next(1, maxRequests); values.Add(Result <int> .Resolve(value)); } var delay = random.Next(maxDelay); await Task.Delay(delay).ConfigureAwait(false); return(values); }; TimeSpan slidingExpiration = (slidingExpirationInMilliseconds > 0) ? TimeSpan.FromMilliseconds(slidingExpirationInMilliseconds) : TimeSpan.Zero; var options = new DataLoaderOptions <Guid> { Caching = caching, Batching = batching, MaxBatchSize = maxBatchSize, SlidingExpiration = slidingExpiration }; var dataLoader = new DataLoader <Guid, int>(options, fetch); var keyArray = new Guid[uniqueKeys]; for (var i = 0; i < keyArray.Length; i++) { keyArray[i] = Guid.NewGuid(); } var requests = new Task <int> [maxRequests]; // act for (var i = 0; i < maxRequests; i++) { requests[i] = Task.Factory.StartNew(async() => { var index = random.Next(uniqueKeys); var delay = random.Next(maxDelay); await Task.Delay(delay).ConfigureAwait(false); return(await dataLoader.LoadAsync(keyArray[index]) .ConfigureAwait(false)); }, TaskCreationOptions.RunContinuationsAsynchronously) .Unwrap(); } // assert var responses = await Task.WhenAll(requests).ConfigureAwait(false); foreach (var response in responses) { Assert.True(response > 0); } }