forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvironment.StackTrace.cs
More file actions
88 lines (76 loc) · 3.17 KB
/
Environment.StackTrace.cs
File metadata and controls
88 lines (76 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// 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;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Xunit;
namespace System.Tests
{
public class EnvironmentStackTrace
{
static string s_stackTrace;
[Fact]
[ActiveIssue("https://github.com/mono/mono/issues/15315", TestRuntimes.Mono)]
public void StackTraceTest()
{
//arrange
List<string> knownFrames = new List<string>()
{
"System.Tests.EnvironmentStackTrace.StaticFrame(Object obj)",
"System.Tests.EnvironmentStackTrace.TestClass..ctor()",
AppContext.TryGetSwitch("Switch.System.Diagnostics.StackTrace.ShowGenericInstantiations", out var showGenericInstantiations)
?
showGenericInstantiations
? "System.Tests.EnvironmentStackTrace.GenericFrame[System.DateTime,System.Text.StringBuilder](DateTime t1, StringBuilder t2)"
: "System.Tests.EnvironmentStackTrace.GenericFrame[T1,T2](T1 t1, T2 t2)"
: "System.Tests.EnvironmentStackTrace.GenericFrame[T1,T2](T1 t1, T2 t2)"
,
"System.Tests.EnvironmentStackTrace.TestFrame()"
};
//act
Task.Run(() => TestFrame()).Wait();
//assert
int index = 0;
foreach (string frame in knownFrames)
{
index = s_stackTrace.IndexOf(frame, index);
Assert.True(index > -1);
index += frame.Length;
}
}
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
private void TestFrame()
{
GenericFrame<DateTime, StringBuilder>(DateTime.Now, null);
}
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
private void GenericFrame<T1, T2>(T1 t1, T2 t2)
{
new TestClass();
}
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
private static void StaticFrame(object obj)
{
s_stackTrace = Environment.StackTrace;
}
private class TestClass
{
[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
public TestClass()
{
StaticFrame(null);
}
}
[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/50957", typeof(PlatformDetection), nameof(PlatformDetection.IsBrowser), nameof(PlatformDetection.IsMonoAOT))]
public void StackTraceDoesNotStartWithInternalFrame()
{
string stackTrace = Environment.StackTrace;
// Find first line of the stacktrace and verify that it is Environment.get_StackTrace itself, not an internal frame
string firstFrame = new StringReader(stackTrace).ReadLine();
Assert.True(firstFrame.IndexOf("System.Environment.get_StackTrace()") != -1);
}
}
}