Skip to content

Commit

Permalink
More updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ejsmith committed Feb 13, 2024
1 parent f69b1b6 commit 7f77da3
Show file tree
Hide file tree
Showing 21 changed files with 79 additions and 79 deletions.
2 changes: 1 addition & 1 deletion src/Foundatio.TestHarness/Caching/CacheClientTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Foundatio.Tests.Caching;

public abstract class CacheClientTestsBase : TestLoggerBase
public abstract class CacheClientTestsBase : TestWithLoggingBase
{
protected CacheClientTestsBase(ITestOutputHelper output) : base(output)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Foundatio.TestHarness/Jobs/JobQueueTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
2 changes: 1 addition & 1 deletion src/Foundatio.TestHarness/Locks/LockTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Foundatio.Tests.Locks;

public abstract class LockTestBase : TestLoggerBase
public abstract class LockTestBase : TestWithLoggingBase
{
protected LockTestBase(ITestOutputHelper output) : base(output) { }

Expand Down
2 changes: 1 addition & 1 deletion src/Foundatio.TestHarness/Messaging/MessageBusTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace Foundatio.Tests.Messaging;

public abstract class MessageBusTestBase : TestLoggerBase
public abstract class MessageBusTestBase : TestWithLoggingBase
{
protected MessageBusTestBase(ITestOutputHelper output) : base(output)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Foundatio.TestHarness/Metrics/MetricsClientTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Foundatio.Tests.Metrics;

public abstract class MetricsClientTestBase : TestLoggerBase
public abstract class MetricsClientTestBase : TestWithLoggingBase
{
public MetricsClientTestBase(ITestOutputHelper output) : base(output) { }

Expand Down
2 changes: 1 addition & 1 deletion src/Foundatio.TestHarness/Queue/QueueTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Foundatio.Tests.Serializer;

public abstract class SerializerTestsBase : TestLoggerBase
public abstract class SerializerTestsBase : TestWithLoggingBase
{
protected SerializerTestsBase(ITestOutputHelper output) : base(output) { }

Expand Down
2 changes: 1 addition & 1 deletion src/Foundatio.TestHarness/Storage/FileStorageTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace Foundatio.Tests.Storage;

public abstract class FileStorageTestsBase : TestLoggerBase
public abstract class FileStorageTestsBase : TestWithLoggingBase
{
protected FileStorageTestsBase(ITestOutputHelper output) : base(output) { }

Expand Down
12 changes: 9 additions & 3 deletions src/Foundatio.Xunit/Logging/TestLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class TestLogger : ILoggerFactory
{
private readonly Dictionary<string, LogLevel> _logLevels = new();
private readonly Queue<LogEntry> _logEntries = new();
private int _logEntriesWritten;

public TestLogger(Action<TestLoggerOptions> configure = null)
{
Expand Down Expand Up @@ -65,7 +66,14 @@ public int MaxLogEntriesToWrite
public IReadOnlyList<LogEntry> 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)
{
Expand All @@ -91,8 +99,6 @@ internal void AddLogEntry(LogEntry logEntry)
}
}

private int _logEntriesWritten = 0;

public ILogger CreateLogger(string categoryName)
{
return new TestLoggerLogger(categoryName, this);
Expand Down
55 changes: 16 additions & 39 deletions src/Foundatio.Xunit/Logging/TestLoggerBase.cs
Original file line number Diff line number Diff line change
@@ -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<TestLoggerFixture>, IAsyncLifetime
{
protected readonly ILogger _logger;
protected readonly List<IDisposable> _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<TService>() where TService : notnull
protected TestLoggerBase(ITestOutputHelper output, TestLoggerFixture fixture)
{
return Services.GetRequiredService<TService>();
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;
}
}
1 change: 0 additions & 1 deletion src/Foundatio.Xunit/Logging/TestLoggerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public void AddServiceRegistrations(Action<IServiceCollection> registerServices)
}

public IServiceProvider Services => _serviceProvider.Value;

public TestLogger TestLogger => _testLogger.Value;
public ILogger Log => _log.Value;

Expand Down
4 changes: 1 addition & 3 deletions src/Foundatio.Xunit/Logging/TestWithLoggingBase.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/Foundatio.Tests/Jobs/JobTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace Foundatio.Tests.Jobs;

public class JobTests : TestLoggerBase
public class JobTests : TestWithLoggingBase
{
public JobTests(ITestOutputHelper output) : base(output) { }

Expand Down
2 changes: 1 addition & 1 deletion tests/Foundatio.Tests/Jobs/WorkItemJobTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

namespace Foundatio.Tests.Jobs;

public class WorkItemJobTests : TestLoggerBase
public class WorkItemJobTests : TestWithLoggingBase
{
public WorkItemJobTests(ITestOutputHelper output) : base(output) { }

Expand Down
2 changes: 1 addition & 1 deletion tests/Foundatio.Tests/Metrics/DiagnosticsMetricsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Foundatio.Tests.Metrics;

public class DiagnosticsMetricsTests : TestLoggerBase, IDisposable
public class DiagnosticsMetricsTests : TestWithLoggingBase, IDisposable
{
private readonly DiagnosticsMetricsClient _client;

Expand Down
2 changes: 1 addition & 1 deletion tests/Foundatio.Tests/Utility/CloneTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Foundatio.Tests.Utility;

public class CloneTests : TestLoggerBase
public class CloneTests : TestWithLoggingBase
{
public CloneTests(ITestOutputHelper output) : base(output) { }

Expand Down
2 changes: 1 addition & 1 deletion tests/Foundatio.Tests/Utility/DataDictionaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Foundatio.Tests.Utility;

public class DataDictionaryTests : TestLoggerBase
public class DataDictionaryTests : TestWithLoggingBase
{
public DataDictionaryTests(ITestOutputHelper output) : base(output) { }

Expand Down
2 changes: 1 addition & 1 deletion tests/Foundatio.Tests/Utility/RunTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Foundatio.Tests.Utility;

public class RunTests : TestLoggerBase
public class RunTests : TestWithLoggingBase
{
public RunTests(ITestOutputHelper output) : base(output) { }

Expand Down
2 changes: 1 addition & 1 deletion tests/Foundatio.Tests/Utility/ScheduledTimerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Foundatio.Tests.Utility;

public class ScheduledTimerTests : TestLoggerBase
public class ScheduledTimerTests : TestWithLoggingBase
{
public ScheduledTimerTests(ITestOutputHelper output) : base(output)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Foundatio.Tests/Utility/SystemClockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Foundatio.Tests.Utility;

public class SystemClockTests : TestLoggerBase
public class SystemClockTests : TestWithLoggingBase
{
public SystemClockTests(ITestOutputHelper output) : base(output) { }

Expand Down
54 changes: 37 additions & 17 deletions tests/Foundatio.Tests/Utility/TestLoggerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using Foundatio.Xunit;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand All @@ -7,16 +8,13 @@

namespace Foundatio.Tests.Utility;

public class TestLoggerTests : IClassFixture<TestLoggerFixture>
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<SomeClass>());
}

Expand All @@ -31,7 +29,7 @@ public void CanUseTestLogger()

var someClass = provider.GetRequiredService<SomeClass>();

someClass.DoSomething();
someClass.DoSomething(1);

var testLogger = provider.GetTestLogger();
Assert.Single(testLogger.LogEntries);
Expand All @@ -40,27 +38,49 @@ public void CanUseTestLogger()
testLogger.Clear();
testLogger.SetLogLevel<SomeClass>(LogLevel.Error);

someClass.DoSomething();
someClass.DoSomething(2);
Assert.Empty(testLogger.LogEntries);
}

[Fact]
public void CanUseTestLoggerFixture()
{
var someClass = _fixture.Services.GetRequiredService<SomeClass>();
var someClass = Services.GetRequiredService<SomeClass>();

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<SomeClass>(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<SomeClass>(LogLevel.Error);

someClass.DoSomething(1002);

Assert.Empty(TestLogger.LogEntries);
TestLogger.SetLogLevel<SomeClass>(LogLevel.Information);
}

[Fact]
public void CanUseTestLoggerFixture2()
{
var someClass = Services.GetRequiredService<SomeClass>();

someClass.DoSomething(1);

Assert.Single(TestLogger.LogEntries);
Assert.Contains("Doing something", TestLogger.LogEntries[0].Message);

TestLogger.Clear();
TestLogger.SetLogLevel<SomeClass>(LogLevel.Error);

someClass.DoSomething(2);
Assert.Empty(TestLogger.LogEntries);
}
}

public class SomeClass
Expand All @@ -72,8 +92,8 @@ public SomeClass(ILogger<SomeClass> logger)
_logger = logger;
}

public void DoSomething()
public void DoSomething(int number)
{
_logger.LogInformation("Doing something");
_logger.LogInformation("Doing something {Number}", number);
}
}

0 comments on commit 7f77da3

Please sign in to comment.