diff --git a/AsbCloudInfrastructure.Tests/Services/SAUB/TelemetryDataSaubCacheTests.cs b/AsbCloudInfrastructure.Tests/Services/SAUB/TelemetryDataSaubCacheTests.cs index 53893067..7f9a76bd 100644 --- a/AsbCloudInfrastructure.Tests/Services/SAUB/TelemetryDataSaubCacheTests.cs +++ b/AsbCloudInfrastructure.Tests/Services/SAUB/TelemetryDataSaubCacheTests.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; using AsbCloudApp.Data.SAUB; +using AsbCloudApp.Repositories; using AsbCloudDb.Model; using AsbCloudInfrastructure.Background; using AsbCloudInfrastructure.Services.SAUB; @@ -14,6 +15,7 @@ namespace AsbCloudInfrastructure.Tests.Services.SAUB; public class TelemetryDataSaubCacheTests { + private static object _lock = new object(); private const int idTelemetry = 1; private readonly IEnumerable fakeTelemetries = new[] @@ -29,20 +31,22 @@ public class TelemetryDataSaubCacheTests public TelemetryDataSaubCacheTests() { serviceProviderMock.GetService().Returns(new BackgroundWorker(serviceProviderMock)); - telemetryDataCache = TelemetryDataCache.GetInstance(serviceProviderMock); - telemetryDataCacheType = telemetryDataCache.GetType(); + } [Fact] public void AddRange_ShouldReturn_AddedElementToCache() { //arrange - telemetryDataCacheType.GetField("isLoading", BindingFlags.NonPublic | BindingFlags.Instance)?.SetValue(telemetryDataCache, false); + telemetryDataCache.AddRange(idTelemetry, fakeTelemetries); + + telemetryDataCacheType + .GetField("isLoading", BindingFlags.NonPublic | BindingFlags.Instance)! + .SetValue(telemetryDataCache, false); //act - telemetryDataCache.AddRange(idTelemetry, fakeTelemetries); var lastTelemetry = telemetryDataCache.GetLastOrDefault(idTelemetry); //assert @@ -53,10 +57,12 @@ public class TelemetryDataSaubCacheTests public void AddRange_ShouldReturn_NotAddedToCache() { //arrange - telemetryDataCacheType.GetField("isLoading", BindingFlags.NonPublic | BindingFlags.Instance)?.SetValue(telemetryDataCache, true); + telemetryDataCache.AddRange(idTelemetry, fakeTelemetries); + telemetryDataCacheType + .GetField("isLoading", BindingFlags.NonPublic | BindingFlags.Instance)! + .SetValue(telemetryDataCache, true); //act - telemetryDataCache.AddRange(idTelemetry, fakeTelemetries); var lastTelemetry = telemetryDataCache.GetLastOrDefault(idTelemetry); //assert diff --git a/AsbCloudInfrastructure/Services/SAUB/TelemetryDataCache.cs b/AsbCloudInfrastructure/Services/SAUB/TelemetryDataCache.cs index da0a9486..ddfeef7f 100644 --- a/AsbCloudInfrastructure/Services/SAUB/TelemetryDataCache.cs +++ b/AsbCloudInfrastructure/Services/SAUB/TelemetryDataCache.cs @@ -139,6 +139,9 @@ public class TelemetryDataCache : ITelemetryDataCache where TDto : A public virtual TDto? GetLastOrDefault(int idTelemetry) { + if (isLoading) + return default; + if (!caches.TryGetValue(idTelemetry, out TelemetryDataCacheItem? cacheItem)) return default;