Skip to content

Commit eec1993

Browse files
committed
Employed disposed flag in async disposable pattern scope #33
The compile problem in the example project has been addressed in this code commitment too.
1 parent c6faa14 commit eec1993

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Fixtures/TestFixture.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.Extensions.Configuration;
22
using Microsoft.Extensions.DependencyInjection;
3+
using System.Threading.Tasks;
34
using Xunit.Microsoft.DependencyInjection.Abstracts;
45
using Xunit.Microsoft.DependencyInjection.ExampleTests.Services;
56

@@ -12,6 +13,9 @@ protected override void AddServices(IServiceCollection services, IConfiguration
1213
.AddTransient<ICalculator, Calculator>()
1314
.Configure<Options>(config => configuration.GetSection("Options").Bind(config));
1415

16+
protected override ValueTask DisposeAsyncCore()
17+
=> new ValueTask();
18+
1519
protected override string GetConfigurationFile()
1620
=> "appsettings.json";
1721
}

examples/Xunit.Microsoft.DependencyInjection.ExampleTests/IntegrationTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.Extensions.Options;
2+
using System.Threading.Tasks;
23
using Xunit.Abstractions;
34
using Xunit.Microsoft.DependencyInjection.Abstracts;
45
using Xunit.Microsoft.DependencyInjection.ExampleTests.Fixtures;
@@ -40,5 +41,8 @@ public void Test2(int x, int y)
4041
protected override void Clear()
4142
{
4243
}
44+
45+
protected override ValueTask DisposeAsyncCore()
46+
=> new ValueTask();
4347
}
4448
}

src/Abstracts/TestBed.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public abstract class TestBed<TFixture> : IDisposable, IClassFixture<TFixture>,
1010
protected readonly ITestOutputHelper _testOutputHelper;
1111
protected readonly TFixture _fixture;
1212
private bool _disposedValue;
13+
private bool _disposedAsync;
1314

1415
public TestBed(ITestOutputHelper testOutputHelper, TFixture fixture)
1516
=> (_testOutputHelper, _fixture) = (testOutputHelper, fixture);
@@ -48,8 +49,12 @@ public void Dispose()
4849

4950
public async ValueTask DisposeAsync()
5051
{
51-
await DisposeAsyncCore();
52-
GC.SuppressFinalize(this);
52+
if (!_disposedAsync)
53+
{
54+
await DisposeAsyncCore();
55+
GC.SuppressFinalize(this);
56+
_disposedAsync = true;
57+
}
5358
}
5459

5560
protected abstract ValueTask DisposeAsyncCore();

src/Abstracts/TestBedFixture.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public abstract class TestBedFixture : IDisposable, IAsyncDisposable
1414
private readonly IServiceCollection _services;
1515
private IServiceProvider _serviceProvider;
1616
private bool _disposedValue;
17+
private bool _disposedAsync;
1718

1819
protected TestBedFixture()
1920
{
@@ -99,8 +100,12 @@ public void Dispose()
99100

100101
public async ValueTask DisposeAsync()
101102
{
102-
await DisposeAsyncCore();
103-
GC.SuppressFinalize(this);
103+
if (!_disposedAsync)
104+
{
105+
await DisposeAsyncCore();
106+
GC.SuppressFinalize(this);
107+
_disposedAsync = true;
108+
}
104109
}
105110

106111
protected abstract ValueTask DisposeAsyncCore();

0 commit comments

Comments
 (0)