From eea023264ab916a5bf82309c5b91f8e50903fa55 Mon Sep 17 00:00:00 2001 From: "Eric J. Smith" Date: Wed, 7 Aug 2024 11:18:26 -0500 Subject: [PATCH] More test cleanup --- .../Jobs/Sample2Job.cs | 1 - samples/Foundatio.HostingSample/Program.cs | 2 - .../Jobs/JobHostExtensions.cs | 1 - .../Jobs/ScheduledJobService.cs | 1 - .../Caching/CacheClientTestsBase.cs | 2 - .../Jobs/JobQueueTestsBase.cs | 4 -- .../Jobs/SampleQueueJob.cs | 1 - .../Jobs/WithLockingJob.cs | 1 - .../Metrics/DiagnosticsMetricsCollector.cs | 4 ++ .../Queue/QueueTestBase.cs | 40 ++++++------------- .../Logging/TestLoggerOptions.cs | 1 - src/Foundatio/Lock/CacheLockProvider.cs | 4 +- src/Foundatio/Queues/QueueBase.cs | 20 +++++----- .../Caching/InMemoryCacheClientTests.cs | 1 - tests/Foundatio.Tests/Hosting/HostingTests.cs | 1 - tests/Foundatio.Tests/Jobs/JobTests.cs | 3 -- .../Foundatio.Tests/Jobs/WorkItemJobTests.cs | 2 - .../Queue/InMemoryQueueTests.cs | 1 - 18 files changed, 30 insertions(+), 60 deletions(-) diff --git a/samples/Foundatio.HostingSample/Jobs/Sample2Job.cs b/samples/Foundatio.HostingSample/Jobs/Sample2Job.cs index 04f2a6b0..6dc2900e 100644 --- a/samples/Foundatio.HostingSample/Jobs/Sample2Job.cs +++ b/samples/Foundatio.HostingSample/Jobs/Sample2Job.cs @@ -2,7 +2,6 @@ using System.Threading; using System.Threading.Tasks; using Foundatio.Jobs; -using Foundatio.Utility; using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Logging; diff --git a/samples/Foundatio.HostingSample/Program.cs b/samples/Foundatio.HostingSample/Program.cs index 258d0b7d..aadc37f0 100644 --- a/samples/Foundatio.HostingSample/Program.cs +++ b/samples/Foundatio.HostingSample/Program.cs @@ -6,8 +6,6 @@ using Foundatio.Caching; using Foundatio.Extensions.Hosting.Jobs; using Foundatio.Extensions.Hosting.Startup; -using Foundatio.Jobs; -using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; diff --git a/src/Foundatio.Extensions.Hosting/Jobs/JobHostExtensions.cs b/src/Foundatio.Extensions.Hosting/Jobs/JobHostExtensions.cs index ca0be9ad..1beff32c 100644 --- a/src/Foundatio.Extensions.Hosting/Jobs/JobHostExtensions.cs +++ b/src/Foundatio.Extensions.Hosting/Jobs/JobHostExtensions.cs @@ -2,7 +2,6 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; -using Foundatio.Extensions.Hosting.Startup; using Foundatio.Jobs; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; diff --git a/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobService.cs b/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobService.cs index ce297019..db521a86 100644 --- a/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobService.cs +++ b/src/Foundatio.Extensions.Hosting/Jobs/ScheduledJobService.cs @@ -1,5 +1,4 @@ using System; -using System.Linq; using System.Threading; using System.Threading.Tasks; using Foundatio.Extensions.Hosting.Startup; diff --git a/src/Foundatio.TestHarness/Caching/CacheClientTestsBase.cs b/src/Foundatio.TestHarness/Caching/CacheClientTestsBase.cs index 88519656..e95d892d 100644 --- a/src/Foundatio.TestHarness/Caching/CacheClientTestsBase.cs +++ b/src/Foundatio.TestHarness/Caching/CacheClientTestsBase.cs @@ -6,10 +6,8 @@ using System.Threading; using System.Threading.Tasks; using Foundatio.Caching; -using Foundatio.Metrics; using Foundatio.Utility; using Foundatio.Xunit; -using Iced.Intel; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Time.Testing; using Xunit; diff --git a/src/Foundatio.TestHarness/Jobs/JobQueueTestsBase.cs b/src/Foundatio.TestHarness/Jobs/JobQueueTestsBase.cs index 0d5b620a..dcdfd55b 100644 --- a/src/Foundatio.TestHarness/Jobs/JobQueueTestsBase.cs +++ b/src/Foundatio.TestHarness/Jobs/JobQueueTestsBase.cs @@ -8,10 +8,8 @@ using Foundatio.Caching; using Foundatio.Jobs; using Foundatio.Lock; -using Foundatio.Metrics; using Foundatio.Queues; using Foundatio.Tests.Metrics; -using Foundatio.Utility; using Foundatio.Xunit; using Microsoft.Extensions.Logging; using Xunit; @@ -178,8 +176,6 @@ await Parallel.ForEachAsync(Enumerable.Range(1, jobCount), async (index, _) => } _logger.LogInformation("Done getting queue stats"); - //var queueSummary = await metrics.GetQueueStatsAsync("test.samplequeueworkitem"); - //Assert.Equal(queueStats.Sum(s => s.Completed), queueSummary.Completed.Count); Assert.InRange(queueStats.Sum(s => s.Completed), 0, workItemCount); } finally diff --git a/src/Foundatio.TestHarness/Jobs/SampleQueueJob.cs b/src/Foundatio.TestHarness/Jobs/SampleQueueJob.cs index ec45bfe9..c8110ada 100644 --- a/src/Foundatio.TestHarness/Jobs/SampleQueueJob.cs +++ b/src/Foundatio.TestHarness/Jobs/SampleQueueJob.cs @@ -6,7 +6,6 @@ using Exceptionless; using Foundatio.Jobs; using Foundatio.Lock; -using Foundatio.Metrics; using Foundatio.Queues; using Microsoft.Extensions.Logging; diff --git a/src/Foundatio.TestHarness/Jobs/WithLockingJob.cs b/src/Foundatio.TestHarness/Jobs/WithLockingJob.cs index 7930b795..853c95dd 100644 --- a/src/Foundatio.TestHarness/Jobs/WithLockingJob.cs +++ b/src/Foundatio.TestHarness/Jobs/WithLockingJob.cs @@ -5,7 +5,6 @@ using Foundatio.Jobs; using Foundatio.Lock; using Foundatio.Messaging; -using Foundatio.Utility; using Microsoft.Extensions.Logging; using Xunit; diff --git a/src/Foundatio.TestHarness/Metrics/DiagnosticsMetricsCollector.cs b/src/Foundatio.TestHarness/Metrics/DiagnosticsMetricsCollector.cs index 9370488d..9fbd2576 100644 --- a/src/Foundatio.TestHarness/Metrics/DiagnosticsMetricsCollector.cs +++ b/src/Foundatio.TestHarness/Metrics/DiagnosticsMetricsCollector.cs @@ -15,6 +15,7 @@ namespace Foundatio.Tests.Metrics; public class DiagnosticsMetricsCollector : IDisposable { + private readonly Timer _timer; private readonly MeterListener _meterListener = new(); private readonly ConcurrentQueue> _byteMeasurements = new(); private readonly ConcurrentQueue> _shortMeasurements = new(); @@ -97,6 +98,8 @@ public DiagnosticsMetricsCollector(Func shouldCollect, ILogger log }); _meterListener.Start(); + + _timer = new Timer(_ => RecordObservableInstruments(), null, TimeSpan.Zero, TimeSpan.FromMilliseconds(50)); } public void RecordObservableInstruments() @@ -344,6 +347,7 @@ public async Task WaitForCounterAsync(string name, Func work, lon public void Dispose() { GC.SuppressFinalize(this); + _timer.Dispose(); _meterListener?.Dispose(); } } diff --git a/src/Foundatio.TestHarness/Queue/QueueTestBase.cs b/src/Foundatio.TestHarness/Queue/QueueTestBase.cs index 155003f9..70dd8c2e 100644 --- a/src/Foundatio.TestHarness/Queue/QueueTestBase.cs +++ b/src/Foundatio.TestHarness/Queue/QueueTestBase.cs @@ -1074,32 +1074,35 @@ Task Handler(object sender, CompletedEventArgs e) _logger.LogTrace("Before dequeue"); var item = await queue.DequeueAsync(); + await Task.Delay(100); await item.CompleteAsync(); item = await queue.DequeueAsync(); + await Task.Delay(100); await item.CompleteAsync(); item = await queue.DequeueAsync(); + await Task.Delay(100); await item.AbandonAsync(); _logger.LogTrace("Before asserts"); Assert.Equal(2, completedCount); metricsCollector.RecordObservableInstruments(); - //Assert.InRange(metricsCollector.GetMax("foundatio.workitemdata.count"), 1, 3); - //Assert.InRange(metricsCollector.GetMax("foundatio.workitemdata.working"), 0, 1); + Assert.InRange(metricsCollector.GetMax("foundatio.workitemdata.count"), 1, 3); + Assert.InRange(metricsCollector.GetMax("foundatio.workitemdata.working"), 0, 1); - Assert.Equal(3, metricsCollector.GetCount("foundatio.workitemdata.simple.enqueued")); - Assert.Equal(3, metricsCollector.GetCount("foundatio.workitemdata.enqueued")); + Assert.Equal(3, metricsCollector.GetCount("foundatio.workitemdata.simple.enqueued")); + Assert.Equal(3, metricsCollector.GetCount("foundatio.workitemdata.enqueued")); - Assert.Equal(3, metricsCollector.GetCount("foundatio.workitemdata.simple.dequeued")); - Assert.Equal(3, metricsCollector.GetCount("foundatio.workitemdata.dequeued")); + Assert.Equal(3, metricsCollector.GetCount("foundatio.workitemdata.simple.dequeued")); + Assert.Equal(3, metricsCollector.GetCount("foundatio.workitemdata.dequeued")); - Assert.Equal(2, metricsCollector.GetCount("foundatio.workitemdata.simple.completed")); - Assert.Equal(2, metricsCollector.GetCount("foundatio.workitemdata.completed")); + Assert.Equal(2, metricsCollector.GetCount("foundatio.workitemdata.simple.completed")); + Assert.Equal(2, metricsCollector.GetCount("foundatio.workitemdata.completed")); - Assert.Equal(1, metricsCollector.GetCount("foundatio.workitemdata.simple.abandoned")); - Assert.Equal(1, metricsCollector.GetCount("foundatio.workitemdata.abandoned")); + Assert.Equal(1, metricsCollector.GetCount("foundatio.workitemdata.simple.abandoned")); + Assert.Equal(1, metricsCollector.GetCount("foundatio.workitemdata.abandoned")); var measurements = metricsCollector.GetMeasurements("foundatio.workitemdata.simple.queuetime"); Assert.Equal(3, measurements.Count); @@ -1110,23 +1113,6 @@ Task Handler(object sender, CompletedEventArgs e) Assert.Equal(3, measurements.Count); measurements = metricsCollector.GetMeasurements("foundatio.workitemdata.processtime"); Assert.Equal(3, measurements.Count); - - if (_assertStats) - { - /*var queueStats = await metrics.GetQueueStatsAsync("metric.workitemdata"); - Assert.Equal(3, queueStats.Enqueued.Count); - Assert.Equal(3, queueStats.Dequeued.Count); - Assert.Equal(2, queueStats.Completed.Count); - Assert.Equal(1, queueStats.Abandoned.Count); - Assert.InRange(queueStats.Count.Max, 1, 3); - Assert.InRange(queueStats.Working.Max, 0, 1); - - var subQueueStats = await metrics.GetQueueStatsAsync("metric.workitemdata", "simple"); - Assert.Equal(3, subQueueStats.Enqueued.Count); - Assert.Equal(3, subQueueStats.Dequeued.Count); - Assert.Equal(2, subQueueStats.Completed.Count); - Assert.Equal(1, subQueueStats.Abandoned.Count);*/ - } } } diff --git a/src/Foundatio.Xunit/Logging/TestLoggerOptions.cs b/src/Foundatio.Xunit/Logging/TestLoggerOptions.cs index 354ad063..cfd77d8b 100644 --- a/src/Foundatio.Xunit/Logging/TestLoggerOptions.cs +++ b/src/Foundatio.Xunit/Logging/TestLoggerOptions.cs @@ -1,5 +1,4 @@ using System; -using Foundatio.Utility; using Microsoft.Extensions.Logging; using Xunit.Abstractions; diff --git a/src/Foundatio/Lock/CacheLockProvider.cs b/src/Foundatio/Lock/CacheLockProvider.cs index 140cbdd7..3bfb7ac3 100644 --- a/src/Foundatio/Lock/CacheLockProvider.cs +++ b/src/Foundatio/Lock/CacheLockProvider.cs @@ -25,7 +25,9 @@ public class CacheLockProvider : ILockProvider, IHaveLogger, IHaveTimeProvider private readonly Histogram _lockWaitTimeHistogram; private readonly Counter _lockTimeoutCounter; - public CacheLockProvider(ICacheClient cacheClient, IMessageBus messageBus, TimeProvider timeProvider = null, ILoggerFactory loggerFactory = null) + public CacheLockProvider(ICacheClient cacheClient, IMessageBus messageBus, ILoggerFactory loggerFactory = null) : this(cacheClient, messageBus, null, loggerFactory) { } + + public CacheLockProvider(ICacheClient cacheClient, IMessageBus messageBus, TimeProvider timeProvider, ILoggerFactory loggerFactory = null) { _timeProvider = timeProvider ?? cacheClient.GetTimeProvider(); _logger = loggerFactory?.CreateLogger() ?? NullLogger.Instance; diff --git a/src/Foundatio/Queues/QueueBase.cs b/src/Foundatio/Queues/QueueBase.cs index 84ab845a..2231b429 100644 --- a/src/Foundatio/Queues/QueueBase.cs +++ b/src/Foundatio/Queues/QueueBase.cs @@ -19,13 +19,13 @@ public abstract class QueueBase : MaintenanceBase, IQueue, IHave private readonly string _metricsPrefix; protected readonly ISerializer _serializer; - private readonly Counter _enqueuedCounter; - private readonly Counter _dequeuedCounter; + private readonly Counter _enqueuedCounter; + private readonly Counter _dequeuedCounter; private readonly Histogram _queueTimeHistogram; - private readonly Counter _completedCounter; + private readonly Counter _completedCounter; private readonly Histogram _processTimeHistogram; private readonly Histogram _totalTimeHistogram; - private readonly Counter _abandonedCounter; + private readonly Counter _abandonedCounter; #pragma warning disable IDE0052 // Remove unread private members private readonly ObservableGauge _countGauge; private readonly ObservableGauge _workingGauge; @@ -50,13 +50,13 @@ protected QueueBase(TOptions options) : base(options?.TimeProvider, options?.Log _queueDisposedCancellationTokenSource = new CancellationTokenSource(); // setup meters - _enqueuedCounter = FoundatioDiagnostics.Meter.CreateCounter(GetFullMetricName("enqueued"), description: "Number of enqueued items"); - _dequeuedCounter = FoundatioDiagnostics.Meter.CreateCounter(GetFullMetricName("dequeued"), description: "Number of dequeued items"); + _enqueuedCounter = FoundatioDiagnostics.Meter.CreateCounter(GetFullMetricName("enqueued"), description: "Number of enqueued items"); + _dequeuedCounter = FoundatioDiagnostics.Meter.CreateCounter(GetFullMetricName("dequeued"), description: "Number of dequeued items"); _queueTimeHistogram = FoundatioDiagnostics.Meter.CreateHistogram(GetFullMetricName("queuetime"), description: "Time in queue", unit: "ms"); - _completedCounter = FoundatioDiagnostics.Meter.CreateCounter(GetFullMetricName("completed"), description: "Number of completed items"); + _completedCounter = FoundatioDiagnostics.Meter.CreateCounter(GetFullMetricName("completed"), description: "Number of completed items"); _processTimeHistogram = FoundatioDiagnostics.Meter.CreateHistogram(GetFullMetricName("processtime"), description: "Time to process items", unit: "ms"); _totalTimeHistogram = FoundatioDiagnostics.Meter.CreateHistogram(GetFullMetricName("totaltime"), description: "Total time in queue", unit: "ms"); - _abandonedCounter = FoundatioDiagnostics.Meter.CreateCounter(GetFullMetricName("abandoned"), description: "Number of abandoned items"); + _abandonedCounter = FoundatioDiagnostics.Meter.CreateCounter(GetFullMetricName("abandoned"), description: "Number of abandoned items"); var queueMetricValues = new InstrumentsValues(() => { @@ -308,7 +308,7 @@ protected string GetSubMetricName(T data) return haveStatName?.SubMetricName; } - protected readonly ConcurrentDictionary> _counters = new(); + protected readonly ConcurrentDictionary> _counters = new(); private void IncrementSubCounter(T data, string name, in TagList tags) { if (data is not IHaveSubMetricName) @@ -319,7 +319,7 @@ private void IncrementSubCounter(T data, string name, in TagList tags) return; var fullName = GetFullMetricName(subMetricName, name); - _counters.GetOrAdd(fullName, FoundatioDiagnostics.Meter.CreateCounter(fullName)).Add(1, tags); + _counters.GetOrAdd(fullName, FoundatioDiagnostics.Meter.CreateCounter(fullName)).Add(1, tags); } protected readonly ConcurrentDictionary> _histograms = new(); diff --git a/tests/Foundatio.Tests/Caching/InMemoryCacheClientTests.cs b/tests/Foundatio.Tests/Caching/InMemoryCacheClientTests.cs index 20b1fee8..b5a3b421 100644 --- a/tests/Foundatio.Tests/Caching/InMemoryCacheClientTests.cs +++ b/tests/Foundatio.Tests/Caching/InMemoryCacheClientTests.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Threading.Tasks; using Foundatio.Caching; -using Foundatio.Utility; using Microsoft.Extensions.Logging; using Xunit; using Xunit.Abstractions; diff --git a/tests/Foundatio.Tests/Hosting/HostingTests.cs b/tests/Foundatio.Tests/Hosting/HostingTests.cs index b5eb71e4..1ce456a8 100644 --- a/tests/Foundatio.Tests/Hosting/HostingTests.cs +++ b/tests/Foundatio.Tests/Hosting/HostingTests.cs @@ -10,7 +10,6 @@ using Microsoft.AspNetCore.TestHost; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; using Xunit; using Xunit.Abstractions; diff --git a/tests/Foundatio.Tests/Jobs/JobTests.cs b/tests/Foundatio.Tests/Jobs/JobTests.cs index 23312e9b..76d2d6e9 100644 --- a/tests/Foundatio.Tests/Jobs/JobTests.cs +++ b/tests/Foundatio.Tests/Jobs/JobTests.cs @@ -1,14 +1,11 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Threading; using System.Threading.Tasks; using Foundatio.Caching; using Foundatio.Jobs; -using Foundatio.Metrics; -using Foundatio.Utility; using Foundatio.Xunit; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; diff --git a/tests/Foundatio.Tests/Jobs/WorkItemJobTests.cs b/tests/Foundatio.Tests/Jobs/WorkItemJobTests.cs index 67c1083d..9f977610 100644 --- a/tests/Foundatio.Tests/Jobs/WorkItemJobTests.cs +++ b/tests/Foundatio.Tests/Jobs/WorkItemJobTests.cs @@ -9,10 +9,8 @@ using Foundatio.AsyncEx; using Foundatio.Jobs; using Foundatio.Messaging; -using Foundatio.Metrics; using Foundatio.Queues; using Foundatio.Tests.Extensions; -using Foundatio.Utility; using Foundatio.Xunit; using Microsoft.Extensions.Logging; using Xunit; diff --git a/tests/Foundatio.Tests/Queue/InMemoryQueueTests.cs b/tests/Foundatio.Tests/Queue/InMemoryQueueTests.cs index 295ea1e3..4ee2d285 100644 --- a/tests/Foundatio.Tests/Queue/InMemoryQueueTests.cs +++ b/tests/Foundatio.Tests/Queue/InMemoryQueueTests.cs @@ -4,7 +4,6 @@ using System.Threading; using System.Threading.Tasks; using Foundatio.Queues; -using Foundatio.Utility; using Microsoft.Extensions.Logging; using Xunit; using Xunit.Abstractions;