Added logging to module framework

Implemented logging to Adafruit and changed database loading behavior.
This commit is contained in:
2021-08-07 17:20:46 -05:00
parent c94ea4a624
commit 38ffb3c7e1
36 changed files with 304 additions and 240 deletions

View File

@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Props.Shop.Framework;
using Xunit;
@@ -8,16 +10,12 @@ namespace Props.Shop.Adafruit.Tests
public class AdafruitShopTest
{
[Fact]
public async Task TestSearch() {
public void TestSearch() {
AdafruitShop mockAdafruitShop = new AdafruitShop();
mockAdafruitShop.Initialize(null);
await mockAdafruitShop.InitializeAsync(null);
int count = 0;
foreach (ProductListing listing in mockAdafruitShop.Search("raspberry pi", new Filters()))
{
count += 1;
}
Assert.True(count > 0);
mockAdafruitShop.Initialize(null, LoggerFactory.Create(builder => {
builder.AddXUnit();
}));
Assert.NotEmpty(mockAdafruitShop.Search("raspberry pi", new Filters()).ToEnumerable());
}
}
}

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Threading;
@@ -10,58 +11,65 @@ namespace Props.Shop.Adafruit.Tests.Api
{
public class FakeProductListingManager : IProductListingManager
{
private ProductListingsParser parser;
private Timer updateTimer;
private Timer refreshTimer;
private bool disposedValue;
private volatile Task<IDictionary<string, IList<ProductListing>>> activeListings;
private DateTime? lastDownload;
private ProductListingsParser parser = new ProductListingsParser();
private readonly ConcurrentDictionary<string, ProductListing> activeProductListingUrls = new ConcurrentDictionary<string, ProductListing>();
private volatile Dictionary<string, IList<ProductListing>> activeListings;
public IDictionary<string, IList<ProductListing>> ActiveListings => activeListings;
public Task<IDictionary<string, IList<ProductListing>>> ProductListings => activeListings;
public event EventHandler DataUpdateEvent;
public FakeProductListingManager()
public async Task<ProductListing> GetProductListingFromUrl(string url)
{
parser = new ProductListingsParser();
if (disposedValue) throw new ObjectDisposedException("ProductListingManager");
await activeListings;
return activeProductListingUrls[url];
}
public Task DownloadListings()
public void RefreshProductListings()
{
if (disposedValue) throw new ObjectDisposedException("FakeProductListingManager");
using (Stream stream = File.OpenRead("./Assets/products.json"))
{
parser.BuildProductListings(stream);
}
if (disposedValue) throw new ObjectDisposedException("ProductListingManager");
if ((lastDownload != null && DateTime.UtcNow - lastDownload <= TimeSpan.FromMilliseconds(5 * 60 * 1000)) || (activeListings != null && !activeListings.IsCompleted)) return;
activeListings = DownloadListings();
}
Dictionary<string, IList<ProductListing>> results = new Dictionary<string, IList<ProductListing>>();
private Task<IDictionary<string, IList<ProductListing>>> DownloadListings() {
if (disposedValue) throw new ObjectDisposedException("ProductListingManager");
lastDownload = DateTime.UtcNow;
parser.BuildProductListings(File.OpenRead("./Assets/products.json"));
Dictionary<string, IList<ProductListing>> listingNames = new Dictionary<string, IList<ProductListing>>();
activeProductListingUrls.Clear();
foreach (ProductListing product in parser.ProductListings)
{
IList<ProductListing> sameProducts = results.GetValueOrDefault(product.Name);
activeProductListingUrls.TryAdd(product.URL, product);
IList<ProductListing> sameProducts = listingNames.GetValueOrDefault(product.Name);
if (sameProducts == null) {
sameProducts = new List<ProductListing>();
results.Add(product.Name, sameProducts);
listingNames.Add(product.Name, sameProducts);
}
sameProducts.Add(product);
}
activeListings = results;
DataUpdateEvent?.Invoke(this, null);
return Task.CompletedTask;
return Task.FromResult<IDictionary<string, IList<ProductListing>>>(listingNames);
}
public void StartUpdateTimer(int delay = 300000, int period = 300000)
{
if (disposedValue) throw new ObjectDisposedException("FakeProductListingManager");
if (updateTimer != null) throw new InvalidOperationException("Update timer already started.");
updateTimer = new Timer((state) => DownloadListings(), null, delay, period);
if (disposedValue) throw new ObjectDisposedException("ProductListingManager");
if (refreshTimer != null) throw new InvalidOperationException("Refresh timer already running.");
refreshTimer = new Timer((state) => {
RefreshProductListings();
}, null, delay, period);
}
public void StopUpdateTimer()
{
if (disposedValue) throw new ObjectDisposedException("FakeProductListingManager");
if (updateTimer == null) throw new InvalidOperationException("Update timer not started.");
updateTimer.Dispose();
updateTimer = null;
if (disposedValue) throw new ObjectDisposedException("ProductListingManager");
if (refreshTimer == null) throw new InvalidOperationException("Refresh timer not running.");
refreshTimer.Dispose();
refreshTimer = null;
}
protected virtual void Dispose(bool disposing)
@@ -70,8 +78,8 @@ namespace Props.Shop.Adafruit.Tests.Api
{
if (disposing)
{
updateTimer?.Dispose();
updateTimer = null;
refreshTimer?.Dispose();
refreshTimer = null;
}
disposedValue = true;

View File

@@ -1,3 +1,4 @@
using System.Linq;
using System.Threading.Tasks;
using Props.Shop.Adafruit.Api;
using Xunit;
@@ -7,13 +8,13 @@ namespace Props.Shop.Adafruit.Tests.Api
public class SearchManagerTest
{
[Fact]
public async Task SearchTest()
public void SearchTest()
{
FakeProductListingManager stubProductListingManager = new FakeProductListingManager();
SearchManager searchManager = new SearchManager(stubProductListingManager);
await stubProductListingManager.DownloadListings();
stubProductListingManager.RefreshProductListings();
searchManager.Similarity = 0.8f;
Assert.NotEmpty(searchManager.Search("Raspberry Pi"));
Assert.NotEmpty(searchManager.Search("Raspberry Pi").ToEnumerable());
searchManager.Dispose();
}
}

View File

@@ -7,7 +7,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MartinCostello.Logging.XUnit" Version="0.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="System.Linq.Async" Version="5.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>