From 658e4e51e75dd5985125d2b5fefdb3d772cd13f4 Mon Sep 17 00:00:00 2001 From: Westin Musser <127992899+westin-m@users.noreply.github.com> Date: Wed, 13 Nov 2024 17:10:12 -0800 Subject: [PATCH] Prevent inlining to guarantee stack frames in test (#3002) * Prevent inlining to guarantee stack frames --- .../Validation/ValidationErrorTests.cs | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/test/Microsoft.IdentityModel.Tokens.Tests/Validation/ValidationErrorTests.cs b/test/Microsoft.IdentityModel.Tokens.Tests/Validation/ValidationErrorTests.cs index c1151641f8..03915c766c 100644 --- a/test/Microsoft.IdentityModel.Tokens.Tests/Validation/ValidationErrorTests.cs +++ b/test/Microsoft.IdentityModel.Tokens.Tests/Validation/ValidationErrorTests.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using System.Runtime.CompilerServices; using Xunit; namespace Microsoft.IdentityModel.Tokens.Tests @@ -10,29 +11,33 @@ public class ValidationErrorTests [Fact] public void ExceptionCreatedFromValidationError_ContainsTheRightStackTrace() { - var validationError = new ValidationErrorReturningClass().firstMethod(); + var validationError = new ValidationErrorReturningClass().FirstMethod(); Assert.NotNull(validationError); Assert.NotNull(validationError.StackFrames); Assert.Equal(3, validationError.StackFrames.Count); Assert.NotNull(validationError.GetException()); Assert.NotNull(validationError.GetException().StackTrace); - Assert.Equal("thirdMethod", validationError.StackFrames[0].GetMethod().Name); - Assert.Equal("secondMethod", validationError.StackFrames[1].GetMethod().Name); - Assert.Equal("firstMethod", validationError.StackFrames[2].GetMethod().Name); + Assert.Equal("ThirdMethod", validationError.StackFrames[0].GetMethod().Name); + Assert.Equal("SecondMethod", validationError.StackFrames[1].GetMethod().Name); + Assert.Equal("FirstMethod", validationError.StackFrames[2].GetMethod().Name); } + class ValidationErrorReturningClass { - public ValidationError firstMethod() + [MethodImpl(MethodImplOptions.NoInlining)] + public ValidationError FirstMethod() { - return secondMethod().AddCurrentStackFrame(); + return SecondMethod().AddCurrentStackFrame(); } - public ValidationError secondMethod() + [MethodImpl(MethodImplOptions.NoInlining)] + public ValidationError SecondMethod() { - return thirdMethod().AddCurrentStackFrame(); + return ThirdMethod().AddCurrentStackFrame(); } - public ValidationError thirdMethod() + [MethodImpl(MethodImplOptions.NoInlining)] + public ValidationError ThirdMethod() { return new ValidationError( new MessageDetail("This is a test error"),