public static async Task WriteAsyncEnumerable_ElementSerializationThrows_ShouldDisposeEnumerator() { using var stream = new Utf8MemoryStream(); var asyncEnumerable = new MockedAsyncEnumerable <IEnumerable <int> >(Enumerable.Repeat(ThrowingEnumerable(), 2)); await Assert.ThrowsAsync <DivideByZeroException>(() => JsonSerializer.SerializeAsync(stream, new { Data = asyncEnumerable })); Assert.Equal(1, asyncEnumerable.TotalCreatedEnumerators); Assert.Equal(1, asyncEnumerable.TotalDisposedEnumerators);
public static async Task WriteAsyncEnumerable_LongRunningEnumeration_Cancellation() { var longRunningEnumerable = new MockedAsyncEnumerable <int>( source: Enumerable.Range(1, 100), delayInterval: 1, delay: TimeSpan.FromMinutes(1)); using var utf8Stream = new Utf8MemoryStream(); using var cts = new CancellationTokenSource(delay: TimeSpan.FromSeconds(5)); await Assert.ThrowsAsync <TaskCanceledException>(async() => await JsonSerializer.SerializeAsync(utf8Stream, longRunningEnumerable, cancellationToken: cts.Token)); Assert.Equal(1, longRunningEnumerable.TotalCreatedEnumerators); Assert.Equal(1, longRunningEnumerable.TotalDisposedEnumerators); }
public static async Task WriteNestedAsyncEnumerable <TElement>(IEnumerable <TElement> source, int delayInterval, int bufferSize) { JsonSerializerOptions options = new JsonSerializerOptions { DefaultBufferSize = bufferSize }; string expectedJson = JsonSerializer.Serialize(new { Data = source }); using var stream = new Utf8MemoryStream(); var asyncEnumerable = new MockedAsyncEnumerable <TElement>(source, delayInterval); await JsonSerializer.SerializeAsync(stream, new { Data = asyncEnumerable }, options); JsonTestHelper.AssertJsonEqual(expectedJson, stream.ToString()); Assert.Equal(1, asyncEnumerable.TotalCreatedEnumerators); Assert.Equal(1, asyncEnumerable.TotalDisposedEnumerators); }
public static async Task WriteAsyncEnumerableOfAsyncEnumerables <TElement>(IEnumerable <TElement> source, int delayInterval, int bufferSize) { JsonSerializerOptions options = new JsonSerializerOptions { DefaultBufferSize = bufferSize }; const int OuterEnumerableCount = 5; string expectedJson = JsonSerializer.Serialize(Enumerable.Repeat(source, OuterEnumerableCount)); var innerAsyncEnumerable = new MockedAsyncEnumerable <TElement>(source, delayInterval); var outerAsyncEnumerable = new MockedAsyncEnumerable <IAsyncEnumerable <TElement> >( Enumerable.Repeat(innerAsyncEnumerable, OuterEnumerableCount), delayInterval); using var stream = new Utf8MemoryStream(); await JsonSerializer.SerializeAsync(stream, outerAsyncEnumerable, options); JsonTestHelper.AssertJsonEqual(expectedJson, stream.ToString()); Assert.Equal(1, outerAsyncEnumerable.TotalCreatedEnumerators); Assert.Equal(1, outerAsyncEnumerable.TotalDisposedEnumerators); Assert.Equal(OuterEnumerableCount, innerAsyncEnumerable.TotalCreatedEnumerators); Assert.Equal(OuterEnumerableCount, innerAsyncEnumerable.TotalDisposedEnumerators); }