Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore : Do not block async context on acquire lock #1188

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
<PackageVersion Include="MySqlConnector" Version="2.2.5"/>
<PackageVersion Include="NATS.Client" Version="1.0.8"/>
<PackageVersion Include="Neo4j.Driver" Version="5.5.0"/>
<PackageVersion Include="Npgsql" Version="6.0.10"/>
<PackageVersion Include="Npgsql" Version="6.0.11"/>
<PackageVersion Include="Oracle.ManagedDataAccess.Core" Version="3.21.90"/>
<PackageVersion Include="RabbitMQ.Client" Version="6.4.0"/>
<PackageVersion Include="RavenDB.Client" Version="5.4.100"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace DotNet.Testcontainers.Configurations
namespace DotNet.Testcontainers.Configurations
{
using System;

Expand Down
41 changes: 21 additions & 20 deletions src/Testcontainers/Containers/DockerContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,34 +261,34 @@ public Task<long> GetExitCodeAsync(CancellationToken ct = default)
/// <inheritdoc />
public virtual async Task StartAsync(CancellationToken ct = default)
{
using (_ = AcquireLock())
{
var futureResources = Array.Empty<IFutureResource>()
.Concat(_configuration.Mounts)
.Concat(_configuration.Networks);
using var disposable = await AcquireLockAsync(ct)
.ConfigureAwait(false);

await Task.WhenAll(futureResources.Select(resource => resource.CreateAsync(ct)))
.ConfigureAwait(false);
var futureResources = Array.Empty<IFutureResource>()
.Concat(_configuration.Mounts)
.Concat(_configuration.Networks);

await Task.WhenAll(_configuration.Containers.Select(resource => resource.StartAsync(ct)))
.ConfigureAwait(false);
await Task.WhenAll(futureResources.Select(resource => resource.CreateAsync(ct)))
.ConfigureAwait(false);

await UnsafeCreateAsync(ct)
.ConfigureAwait(false);
await Task.WhenAll(_configuration.Containers.Select(resource => resource.StartAsync(ct)))
.ConfigureAwait(false);

await UnsafeStartAsync(ct)
.ConfigureAwait(false);
}
await UnsafeCreateAsync(ct)
.ConfigureAwait(false);

await UnsafeStartAsync(ct)
.ConfigureAwait(false);
}

/// <inheritdoc />
public virtual async Task StopAsync(CancellationToken ct = default)
{
using (_ = AcquireLock())
{
await UnsafeStopAsync(ct)
.ConfigureAwait(false);
}
using var disposable = await AcquireLockAsync(ct)
.ConfigureAwait(false);

await UnsafeStopAsync(ct)
.ConfigureAwait(false);
}

/// <inheritdoc />
Expand Down Expand Up @@ -344,7 +344,8 @@ protected override async ValueTask DisposeAsyncCore()
return;
}

using (_ = AcquireLock())
using (_ = await AcquireLockAsync()
.ConfigureAwait(false))
{
if (Guid.Empty.Equals(_configuration.SessionId))
{
Expand Down
20 changes: 10 additions & 10 deletions src/Testcontainers/Images/FutureDockerImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,21 @@ public string GetHostname()
/// <inheritdoc />
public async Task CreateAsync(CancellationToken ct = default)
{
using (_ = AcquireLock())
{
await UnsafeCreateAsync(ct)
.ConfigureAwait(false);
}
using var disposable = await AcquireLockAsync(ct)
.ConfigureAwait(false);

await UnsafeCreateAsync(ct)
.ConfigureAwait(false);
}

/// <inheritdoc />
public async Task DeleteAsync(CancellationToken ct = default)
{
using (_ = AcquireLock())
{
await UnsafeDeleteAsync(ct)
.ConfigureAwait(false);
}
using var disposable = await AcquireLockAsync(ct)
.ConfigureAwait(false);

await UnsafeDeleteAsync(ct)
.ConfigureAwait(false);
}

/// <inheritdoc />
Expand Down
20 changes: 10 additions & 10 deletions src/Testcontainers/Networks/DockerNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ public string Name
/// <inheritdoc />
public async Task CreateAsync(CancellationToken ct = default)
{
using (_ = AcquireLock())
{
await UnsafeCreateAsync(ct)
.ConfigureAwait(false);
}
using var disposable = await AcquireLockAsync(ct)
.ConfigureAwait(false);

await UnsafeCreateAsync(ct)
.ConfigureAwait(false);
}

/// <inheritdoc />
public async Task DeleteAsync(CancellationToken ct = default)
{
using (_ = AcquireLock())
{
await UnsafeDeleteAsync(ct)
.ConfigureAwait(false);
}
using var disposable = await AcquireLockAsync(ct)
.ConfigureAwait(false);

await UnsafeDeleteAsync(ct)
.ConfigureAwait(false);
}

/// <inheritdoc />
Expand Down
8 changes: 5 additions & 3 deletions src/Testcontainers/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ protected virtual ValueTask DisposeAsyncCore()
/// <summary>
/// Acquires a lock to access the resource thread-safe.
/// </summary>
/// <returns>An <see cref="IDisposable" /> that releases the lock on <see cref="IDisposable.Dispose" />.</returns>
protected virtual IDisposable AcquireLock()
/// <returns>A <see cref="IDisposable" /> that releases the lock on <see cref="IDisposable.Dispose" />.</returns>
protected virtual async Task<IDisposable> AcquireLockAsync(CancellationToken ct = default)
{
await _semaphoreSlim.WaitAsync(ct)
.ConfigureAwait(false);

return new Lock(_semaphoreSlim);
}

Expand Down Expand Up @@ -102,7 +105,6 @@ private sealed class Lock : IDisposable
public Lock(SemaphoreSlim semaphoreSlim)
{
_semaphoreSlim = semaphoreSlim;
_semaphoreSlim.Wait();
}

public void Dispose()
Expand Down
1 change: 1 addition & 0 deletions src/Testcontainers/Testcontainers.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net8.0;netstandard2.0;netstandard2.1</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Configurations>Debug;Release</Configurations>
<RootNamespace>DotNet.Testcontainers</RootNamespace>
</PropertyGroup>
Expand Down
20 changes: 10 additions & 10 deletions src/Testcontainers/Volumes/DockerVolume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ public string Name
/// <inheritdoc />
public async Task CreateAsync(CancellationToken ct = default)
{
using (_ = AcquireLock())
{
await UnsafeCreateAsync(ct)
.ConfigureAwait(false);
}
using var disposable = await AcquireLockAsync(ct)
.ConfigureAwait(false);

await UnsafeCreateAsync(ct)
.ConfigureAwait(false);
}

/// <inheritdoc />
public async Task DeleteAsync(CancellationToken ct = default)
{
using (_ = AcquireLock())
{
await UnsafeDeleteAsync(ct)
.ConfigureAwait(false);
}
using var disposable = await AcquireLockAsync(ct)
.ConfigureAwait(false);

await UnsafeDeleteAsync(ct)
.ConfigureAwait(false);
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Testcontainers.Tests;
namespace Testcontainers.Tests;

public sealed class WaitStrategyTest
{
Expand Down
Loading