public async Task VerifyMetricForHighLoad(Metrics type, bool shouldSucceed) { // Arrange var metric = new Metric { Type = type.AsInt(), Source = "the-source" }; var discrepancyService = new DiscrepancyService( new Mock <ILogger <DiscrepancyService> >().Object, new Mock <IDataContext>().Object, new Mock <INotificationService>().Object, new Mock <IConfiguration>().Object ); // Act if (shouldSucceed) { // It will pass metric check, will try to interact with data layer and fail await Assert.ThrowsAsync <NullReferenceException>( async() => await discrepancyService.FindHighLoadsAsync(metric, new TimeSpan()) ); } else { await Assert.ThrowsAsync <ArgumentException>( async() => await discrepancyService.FindHighLoadsAsync(metric, new TimeSpan()) ); } }
public async Task FindsHighLoad() { // Arrange var mockConfig = new Mock <IConfiguration>(); mockConfig .SetupGet(conf => conf["ServiceManager:DiscrepancyService:Load:Threshold"]) .Returns(90.ToString()); mockConfig .SetupGet(conf => conf["ServiceManager:DiscrepancyService:Load:MaxFailures"]) .Returns(2.ToString()); // 3 is discrepancy mockConfig .SetupGet(conf => conf["ServiceManager:DiscrepancyService:DataTimeframe"]) .Returns(1800.ToString()); var context = _serviceProvider.GetRequiredService <IDataContext>(); var metric = await context.Metrics.AddAsync( new Metric { Source = "the-source", Type = Metrics.CpuLoad.AsInt() } ); var dataPoints = new List <NumericDataPoint>() { new NumericDataPoint { // Good Timestamp = DateTime.UtcNow.AddMinutes(0), Value = 68, Metric = metric.Entity }, new NumericDataPoint { // Bad Timestamp = DateTime.UtcNow.AddMinutes(-1), Value = 91, Metric = metric.Entity }, new NumericDataPoint { // Bad Timestamp = DateTime.UtcNow.AddMinutes(-2), Value = 99, Metric = metric.Entity }, new NumericDataPoint { // Bad Timestamp = DateTime.UtcNow.AddMinutes(-3), Value = 95, Metric = metric.Entity }, new NumericDataPoint { // Good Timestamp = DateTime.UtcNow.AddMinutes(-4), Value = 68, Metric = metric.Entity }, new NumericDataPoint { // Bad Timestamp = DateTime.UtcNow.AddMinutes(-5), Value = 99, Metric = metric.Entity }, new NumericDataPoint { // Bad Timestamp = DateTime.UtcNow.AddMinutes(-6), Value = 98, Metric = metric.Entity }, new NumericDataPoint { // Good Timestamp = DateTime.UtcNow.AddMinutes(-7), Value = 68, Metric = metric.Entity } }; await context.NumericDataPoints.AddRangeAsync(dataPoints); await context.SaveChangesAsync(); var discrepancyService = new DiscrepancyService( new Mock <ILogger <DiscrepancyService> >().Object, context, new Mock <INotificationService>().Object, mockConfig.Object ); var expected = new List <Discrepancy> { new Discrepancy { DateFirstOffense = dataPoints[3].Timestamp, Type = DiscrepancyType.HighLoad, MetricType = Metrics.CpuLoad, MetricSource = "the-source" } }; // Act var actual = await discrepancyService .FindHighLoadsAsync( metric.Entity, new TimeSpan(0, 30, 0) ); // Assert Assert.Equal(expected, actual); }