예제 #1
0
        public async Task VerifyMetricForGaps(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 & Assert
            if (shouldSucceed)
            {
                // It will pass metric check, will try to interact with data layer and fail
                await Assert.ThrowsAsync <NullReferenceException>(
                    async() => await discrepancyService.FindGapsAsync(metric, new TimeSpan())
                    );
            }
            else
            {
                await Assert.ThrowsAsync <ArgumentException>(
                    async() => await discrepancyService.FindGapsAsync(metric, new TimeSpan())
                    );
            }
        }
예제 #2
0
        public async Task FindsGap()
        {
            // Arrange
            var mockConfig = new Mock <IConfiguration>();

            mockConfig
            .SetupGet(conf => conf["ServiceManager:DiscrepancyService:Gaps:MaxDifference"])
            .Returns(60.ToString());
            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 {
                    Timestamp = DateTime.UtcNow.AddMinutes(0),
                    Metric    = metric.Entity
                },
                new NumericDataPoint {
                    Timestamp = DateTime.UtcNow.AddMinutes(-1),
                    Metric    = metric.Entity
                },
                new NumericDataPoint {
                    Timestamp = DateTime.UtcNow.AddMinutes(-2),
                    Metric    = metric.Entity
                },
                new NumericDataPoint {
                    Timestamp = DateTime.UtcNow.AddMinutes(-4),
                    Metric    = metric.Entity
                },
                new NumericDataPoint {
                    Timestamp = DateTime.UtcNow.AddMinutes(-5),
                    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.GapInData,
                    MetricType       = Metrics.CpuLoad,
                    MetricSource     = "the-source"
                }
            };

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

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