Add worker tests for NucuCar.Sensors.EnvironmentSensor

This commit is contained in:
Denis-Cosmin Nutiu 2019-12-01 16:38:14 +02:00
parent fb43090994
commit a5600e40cd
4 changed files with 108 additions and 19 deletions

View file

@ -33,11 +33,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
{ {
_sensorStateEnum = SensorStateEnum.Uninitialized; _sensorStateEnum = SensorStateEnum.Uninitialized;
_logger = logger; _logger = logger;
if (options.Value.ServiceEnabled) if (!options.Value.ServiceEnabled)
{
InitializeSensor();
}
else
{ {
_logger?.LogInformation("BME680 Sensor is disabled!"); _logger?.LogInformation("BME680 Sensor is disabled!");
_sensorStateEnum = SensorStateEnum.Disabled; _sensorStateEnum = SensorStateEnum.Disabled;
@ -62,9 +58,9 @@ namespace NucuCar.Sensors.EnvironmentSensor
_bme680?.Dispose(); _bme680?.Dispose();
} }
public void InitializeSensor() public virtual void InitializeSensor()
{ {
if (_sensorStateEnum == SensorStateEnum.Initialized) if (_sensorStateEnum == SensorStateEnum.Initialized || _sensorStateEnum == SensorStateEnum.Disabled)
{ {
return; return;
} }
@ -94,7 +90,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
} }
} }
public async Task TakeMeasurement() public virtual async Task TakeMeasurement()
{ {
if (_sensorStateEnum != SensorStateEnum.Initialized) if (_sensorStateEnum != SensorStateEnum.Initialized)
{ {

View file

@ -45,6 +45,7 @@ namespace NucuCar.Sensors.EnvironmentSensor
_telemetryPublisher?.RegisterTelemeter(_bme680Sensor.Object); _telemetryPublisher?.RegisterTelemeter(_bme680Sensor.Object);
} }
_bme680Sensor.Object.InitializeSensor();
while (!stoppingToken.IsCancellationRequested) while (!stoppingToken.IsCancellationRequested)
{ {
/* If sensor is ok attempt to read. */ /* If sensor is ok attempt to read. */

View file

@ -0,0 +1,84 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using NucuCar.Sensors;
using NucuCar.Sensors.EnvironmentSensor;
using NucuCar.Sensors.Telemetry;
using NucuCarSensorsProto;
using Xunit;
namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests
{
public class Bme680WorkerTest
{
private readonly Mock<ILogger<Bme680Worker>> _mockLogger;
private readonly Mock<IOptions<Bme680Config>> _mockOptions;
private readonly Mock<SensorTelemetry> _mockSensorTelemetry;
private readonly Mock<TestBme680Sensor> _mockTestBme680Sensor;
private readonly Mock<ISensor<Bme680Sensor>> _mockBme680ISensor;
private readonly CancellationTokenSource _cts;
public Bme680WorkerTest()
{
_cts = new CancellationTokenSource();
_mockLogger = new Mock<ILogger<Bme680Worker>>();
_mockOptions = new Mock<IOptions<Bme680Config>>();
_mockSensorTelemetry = new Mock<SensorTelemetry>();
_mockTestBme680Sensor = new Mock<TestBme680Sensor>();
_mockBme680ISensor = new Mock<ISensor<Bme680Sensor>>();
_mockBme680ISensor.Setup(o => o.Object).Returns(_mockTestBme680Sensor.Object);
}
[Fact]
public async Task Test_Bme680Worker_ServiceDisabled()
{
_mockOptions.Setup(o => o.Value).Returns(new Bme680Config()
{
ServiceEnabled = false
});
var service = new Bme680Worker(_mockLogger.Object, _mockOptions.Object, _mockSensorTelemetry.Object,
_mockBme680ISensor.Object);
await service.StartAsync(_cts.Token);
_mockTestBme680Sensor.Verify(s => s.InitializeSensor(), Times.Never);
}
[Fact]
public async Task Test_Bme680Worker_SensorIsInitialized()
{
_mockOptions.Setup(o => o.Value).Returns(new Bme680Config()
{
ServiceEnabled = true,
MeasurementInterval = 1000
});
var service = new Bme680Worker(_mockLogger.Object, _mockOptions.Object, _mockSensorTelemetry.Object,
_mockBme680ISensor.Object);
await service.StartAsync(_cts.Token);
_mockTestBme680Sensor.Verify(s => s.InitializeSensor(), Times.AtLeastOnce);
await service.StopAsync(_cts.Token);
}
[Fact]
public async Task Test_Bme680Worker_SensorIsBeingMeasured()
{
_mockOptions.Setup(o => o.Value).Returns(new Bme680Config()
{
ServiceEnabled = true,
MeasurementInterval = 100
});
_mockTestBme680Sensor.Setup(s => s.GetState()).Returns(SensorStateEnum.Initialized);
var service = new Bme680Worker(_mockLogger.Object, _mockOptions.Object, _mockSensorTelemetry.Object,
_mockBme680ISensor.Object);
await service.StartAsync(_cts.Token);
_mockTestBme680Sensor.Verify(s => s.InitializeSensor(), Times.AtLeastOnce);
_mockTestBme680Sensor.Verify(s => s.TakeMeasurement(), Times.AtLeastOnce);
await service.StopAsync(_cts.Token);
}
}
}

View file

@ -1,22 +1,30 @@
using System.Threading.Tasks;
using NucuCar.Sensors.EnvironmentSensor; using NucuCar.Sensors.EnvironmentSensor;
using NucuCarSensorsProto; using NucuCarSensorsProto;
namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests namespace NucuCar.UnitTests.NucuCar.Sensors.Tests.EnvironmentSensor.Tests
{ {
public partial class Bme680GrpcServiceTest public class TestBme680Sensor : Bme680Sensor
{ {
public class TestBme680Sensor : Bme680Sensor public override Task TakeMeasurement()
{
return Task.CompletedTask;
}
public override void InitializeSensor()
{ {
// TODO Make more generic
public override EnvironmentSensorMeasurement GetMeasurement()
{
return new EnvironmentSensorMeasurement();
}
public override SensorStateEnum GetState() }
{
return SensorStateEnum.Error; // TODO Make more generic
} public override EnvironmentSensorMeasurement GetMeasurement()
{
return new EnvironmentSensorMeasurement();
}
public override SensorStateEnum GetState()
{
return SensorStateEnum.Error;
} }
} }
} }