From 49ef7dcb39e6212deb5809ae5e81eef59328c9dc Mon Sep 17 00:00:00 2001 From: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com> Date: Tue, 26 Nov 2024 14:46:50 +0100 Subject: [PATCH] fix: Remove Sonar code smells --- src/Testcontainers.Xunit/ContainerLifetime.cs | 12 +++++++--- .../DbContainerFixture.cs | 4 ++-- src/Testcontainers.Xunit/DbContainerTest.cs | 5 ++-- src/Testcontainers.Xunit/MessageSinkLogger.cs | 23 ++++++++++++++++--- 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/Testcontainers.Xunit/ContainerLifetime.cs b/src/Testcontainers.Xunit/ContainerLifetime.cs index a88ea88b0..a96caa133 100644 --- a/src/Testcontainers.Xunit/ContainerLifetime.cs +++ b/src/Testcontainers.Xunit/ContainerLifetime.cs @@ -36,10 +36,16 @@ public TContainerEntity Container #if !XUNIT_V3 /// - LifetimeTask IAsyncLifetime.DisposeAsync() => DisposeAsync(); + LifetimeTask IAsyncLifetime.DisposeAsync() => DisposeAsyncCore(); #else /// - LifetimeTask IAsyncDisposable.DisposeAsync() => DisposeAsync(); + async LifetimeTask IAsyncDisposable.DisposeAsync() + { + await DisposeAsyncCore() + .ConfigureAwait(false); + + GC.SuppressFinalize(this); + } #endif /// @@ -80,7 +86,7 @@ await Container.StartAsync() } /// - protected virtual async LifetimeTask DisposeAsync() + protected virtual async LifetimeTask DisposeAsyncCore() { if (_exception == null) { diff --git a/src/Testcontainers.Xunit/DbContainerFixture.cs b/src/Testcontainers.Xunit/DbContainerFixture.cs index 32c60fb15..c8e6ffbda 100644 --- a/src/Testcontainers.Xunit/DbContainerFixture.cs +++ b/src/Testcontainers.Xunit/DbContainerFixture.cs @@ -25,7 +25,7 @@ await base.InitializeAsync() } /// - protected override async LifetimeTask DisposeAsync() + protected override async LifetimeTask DisposeAsyncCore() { if (_testMethods != null) { @@ -33,7 +33,7 @@ await _testMethods.DisposeAsync() .ConfigureAwait(true); } - await base.DisposeAsync() + await base.DisposeAsyncCore() .ConfigureAwait(true); } diff --git a/src/Testcontainers.Xunit/DbContainerTest.cs b/src/Testcontainers.Xunit/DbContainerTest.cs index 907e29efc..aed622ebe 100644 --- a/src/Testcontainers.Xunit/DbContainerTest.cs +++ b/src/Testcontainers.Xunit/DbContainerTest.cs @@ -12,7 +12,6 @@ public abstract class DbContainerTest(ITestOut where TBuilderEntity : IContainerBuilder, new() where TContainerEntity : IContainer, IDatabaseContainer { - private readonly CancellationTokenSource _cts = new CancellationTokenSource(); private DbContainerTestMethods _testMethods; /// @@ -25,7 +24,7 @@ await base.InitializeAsync() } /// - protected override async LifetimeTask DisposeAsync() + protected override async LifetimeTask DisposeAsyncCore() { if (_testMethods != null) { @@ -33,7 +32,7 @@ await _testMethods.DisposeAsync() .ConfigureAwait(true); } - await base.DisposeAsync() + await base.DisposeAsyncCore() .ConfigureAwait(true); } diff --git a/src/Testcontainers.Xunit/MessageSinkLogger.cs b/src/Testcontainers.Xunit/MessageSinkLogger.cs index f143f98e9..29a53d166 100644 --- a/src/Testcontainers.Xunit/MessageSinkLogger.cs +++ b/src/Testcontainers.Xunit/MessageSinkLogger.cs @@ -2,20 +2,37 @@ namespace Testcontainers.Xunit; internal sealed class MessageSinkLogger(IMessageSink messageSink) : Logger { + private readonly IMessageSink _messageSink = messageSink; + protected override void Log(TState state, Exception exception, Func formatter) { - if (messageSink == null) + if (_messageSink == null) { return; } var message = GetMessage(state, exception, formatter); - messageSink.OnMessage(new DiagnosticMessage($"[testcontainers.org] {message}")); + _messageSink.OnMessage(new DiagnosticMessage($"[testcontainers.org] {message}")); + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(this, obj)) + { + return true; + } + + if (obj is MessageSinkLogger other) + { + return Equals(_messageSink, other._messageSink); + } + + return false; } /// /// The hash code of the underlying message sink, because /// logs the runtime information once per Docker Engine API client and logger. /// - public override int GetHashCode() => messageSink?.GetHashCode() ?? 0; + public override int GetHashCode() => _messageSink?.GetHashCode() ?? 0; } \ No newline at end of file