Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ rootValue is not null &&
supportContinuation: true,
supportAsync: true);

state.CancellationToken = cancellationToken;

using var bufferWriter = new PooledByteBufferWriter(Options.DefaultBufferSize);
using var writer = new Utf8JsonWriter(bufferWriter, Options.GetWriterOptions());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
Expand Down Expand Up @@ -116,6 +117,28 @@ public async Task WriteNestedAsyncEnumerable_Nullable<TElement>(IEnumerable<TEle
Assert.Equal(1, asyncEnumerable.TotalDisposedEnumerators);
}

[Fact]
public async Task WriteAsyncEnumerable_CancellationToken_IsPassedToAsyncEnumerator()
{
// Regression test for https://github.com/dotnet/runtime/issues/79556
using var utf8Stream = new Utf8MemoryStream(ignoreCancellationTokenOnWriteAsync: true);
using var cts = new CancellationTokenSource();

IAsyncEnumerable<int> value = CreateEnumerable();
await JsonSerializer.SerializeAsync(utf8Stream, value, cancellationToken: cts.Token);
Assert.Equal("[1,2]", utf8Stream.AsString());

async IAsyncEnumerable<int> CreateEnumerable([EnumeratorCancellation] CancellationToken cancellationToken = default)
{
yield return 1;
await Task.Delay(20);
Assert.False(cancellationToken.IsCancellationRequested);
cts.Cancel();
Assert.True(cancellationToken.IsCancellationRequested);
yield return 2;
}
}

[Theory, OuterLoop]
[InlineData(5000, 1000, true)]
[InlineData(5000, 1000, false)]
Expand Down
14 changes: 13 additions & 1 deletion src/libraries/System.Text.Json/tests/Common/Utf8MemoryStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,31 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace System.Text.Json.Serialization.Tests
{
public sealed class Utf8MemoryStream : MemoryStream
{
public Utf8MemoryStream() : base()
private readonly bool _ignoreCancellationTokenOnWriteAsync;

public Utf8MemoryStream(bool ignoreCancellationTokenOnWriteAsync = false) : base()
{
_ignoreCancellationTokenOnWriteAsync = ignoreCancellationTokenOnWriteAsync;
}

public Utf8MemoryStream(string text) : base(Encoding.UTF8.GetBytes(text))
{
}

#if NETCOREAPP
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
=> base.WriteAsync(buffer, _ignoreCancellationTokenOnWriteAsync ? default : cancellationToken);
#endif
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
=> base.WriteAsync(buffer, offset, count, _ignoreCancellationTokenOnWriteAsync ? default : cancellationToken);

public string AsString() => Encoding.UTF8.GetString(ToArray());
}
}