Skip to content

Commit

Permalink
Fix #1200 - Add Metrics services to Quartz Jobs (#1203)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbeaugrand authored Sep 11, 2022
1 parent be32ea1 commit 80231c8
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 287 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
namespace AzureIoTHub.Portal.Tests.Unit.Server.Services
{
using System;
using System.Threading;
using AzureIoTHub.Portal.Domain;
using AzureIoTHub.Portal.Domain.Shared.Constants;
using AzureIoTHub.Portal.Server.Services;
using AzureIoTHub.Portal.Shared.Models.v1._0;
Expand All @@ -23,7 +21,6 @@ public class ConcentratorMetricExporterServiceTests : IDisposable
private PortalMetric portalMetric;

private Mock<ILogger<ConcentratorMetricExporterService>> mockLogger;
private Mock<ConfigHandler> mockConfigHandler;

private readonly Counter concentratorCounter = Metrics.CreateCounter(MetricName.ConcentratorCount, "Concentrators count");
private readonly Counter connectedConcentratorCounter = Metrics.CreateCounter(MetricName.ConnectedConcentratorCount, "Connected concentrators count");
Expand All @@ -34,29 +31,24 @@ public void SetUp()
this.mockRepository = new MockRepository(MockBehavior.Strict);

this.mockLogger = this.mockRepository.Create<ILogger<ConcentratorMetricExporterService>>();
this.mockConfigHandler = this.mockRepository.Create<ConfigHandler>();

this.portalMetric = new PortalMetric();

this.concentratorMetricExporterService =
new ConcentratorMetricExporterService(this.mockLogger.Object, this.mockConfigHandler.Object, this.portalMetric);
new ConcentratorMetricExporterService(this.mockLogger.Object, this.portalMetric);
}

