Skip to content
Closed
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
1 change: 1 addition & 0 deletions src/Mvc/Mvc.Testing/src/PublicAPI.Unshipped.txt
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
14 changes: 11 additions & 3 deletions src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to actually call DisposeAsync on the host.

GC.SuppressFinalize(this);
Copy link
Member

Choose a reason for hiding this comment

The 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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>();
Copy link
Member

Choose a reason for hiding this comment

The 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; }
Expand All @@ -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)
{
Expand Down