예제 #1
0
        public async Task VerifyMetricForLowHealth(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.FindLowHealthsAsync(metric, new TimeSpan())
                    );
            }
            else
            {
                await Assert.ThrowsAsync <ArgumentException>(
                    async() => await discrepancyService.FindLowHealthsAsync(metric, new TimeSpan())
                    );
            }
        }
예제 #2
0
        public async Task FindsLowHealth()
        {
            // Arrange
            var mockConfig = new Mock <IConfiguration>();

            mockConfig
            .SetupGet(conf => conf["ServiceManager:DiscrepancyService:Health:Threshold"])
            .Returns(99.ToString());
            mockConfig
            .SetupGet(conf => conf["ServiceManager:DiscrepancyService:LoHealthad:MaxFailures"])
            .Returns(1.ToString());                     // 3 is discrepancy

            var context = _serviceProvider.GetRequiredService <IDataContext>();
            var metric  = await context.Metrics.AddAsync(
                new Metric
            {
                Source = "the-source",
                Type   = Metrics.Health.AsInt()
            }
                );

            var dataPoints = new List <HealthReport>()
            {
                new HealthReport {                 // good
                    Data = new List <HealthReportDataPoint> {
                        new HealthReportDataPoint {
                            Label = AutoLabels.Normal.ToString()
                        },
                        new HealthReportDataPoint {
                            Label = AutoLabels.Normal.ToString()
                        },
                    },
                    Timestamp = DateTime.UtcNow,
                    Metric    = metric.Entity
                },
                new HealthReport {
                    Data = new List <HealthReportDataPoint> {                    // bad
                        new HealthReportDataPoint {
                            Label = AutoLabels.Normal.ToString()
                        },
                        new HealthReportDataPoint {
                            Label = AutoLabels.Critical.ToString()
                        },
                    },
                    Timestamp = DateTime.UtcNow.AddMinutes(-1),
                    Metric    = metric.Entity
                },
                new HealthReport {
                    Data = new List <HealthReportDataPoint> {                    // bad
                        new HealthReportDataPoint {
                            Label = AutoLabels.Normal.ToString()
                        },
                        new HealthReportDataPoint {
                            Label = AutoLabels.Warning.ToString()
                        },
                    },
                    Timestamp = DateTime.UtcNow.AddMinutes(-2),
                    Metric    = metric.Entity
                },
                new HealthReport {                 // good
                    Data = new List <HealthReportDataPoint> {
                        new HealthReportDataPoint {
                            Label = AutoLabels.Normal.ToString()
                        },
                        new HealthReportDataPoint {
                            Label = AutoLabels.Normal.ToString()
                        },
                    },
                    Timestamp = DateTime.UtcNow.AddMinutes(-3),
                    Metric    = metric.Entity
                }
            };

            await context.HealthReports.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[2].Timestamp,
                    Type             = DiscrepancyType.LowHealth,
                    MetricType       = Metrics.Health,
                    MetricSource     = "the-source"
                }
            };

            // Act
            var actual = await discrepancyService
                         .FindLowHealthsAsync(
                metric.Entity,
                new TimeSpan(0, 30, 0)
                );

            // Assert
            Assert.Equal(expected, actual);
        }