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

Code before:
namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests;

public class TlsListenerTests
{
    [Theory]
    [MemberData(nameof(ValidClientHelloData))]
    public Task OnTlsClientHelloAsync_ValidData(int id, byte[] packetBytes)
        => RunTlsClientHelloCallbackTest(id, packetBytes, tlsClientHelloCallbackExpected: true);

    [Theory]
    [MemberData(nameof(InvalidClientHelloData))]
    public Task OnTlsClientHelloAsync_InvalidData(int id, byte[] packetBytes)
        => RunTlsClientHelloCallbackTest(id, packetBytes, tlsClientHelloCallbackExpected: false);

    [Theory]
    [MemberData(nameof(ValidClientHelloData_Segmented))]
    public Task OnTlsClientHelloAsync_ValidData_MultipleSegments(int id, List<byte[]> packets)
        => RunTlsClientHelloCallbackTest_WithMultipleSegments(id, packets, tlsClientHelloCallbackExpected: true);

    [Theory]
    [MemberData(nameof(InvalidClientHelloData_Segmented))]
    public Task OnTlsClientHelloAsync_InvalidData_MultipleSegments(int id, List<byte[]> packets)
        => RunTlsClientHelloCallbackTest_WithMultipleSegments(id, packets, tlsClientHelloCallbackExpected: false);

    [Fact]
    public async Task RunTlsClientHelloCallbackTest_WithPreCancelledToken()
    {
        var serviceContext = new TestServiceContext();

        var pipe = new Pipe();
        var writer = pipe.Writer;
        var reader = new ObservablePipeReader(pipe.Reader);

        var transport = new DuplexPipe(reader, writer);
        var transportConnection = new DefaultConnectionContext("test", transport, transport);

        var tlsClientHelloCallbackInvoked = false;
        var listener = new TlsListener((ctx, data) => { tlsClientHelloCallbackInvoked = true; });

        var cts = new CancellationTokenSource();
        cts.Cancel();

        await writer.WriteAsync(new byte[1] { 0x16 });
        await Assert.ThrowsAsync<OperationCanceledException>(async () =>
        {
            await listener.OnTlsClientHelloAsync(transportConnection, cts.Token);
        });
        Assert.False(tlsClientHelloCallbackInvoked);
    }

    [Fact]

Review comment (suggestion):
```suggestion
    public async Task RunTlsClientHelloCallbackTest_WithPreCanceledToken()
```

Code after:
using Xunit.Sdk;

namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests;

public class TlsListenerTests
{
    [Theory]
    [MemberData(nameof(ValidClientHelloData))]
    public Task OnTlsClientHelloAsync_ValidData(int id, byte[] packetBytes)
        => RunTlsClientHelloCallbackTest(id, packetBytes, tlsClientHelloCallbackExpected: true);

    [Theory]
    [MemberData(nameof(InvalidClientHelloData))]
    public Task OnTlsClientHelloAsync_InvalidData(int id, byte[] packetBytes)
        => RunTlsClientHelloCallbackTest(id, packetBytes, tlsClientHelloCallbackExpected: false);

    [Theory]
    [MemberData(nameof(ValidClientHelloData_Segmented))]
    public Task OnTlsClientHelloAsync_ValidData_MultipleSegments(int id, List<byte[]> packets)
        => RunTlsClientHelloCallbackTest_WithMultipleSegments(id, packets, tlsClientHelloCallbackExpected: true);

    [Theory]
    [MemberData(nameof(InvalidClientHelloData_Segmented))]
    public Task OnTlsClientHelloAsync_InvalidData_MultipleSegments(int id, List<byte[]> packets)
        => RunTlsClientHelloCallbackTest_WithMultipleSegments(id, packets, tlsClientHelloCallbackExpected: false);

    [Fact]
    public async Task RunTlsClientHelloCallbackTest_WithExtraShortLastingToken()
    {
        var serviceContext = new TestServiceContext();

        var pipe = new Pipe();
        var writer = pipe.Writer;
        var reader = new ObservablePipeReader(pipe.Reader);

        var transport = new DuplexPipe(reader, writer);
        var transportConnection = new DefaultConnectionContext("test", transport, transport);

        var tlsClientHelloCallbackInvoked = false;
        var listener = new TlsListener((ctx, data) => { tlsClientHelloCallbackInvoked = true; });

        var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(3));

        await writer.WriteAsync(new byte[1] { 0x16 });
        await VerifyThrowsAnyAsync(
            async () => await listener.OnTlsClientHelloAsync(transportConnection, cts.Token),
            typeof(OperationCanceledException), typeof(TaskCanceledException));
        Assert.False(tlsClientHelloCallbackInvoked);
    }

    [Fact]