You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ClassCleanupBehavior.EndOfClass causes Microsoft.AspNetCore.Mvc.Testing based tests to fail with TaskCanceledException: The operation was canceled.
#4492
Open
abatishchev opened this issue
Jan 1, 2025
· 0 comments
Test which use an in-memory ASP.NET Core web server and linked HTTP client started failing with the following exception after I replaced [ClassCleanup] with [ClassCleanupBehavior.EndOfClass] per MSTEST0034:
Failed DeleteAccount_Should_Return_Ok [135 ms]
Error Message:
Test method Provisioning.Service.Tests.Functional.Controllers.DataControllerTests.DeleteAccount_Should_Return_Ok threw exception:
System.Threading.Tasks.TaskCanceledException: The operation was canceled. ---> System.Net.Http.HttpRequestException: Error while copying content to a stream. ---> System.IO.IOException: ---> System.IO.IOException: The client aborted the request.
Stack Trace:
at Microsoft.AspNetCore.TestHost.ResponseBodyReaderStream.CheckAborted()
at Microsoft.AspNetCore.TestHost.ResponseBodyReaderStream.ReadAsync(Memory`1 buffer, CancellationToken cancellationToken)
at System.IO.Stream.<CopyToAsync>g__Core|27_0(Stream source, Stream destination, Int32 bufferSize, CancellationToken cancellationToken)
at System.Net.Http.HttpContent.LoadIntoBufferAsyncCore(Task serializeToStreamTask, MemoryStream tempBuffer)
--- End of inner exception stack trace ---
at System.Net.Http.HttpContent.LoadIntoBufferAsyncCore(Task serializeToStreamTask, MemoryStream tempBuffer)
at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.HttpClient.HandleFailure(Exception e, Boolean telemetryStarted, HttpResponseMessage response, CancellationTokenSource cts, CancellationToken cancellationToken, CancellationTokenSource pendingRequestsCts)
at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
at Provisioning.Service.Tests.Functional.Controllers.DataControllerTests.SendAsync[T](HttpMethod httpMethod, Uri url, T resource) in C:\__w\1\s\src\Provisioning\Service.Tests.Functional\Controllers\DataControllerTests.cs:line 103
at Provisioning.Service.Tests.Functional.Controllers.DataControllerTests.DeleteAccount_Should_Return_Ok() in C:\__w\1\s\src\Provisioning\Service.Tests.Functional\Controllers\DataControllerTests.cs:line 45
Steps To Reproduce
[TestClass]publicclassDataControllerTests{protectedstaticWebApplicationFactory<Program>_app;protectedstaticHttpClient_httpClient;[ClassInitialize]publicstaticvoidClassInitialize(TestContextcontext){_app=newWebApplicationFactory<Program>().WithWebHostBuilder(builder =>builder.ConfigureServices(s =>{// setting up some mocks}).UseEnvironment(Environments.Development));_httpClient=_app.CreateDefaultClient();}[ClassCleanup(ClassCleanupBehavior.EndOfClass)]// <-- !publicstaticvoidClassCleanup()=>_app.Dispose();[TestMethod]publicasyncTaskDeleteAccount_Should_Return_Ok(){varaccount=CreateAccount();varurl=newUri($"/data/accounts/{account.Name}",UriKind.Relative);// Actusingvarresponse=awaitSendAsync(HttpMethod.Delete,url,account);// Assertresponse.StatusCode.Should().Be(HttpStatusCode.OK);}privatestaticAccountCreateAccount()=>newAccount();// simple POCOprivateasyncTask<HttpResponseMessage>SendAsync<T>(HttpMethodhttpMethod,Uriurl,Tresource)whereT:class{varrequest=newHttpRequestMessage(httpMethod,url){Content=newJsonContent(resource)};returnawait_httpClient.SendAsync(request,this.TestContext!.CancellationTokenSource.Token);}}
Describe the bug
Test which use an in-memory ASP.NET Core web server and linked HTTP client started failing with the following exception after I replaced
[ClassCleanup]
with[ClassCleanupBehavior.EndOfClass]
per MSTEST0034:Steps To Reproduce
Expected behavior
The tests continue working as before.
Actual behavior
See above.
Additional context
Test project:
NuGet packages:
The text was updated successfully, but these errors were encountered: