public async Task StartAsync_processes_handle_proxy_delegates(int concurrency, int delegatesNumber) { var channel = Channel.CreateUnbounded <HandleProxy>(); var configuration = new BackgroundHandleProxyProcessorConfiguration(concurrency); var logger = Mock.Of <ILogger <BackgroundHandleProxyProcessor> >(); var processor = new BackgroundHandleProxyProcessor(channel.Reader, configuration, logger); _ = processor.StartAsync(CancellationToken.None); AsyncAutoResetEvent resetEvent = new AsyncAutoResetEvent(false); Task WriteAsyncDelegate(int index) => channel.Writer.WriteAsync(cancellationToken => { logger.LogInformation($"item: {index}"); if (index == delegatesNumber - 1) { resetEvent.Set(); } return(Task.CompletedTask); }).AsTask(); Task[] writeTasks = Enumerable.Range(0, delegatesNumber).Select(WriteAsyncDelegate).ToArray(); await Task.WhenAll(writeTasks); await resetEvent.WaitAsync(); Mock.Get(logger) .Verify( l => l.Log( It.Is <LogLevel>(l => l == LogLevel.Information), It.IsAny <EventId>(), It.IsAny <It.IsAnyType>(), It.Is <Exception>(e => e == null), It.IsAny <Func <It.IsAnyType, Exception, string> >()), Times.Exactly(delegatesNumber)); }
public async Task StartAsync_processes_handle_proxy_delegates(int delegatesNumber) { var queue = new BackgroundHandleProxyQueue(); var logger = Mock.Of <ILogger <BackgroundHandleProxyProcessor> >(); var processor = new BackgroundHandleProxyProcessor(queue, logger); _ = processor.StartAsync(CancellationToken.None); AsyncAutoResetEvent resetEvent = new AsyncAutoResetEvent(false); Task EnqueueAsyncDelegate(int index) => queue.EnqueueAsync(cancellationToken => { logger.LogInformation($"item: {index}"); if (index == delegatesNumber - 1) { resetEvent.Set(); } return(Task.CompletedTask); }, default).AsTask(); Task[] enqueueTasks = Enumerable.Range(0, delegatesNumber).Select(EnqueueAsyncDelegate).ToArray(); await Task.WhenAll(enqueueTasks); await resetEvent.WaitAsync(); Mock.Get(logger) .Verify( l => l.Log( It.Is <LogLevel>(l => l == LogLevel.Information), It.IsAny <EventId>(), It.IsAny <It.IsAnyType>(), It.Is <Exception>(e => e == null), It.IsAny <Func <It.IsAnyType, Exception, string> >()), Times.Exactly(delegatesNumber)); }