Skip to content
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
1 change: 1 addition & 0 deletions src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ protected virtual void Dispose(bool disposing)
}

_server?.Dispose();
_host?.StopAsync().Wait();
Copy link
Member

Choose a reason for hiding this comment

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

If we want to test that this is called, we could

  • Create the factory, request the IapplicationLifetime service from the host services (available through the factory).
  • Register a callback on stop.
  • Dispose the factory.
  • Validate the callback was invoked.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Very thanks @javiercn. I added the corresponding test as per your instructions, but instead of IApplicationLifetime, I used IHostApplicationLifetime as IApplicationLifetime is obsolete.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I didn't remember exactly how it was named, so perfect!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

😀

_host?.Dispose();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
Expand Down Expand Up @@ -65,6 +65,21 @@ public void TestingInfrastructure_GenericHost_WithWithHostBuilderHasServices()
Assert.NotNull(factory.Services.GetService(typeof(IConfiguration)));
}

[Fact]
public void TestingInfrastructure_GenericHost_HostShouldStopBeforeDispose()
{
// Act
using var factory = new CustomizedFactory<GenericHostWebSite.Startup>();
var callbackCalled = false;

var lifetimeService = (IHostApplicationLifetime) factory.Services.GetService(typeof(IHostApplicationLifetime));
lifetimeService.ApplicationStopped.Register(() => { callbackCalled = true; });
factory.Dispose();

// Assert
Assert.True(callbackCalled);
}

private class CustomizedFactory<TEntryPoint> : WebApplicationFactory<TEntryPoint> where TEntryPoint : class
{
public bool GetTestAssembliesCalled { get; private set; }
Expand All @@ -73,6 +88,7 @@ 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