[Test]
public void ConcentratorMetricExporterServiceShouldExportConcentratorCountAndConnectedConcentratorCountMetrics()
{
// Arrange
_ = this.mockLogger.Setup(x => x.Log(It.IsAny<LogLevel>(), It.IsAny<EventId>(), It.IsAny<It.IsAnyType>(), It.IsAny<Exception>(), It.IsAny<Func<It.IsAnyType, Exception, string>>()));
_ = this.mockConfigHandler.Setup(handler => handler.MetricExporterRefreshIntervalInSeconds).Returns(1);

this.portalMetric.ConcentratorCount = 15;
this.portalMetric.ConnectedConcentratorCount = 8;

using var cancellationToken = new CancellationTokenSource();

// Act
_ = this.concentratorMetricExporterService.StartAsync(cancellationToken.Token);
cancellationToken.Cancel();
_ = this.concentratorMetricExporterService.Execute(null);

// Assert
_ = this.concentratorCounter.Value.Should().Be(this.portalMetric.ConcentratorCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
namespace AzureIoTHub.Portal.Tests.Unit.Server.Services
{
using System;
using System.Threading;
using AzureIoTHub.Portal.Domain;
using AzureIoTHub.Portal.Domain.Exceptions;
using AzureIoTHub.Portal.Server.Services;
using AzureIoTHub.Portal.Shared.Models.v1._0;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
Expand All @@ -23,7 +20,6 @@ public class ConcentratorMetricLoaderServiceTests : IDisposable
private PortalMetric portalMetric;

private Mock<ILogger<ConcentratorMetricLoaderService>> mockLogger;
private Mock<ConfigHandler> mockConfigHandler;
private Mock<IDeviceService> mockDeviceService;

[SetUp]
Expand All @@ -32,34 +28,25 @@ public void SetUp()
this.mockRepository = new MockRepository(MockBehavior.Strict);

this.mockLogger = this.mockRepository.Create<ILogger<ConcentratorMetricLoaderService>>();
this.mockConfigHandler = this.mockRepository.Create<ConfigHandler>();
this.mockDeviceService = this.mockRepository.Create<IDeviceService>();

this.portalMetric = new PortalMetric();

var services = new ServiceCollection();

_ = services.AddTransient(_ => this.mockDeviceService.Object);

this.concentratorMetricLoaderService =
new ConcentratorMetricLoaderService(this.mockLogger.Object, this.mockConfigHandler.Object, this.portalMetric, services.BuildServiceProvider());
new ConcentratorMetricLoaderService(this.mockLogger.Object, this.portalMetric, this.mockDeviceService.Object);
}

[Test]
public void ConcentratorMetricLoaderServiceShouldLoadConcentratorCountAndConnectedConcentratorCountMetrics()
{
// Arrange
_ = this.mockLogger.Setup(x => x.Log(It.IsAny<LogLevel>(), It.IsAny<EventId>(), It.IsAny<It.IsAnyType>(), It.IsAny<Exception>(), It.IsAny<Func<It.IsAnyType, Exception, string>>()));
_ = this.mockConfigHandler.Setup(handler => handler.MetricLoaderRefreshIntervalInMinutes).Returns(1);

_ = this.mockDeviceService.Setup(service => service.GetConcentratorsCount()).ReturnsAsync(10);
_ = this.mockDeviceService.Setup(service => service.GetConnectedConcentratorsCount()).ReturnsAsync(3);

using var cancellationToken = new CancellationTokenSource();

// Act
_ = this.concentratorMetricLoaderService.StartAsync(cancellationToken.Token);
cancellationToken.Cancel();
_ = this.concentratorMetricLoaderService.Execute(null);

// Assert
_ = this.portalMetric.ConcentratorCount.Should().Be(10);
Expand All @@ -72,16 +59,12 @@ public void ConcentratorMetricLoaderServiceShouldHandleInternalServerErrorExcept
{
// Arrange
_ = this.mockLogger.Setup(x => x.Log(It.IsAny<LogLevel>(), It.IsAny<EventId>(), It.IsAny<It.IsAnyType>(), It.IsAny<Exception>(), It.IsAny<Func<It.IsAnyType, Exception, string>>()));
_ = this.mockConfigHandler.Setup(handler => handler.MetricLoaderRefreshIntervalInMinutes).Returns(1);

_ = this.mockDeviceService.Setup(service => service.GetConcentratorsCount()).ThrowsAsync(new InternalServerErrorException("test"));
_ = this.mockDeviceService.Setup(service => service.GetConnectedConcentratorsCount()).ReturnsAsync(3);

using var cancellationToken = new CancellationTokenSource();

// Act
_ = this.concentratorMetricLoaderService.StartAsync(cancellationToken.Token);
cancellationToken.Cancel();
_ = this.concentratorMetricLoaderService.Execute(null);

// Assert
_ = this.portalMetric.ConcentratorCount.Should().Be(0);
Expand All @@ -95,16 +78,12 @@ public void ConcentratorMetricLoaderServiceShouldHandleInternalServerErrorExcept
{
// Arrange
_ = this.mockLogger.Setup(x => x.Log(It.IsAny<LogLevel>(), It.IsAny<EventId>(), It.IsAny<It.IsAnyType>(), It.IsAny<Exception>(), It.IsAny<Func<It.IsAnyType, Exception, string>>()));
_ = this.mockConfigHandler.Setup(handler => handler.MetricLoaderRefreshIntervalInMinutes).Returns(1);

_ = this.mockDeviceService.Setup(service => service.GetConcentratorsCount()).ReturnsAsync(10);
_ = this.mockDeviceService.Setup(service => service.GetConnectedConcentratorsCount()).ThrowsAsync(new InternalServerErrorException("test"));

using var cancellationToken = new CancellationTokenSource();

// Act
_ = this.concentratorMetricLoaderService.StartAsync(cancellationToken.Token);
cancellationToken.Cancel();
_ = this.concentratorMetricLoaderService.Execute(null);

// Assert
_ = this.portalMetric.ConcentratorCount.Should().Be(10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
namespace AzureIoTHub.Portal.Tests.Unit.Server.Services
{
using System;
using System.Threading;
using AzureIoTHub.Portal.Domain;
using AzureIoTHub.Portal.Domain.Shared.Constants;
using AzureIoTHub.Portal.Server.Services;
using AzureIoTHub.Portal.Shared.Models.v1._0;
Expand All @@ -23,7 +21,6 @@ public class DeviceMetricExporterServiceTests : IDisposable
private PortalMetric portalMetric;

private Mock<ILogger<DeviceMetricExporterService>> mockLogger;
private Mock<ConfigHandler> mockConfigHandler;

private readonly Counter deviceCounter = Metrics.CreateCounter(MetricName.DeviceCount, "Devices count");
private readonly Counter connectedDeviceCounter = Metrics.CreateCounter(MetricName.ConnectedDeviceCount, "Connected devices count");
Expand All @@ -34,29 +31,24 @@ public void SetUp()
this.mockRepository = new MockRepository(MockBehavior.Strict);

this.mockLogger = this.mockRepository.Create<ILogger<DeviceMetricExporterService>>();
this.mockConfigHandler = this.mockRepository.Create<ConfigHandler>();

this.portalMetric = new PortalMetric();

this.deviceMetricExporterService =
new DeviceMetricExporterService(this.mockLogger.Object, this.mockConfigHandler.Object, this.portalMetric);
new DeviceMetricExporterService(this.mockLogger.Object, this.portalMetric);
}

[Test]
public void DeviceMetricExporterServiceShouldExportDeviceCountAndConnectedConnectedDeviceCountMetrics()
{
// Arrange
_ = this.mockLogger.Setup(x => x.Log(It.IsAny<LogLevel>(), It.IsAny<EventId>(), It.IsAny<It.IsAnyType>(), It.IsAny<Exception>(), It.IsAny<Func<It.IsAnyType, Exception, string>>()));
_ = this.mockConfigHandler.Setup(handler => handler.MetricExporterRefreshIntervalInSeconds).Returns(1);

this.portalMetric.DeviceCount = 15;
this.portalMetric.ConnectedDeviceCount = 8;

using var cancellationToken = new CancellationTokenSource();

// Act
_ = this.deviceMetricExporterService.StartAsync(cancellationToken.Token);
cancellationToken.Cancel();
_ = this.deviceMetricExporterService.Execute(null);

// Assert
_ = this.deviceCounter.Value.Should().Be(this.portalMetric.DeviceCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
namespace AzureIoTHub.Portal.Tests.Unit.Server.Services
{
using System;
using System.Threading;
using AzureIoTHub.Portal.Domain;
using AzureIoTHub.Portal.Domain.Exceptions;
using AzureIoTHub.Portal.Server.Services;
using AzureIoTHub.Portal.Shared.Models.v1._0;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Moq;
using NUnit.Framework;
Expand All @@ -23,7 +20,6 @@ public class DeviceMetricLoaderServiceTests : IDisposable
private PortalMetric portalMetric;

private Mock<ILogger<DeviceMetricLoaderService>> mockLogger;
private Mock<ConfigHandler> mockConfigHandler;
private Mock<IDeviceService> mockDeviceService;

[SetUp]
Expand All @@ -32,34 +28,25 @@ public void SetUp()
this.mockRepository = new MockRepository(MockBehavior.Strict);

this.mockLogger = this.mockRepository.Create<ILogger<DeviceMetricLoaderService>>();
this.mockConfigHandler = this.mockRepository.Create<ConfigHandler>();
this.mockDeviceService = this.mockRepository.Create<IDeviceService>();

this.portalMetric = new PortalMetric();

var services = new ServiceCollection();

_ = services.AddTransient(_ => this.mockDeviceService.Object);

this.deviceMetricLoaderService =
new DeviceMetricLoaderService(this.mockLogger.Object, this.mockConfigHandler.Object, this.portalMetric, services.BuildServiceProvider());
new DeviceMetricLoaderService(this.mockLogger.Object, this.portalMetric, this.mockDeviceService.Object);
}

[Test]
public void DeviceMetricLoaderServiceShouldLoadDeviceCountAndConnectedDeviceCountMetrics()
{
// Arrange
_ = this.mockLogger.Setup(x => x.Log(It.IsAny<LogLevel>(), It.IsAny<EventId>(), It.IsAny<It.IsAnyType>(), It.IsAny<Exception>(), It.IsAny<Func<It.IsAnyType, Exception, string>>()));
_ = this.mockConfigHandler.Setup(handler => handler.MetricLoaderRefreshIntervalInMinutes).Returns(1);

_ = this.mockDeviceService.Setup(service => service.GetDevicesCount()).ReturnsAsync(10);
_ = this.mockDeviceService.Setup(service => service.GetConnectedDevicesCount()).ReturnsAsync(3);

using var cancellationToken = new CancellationTokenSource();

// Act
_ = this.deviceMetricLoaderService.StartAsync(cancellationToken.Token);
cancellationToken.Cancel();
_ = this.deviceMetricLoaderService.Execute(null);

// Assert
_ = this.portalMetric.DeviceCount.Should().Be(10);
Expand All @@ -72,16 +59,12 @@ public void DeviceMetricLoaderServiceShouldHandleInternalServerErrorExceptionWhe
{
// Arrange
_ = this.mockLogger.Setup(x => x.Log(It.IsAny<LogLevel>(), It.IsAny<EventId>(), It.IsAny<It.IsAnyType>(), It.IsAny<Exception>(), It.IsAny<Func<It.IsAnyType, Exception, string>>()));
_ = this.mockConfigHandler.Setup(handler => handler.MetricLoaderRefreshIntervalInMinutes).Returns(1);

_ = this.mockDeviceService.Setup(service => service.GetDevicesCount()).ThrowsAsync(new InternalServerErrorException("test"));
_ = this.mockDeviceService.Setup(service => service.GetConnectedDevicesCount()).ReturnsAsync(3);

using var cancellationToken = new CancellationTokenSource();

// Act
_ = this.deviceMetricLoaderService.StartAsync(cancellationToken.Token);
cancellationToken.Cancel();
_ = this.deviceMetricLoaderService.Execute(null);

// Assert
_ = this.portalMetric.DeviceCount.Should().Be(0);
Expand All @@ -95,16 +78,12 @@ public void DeviceMetricLoaderServiceShouldHandleInternalServerErrorExceptionWhe
{
// Arrange
_ = this.mockLogger.Setup(x => x.Log(It.IsAny<LogLevel>(), It.IsAny<EventId>(), It.IsAny<It.IsAnyType>(), It.IsAny<Exception>(), It.IsAny<Func<It.IsAnyType, Exception, string>>()));
_ = this.mockConfigHandler.Setup(handler => handler.MetricLoaderRefreshIntervalInMinutes).Returns(1);

_ = this.mockDeviceService.Setup(service => service.GetDevicesCount()).ReturnsAsync(10);
_ = this.mockDeviceService.Setup(service => service.GetConnectedDevicesCount()).ThrowsAsync(new InternalServerErrorException("test"));

using var cancellationToken = new CancellationTokenSource();

// Act
_ = this.deviceMetricLoaderService.StartAsync(cancellationToken.Token);
cancellationToken.Cancel();
_ = this.deviceMetricLoaderService.Execute(null);

// Assert
_ = this.portalMetric.DeviceCount.Should().Be(10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
namespace AzureIoTHub.Portal.Tests.Unit.Server.Services
{
using System;
using System.Threading;
using AzureIoTHub.Portal.Domain;
using AzureIoTHub.Portal.Domain.Shared.Constants;
using AzureIoTHub.Portal.Server.Services;
using AzureIoTHub.Portal.Shared.Models.v1._0;
Expand All @@ -23,7 +21,6 @@ public class EdgeDeviceMetricExporterServiceTests : IDisposable
private PortalMetric portalMetric;

private Mock<ILogger<EdgeDeviceMetricExporterService>> mockLogger;
private Mock<ConfigHandler> mockConfigHandler;

private readonly Counter edgeDeviceCounter = Metrics.CreateCounter(MetricName.EdgeDeviceCount, "Edge devices count");
private readonly Counter connectedEdgeDeviceCounter = Metrics.CreateCounter(MetricName.ConnectedEdgeDeviceCount, "Connected edge devices count");
Expand All @@ -35,30 +32,25 @@ public void SetUp()
this.mockRepository = new MockRepository(MockBehavior.Strict);

this.mockLogger = this.mockRepository.Create<ILogger<EdgeDeviceMetricExporterService>>();
this.mockConfigHandler = this.mockRepository.Create<ConfigHandler>();

this.portalMetric = new PortalMetric();

this.edgeDeviceMetricExporterService =
new EdgeDeviceMetricExporterService(this.mockLogger.Object, this.mockConfigHandler.Object, this.portalMetric);
new EdgeDeviceMetricExporterService(this.mockLogger.Object, this.portalMetric);
}

[Test]
public void DeviceMetricExporterServiceShouldExportDeviceCountAndConnectedConnectedDeviceCountMetrics()
{
// Arrange
_ = this.mockLogger.Setup(x => x.Log(It.IsAny<LogLevel>(), It.IsAny<EventId>(), It.IsAny<It.IsAnyType>(), It.IsAny<Exception>(), It.IsAny<Func<It.IsAnyType, Exception, string>>()));
_ = this.mockConfigHandler.Setup(handler => handler.MetricExporterRefreshIntervalInSeconds).Returns(1);

this.portalMetric.EdgeDeviceCount = 15;
this.portalMetric.ConnectedEdgeDeviceCount = 8;
this.portalMetric.FailedDeploymentCount = 3;

using var cancellationToken = new CancellationTokenSource();

// Act
_ = this.edgeDeviceMetricExporterService.StartAsync(cancellationToken.Token);
cancellationToken.Cancel();
_ = this.edgeDeviceMetricExporterService.Execute(null);

// Assert
_ = this.edgeDeviceCounter.Value.Should().Be(this.portalMetric.EdgeDeviceCount);
Expand Down
Loading

0 comments on commit 80231c8

Please sign in to comment.