public async Task VerifyMetricForPings(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.FindPingFailuresAsync(metric, new TimeSpan()) ); } else { await Assert.ThrowsAsync <ArgumentException>( async() => await discrepancyService.FindPingFailuresAsync(metric, new TimeSpan()) ); } }
public async Task FindsPingFailure() { // Arrange var mockConfig = new Mock <IConfiguration>(); 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.Ping.AsInt() } ); var dataPoints = new List <PingDataPoint> { new PingDataPoint { Timestamp = DateTime.UtcNow.AddMinutes(0), Metric = metric.Entity, HttpStatusCode = HttpStatusCode.OK.AsInt() }, new PingDataPoint { Timestamp = DateTime.UtcNow.AddMinutes(-1), Metric = metric.Entity, HttpStatusCode = HttpStatusCode.ServiceUnavailable.AsInt() }, new PingDataPoint { Timestamp = DateTime.UtcNow.AddMinutes(-2), Metric = metric.Entity, HttpStatusCode = HttpStatusCode.ServiceUnavailable.AsInt() }, new PingDataPoint { Timestamp = DateTime.UtcNow.AddMinutes(-3), Metric = metric.Entity, HttpStatusCode = HttpStatusCode.OK.AsInt() }, new PingDataPoint { Timestamp = DateTime.UtcNow.AddMinutes(-4), Metric = metric.Entity, HttpStatusCode = HttpStatusCode.OK.AsInt() } }; await context.PingDataPoints.AddRangeAsync(dataPoints); await context.PingSettings.AddAsync( new PingSetting { ServerUrl = "the-source", MaxFailures = 1 } ); 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[2].Timestamp, Type = DiscrepancyType.PingFailedNTimes, MetricType = Metrics.Ping, MetricSource = "the-source" } }; // Act var actual = await discrepancyService .FindPingFailuresAsync( metric.Entity, new TimeSpan(0, 30, 0) ); // Assert Assert.Equal(expected, actual); }