Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite System.Text.Json stream tests to be async friendly and enable on WASM #38663

Merged
merged 8 commits into from
Jul 10, 2020
6 changes: 3 additions & 3 deletions src/libraries/System.Text.Json/tests/JsonDocumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3646,8 +3646,8 @@ private static string GetCompactJson(TestCaseType testCaseType, string jsonStrin
return s_compactJson[testCaseType] = existing;
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
public static void VerifyMultiThreadedDispose()
[Fact]
public static async Task VerifyMultiThreadedDispose()
{
Action<object> disposeAction = (object document) => ((JsonDocument)document).Dispose();

Expand All @@ -3666,7 +3666,7 @@ public static void VerifyMultiThreadedDispose()
}
}

Task.WaitAll(tasks);
await Task.WhenAll(tasks);

// When ArrayPool gets corrupted, the Rent method might return an already rented array, which is incorrect.
// So we will rent as many arrays as calls to JsonElement.Dispose and check they are unique.
Expand Down
28 changes: 14 additions & 14 deletions src/libraries/System.Text.Json/tests/Serialization/CacheTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
Expand All @@ -11,18 +11,18 @@ namespace System.Text.Json.Serialization.Tests
public static class CacheTests
{
[Fact, OuterLoop]
public static void MultipleThreads_SameType_DifferentJson_Looping()
public static async Task MultipleThreads_SameType_DifferentJson_Looping()
{
const int Iterations = 100;

for (int i = 0; i < Iterations; i++)
{
MultipleThreads_SameType_DifferentJson();
await MultipleThreads_SameType_DifferentJson();
}
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
public static void MultipleThreads_SameType_DifferentJson()
[Fact]
public static async Task MultipleThreads_SameType_DifferentJson()
{
// Use local options to avoid obtaining already cached metadata from the default options.
var options = new JsonSerializerOptions();
Expand Down Expand Up @@ -69,22 +69,22 @@ void SerializeObject()
tasks[i + 3] = Task.Run(() => SerializeObject());
};

Task.WaitAll(tasks);
await Task.WhenAll(tasks);
}

[Fact, OuterLoop]
public static void MultipleThreads_DifferentTypes_Looping()
public static async Task MultipleThreads_DifferentTypes_Looping()
{
const int Iterations = 100;

for (int i = 0; i < Iterations; i++)
{
MultipleThreads_DifferentTypes();
await MultipleThreads_DifferentTypes();
}
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
public static void MultipleThreads_DifferentTypes()
[Fact]
public static async Task MultipleThreads_DifferentTypes()
{
// Use local options to avoid obtaining already cached metadata from the default options.
var options = new JsonSerializerOptions();
Expand Down Expand Up @@ -121,7 +121,7 @@ void Test(int i)
tasks[i + 1] = Task.Run(() => Test(TestClassCount - 2));
}

Task.WaitAll(tasks);
await Task.WhenAll(tasks);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI this area of code really does care about threading, and the changes shouldn't affect the multi-threading aspects.

}

[Fact]
Expand Down Expand Up @@ -164,9 +164,9 @@ public static void PropertyCacheWithMinInputsLast()
// this options is not the default options instance the tests will not use previously cached metadata.
private static JsonSerializerOptions s_options = new JsonSerializerOptions();

[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[Theory]
[MemberData(nameof(WriteSuccessCases))]
public static void MultipleTypes(ITestClass testObj)
public static async Task MultipleTypes(ITestClass testObj)
{
Type type = testObj.GetType();

Expand Down Expand Up @@ -199,7 +199,7 @@ void Deserialize()
tasks[i + 1] = Task.Run(() => Serialize());
};

Task.WaitAll(tasks);
await Task.WhenAll(tasks);
}

public static IEnumerable<object[]> WriteSuccessCases
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ public abstract partial class ConstructorTests
[Fact]
public async Task NonPublicCtors_NotSupported()
{
async Task RunTest<T>()
async Task RunTestAsync<T>()
{
NotSupportedException ex = await Assert.ThrowsAsync<NotSupportedException>(() => Serializer.DeserializeAsync<T>("{}"));
Assert.Contains("JsonConstructorAttribute", ex.ToString());
}

await RunTest<PrivateParameterlessCtor>();
await RunTest<InternalParameterlessCtor>();
await RunTest<ProtectedParameterlessCtor>();
await RunTest<PrivateParameterizedCtor>();
await RunTest<InternalParameterizedCtor>();
await RunTest<ProtectedParameterizedCtor>();
await RunTest<PrivateParameterizedCtor_WithAttribute>();
await RunTest<InternalParameterizedCtor_WithAttribute>();
await RunTest<ProtectedParameterizedCtor_WithAttribute>();
await RunTestAsync<PrivateParameterlessCtor>();
await RunTestAsync<InternalParameterlessCtor>();
await RunTestAsync<ProtectedParameterlessCtor>();
await RunTestAsync<PrivateParameterizedCtor>();
await RunTestAsync<InternalParameterizedCtor>();
await RunTestAsync<ProtectedParameterizedCtor>();
await RunTestAsync<PrivateParameterizedCtor_WithAttribute>();
await RunTestAsync<InternalParameterizedCtor_WithAttribute>();
await RunTestAsync<ProtectedParameterizedCtor_WithAttribute>();
}

[Fact]
Expand All @@ -38,47 +38,47 @@ public async Task SinglePublicParameterizedCtor_SingleParameterlessCtor_NoAttrib
[Fact]
public async Task MultiplePublicParameterizedCtors_SingleParameterlessCtor_NoAttribute_Supported_UseParameterlessCtor()
{
async Task RunTest<T>()
async Task RunTestAsync<T>()
{
var obj1 = await Serializer.DeserializeAsync<T>(@"{""MyInt"":1,""MyString"":""1""}");
Assert.Equal(@"{""MyInt"":0,""MyString"":null}", JsonSerializer.Serialize(obj1));
}

await RunTest<SingleParameterlessCtor_MultiplePublicParameterizedCtor>();
await RunTest<SingleParameterlessCtor_MultiplePublicParameterizedCtor_Struct>();
await RunTestAsync<SingleParameterlessCtor_MultiplePublicParameterizedCtor>();
await RunTestAsync<SingleParameterlessCtor_MultiplePublicParameterizedCtor_Struct>();
}

[Fact]
public async Task SinglePublicParameterizedCtor_NoPublicParameterlessCtor_NoAttribute_Supported()
{
async Task RunTest<T>()
async Task RunTestAsync<T>()
{
var obj1 = await Serializer.DeserializeAsync<T>(@"{""MyInt"":1}");
Assert.Equal(@"{""MyInt"":1}", JsonSerializer.Serialize(obj1));
}

await RunTest<PublicParameterizedCtor>();
await RunTest<PrivateParameterlessConstructor_PublicParameterizedCtor>();
await RunTestAsync<PublicParameterizedCtor>();
await RunTestAsync<PrivateParameterlessConstructor_PublicParameterizedCtor>();
}

[Fact]
public async Task SinglePublicParameterizedCtor_NoPublicParameterlessCtor_WithAttribute_Supported()
{
async Task RunTest<T>()
async Task RunTestAsync<T>()
{
var obj1 = await Serializer.DeserializeAsync<T>(@"{""MyInt"":1}");
Assert.Equal(@"{""MyInt"":1}", JsonSerializer.Serialize(obj1));
}

await RunTest<PublicParameterizedCtor_WithAttribute>();
await RunTest<Struct_PublicParameterizedConstructor_WithAttribute>();
await RunTest<PrivateParameterlessConstructor_PublicParameterizedCtor_WithAttribute>();
await RunTestAsync<PublicParameterizedCtor_WithAttribute>();
await RunTestAsync<Struct_PublicParameterizedConstructor_WithAttribute>();
await RunTestAsync<PrivateParameterlessConstructor_PublicParameterizedCtor_WithAttribute>();
}

[Fact]
public async Task Class_MultiplePublicParameterizedCtors_NoPublicParameterlessCtor_NoAttribute_NotSupported()
public Task Class_MultiplePublicParameterizedCtors_NoPublicParameterlessCtor_NoAttribute_NotSupported()
{
await Assert.ThrowsAsync<NotSupportedException>(() => Serializer.DeserializeAsync<MultiplePublicParameterizedCtor>(@"{""MyInt"":1,""MyString"":""1""}"));
return Assert.ThrowsAsync<NotSupportedException>(() => Serializer.DeserializeAsync<MultiplePublicParameterizedCtor>(@"{""MyInt"":1,""MyString"":""1""}"));
}

[Fact]
Expand Down Expand Up @@ -116,32 +116,32 @@ public async Task PublicParameterlessCtor_MultiplePublicParameterizedCtors_WithA
[Fact]
public async Task MultipleAttributes_NotSupported()
{
async Task RunTest<T>()
async Task RunTestAsync<T>()
{
await Assert.ThrowsAsync<InvalidOperationException>(() => Serializer.DeserializeAsync<T>("{}"));
}

await RunTest<MultiplePublicParameterizedCtor_WithMultipleAttributes>();
await RunTest<PublicParameterlessConstructor_PublicParameterizedCtor_WithMultipleAttributes>();
await RunTest<PrivateParameterlessCtor_InternalParameterizedCtor_WithMultipleAttributes>();
await RunTest<ProtectedParameterlessCtor_PrivateParameterizedCtor_WithMultipleAttributes>();
await RunTest<PublicParameterlessCtor_PrivateParameterizedCtor_WithMultipleAttributes>();
await RunTest<PublicParameterizedCtor_PublicParameterizedCtor_WithMultipleAttributes>();
await RunTest<Struct_PublicParameterizedCtor_PrivateParameterizedCtor_WithMultipleAttributes>();
await RunTest<Point_2D_Struct_WithMultipleAttributes>();
await RunTest<Point_2D_Struct_WithMultipleAttributes_OneNonPublic>();
await RunTestAsync<MultiplePublicParameterizedCtor_WithMultipleAttributes>();
await RunTestAsync<PublicParameterlessConstructor_PublicParameterizedCtor_WithMultipleAttributes>();
await RunTestAsync<PrivateParameterlessCtor_InternalParameterizedCtor_WithMultipleAttributes>();
await RunTestAsync<ProtectedParameterlessCtor_PrivateParameterizedCtor_WithMultipleAttributes>();
await RunTestAsync<PublicParameterlessCtor_PrivateParameterizedCtor_WithMultipleAttributes>();
await RunTestAsync<PublicParameterizedCtor_PublicParameterizedCtor_WithMultipleAttributes>();
await RunTestAsync<Struct_PublicParameterizedCtor_PrivateParameterizedCtor_WithMultipleAttributes>();
await RunTestAsync<Point_2D_Struct_WithMultipleAttributes>();
await RunTestAsync<Point_2D_Struct_WithMultipleAttributes_OneNonPublic>();
}

[Fact]
public async Task AttributeIgnoredOnIEnumerable()
{
async Task RunTest<T>()
async Task RunTestAsync<T>()
{
await Assert.ThrowsAsync<NotSupportedException>(() => Serializer.DeserializeAsync<T>("[]"));
}

await RunTest<Parameterized_StackWrapper>();
await RunTest<Parameterized_WrapperForICollection>();
await RunTestAsync<Parameterized_StackWrapper>();
await RunTestAsync<Parameterized_WrapperForICollection>();
}

[Fact]
Expand Down
Loading