public async Task WriteNestedAsyncEnumerable_Nullable <TElement>(IEnumerable <TElement> source, int delayInterval, int bufferSize) { if (StreamingSerializer?.IsAsyncSerializer != true) { return; } // Primarily tests the ability of NullableConverter to flow async serialization state JsonSerializerOptions options = new JsonSerializerOptions { DefaultBufferSize = bufferSize, IncludeFields = true, }; string expectedJson = JsonSerializer.Serialize <(IEnumerable <TElement>, bool)?>((source, false), options); using var stream = new Utf8MemoryStream(); var asyncEnumerable = new MockedAsyncEnumerable <TElement>(source, delayInterval); await StreamingSerializer.SerializeWrapper <(IAsyncEnumerable <TElement>, bool)?>(stream, (asyncEnumerable, false), options); JsonTestHelper.AssertJsonEqual(expectedJson, stream.AsString()); Assert.Equal(1, asyncEnumerable.TotalCreatedEnumerators); Assert.Equal(1, asyncEnumerable.TotalDisposedEnumerators); }
public void WriteNestedAsyncEnumerableSync_ThrowsNotSupportedException() { IAsyncEnumerable <int> asyncEnumerable = new MockedAsyncEnumerable <int>(Enumerable.Range(1, 10)); Assert.Throws <NotSupportedException>(() => JsonSerializer.Serialize(new { Data = asyncEnumerable })); Assert.Throws <NotSupportedException>(() => JsonSerializer.Serialize(new MemoryStream(), new { Data = asyncEnumerable })); }
public async Task WriteAsyncEnumerableOfAsyncEnumerables <TElement>(IEnumerable <TElement> source, int delayInterval, int bufferSize) { if (StreamingSerializer?.IsAsyncSerializer != true) { return; } 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 StreamingSerializer.SerializeWrapper(stream, outerAsyncEnumerable, options); JsonTestHelper.AssertJsonEqual(expectedJson, stream.AsString()); Assert.Equal(1, outerAsyncEnumerable.TotalCreatedEnumerators); Assert.Equal(1, outerAsyncEnumerable.TotalDisposedEnumerators); Assert.Equal(OuterEnumerableCount, innerAsyncEnumerable.TotalCreatedEnumerators); Assert.Equal(OuterEnumerableCount, innerAsyncEnumerable.TotalDisposedEnumerators); }
public async Task WriteAsyncEnumerable_ElementSerializationThrows_ShouldDisposeEnumerator() { using var stream = new Utf8MemoryStream(); var asyncEnumerable = new MockedAsyncEnumerable <IEnumerable <int> >(Enumerable.Repeat(ThrowingEnumerable(), 2)); await Assert.ThrowsAsync <DivideByZeroException>(async() => await JsonSerializerWrapperForStream.SerializeWrapper(stream, new { Data = asyncEnumerable })); Assert.Equal(1, asyncEnumerable.TotalCreatedEnumerators); Assert.Equal(1, asyncEnumerable.TotalDisposedEnumerators);
public 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 async Task WriteAsyncEnumerable_ElementSerializationThrows_ShouldDisposeEnumerator() { if (StreamingSerializer?.IsAsyncSerializer != true) { return; } using var stream = new Utf8MemoryStream(); var asyncEnumerable = new MockedAsyncEnumerable <IEnumerable <int> >(Enumerable.Repeat(ThrowingEnumerable(), 2)); await Assert.ThrowsAsync <DivideByZeroException>(async() => await StreamingSerializer.SerializeWrapper(stream, new AsyncEnumerableDto <IEnumerable <int> > { Data = asyncEnumerable })); Assert.Equal(1, asyncEnumerable.TotalCreatedEnumerators); Assert.Equal(1, asyncEnumerable.TotalDisposedEnumerators);
public async Task WriteNestedAsyncEnumerable <TElement>(IEnumerable <TElement> source, int delayInterval, int bufferSize) { JsonSerializerOptions options = new JsonSerializerOptions { DefaultBufferSize = bufferSize }; string expectedJson = await JsonSerializerWrapperForString.SerializeWrapper(new { Data = source }); using var stream = new Utf8MemoryStream(); var asyncEnumerable = new MockedAsyncEnumerable <TElement>(source, delayInterval); await JsonSerializerWrapperForStream.SerializeWrapper(stream, new { Data = asyncEnumerable }, options); JsonTestHelper.AssertJsonEqual(expectedJson, stream.ToString()); Assert.Equal(1, asyncEnumerable.TotalCreatedEnumerators); Assert.Equal(1, asyncEnumerable.TotalDisposedEnumerators); }
public static async Task WriteRootLevelAsyncEnumerable <TElement>(IEnumerable <TElement> source, int delayInterval, int bufferSize) { JsonSerializerOptions options = new JsonSerializerOptions { DefaultBufferSize = bufferSize }; string expectedJson = JsonSerializer.Serialize(source); using var stream = new Utf8MemoryStream(); var asyncEnumerable = new MockedAsyncEnumerable <TElement>(source, delayInterval); await JsonSerializer.SerializeAsync(stream, asyncEnumerable, options); JsonTestHelper.AssertJsonEqual(expectedJson, stream.ToString()); Assert.Equal(1, asyncEnumerable.TotalCreatedEnumerators); Assert.Equal(1, asyncEnumerable.TotalDisposedEnumerators); }
public async Task WriteSequentialNestedAsyncEnumerables <TElement>(IEnumerable <TElement> source, int delayInterval, int bufferSize) { if (StreamingSerializer?.IsAsyncSerializer != true) { return; } JsonSerializerOptions options = new JsonSerializerOptions { DefaultBufferSize = bufferSize }; string expectedJson = JsonSerializer.Serialize(new { Data1 = source, Data2 = source }); using var stream = new Utf8MemoryStream(); var asyncEnumerable = new MockedAsyncEnumerable <TElement>(source, delayInterval); await StreamingSerializer.SerializeWrapper(stream, new { Data1 = asyncEnumerable, Data2 = asyncEnumerable }, options); JsonTestHelper.AssertJsonEqual(expectedJson, stream.AsString()); Assert.Equal(2, asyncEnumerable.TotalCreatedEnumerators); Assert.Equal(2, asyncEnumerable.TotalDisposedEnumerators); }
public async Task WriteNestedAsyncEnumerableSync_ThrowsNotSupportedException() { IAsyncEnumerable <int> asyncEnumerable = new MockedAsyncEnumerable <int>(Enumerable.Range(1, 10)); await Assert.ThrowsAsync <NotSupportedException>(async() => await JsonSerializerWrapperForString.SerializeWrapper(new { Data = asyncEnumerable })); }
public static void WriteRootLevelAsyncEnumerableSync_ThrowsNotSupportedException() { IAsyncEnumerable <int> asyncEnumerable = new MockedAsyncEnumerable <int>(Enumerable.Range(1, 10)); Assert.Throws <NotSupportedException>(() => JsonSerializer.Serialize(asyncEnumerable)); }