Exemple #1
0
        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);
Exemple #2
0
        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 async Task PreserveReferenceOfTypeObjectAsync()
        {
            var root = new ClassWithObjectProperty();

            root.Child   = new ClassWithObjectProperty();
            root.Sibling = root.Child;

            Assert.Same(root.Child, root.Sibling);

            var stream = new MemoryStream();
            await JsonSerializerWrapperForStream.SerializeWrapper(stream, root, s_serializerOptionsPreserve);

            stream.Position = 0;

            ClassWithObjectProperty rootCopy = await JsonSerializer.DeserializeAsync <ClassWithObjectProperty>(stream, s_serializerOptionsPreserve);

            Assert.Same(rootCopy.Child, rootCopy.Sibling);
        }
Exemple #4
0
        public async Task WriteAsyncEnumerableOfAsyncEnumerables <TElement>(IEnumerable <TElement> source, int delayInterval, int bufferSize)
        {
            JsonSerializerOptions options = new JsonSerializerOptions
            {
                DefaultBufferSize = bufferSize
            };

            const int OuterEnumerableCount = 5;
            string    expectedJson         = await JsonSerializerWrapperForString.SerializeWrapper(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 JsonSerializerWrapperForStream.SerializeWrapper(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);
        }