-
Notifications
You must be signed in to change notification settings - Fork 10k
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
IHostApplicationLifetime StopApplication not respected in AspNetCore.Mvc.Testing #25857
Comments
Thanks for contacting us. |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
This, and the fact that MVC.Testing inner infrastructure only disposes the host without stopping it first, makes MVC.Testing really hard to use with any service that has lasting effects on infrastructure. |
This is a dupe of #29348 |
@javiercn hello. Sorry, but it does not look like the merged PR fixes this bug. And this bug is not a duplicate of my issue. This bug describes the test host ignoring The PR only forces the host to stop when the factory is disposed. |
@quixoticaxis My bad, I misunderstood it. |
@Tratcher I believe this is a potential issue/enhancement in TestServer, do you have any thoughts? |
@GeeWee While I appreciate the minimal repro, what are you really hoping to accomplish with StopApplication? It's not clear why StopApplication is a useful scenario for TestServer to emulate. |
@Tratcher I don't know the reasons of this issue's author, but in our code we often encounter the following pattern: public class ServiceA : IHostedService
{
public Task StartAsync(CancellationToken token)
{
executing = ExecuteAsync();
}
private Task ExecuteAsync()
{
try
{
await serviceLogic.LoopAsync();
}
catch
{
Log("The service A cannot proceed, stopping the application");
lifetime.StopApplication();
}
}
private Task executing;
} This code enables clean shutdown if any of the services fail, because in the real application |
Similar in case of |
I guess the best it could do is stop future requests. That seems fine. |
We also have a background service does following, would be good have a way to gracefully stop it after test completed (either pass or failed)
|
@jeremy001181 that seems like a different ask than above. That would be handled by calling StopAsync on the host. (Not sure how accessible that is from WebApplicationFactory). |
Just want to add another use case. Our application does not migrate the database (this is deferred onto a different application that is guaranteed to run before any other in kubernetes). However in our tests we do want this to run and we want a separate database for all tests as they run in parallel. To that end we inject a startup filter that is ran before all others. This startup filter ties into IHostApplicationLifetime.ApplicationStopping and IHostApplicationLifetime.ApplicationStopped to delete the database it has just created and migrated. Currently we're having to override WebApplicationFactory.Dispose and through reflection get the _host field and call StopAsync on that to trigger this path. An approach mentioned in #29348. There is other reasons why we want these IHostApplicationLifetime callbacks to be triggered, but this one, which is solely for the tests itself, was not yet mentioned here. I guess the only question left to ask is: Is this something that you guys would like to see implemented? If so, I would be willing to commit some time to make a PR. |
Maybe also a little extra information for when this would be useful. I've been developing BetterHostedServices which relies on being able to understand when the application is being shut down to do some of its magic. Currently I have to jump through a few hoops because it's not easily possible to integration-test anything that relies on shutting down the |
Are there any workarounds for this? It seems that WebApplicationFactory always leave the process running. |
@tapizquent , there was a PR that forces the host to stop on disposing the factory. For other cases, you can use the hack I posted in #29348. |
Describe the bug
I'm trying to run integration tests, with some things that call
IHostApplicationLifetime.StopApplication
from an IHostedServiceIn a "real project" this works, but it does not when using the TestServer.
Test case below
To Reproduce
The IHostedService calls StopApplication. I then don't expect the
GetAsync
call to work then.The logs look like this:
So it starts off by writing "Application is shutting down" and then proceeds to start the application.
Further technical details
dotnet --info
Rider, but I don't think that matters. All of this is run from the Console.
The text was updated successfully, but these errors were encountered: