Skip to content

Commit

Permalink
Make TooDeepJsonDocument test more consistent across platforms (#105445)
Browse files Browse the repository at this point in the history
* Make TooDeepJsonDocument test more consistent across platforms

Run the test on a thread with as consistent a stack size as possible so that we don't inadvertently succeed due to having a really large stack.

* Disable test on mono
  • Loading branch information
stephentoub authored and directhex committed Jul 26, 2024
1 parent ec5ac99 commit a16097d
Showing 1 changed file with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

using System.Buffers;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace System.Text.Json.Serialization.Tests
Expand Down Expand Up @@ -219,22 +220,36 @@ public static void DeepEquals_NotEqualValuesReturnFalse(string value1, string va
}

[Theory]
[InlineData(10)]
[InlineData(100)]
[InlineData(500)]
[InlineData(5)]
[InlineData(50)]
public static void DeepEquals_DeepJsonDocument(int depth)
{
using JsonDocument jDoc = CreateDeepJsonDocument(depth);
JsonElement element = jDoc.RootElement;
Assert.True(JsonElement.DeepEquals(element, element));
}

[Fact]
public static void DeepEquals_TooDeepJsonDocument_ThrowsInsufficientExecutionStackException()
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/105490", TestRuntimes.Mono)]
public static async Task DeepEquals_TooDeepJsonDocument_ThrowsInsufficientExecutionStackException()
{
using JsonDocument jDoc = CreateDeepJsonDocument(10_000);
JsonElement element = jDoc.RootElement;
Assert.Throws<InsufficientExecutionStackException>(() => JsonElement.DeepEquals(element, element));
var tcs = new TaskCompletionSource<bool>();
new Thread(() =>
{
try
{
using JsonDocument jDoc = CreateDeepJsonDocument(10_000);
JsonElement element = jDoc.RootElement;
Assert.Throws<InsufficientExecutionStackException>(() => JsonElement.DeepEquals(element, element));
tcs.SetResult(true);
}
catch (Exception e)
{
tcs.SetException(e);
}
}, maxStackSize: 100_000) { IsBackground = true }.Start();

await tcs.Task;
}

private static JsonDocument CreateDeepJsonDocument(int depth)
Expand Down

0 comments on commit a16097d

Please sign in to comment.