Repository: dotnet/aspnetcore
File: src/Servers/Kestrel/test/FunctionalTests/RequestTests.cs (C#)

Code before:
                logMessageReceived.SetResult();
            }
        };

        await using var server = new TestServer(async context =>
        {
            var buffer = new byte[1024];

            // Attempt to read more of the body than will show up.
            await context.Request.Body.ReadAsync(buffer, 0, buffer.Length);
        }, new TestServiceContext(LoggerFactory));

        using (var connection = server.CreateConnection())
        {
            await connection.Send(
                "POST / HTTP/1.1",
                "Host:",
                "Connection: keep-alive",
                "Content-Type: application/json",
                "Content-Length: 100",  // Declare a larger body than will be sent
                "",
                "");
        }

        // Log message should not have appeared.
        await Assert.ThrowsAsync<TimeoutException>(() => logMessageReceived.Task.TimeoutAfter(TimeSpan.FromSeconds(1)));
    }

    [Fact]
    public async Task ConnectionResetBetweenRequestsIsLoggedAsDebug()
    {
        var connectionReset = new SemaphoreSlim(0);
        var loggedHigherThanDebug = false;

        TestSink.MessageLogged += context =>
        {
            if (context.LoggerName != "Microsoft.AspNetCore.Server.Kestrel" &&
                context.LoggerName != "Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets")
            {
                return;
            }

            if (context.LogLevel > LogLevel.Debug)
            {
                loggedHigherThanDebug = true;
            }

            if (context.EventId.Id == _connectionResetEventId)
            {
                connectionReset.Release();
            }

Review comment (suggestion):
I don't like relying on real world timeouts in tests. It's often a flaky test waiting to happen.

We should be able to wait on `TestServer.DisposeAsync()` and check that the log wasn't received without a timeout.

Code after:
        TestSink.MessageLogged += context =>
        {
            if (context.LoggerName == "Microsoft.AspNetCore.Server.Kestrel.BadRequests"
                && context.EventId == badRequestEventId
                && context.LogLevel == LogLevel.Debug)
            {
                badRequestLogged.TrySetResult();
            }
            else if (context.LoggerName == "Microsoft.AspNetCore.Server.Kestrel"
                    && context.EventId.Id == appErrorEventId
                    && context.LogLevel > LogLevel.Debug)
            {
                appErrorLogged.TrySetResult();
            }
            else if (context.LoggerName == "Microsoft.AspNetCore.Server.Kestrel.Connections"
                    && context.EventId == connectionStopEventId)
            {
                connectionStoppedLogged.TrySetResult();
            }
        };

        await using var server = new TestServer(async context =>
        {
            var buffer = new byte[1024];

            // Attempt to read more of the body than will show up.
            await context.Request.Body.ReadAsync(buffer, 0, buffer.Length);
        }, new TestServiceContext(LoggerFactory));

        using (var connection = server.CreateConnection())
        {
            await connection.Send(
                "POST / HTTP/1.1",
                "Host:",
                "Connection: keep-alive",
                "Content-Type: application/json",
                "Content-Length: 100",  // Declare a larger body than will be sent
                "",
                "");
        }

        await connectionStoppedLogged.Task.DefaultTimeout();

        // Bad request log message should have fired.
        await badRequestLogged.Task.DefaultTimeout();

        // App error log message should not have fired.
        Assert.False(appErrorLogged.Task.IsCompleted);
    }

    [Fact]