Skip to content

Commit

Permalink
fix: Remove Sonar code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn committed Nov 26, 2024
1 parent 1cf48b2 commit 49ef7dc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
12 changes: 9 additions & 3 deletions src/Testcontainers.Xunit/ContainerLifetime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,16 @@ public TContainerEntity Container

#if !XUNIT_V3
/// <inheritdoc />
LifetimeTask IAsyncLifetime.DisposeAsync() => DisposeAsync();
LifetimeTask IAsyncLifetime.DisposeAsync() => DisposeAsyncCore();
#else
/// <inheritdoc />
LifetimeTask IAsyncDisposable.DisposeAsync() => DisposeAsync();
async LifetimeTask IAsyncDisposable.DisposeAsync()
{
await DisposeAsyncCore()
.ConfigureAwait(false);

GC.SuppressFinalize(this);
}
#endif

/// <summary>
Expand Down Expand Up @@ -80,7 +86,7 @@ await Container.StartAsync()
}

/// <inheritdoc cref="IAsyncLifetime" />
protected virtual async LifetimeTask DisposeAsync()
protected virtual async LifetimeTask DisposeAsyncCore()
{
if (_exception == null)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Testcontainers.Xunit/DbContainerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ await base.InitializeAsync()
}

/// <inheritdoc />
protected override async LifetimeTask DisposeAsync()
protected override async LifetimeTask DisposeAsyncCore()
{
if (_testMethods != null)
{
await _testMethods.DisposeAsync()
.ConfigureAwait(true);
}

await base.DisposeAsync()
await base.DisposeAsyncCore()
.ConfigureAwait(true);
}

Expand Down
5 changes: 2 additions & 3 deletions src/Testcontainers.Xunit/DbContainerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public abstract class DbContainerTest<TBuilderEntity, TContainerEntity>(ITestOut
where TBuilderEntity : IContainerBuilder<TBuilderEntity, TContainerEntity>, new()
where TContainerEntity : IContainer, IDatabaseContainer
{
private readonly CancellationTokenSource _cts = new CancellationTokenSource();
private DbContainerTestMethods _testMethods;

/// <inheritdoc />
Expand All @@ -25,15 +24,15 @@ await base.InitializeAsync()
}

/// <inheritdoc />
protected override async LifetimeTask DisposeAsync()
protected override async LifetimeTask DisposeAsyncCore()
{
if (_testMethods != null)
{
await _testMethods.DisposeAsync()
.ConfigureAwait(true);
}

await base.DisposeAsync()
await base.DisposeAsyncCore()
.ConfigureAwait(true);
}

Expand Down
23 changes: 20 additions & 3 deletions src/Testcontainers.Xunit/MessageSinkLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,37 @@ namespace Testcontainers.Xunit;

internal sealed class MessageSinkLogger(IMessageSink messageSink) : Logger
{
private readonly IMessageSink _messageSink = messageSink;

protected override void Log<TState>(TState state, Exception exception, Func<TState, Exception, string> 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;
}

/// <returns>
/// The hash code of the underlying message sink, because <see cref="DotNet.Testcontainers.Clients.DockerApiClient.LogContainerRuntimeInfoAsync" />
/// logs the runtime information once per Docker Engine API client and logger.
/// </returns>
public override int GetHashCode() => messageSink?.GetHashCode() ?? 0;
public override int GetHashCode() => _messageSink?.GetHashCode() ?? 0;
}

0 comments on commit 49ef7dc

Please sign in to comment.