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

Code before:
            typeof(OperationCanceledException), typeof(TaskCanceledException));
        Assert.False(tlsClientHelloCallbackInvoked);
    }

    [Fact]
    public async Task RunTlsClientHelloCallbackTest_WithPendingCancellation()
    {
        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();
        await writer.WriteAsync(new byte[1] { 0x16 });
        var listenerTask = listener.OnTlsClientHelloAsync(transportConnection, cts.Token);
        await writer.WriteAsync(new byte[2] { 0x03, 0x01 });
        cts.Cancel();

        await VerifyThrowsAnyAsync(() => listenerTask, typeof(OperationCanceledException), typeof(TaskCanceledException));
        Assert.False(tlsClientHelloCallbackInvoked);
    }

    [Fact]
    public async Task RunTlsClientHelloCallbackTest_DeterministicallyReads()
    {
        /* Current test ensures that we read the input stream only a limited number of times.
         * It is a guard against incorrect transport.AdvanceTo() usage leading to infinite loop / more reads than should happen.
         */

        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; });

        await writer.WriteAsync(new byte[1] { 0x16 });
        var listenerTask = listener.OnTlsClientHelloAsync(transportConnection, CancellationToken.None);
        await writer.WriteAsync(new byte[2] { 0x03, 0x01 });

Review comment (suggestion):
Normally we just use `Assert.ThrowsAnyAsync<OperationCanceledException>(...);` for this

Code after:
    [Fact]
    public async Task RunTlsClientHelloCallbackTest_WithPendingCancellation()
    {
        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();
        await writer.WriteAsync(new byte[1] { 0x16 });
        var listenerTask = listener.OnTlsClientHelloAsync(transportConnection, cts.Token);
        await writer.WriteAsync(new byte[2] { 0x03, 0x01 });
        cts.Cancel();

        await Assert.ThrowsAnyAsync<OperationCanceledException>(() => listenerTask);
        Assert.False(tlsClientHelloCallbackInvoked);
    }

    [Fact]
    public async Task RunTlsClientHelloCallbackTest_DeterministicallyReads()
    {
        /* Current test ensures that we read the input stream only a limited number of times.
         * It is a guard against incorrect transport.AdvanceTo() usage leading to infinite loop / more reads than should happen.
         */

        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; });

        await writer.WriteAsync(new byte[1] { 0x16 });
        var listenerTask = listener.OnTlsClientHelloAsync(transportConnection, CancellationToken.None);
        await writer.WriteAsync(new byte[2] { 0x03, 0x01 });
        await writer.WriteAsync(new byte[2] { 0x00, 0x20 });
        await writer.CompleteAsync();

        await listenerTask;