Skip to content

Commit

Permalink
Prevent inlining to guarantee stack frames in test (#3002)
Browse files Browse the repository at this point in the history
* Prevent inlining to guarantee stack frames
  • Loading branch information
westin-m authored Nov 14, 2024
1 parent 04d0d13 commit 658e4e5
Showing 1 changed file with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"),
Expand Down

0 comments on commit 658e4e5

Please sign in to comment.