From eec1993b38d336fe93f49451036223e5a901e9f0 Mon Sep 17 00:00:00 2001 From: Arash Sabet Date: Tue, 29 Dec 2020 11:30:35 -0500 Subject: [PATCH] Employed disposed flag in async disposable pattern scope #33 The compile problem in the example project has been addressed in this code commitment too. --- .../Fixtures/TestFixture.cs | 4 ++++ .../IntegrationTests.cs | 4 ++++ src/Abstracts/TestBed.cs | 9 +++++++-- src/Abstracts/TestBedFixture.cs | 9 +++++++-- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Fixtures/TestFixture.cs b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Fixtures/TestFixture.cs index 054d0e4..6ddfeb9 100644 --- a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Fixtures/TestFixture.cs +++ b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/Fixtures/TestFixture.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using System.Threading.Tasks; using Xunit.Microsoft.DependencyInjection.Abstracts; using Xunit.Microsoft.DependencyInjection.ExampleTests.Services; @@ -12,6 +13,9 @@ protected override void AddServices(IServiceCollection services, IConfiguration .AddTransient() .Configure(config => configuration.GetSection("Options").Bind(config)); + protected override ValueTask DisposeAsyncCore() + => new ValueTask(); + protected override string GetConfigurationFile() => "appsettings.json"; } diff --git a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/IntegrationTests.cs b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/IntegrationTests.cs index 5a8fa5a..a11ed6c 100644 --- a/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/IntegrationTests.cs +++ b/examples/Xunit.Microsoft.DependencyInjection.ExampleTests/IntegrationTests.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Options; +using System.Threading.Tasks; using Xunit.Abstractions; using Xunit.Microsoft.DependencyInjection.Abstracts; using Xunit.Microsoft.DependencyInjection.ExampleTests.Fixtures; @@ -40,5 +41,8 @@ public void Test2(int x, int y) protected override void Clear() { } + + protected override ValueTask DisposeAsyncCore() + => new ValueTask(); } } diff --git a/src/Abstracts/TestBed.cs b/src/Abstracts/TestBed.cs index 3337b09..da807b5 100644 --- a/src/Abstracts/TestBed.cs +++ b/src/Abstracts/TestBed.cs @@ -10,6 +10,7 @@ public abstract class TestBed : IDisposable, IClassFixture, protected readonly ITestOutputHelper _testOutputHelper; protected readonly TFixture _fixture; private bool _disposedValue; + private bool _disposedAsync; public TestBed(ITestOutputHelper testOutputHelper, TFixture fixture) => (_testOutputHelper, _fixture) = (testOutputHelper, fixture); @@ -48,8 +49,12 @@ public void Dispose() public async ValueTask DisposeAsync() { - await DisposeAsyncCore(); - GC.SuppressFinalize(this); + if (!_disposedAsync) + { + await DisposeAsyncCore(); + GC.SuppressFinalize(this); + _disposedAsync = true; + } } protected abstract ValueTask DisposeAsyncCore(); diff --git a/src/Abstracts/TestBedFixture.cs b/src/Abstracts/TestBedFixture.cs index cdcee27..d6c2c91 100644 --- a/src/Abstracts/TestBedFixture.cs +++ b/src/Abstracts/TestBedFixture.cs @@ -14,6 +14,7 @@ public abstract class TestBedFixture : IDisposable, IAsyncDisposable private readonly IServiceCollection _services; private IServiceProvider _serviceProvider; private bool _disposedValue; + private bool _disposedAsync; protected TestBedFixture() { @@ -99,8 +100,12 @@ public void Dispose() public async ValueTask DisposeAsync() { - await DisposeAsyncCore(); - GC.SuppressFinalize(this); + if (!_disposedAsync) + { + await DisposeAsyncCore(); + GC.SuppressFinalize(this); + _disposedAsync = true; + } } protected abstract ValueTask DisposeAsyncCore();