public async Task Test_CanReportExecutionStats_SerialCalls(int nReports) { StandardExecutionPerformanceMonitor perfMon = new StandardExecutionPerformanceMonitor(); ConcurrentQueue <Tuple <string, long> > perfStatsToReport = GenerateSamplePerformanceStats(nReports, out List <TaskPerformanceStats> expectedWrittenStats); Mock <IExecutionPerformanceMonitorWriter> writerMock = new Mock <IExecutionPerformanceMonitorWriter>(); List <TaskPerformanceStats> actualWrittenStats = new List <TaskPerformanceStats>(); writerMock.Setup(w => w.WriteAsync(It.IsAny <IEnumerable <TaskPerformanceStats> >())) .Callback <IEnumerable <TaskPerformanceStats> >(ws => actualWrittenStats.AddRange(ws)); await perfMon.StartFlushingAsync(writerMock.Object); while (perfStatsToReport.TryDequeue(out Tuple <string, long> execTime)) { await perfMon.ReportExecutionTimeAsync(execTime.Item1, execTime.Item2, 0); } await perfMon.StopFlushingAsync(); CollectionAssert.AreEquivalent(expectedWrittenStats, actualWrittenStats); }
public async Task Test_CanReportExecutionStats_ParallelCalls(int nReports, int nProducers) { StandardExecutionPerformanceMonitor perfMon = new StandardExecutionPerformanceMonitor(); ConcurrentQueue <Tuple <string, long> > perfStatsToReport = GenerateSamplePerformanceStats(nReports, out List <TaskPerformanceStats> expectedWrittenStats); Mock <IExecutionPerformanceMonitorWriter> writerMock = new Mock <IExecutionPerformanceMonitorWriter>(); ConcurrentBag <TaskPerformanceStats> actualWrittenStats = new ConcurrentBag <TaskPerformanceStats>(); Task[] producers = new Task[nProducers]; writerMock.Setup(w => w.WriteAsync(It.IsAny <IEnumerable <TaskPerformanceStats> >())) .Callback <IEnumerable <TaskPerformanceStats> >(ws => { foreach (TaskPerformanceStats s in ws) { actualWrittenStats.Add(s); } }); await perfMon.StartFlushingAsync(writerMock.Object); for (int i = 0; i < nProducers; i++) { producers[i] = Task.Run(async() => { while (perfStatsToReport.TryDequeue(out Tuple <string, long> execTime)) { await perfMon.ReportExecutionTimeAsync(execTime.Item1, execTime.Item2, 0); } }); } await Task.WhenAll(producers); await perfMon.StopFlushingAsync(); CollectionAssert.AreEquivalent(expectedWrittenStats.ToArray(), actualWrittenStats.ToArray()); }