dotnet / dotnet/aspnetcore

TestHost keep the test running infinitely

Open
#54,347 2 comments 0 reactions 0 assignees View on GitHub
area-minimal
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

The test keeps running when some exception is thrown on the scope log creation (and the test never ends).

> I have used a library that because of its bad behavior/configuration some exceptions is thrown `ILogger.BeginScope` and as the test keeps running, it makes diagnosing the problem more complex

### Expected Behavior

The HttpClient should return `InternalServerError`.

### Steps To Reproduce

On `Program.cs`:
```csharp
var app = WebApplication.CreateBuilder(args).Build();
app.MapGet("/hello-world", ([FromServices] ILogger logger) =>
{
logger.LogInformation("Starting...");
return "Hi";
});
app.Run();
```

On TestClass:
```csharp
[Fact]
public async Task Test1()
{
// Arrange
var client = new CustomWebApplicationFactory()
.CreateClient();

// Act
var result = await client.GetAsync("/hello-world");

// Assert
Assert.Equal(HttpStatusCode.InternalServerError, result.StatusCode);
}
internal class CustomWebApplicationFactory : WebApplicationFactory
{
protected override void ConfigureWebHost(IWebHostBuilder builder) => builder
.ConfigureTestServices(services =>
services.AddLogging(options =>
{
options.ClearProviders();
options.AddProvider(new MyLoggerProvider());
})
);
}
internal class MyLoggerProvider : ILoggerProvider
{
public ILogger CreateLogger(string categoryName) => new MyLogger();
public void Dispose() { }
}
internal class MyLogger : ILogger
{
public IDisposable? BeginScope(TState state) where TState : notnull => throw new Exception("💣💣💣");
public bool IsEnabled(LogLevel logLevel) => true;
public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) { }
}
```

### Exceptions (if any)

_No response_

### .NET Version

8.0.200

### Anything else?

Microsoft Visual Studio Professional 2022 (64-bit) - Preview
Version 17.10.0 Preview 1.0

```bash
.NET SDK:
Version: 8.0.200
Commit: 438cab6a9d
Workload version: 8.0.200-manifests.5638171e

Ambiente de runtime:
OS Name: Windows
OS Version: 10.0.19045
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\8.0.200\

Cargas de trabalho do .NET instaladas:
Não há cargas de trabalho instaladas para exibir.

Host:
Version: 8.0.2
Architecture: x64
Commit: 1381d5ebd2

.NET SDKs installed:
7.0.313 [C:\Program Files\dotnet\sdk]
7.0.406 [C:\Program Files\dotnet\sdk]
8.0.200 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.27 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.27 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.27 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.16 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
Not set

global.json file:
Not found
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.