Skip to content

Commit

Permalink
More test cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ejsmith committed Aug 7, 2024
1 parent c5eb189 commit eea0232
Show file tree
Hide file tree
Showing 18 changed files with 30 additions and 60 deletions.
1 change: 0 additions & 1 deletion samples/Foundatio.HostingSample/Jobs/Sample2Job.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 0 additions & 2 deletions samples/Foundatio.HostingSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/Foundatio.Extensions.Hosting/Jobs/JobHostExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Foundatio.Extensions.Hosting.Startup;
Expand Down
2 changes: 0 additions & 2 deletions src/Foundatio.TestHarness/Caching/CacheClientTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 0 additions & 4 deletions src/Foundatio.TestHarness/Jobs/JobQueueTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/Foundatio.TestHarness/Jobs/SampleQueueJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Exceptionless;
using Foundatio.Jobs;
using Foundatio.Lock;
using Foundatio.Metrics;
using Foundatio.Queues;
using Microsoft.Extensions.Logging;

Expand Down
1 change: 0 additions & 1 deletion src/Foundatio.TestHarness/Jobs/WithLockingJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Foundatio.Jobs;
using Foundatio.Lock;
using Foundatio.Messaging;
using Foundatio.Utility;
using Microsoft.Extensions.Logging;
using Xunit;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Foundatio.Tests.Metrics;

public class DiagnosticsMetricsCollector : IDisposable
{
private readonly Timer _timer;
private readonly MeterListener _meterListener = new();
private readonly ConcurrentQueue<RecordedMeasurement<byte>> _byteMeasurements = new();
private readonly ConcurrentQueue<RecordedMeasurement<short>> _shortMeasurements = new();
Expand Down Expand Up @@ -97,6 +98,8 @@ public DiagnosticsMetricsCollector(Func<string, bool> shouldCollect, ILogger log
});

_meterListener.Start();

_timer = new Timer(_ => RecordObservableInstruments(), null, TimeSpan.Zero, TimeSpan.FromMilliseconds(50));
}

public void RecordObservableInstruments()
Expand Down Expand Up @@ -344,6 +347,7 @@ public async Task<bool> WaitForCounterAsync<T>(string name, Func<Task> work, lon
public void Dispose()
{
GC.SuppressFinalize(this);
_timer.Dispose();
_meterListener?.Dispose();
}
}
Expand Down
40 changes: 13 additions & 27 deletions src/Foundatio.TestHarness/Queue/QueueTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1074,32 +1074,35 @@ Task Handler(object sender, CompletedEventArgs<WorkItemData> 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<int>("foundatio.workitemdata.count"), 1, 3);
//Assert.InRange(metricsCollector.GetMax<int>("foundatio.workitemdata.working"), 0, 1);
Assert.InRange(metricsCollector.GetMax<long>("foundatio.workitemdata.count"), 1, 3);
Assert.InRange(metricsCollector.GetMax<long>("foundatio.workitemdata.working"), 0, 1);

Assert.Equal(3, metricsCollector.GetCount<int>("foundatio.workitemdata.simple.enqueued"));
Assert.Equal(3, metricsCollector.GetCount<int>("foundatio.workitemdata.enqueued"));
Assert.Equal(3, metricsCollector.GetCount<long>("foundatio.workitemdata.simple.enqueued"));
Assert.Equal(3, metricsCollector.GetCount<long>("foundatio.workitemdata.enqueued"));

Assert.Equal(3, metricsCollector.GetCount<int>("foundatio.workitemdata.simple.dequeued"));
Assert.Equal(3, metricsCollector.GetCount<int>("foundatio.workitemdata.dequeued"));
Assert.Equal(3, metricsCollector.GetCount<long>("foundatio.workitemdata.simple.dequeued"));
Assert.Equal(3, metricsCollector.GetCount<long>("foundatio.workitemdata.dequeued"));

Assert.Equal(2, metricsCollector.GetCount<int>("foundatio.workitemdata.simple.completed"));
Assert.Equal(2, metricsCollector.GetCount<int>("foundatio.workitemdata.completed"));
Assert.Equal(2, metricsCollector.GetCount<long>("foundatio.workitemdata.simple.completed"));
Assert.Equal(2, metricsCollector.GetCount<long>("foundatio.workitemdata.completed"));

Assert.Equal(1, metricsCollector.GetCount<int>("foundatio.workitemdata.simple.abandoned"));
Assert.Equal(1, metricsCollector.GetCount<int>("foundatio.workitemdata.abandoned"));
Assert.Equal(1, metricsCollector.GetCount<long>("foundatio.workitemdata.simple.abandoned"));
Assert.Equal(1, metricsCollector.GetCount<long>("foundatio.workitemdata.abandoned"));

var measurements = metricsCollector.GetMeasurements<double>("foundatio.workitemdata.simple.queuetime");
Assert.Equal(3, measurements.Count);
Expand All @@ -1110,23 +1113,6 @@ Task Handler(object sender, CompletedEventArgs<WorkItemData> e)
Assert.Equal(3, measurements.Count);
measurements = metricsCollector.GetMeasurements<double>("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);*/
}
}
}

