public async Task SerializeToStreamAsync_CanBeCanceledExternally()
    {
        var tcs = new TaskCompletionSource <byte>(TaskCreationOptions.RunContinuationsAsynchronously);

        var source = new ReadDelegatingStream(new MemoryStream(), async(buffer, cancellation) =>
        {
            if (buffer.Length == 0)
            {
                return(0);
            }

            Assert.False(cancellation.IsCancellationRequested);
            await tcs.Task;
            Assert.True(cancellation.IsCancellationRequested);
            return(0);
        });

        var sut = CreateContent(source);

        using var cts = new CancellationTokenSource();
        var copyToTask = sut.CopyToAsync(new MemoryStream(), cts.Token);

        cts.Cancel();
        tcs.SetResult(0);

        await copyToTask;
    }
    public async Task SerializeToStreamAsync_RespectsContentCancellation()
    {
        var tcs = new TaskCompletionSource <byte>(TaskCreationOptions.RunContinuationsAsynchronously);

        var source = new ReadDelegatingStream(new MemoryStream(), async(buffer, cancellation) =>
        {
            if (buffer.Length == 0)
            {
                return(0);
            }

            Assert.False(cancellation.IsCancellationRequested);
            await tcs.Task;
            Assert.True(cancellation.IsCancellationRequested);
            return(0);
        });

        using var contentCts = ActivityCancellationTokenSource.Rent(TimeSpan.FromSeconds(10), CancellationToken.None);

        var sut = CreateContent(source, contentCancellation: contentCts);

        var copyToTask = sut.CopyToWithCancellationAsync(new MemoryStream());

        contentCts.Cancel();
        tcs.SetResult(0);

        await copyToTask;
    }