diff --git a/src/Foundatio.TestHarness/Caching/CacheClientTestsBase.cs b/src/Foundatio.TestHarness/Caching/CacheClientTestsBase.cs index def4a62c..928eea88 100644 --- a/src/Foundatio.TestHarness/Caching/CacheClientTestsBase.cs +++ b/src/Foundatio.TestHarness/Caching/CacheClientTestsBase.cs @@ -13,7 +13,7 @@ namespace Foundatio.Tests.Caching; -public abstract class CacheClientTestsBase : TestLoggerBase +public abstract class CacheClientTestsBase : TestWithLoggingBase { protected CacheClientTestsBase(ITestOutputHelper output) : base(output) { diff --git a/src/Foundatio.TestHarness/Jobs/JobQueueTestsBase.cs b/src/Foundatio.TestHarness/Jobs/JobQueueTestsBase.cs index 71239077..8af588f6 100644 --- a/src/Foundatio.TestHarness/Jobs/JobQueueTestsBase.cs +++ b/src/Foundatio.TestHarness/Jobs/JobQueueTestsBase.cs @@ -18,7 +18,7 @@ namespace Foundatio.Tests.Jobs; -public abstract class JobQueueTestsBase : TestLoggerBase +public abstract class JobQueueTestsBase : TestWithLoggingBase { private readonly ActivitySource _activitySource = new(nameof(JobQueueTestsBase)); diff --git a/src/Foundatio.TestHarness/Locks/LockTestBase.cs b/src/Foundatio.TestHarness/Locks/LockTestBase.cs index a145c955..1b987aab 100644 --- a/src/Foundatio.TestHarness/Locks/LockTestBase.cs +++ b/src/Foundatio.TestHarness/Locks/LockTestBase.cs @@ -14,7 +14,7 @@ namespace Foundatio.Tests.Locks; -public abstract class LockTestBase : TestLoggerBase +public abstract class LockTestBase : TestWithLoggingBase { protected LockTestBase(ITestOutputHelper output) : base(output) { } diff --git a/src/Foundatio.TestHarness/Messaging/MessageBusTestBase.cs b/src/Foundatio.TestHarness/Messaging/MessageBusTestBase.cs index 57eef070..fc68da43 100644 --- a/src/Foundatio.TestHarness/Messaging/MessageBusTestBase.cs +++ b/src/Foundatio.TestHarness/Messaging/MessageBusTestBase.cs @@ -16,7 +16,7 @@ namespace Foundatio.Tests.Messaging; -public abstract class MessageBusTestBase : TestLoggerBase +public abstract class MessageBusTestBase : TestWithLoggingBase { protected MessageBusTestBase(ITestOutputHelper output) : base(output) { diff --git a/src/Foundatio.TestHarness/Metrics/MetricsClientTestBase.cs b/src/Foundatio.TestHarness/Metrics/MetricsClientTestBase.cs index 7acaa0b8..dd8cde1e 100644 --- a/src/Foundatio.TestHarness/Metrics/MetricsClientTestBase.cs +++ b/src/Foundatio.TestHarness/Metrics/MetricsClientTestBase.cs @@ -14,7 +14,7 @@ namespace Foundatio.Tests.Metrics; -public abstract class MetricsClientTestBase : TestLoggerBase +public abstract class MetricsClientTestBase : TestWithLoggingBase { public MetricsClientTestBase(ITestOutputHelper output) : base(output) { } diff --git a/src/Foundatio.TestHarness/Queue/QueueTestBase.cs b/src/Foundatio.TestHarness/Queue/QueueTestBase.cs index c4774647..be043688 100644 --- a/src/Foundatio.TestHarness/Queue/QueueTestBase.cs +++ b/src/Foundatio.TestHarness/Queue/QueueTestBase.cs @@ -23,7 +23,7 @@ namespace Foundatio.Tests.Queue; -public abstract class QueueTestBase : TestLoggerBase, IDisposable +public abstract class QueueTestBase : TestWithLoggingBase, IDisposable { protected QueueTestBase(ITestOutputHelper output) : base(output) { diff --git a/src/Foundatio.TestHarness/Serializer/SerializerTestsBase.cs b/src/Foundatio.TestHarness/Serializer/SerializerTestsBase.cs index 6935d2c6..85e9651d 100644 --- a/src/Foundatio.TestHarness/Serializer/SerializerTestsBase.cs +++ b/src/Foundatio.TestHarness/Serializer/SerializerTestsBase.cs @@ -9,7 +9,7 @@ namespace Foundatio.Tests.Serializer; -public abstract class SerializerTestsBase : TestLoggerBase +public abstract class SerializerTestsBase : TestWithLoggingBase { protected SerializerTestsBase(ITestOutputHelper output) : base(output) { } diff --git a/src/Foundatio.TestHarness/Storage/FileStorageTestsBase.cs b/src/Foundatio.TestHarness/Storage/FileStorageTestsBase.cs index 772b9b7d..ea3294a0 100644 --- a/src/Foundatio.TestHarness/Storage/FileStorageTestsBase.cs +++ b/src/Foundatio.TestHarness/Storage/FileStorageTestsBase.cs @@ -16,7 +16,7 @@ namespace Foundatio.Tests.Storage; -public abstract class FileStorageTestsBase : TestLoggerBase +public abstract class FileStorageTestsBase : TestWithLoggingBase { protected FileStorageTestsBase(ITestOutputHelper output) : base(output) { } diff --git a/src/Foundatio.Xunit/Logging/TestLogger.cs b/src/Foundatio.Xunit/Logging/TestLogger.cs index 8b5ee870..11554a43 100644 --- a/src/Foundatio.Xunit/Logging/TestLogger.cs +++ b/src/Foundatio.Xunit/Logging/TestLogger.cs @@ -11,6 +11,7 @@ public class TestLogger : ILoggerFactory { private readonly Dictionary _logLevels = new(); private readonly Queue _logEntries = new(); + private int _logEntriesWritten; public TestLogger(Action configure = null) { @@ -65,7 +66,14 @@ public int MaxLogEntriesToWrite public IReadOnlyList LogEntries => _logEntries.ToArray(); - public void Clear() => _logEntries.Clear(); + public void Clear() + { + lock (_logEntries) + { + _logEntries.Clear(); + Interlocked.Exchange(ref _logEntriesWritten, 0); + } + } internal void AddLogEntry(LogEntry logEntry) { @@ -91,8 +99,6 @@ internal void AddLogEntry(LogEntry logEntry) } } - private int _logEntriesWritten = 0; - public ILogger CreateLogger(string categoryName) { return new TestLoggerLogger(categoryName, this); diff --git a/src/Foundatio.Xunit/Logging/TestLoggerBase.cs b/src/Foundatio.Xunit/Logging/TestLoggerBase.cs index 049c3935..6aa2c907 100644 --- a/src/Foundatio.Xunit/Logging/TestLoggerBase.cs +++ b/src/Foundatio.Xunit/Logging/TestLoggerBase.cs @@ -1,61 +1,38 @@ using System; -using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using Xunit; using Xunit.Abstractions; namespace Foundatio.Xunit; -public abstract class TestLoggerBase : IAsyncDisposable +public abstract class TestLoggerBase : IClassFixture, IAsyncLifetime { - protected readonly ILogger _logger; - protected readonly List _disposables = []; - - protected TestLoggerBase(ITestOutputHelper output) - { - var services = new ServiceCollection(); - RegisterRequiredServices(services, output); - var sp = services.BuildServiceProvider(); - _disposables.Add(sp); - Services = sp; - Log = Services.GetTestLogger(); - _logger = Log.CreateLogger(GetType()); - } - - protected IServiceProvider Services { get; } - - protected TService GetService() where TService : notnull + protected TestLoggerBase(ITestOutputHelper output, TestLoggerFixture fixture) { - return Services.GetRequiredService(); + Fixture = fixture; + fixture.Output = output; + fixture.AddServiceRegistrations(RegisterServices); } - protected TestLogger Log { get; } + protected TestLoggerFixture Fixture { get; } + protected IServiceProvider Services => Fixture.Services; + protected TestLogger TestLogger => Fixture.TestLogger; + protected ILogger Log => Fixture.Log; - private void RegisterRequiredServices(IServiceCollection services, ITestOutputHelper output) + protected virtual void RegisterServices(IServiceCollection services) { - services.AddLogging(c => c.AddTestLogger(output)); - RegisterServices(services); } - protected virtual void RegisterServices(IServiceCollection services) + public virtual Task InitializeAsync() { + return Task.CompletedTask; } - public virtual ValueTask DisposeAsync() + public virtual Task DisposeAsync() { - foreach (var disposable in _disposables) - { - try - { - disposable.Dispose(); - } - catch (Exception ex) - { - _logger?.LogError(ex, "Error disposing resource."); - } - } - - return new ValueTask(Task.CompletedTask); + Fixture.TestLogger.Clear(); + return Task.CompletedTask; } } diff --git a/src/Foundatio.Xunit/Logging/TestLoggerFixture.cs b/src/Foundatio.Xunit/Logging/TestLoggerFixture.cs index 7f8747eb..22388fdc 100644 --- a/src/Foundatio.Xunit/Logging/TestLoggerFixture.cs +++ b/src/Foundatio.Xunit/Logging/TestLoggerFixture.cs @@ -31,7 +31,6 @@ public void AddServiceRegistrations(Action registerServices) } public IServiceProvider Services => _serviceProvider.Value; - public TestLogger TestLogger => _testLogger.Value; public ILogger Log => _log.Value; diff --git a/src/Foundatio.Xunit/Logging/TestWithLoggingBase.cs b/src/Foundatio.Xunit/Logging/TestWithLoggingBase.cs index 9c4e1978..e3adbc67 100644 --- a/src/Foundatio.Xunit/Logging/TestWithLoggingBase.cs +++ b/src/Foundatio.Xunit/Logging/TestWithLoggingBase.cs @@ -1,10 +1,8 @@ -using System; -using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging; using Xunit.Abstractions; namespace Foundatio.Xunit; -[Obsolete("Use TestLoggerBase instead.")] public abstract class TestWithLoggingBase { protected readonly ILogger _logger; diff --git a/tests/Foundatio.Tests/Jobs/JobTests.cs b/tests/Foundatio.Tests/Jobs/JobTests.cs index 54ac43bf..a82cb52f 100644 --- a/tests/Foundatio.Tests/Jobs/JobTests.cs +++ b/tests/Foundatio.Tests/Jobs/JobTests.cs @@ -15,7 +15,7 @@ namespace Foundatio.Tests.Jobs; -public class JobTests : TestLoggerBase +public class JobTests : TestWithLoggingBase { public JobTests(ITestOutputHelper output) : base(output) { } diff --git a/tests/Foundatio.Tests/Jobs/WorkItemJobTests.cs b/tests/Foundatio.Tests/Jobs/WorkItemJobTests.cs index 9de1408f..575f76f6 100644 --- a/tests/Foundatio.Tests/Jobs/WorkItemJobTests.cs +++ b/tests/Foundatio.Tests/Jobs/WorkItemJobTests.cs @@ -19,7 +19,7 @@ namespace Foundatio.Tests.Jobs; -public class WorkItemJobTests : TestLoggerBase +public class WorkItemJobTests : TestWithLoggingBase { public WorkItemJobTests(ITestOutputHelper output) : base(output) { } diff --git a/tests/Foundatio.Tests/Metrics/DiagnosticsMetricsTests.cs b/tests/Foundatio.Tests/Metrics/DiagnosticsMetricsTests.cs index e96e83f5..92c1c4f6 100644 --- a/tests/Foundatio.Tests/Metrics/DiagnosticsMetricsTests.cs +++ b/tests/Foundatio.Tests/Metrics/DiagnosticsMetricsTests.cs @@ -10,7 +10,7 @@ namespace Foundatio.Tests.Metrics; -public class DiagnosticsMetricsTests : TestLoggerBase, IDisposable +public class DiagnosticsMetricsTests : TestWithLoggingBase, IDisposable { private readonly DiagnosticsMetricsClient _client; diff --git a/tests/Foundatio.Tests/Utility/CloneTests.cs b/tests/Foundatio.Tests/Utility/CloneTests.cs index 09cec44b..628da8ee 100644 --- a/tests/Foundatio.Tests/Utility/CloneTests.cs +++ b/tests/Foundatio.Tests/Utility/CloneTests.cs @@ -9,7 +9,7 @@ namespace Foundatio.Tests.Utility; -public class CloneTests : TestLoggerBase +public class CloneTests : TestWithLoggingBase { public CloneTests(ITestOutputHelper output) : base(output) { } diff --git a/tests/Foundatio.Tests/Utility/DataDictionaryTests.cs b/tests/Foundatio.Tests/Utility/DataDictionaryTests.cs index 7cac547e..96deaa5e 100644 --- a/tests/Foundatio.Tests/Utility/DataDictionaryTests.cs +++ b/tests/Foundatio.Tests/Utility/DataDictionaryTests.cs @@ -10,7 +10,7 @@ namespace Foundatio.Tests.Utility; -public class DataDictionaryTests : TestLoggerBase +public class DataDictionaryTests : TestWithLoggingBase { public DataDictionaryTests(ITestOutputHelper output) : base(output) { } diff --git a/tests/Foundatio.Tests/Utility/RunTests.cs b/tests/Foundatio.Tests/Utility/RunTests.cs index d32ca288..fa47098a 100644 --- a/tests/Foundatio.Tests/Utility/RunTests.cs +++ b/tests/Foundatio.Tests/Utility/RunTests.cs @@ -9,7 +9,7 @@ namespace Foundatio.Tests.Utility; -public class RunTests : TestLoggerBase +public class RunTests : TestWithLoggingBase { public RunTests(ITestOutputHelper output) : base(output) { } diff --git a/tests/Foundatio.Tests/Utility/ScheduledTimerTests.cs b/tests/Foundatio.Tests/Utility/ScheduledTimerTests.cs index 947a1a9e..fae29893 100644 --- a/tests/Foundatio.Tests/Utility/ScheduledTimerTests.cs +++ b/tests/Foundatio.Tests/Utility/ScheduledTimerTests.cs @@ -11,7 +11,7 @@ namespace Foundatio.Tests.Utility; -public class ScheduledTimerTests : TestLoggerBase +public class ScheduledTimerTests : TestWithLoggingBase { public ScheduledTimerTests(ITestOutputHelper output) : base(output) { diff --git a/tests/Foundatio.Tests/Utility/SystemClockTests.cs b/tests/Foundatio.Tests/Utility/SystemClockTests.cs index b0c05e1b..dd14b656 100644 --- a/tests/Foundatio.Tests/Utility/SystemClockTests.cs +++ b/tests/Foundatio.Tests/Utility/SystemClockTests.cs @@ -8,7 +8,7 @@ namespace Foundatio.Tests.Utility; -public class SystemClockTests : TestLoggerBase +public class SystemClockTests : TestWithLoggingBase { public SystemClockTests(ITestOutputHelper output) : base(output) { } diff --git a/tests/Foundatio.Tests/Utility/TestLoggerTests.cs b/tests/Foundatio.Tests/Utility/TestLoggerTests.cs index 07f6548d..3e3f23f3 100644 --- a/tests/Foundatio.Tests/Utility/TestLoggerTests.cs +++ b/tests/Foundatio.Tests/Utility/TestLoggerTests.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using Foundatio.Xunit; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -7,16 +8,13 @@ namespace Foundatio.Tests.Utility; -public class TestLoggerTests : IClassFixture +public class TestLoggerTests : TestLoggerBase { private readonly ITestOutputHelper _output; - private readonly TestLoggerFixture _fixture; - public TestLoggerTests(ITestOutputHelper output, TestLoggerFixture fixture) + public TestLoggerTests(ITestOutputHelper output, TestLoggerFixture fixture) : base(output, fixture) { _output = output; - _fixture = fixture; - _fixture.Output = output; fixture.AddServiceRegistrations(s => s.AddSingleton()); } @@ -31,7 +29,7 @@ public void CanUseTestLogger() var someClass = provider.GetRequiredService(); - someClass.DoSomething(); + someClass.DoSomething(1); var testLogger = provider.GetTestLogger(); Assert.Single(testLogger.LogEntries); @@ -40,27 +38,49 @@ public void CanUseTestLogger() testLogger.Clear(); testLogger.SetLogLevel(LogLevel.Error); - someClass.DoSomething(); + someClass.DoSomething(2); Assert.Empty(testLogger.LogEntries); } [Fact] public void CanUseTestLoggerFixture() { - var someClass = _fixture.Services.GetRequiredService(); + var someClass = Services.GetRequiredService(); - someClass.DoSomething(); + for (int i = 1; i <= 9999; i++) + someClass.DoSomething(i); - Assert.Single(_fixture.TestLogger.LogEntries); - Assert.Contains("Doing something", _fixture.TestLogger.LogEntries[0].Message); + Log.LogInformation("Hello 1"); + Log.LogInformation("Hello 2"); - _fixture.TestLogger.Clear(); - _fixture.TestLogger.SetLogLevel(LogLevel.Error); + Assert.Equal(100, TestLogger.LogEntries.Count); + Assert.Contains("Hello 2", TestLogger.LogEntries.Last().Message); - someClass.DoSomething(); - Assert.Empty(_fixture.TestLogger.LogEntries); + Fixture.TestLogger.Clear(); + TestLogger.SetLogLevel(LogLevel.Error); + + someClass.DoSomething(1002); + + Assert.Empty(TestLogger.LogEntries); + TestLogger.SetLogLevel(LogLevel.Information); } + [Fact] + public void CanUseTestLoggerFixture2() + { + var someClass = Services.GetRequiredService(); + + someClass.DoSomething(1); + + Assert.Single(TestLogger.LogEntries); + Assert.Contains("Doing something", TestLogger.LogEntries[0].Message); + + TestLogger.Clear(); + TestLogger.SetLogLevel(LogLevel.Error); + + someClass.DoSomething(2); + Assert.Empty(TestLogger.LogEntries); + } } public class SomeClass @@ -72,8 +92,8 @@ public SomeClass(ILogger logger) _logger = logger; } - public void DoSomething() + public void DoSomething(int number) { - _logger.LogInformation("Doing something"); + _logger.LogInformation("Doing something {Number}", number); } }