-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Implemented IAsyncDisposable for WebApplicationFactory #29456
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
#nullable enable | ||
Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory<TEntryPoint>.DisposeAsync() -> System.Threading.Tasks.ValueTask |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,13 +8,13 @@ | |
using System.Net.Http; | ||
using System.Reflection; | ||
using System.Text.Json; | ||
using System.Runtime.Serialization.Json; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Hosting.Server; | ||
using Microsoft.AspNetCore.TestHost; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyModel; | ||
using Microsoft.Extensions.Hosting; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.Testing | ||
{ | ||
|
@@ -23,7 +23,7 @@ namespace Microsoft.AspNetCore.Mvc.Testing | |
/// </summary> | ||
/// <typeparam name="TEntryPoint">A type in the entry point assembly of the application. | ||
/// Typically the Startup or Program classes can be used.</typeparam> | ||
public class WebApplicationFactory<TEntryPoint> : IDisposable where TEntryPoint : class | ||
public class WebApplicationFactory<TEntryPoint> : IDisposable, IAsyncDisposable where TEntryPoint : class | ||
{ | ||
private bool _disposed; | ||
private TestServer _server; | ||
|
@@ -523,13 +523,21 @@ protected virtual void Dispose(bool disposing) | |
} | ||
|
||
_server?.Dispose(); | ||
_host?.StopAsync().Wait(); | ||
_host?.StopAsync().GetAwaiter().GetResult(); | ||
_host?.Dispose(); | ||
} | ||
|
||
_disposed = true; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public ValueTask DisposeAsync() | ||
{ | ||
Dispose(disposing: false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We want to actually call DisposeAsync on the host. |
||
GC.SuppressFinalize(this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this, there's no finalizer. |
||
return ValueTask.CompletedTask; | ||
} | ||
|
||
private class DelegatedWebApplicationFactory : WebApplicationFactory<TEntryPoint> | ||
{ | ||
private readonly Func<IWebHostBuilder, TestServer> _createServer; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using Microsoft.AspNetCore.TestHost; | ||
|
@@ -80,6 +81,21 @@ public void TestingInfrastructure_GenericHost_HostShouldStopBeforeDispose() | |
Assert.True(callbackCalled); | ||
} | ||
|
||
[Fact] | ||
public async Task TestingInfrastructure_GenericHost_HostDisposeAsync() | ||
{ | ||
// Act | ||
await using var factory = new CustomizedFactory<GenericHostWebSite.Startup>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure this adds a service that is only IAsyncDisposable, that's the real test. |
||
var callbackCalled = false; | ||
|
||
var lifetimeService = (IHostApplicationLifetime) factory.Services.GetService(typeof(IHostApplicationLifetime)); | ||
lifetimeService.ApplicationStopped.Register(() => { callbackCalled = true; }); | ||
await factory.DisposeAsync(); | ||
|
||
// Assert | ||
Assert.True(callbackCalled); | ||
} | ||
|
||
private class CustomizedFactory<TEntryPoint> : WebApplicationFactory<TEntryPoint> where TEntryPoint : class | ||
{ | ||
public bool GetTestAssembliesCalled { get; private set; } | ||
|
@@ -88,7 +104,6 @@ private class CustomizedFactory<TEntryPoint> : WebApplicationFactory<TEntryPoint | |
public bool CreateServerCalled { get; private set; } | ||
public bool CreateHostCalled { get; private set; } | ||
public IList<string> ConfigureWebHostCalled { get; private set; } = new List<string>(); | ||
public bool DisposeHostCalled { get; private set; } | ||
|
||
protected override void ConfigureWebHost(IWebHostBuilder builder) | ||
{ | ||
|
Uh oh!
There was an error while loading. Please reload this page.