Skip to content

Commit

Permalink
Make sure that logger factory is there
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyzimarev committed Dec 27, 2024
1 parent 2084fa2 commit 4bfbe42
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using Bogus;
using DotNet.Testcontainers.Containers;
using Eventuous.TestHelpers;
using Eventuous.TestHelpers.TUnit.Logging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using TUnit.Core.Interfaces;

namespace Eventuous.Tests.Persistence.Base.Fixtures;
Expand All @@ -18,7 +20,7 @@ public abstract class StoreFixtureBase {
public TypeMapper TypeMapper { get; } = new();
}

public abstract partial class StoreFixtureBase<TContainer> : StoreFixtureBase, IStartableFixture where TContainer : DockerContainer {
public abstract partial class StoreFixtureBase<TContainer>(LogLevel logLevel) : StoreFixtureBase, IStartableFixture where TContainer : DockerContainer {
public virtual async Task InitializeAsync() {
Container = CreateContainer();
await Container.StartAsync();
Expand All @@ -28,6 +30,7 @@ public virtual async Task InitializeAsync() {
Serializer = new DefaultEventSerializer(TestPrimitives.DefaultOptions, TypeMapper);
services.AddSingleton(Serializer);
services.AddSingleton(TypeMapper);
services.AddLogging(b => ConfigureLogging(b.ForTests(logLevel)).SetMinimumLevel(logLevel));
SetupServices(services);

Provider = services.BuildServiceProvider();
Expand All @@ -43,9 +46,11 @@ protected async Task Start() {
var inits = Provider.GetServices<IHostedService>();

foreach (var hostedService in inits) {
await hostedService.StartAsync(default);
await hostedService.StartAsync(CancellationToken.None);
}
}

protected virtual ILoggingBuilder ConfigureLogging(ILoggingBuilder builder) => builder;

public virtual async ValueTask DisposeAsync() {
if (_disposed) return;
Expand All @@ -54,7 +59,7 @@ public virtual async ValueTask DisposeAsync() {
var inits = Provider.GetServices<IHostedService>();

foreach (var hostedService in inits) {
await hostedService.StopAsync(default);
await hostedService.StopAsync(CancellationToken.None);
}

await Provider.DisposeAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Eventuous.Subscriptions;
using Eventuous.Subscriptions.Checkpoints;
using Eventuous.Sut.Domain;
using Eventuous.TestHelpers.TUnit.Logging;
using Eventuous.Tests.Persistence.Base.Fixtures;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand All @@ -19,11 +18,9 @@ public abstract class SubscriptionFixtureBase<TContainer, TSubscription, TSubscr
where TSubscription : EventSubscription<TSubscriptionOptions>
where TSubscriptionOptions : SubscriptionOptions {
readonly bool _autoStart;
readonly LogLevel _logLevel;

protected SubscriptionFixtureBase(bool autoStart = true, LogLevel logLevel = LogLevel.Information) {
protected SubscriptionFixtureBase(bool autoStart = true, LogLevel logLevel = LogLevel.Information) : base(logLevel) {
_autoStart = autoStart;
_logLevel = logLevel;
TypeMapper.RegisterKnownEventTypes(typeof(BookingEvents.BookingImported).Assembly);
}

Expand Down Expand Up @@ -58,7 +55,6 @@ protected override void SetupServices(IServiceCollection services) {

var host = services.First(x => !x.IsKeyedService && x.ImplementationFactory?.GetType() == typeof(Func<IServiceProvider, SubscriptionHostedService>));
services.Remove(host);
services.AddLogging(b => ConfigureLogging(b.ForTests(_logLevel)).SetMinimumLevel(_logLevel));
}

protected override void GetDependencies(IServiceProvider provider) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Eventuous.Tests.Persistence.Base.Fixtures;
using Eventuous.Tests.Subscriptions.Base;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Eventuous.Tests.OpenTelemetry.Fixtures;

Expand All @@ -30,7 +31,7 @@ public abstract class MetricsSubscriptionFixtureBase<TContainer, TProducer, TSub
// ReSharper disable once StaticMemberInGenericType
static readonly KeyValuePair<string, string> DefaultTag = new("test", "foo");

protected MetricsSubscriptionFixtureBase() {
protected MetricsSubscriptionFixtureBase() : base(LogLevel.Information) {
TypeMapper.RegisterKnownEventTypes(typeof(TestEvent).Assembly);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ public class StoreFixture : StoreFixtureBase<EventStoreDbContainer> {

static StoreFixture() => AppContext.SetSwitch("System.Net.SocketsHttpHandler.Http2FlowControl.DisableDynamicWindowSizing", true);

public StoreFixture() => ActivitySource.AddActivityListener(_listener);
public StoreFixture() : this(LogLevel.Information) { }

public StoreFixture(LogLevel logLevel) : base(logLevel) {
ActivitySource.AddActivityListener(_listener);
}

protected override void SetupServices(IServiceCollection services) {
services.AddEventStoreClient(Container.GetConnectionString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

namespace Eventuous.Tests.EventStore.Subscriptions.Fixtures;

public abstract class LegacySubscriptionFixture<T>: IAsyncInitializer, IAsyncDisposable where T : class, IEventHandler {
public abstract class LegacySubscriptionFixture<T> : IAsyncInitializer, IAsyncDisposable where T : class, IEventHandler {
protected StreamName Stream { get; } = new($"test-{Guid.NewGuid():N}");
protected StoreFixture StoreFixture { get; } = new();
protected StoreFixture StoreFixture { get; }
protected T Handler { get; }
protected EventStoreProducer Producer { get; private set; } = null!;
protected ILogger Log { get; }
Expand All @@ -19,6 +19,7 @@ public abstract class LegacySubscriptionFixture<T>: IAsyncInitializer, IAsyncDis
protected LegacySubscriptionFixture(T handler, StreamName? stream = null, LogLevel logLevel = LogLevel.Information) {
if (stream is { } s) Stream = s;

StoreFixture = new(logLevel);
LoggerFactory = LoggingExtensions.GetLoggerFactory(logLevel);
Handler = handler;
Log = LoggerFactory.CreateLogger(GetType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class PersistentSubscriptionFixture<TSubscription, TOptions, THandler>(
public THandler Handler { get; } = handler;
public EventStoreProducer Producer { get; private set; } = null!;
protected ILogger Log { get; set; } = null!;
protected StoreFixture Fixture { get; } = new();
protected StoreFixture Fixture { get; } = new(logLevel);
TSubscription Subscription { get; set; } = null!;

public ValueTask Start() => Subscription.SubscribeWithLog(Log);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public sealed class StreamSubscriptionDeletedEventsTests {
readonly LoggingEventListener _listener;

Check warning on line 16 in src/EventStore/test/Eventuous.Tests.EventStore/Subscriptions/StreamSubscriptionDeletedEventsTests.cs

View workflow job for this annotation

GitHub Actions / nuget

_listener should be disposed within a clean up method

public StreamSubscriptionDeletedEventsTests() {
_fixture = new();
_fixture = new(LogLevel.Information);
_loggerFactory = LoggingExtensions.GetLoggerFactory();
_listener = new(_loggerFactory);
_fixture.TypeMapper.RegisterKnownEventTypes(typeof(BookingEvents.BookingImported).Assembly);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class StreamSubscriptionWithLinksTests : StoreFixture {
readonly List<Checkpoint> _checkpoints = [];
readonly string _prefix = $"{Faker.Commerce.ProductAdjective()}{Faker.Commerce.Product()}";

public StreamSubscriptionWithLinksTests() {
public StreamSubscriptionWithLinksTests() : base(LogLevel.Information) {
AutoStart = false;
TypeMapper.AddType<TestEvent>(TestEvent.TypeName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class SubscriptionIgnoredMessagesTests : StoreFixture {
ICheckpointStore _checkpointStore = null!;
TestEventHandler _handler = null!;

public SubscriptionIgnoredMessagesTests() {
public SubscriptionIgnoredMessagesTests() : base(LogLevel.Information) {
AutoStart = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Eventuous.Tests.Postgres.Store;

// ReSharper disable once PartialTypeWithSinglePart
public partial class StoreFixture : StoreFixtureBase<PostgreSqlContainer> {
public partial class StoreFixture() : StoreFixtureBase<PostgreSqlContainer>(LogLevel.Information) {
protected NpgsqlDataSource DataSource { get; private set; } = null!;

readonly string _schemaName = GetSchemaName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Eventuous.Tests.SqlServer.Store;

public sealed class StoreFixture : StoreFixtureBase<MsSqlContainer> {
public sealed class StoreFixture() : StoreFixtureBase<MsSqlContainer>(LogLevel.Information) {
readonly string _schemaName = GetSchemaName();

protected override void SetupServices(IServiceCollection services) {
Expand Down

0 comments on commit 4bfbe42

Please sign in to comment.