Expand Down
1 change: 0 additions & 1 deletion src/Foundatio.Xunit/Logging/TestLoggerOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Foundatio.Utility;
using Microsoft.Extensions.Logging;
using Xunit.Abstractions;

Expand Down
4 changes: 3 additions & 1 deletion src/Foundatio/Lock/CacheLockProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public class CacheLockProvider : ILockProvider, IHaveLogger, IHaveTimeProvider
private readonly Histogram<double> _lockWaitTimeHistogram;
private readonly Counter<int> _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<CacheLockProvider>() ?? NullLogger<CacheLockProvider>.Instance;
Expand Down
20 changes: 10 additions & 10 deletions src/Foundatio/Queues/QueueBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public abstract class QueueBase<T, TOptions> : MaintenanceBase, IQueue<T>, IHave
private readonly string _metricsPrefix;
protected readonly ISerializer _serializer;

private readonly Counter<int> _enqueuedCounter;
private readonly Counter<int> _dequeuedCounter;
private readonly Counter<long> _enqueuedCounter;
private readonly Counter<long> _dequeuedCounter;
private readonly Histogram<double> _queueTimeHistogram;
private readonly Counter<int> _completedCounter;
private readonly Counter<long> _completedCounter;
private readonly Histogram<double> _processTimeHistogram;
private readonly Histogram<double> _totalTimeHistogram;
private readonly Counter<int> _abandonedCounter;
private readonly Counter<long> _abandonedCounter;
#pragma warning disable IDE0052 // Remove unread private members
private readonly ObservableGauge<long> _countGauge;
private readonly ObservableGauge<long> _workingGauge;
Expand All @@ -50,13 +50,13 @@ protected QueueBase(TOptions options) : base(options?.TimeProvider, options?.Log
_queueDisposedCancellationTokenSource = new CancellationTokenSource();

// setup meters
_enqueuedCounter = FoundatioDiagnostics.Meter.CreateCounter<int>(GetFullMetricName("enqueued"), description: "Number of enqueued items");
_dequeuedCounter = FoundatioDiagnostics.Meter.CreateCounter<int>(GetFullMetricName("dequeued"), description: "Number of dequeued items");
_enqueuedCounter = FoundatioDiagnostics.Meter.CreateCounter<long>(GetFullMetricName("enqueued"), description: "Number of enqueued items");
_dequeuedCounter = FoundatioDiagnostics.Meter.CreateCounter<long>(GetFullMetricName("dequeued"), description: "Number of dequeued items");
_queueTimeHistogram = FoundatioDiagnostics.Meter.CreateHistogram<double>(GetFullMetricName("queuetime"), description: "Time in queue", unit: "ms");
_completedCounter = FoundatioDiagnostics.Meter.CreateCounter<int>(GetFullMetricName("completed"), description: "Number of completed items");
_completedCounter = FoundatioDiagnostics.Meter.CreateCounter<long>(GetFullMetricName("completed"), description: "Number of completed items");
_processTimeHistogram = FoundatioDiagnostics.Meter.CreateHistogram<double>(GetFullMetricName("processtime"), description: "Time to process items", unit: "ms");
_totalTimeHistogram = FoundatioDiagnostics.Meter.CreateHistogram<double>(GetFullMetricName("totaltime"), description: "Total time in queue", unit: "ms");
_abandonedCounter = FoundatioDiagnostics.Meter.CreateCounter<int>(GetFullMetricName("abandoned"), description: "Number of abandoned items");
_abandonedCounter = FoundatioDiagnostics.Meter.CreateCounter<long>(GetFullMetricName("abandoned"), description: "Number of abandoned items");

var queueMetricValues = new InstrumentsValues<long, long, long>(() =>
{
Expand Down Expand Up @@ -308,7 +308,7 @@ protected string GetSubMetricName(T data)
return haveStatName?.SubMetricName;
}

protected readonly ConcurrentDictionary<string, Counter<int>> _counters = new();
protected readonly ConcurrentDictionary<string, Counter<long>> _counters = new();
private void IncrementSubCounter(T data, string name, in TagList tags)
{
if (data is not IHaveSubMetricName)
Expand All @@ -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<int>(fullName)).Add(1, tags);
_counters.GetOrAdd(fullName, FoundatioDiagnostics.Meter.CreateCounter<long>(fullName)).Add(1, tags);
}

protected readonly ConcurrentDictionary<string, Histogram<double>> _histograms = new();
Expand Down
1 change: 0 additions & 1 deletion tests/Foundatio.Tests/Caching/InMemoryCacheClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion tests/Foundatio.Tests/Hosting/HostingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 0 additions & 3 deletions tests/Foundatio.Tests/Jobs/JobTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 0 additions & 2 deletions tests/Foundatio.Tests/Jobs/WorkItemJobTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion tests/Foundatio.Tests/Queue/InMemoryQueueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit eea0232

Please sign in to comment.