-
Notifications
You must be signed in to change notification settings - Fork 374
/
Copy pathGetProcessInfoTests.cs
195 lines (167 loc) · 8.44 KB
/
GetProcessInfoTests.cs
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Microsoft.Diagnostics.CommonTestRunner;
using Microsoft.Diagnostics.TestHelpers;
using Xunit;
using Xunit.Abstractions;
using Xunit.Extensions;
using TestRunner = Microsoft.Diagnostics.CommonTestRunner.TestRunner;
// Newer SDKs flag MemberData(nameof(Configurations)) with this error
// Avoid unnecessary zero-length array allocations. Use Array.Empty<object>() instead.
#pragma warning disable CA1825
namespace Microsoft.Diagnostics.NETCore.Client
{
public class GetProcessInfoTests
{
private readonly ITestOutputHelper _output;
public static IEnumerable<object[]> Configurations => TestRunner.Configurations;
public GetProcessInfoTests(ITestOutputHelper outputHelper)
{
_output = outputHelper;
}
[SkippableTheory, MemberData(nameof(Configurations))]
public Task BasicProcessInfoNoSuspendTest(TestConfiguration config)
{
return BasicProcessInfoTestCore(config, useAsync: false, suspend: false);
}
[SkippableTheory, MemberData(nameof(Configurations))]
public Task BasicProcessInfoNoSuspendTestAsync(TestConfiguration config)
{
return BasicProcessInfoTestCore(config, useAsync: true, suspend: false);
}
[SkippableTheory, MemberData(nameof(Configurations))]
public Task BasicProcessInfoSuspendTest(TestConfiguration config)
{
return BasicProcessInfoTestCore(config, useAsync: false, suspend: true);
}
[SkippableTheory, MemberData(nameof(Configurations))]
public Task BasicProcessInfoSuspendTestAsync(TestConfiguration config)
{
return BasicProcessInfoTestCore(config, useAsync: true, suspend: true);
}
private async Task BasicProcessInfoTestCore(TestConfiguration config, bool useAsync, bool suspend)
{
if (config.RuntimeFrameworkVersionMajor < 5)
{
throw new SkipTestException("Not supported on < .NET 5.0");
}
await using TestRunner runner = await TestRunner.Create(config, _output, "Tracee");
if (suspend)
{
runner.SuspendDefaultDiagnosticPort();
}
await runner.Start(testProcessTimeout: 60_000, waitForTracee: !suspend);
try
{
DiagnosticsClientApiShim clientShim = new(new DiagnosticsClient(runner.Pid), useAsync);
// While suspended, the runtime will not provide entrypoint information.
ProcessInfo processInfoBeforeResume = null;
if (suspend)
{
// when the process is just starting up, the IPC channel may not be ready yet. We need to be prepared for the connection attempt to fail.
// If 100 retries over 10 seconds fail then we'll go ahead and fail the test.
const int retryCount = 100;
for (int i = 0; i < retryCount; i++)
{
try
{
processInfoBeforeResume = await clientShim.GetProcessInfo();
break;
}
catch (ServerNotAvailableException) when (i < retryCount-1)
{
_output.WriteLine($"Failed to connect to the IPC channel as the process is starting up. Attempt {i} of {retryCount}. Waiting 0.1 seconds, then retrying.");
await Task.Delay(100);
}
}
ValidateProcessInfo(runner.Pid, processInfoBeforeResume);
Assert.True((config.RuntimeFrameworkVersionMajor < 8) == string.IsNullOrEmpty(processInfoBeforeResume.ManagedEntrypointAssemblyName));
await clientShim.ResumeRuntime();
await runner.WaitForTracee();
}
// The entrypoint information is available some short time after the runtime
// begins to execute. Retry getting process information until entrypoint is available.
ProcessInfo processInfo = await GetProcessInfoWithEntrypointAsync(clientShim);
ValidateProcessInfo(runner.Pid, processInfo);
// This is only true if targetFramework for the tracee app is greater than
Assert.Equal("Tracee", processInfo.ManagedEntrypointAssemblyName);
if (suspend)
{
Assert.Equal(processInfoBeforeResume.ProcessId, processInfo.ProcessId);
Assert.Equal(processInfoBeforeResume.RuntimeInstanceCookie, processInfo.RuntimeInstanceCookie);
Assert.Equal(processInfoBeforeResume.OperatingSystem, processInfo.OperatingSystem);
Assert.Equal(processInfoBeforeResume.ProcessArchitecture, processInfo.ProcessArchitecture);
Assert.Equal(processInfoBeforeResume.ClrProductVersionString, processInfo.ClrProductVersionString);
// Given we are in a .NET 6.0+ app, we should have ProcessInfo2 available. Pre and post pause should differ.
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Assert.Equal($"\"{runner.ExePath}\" {runner.Arguments}", processInfoBeforeResume.CommandLine);
Assert.Equal($"\"{runner.ExePath}\" {runner.Arguments}", processInfo.CommandLine);
}
else
{
Assert.Equal($"{runner.ExePath}", processInfoBeforeResume.CommandLine);
Assert.Equal($"{runner.ExePath} {runner.ManagedArguments}", processInfo.CommandLine);
}
}
}
finally
{
runner.PrintStatus();
}
}
/// <summary>
/// Get process information with entrypoint information with exponential backoff on retries.
/// </summary>
private async Task<ProcessInfo> GetProcessInfoWithEntrypointAsync(DiagnosticsClientApiShim shim)
{
int retryMilliseconds = 5;
int currentAttempt = 1;
const int maxAttempts = 10;
_output.WriteLine("Getting process info with entrypoint:");
while (currentAttempt <= maxAttempts)
{
_output.WriteLine("- Attempt {0} of {1}.", currentAttempt, maxAttempts);
ProcessInfo processInfo = await shim.GetProcessInfo();
Assert.NotNull(processInfo);
if (!string.IsNullOrEmpty(processInfo.ManagedEntrypointAssemblyName))
{
_output.WriteLine("Got process info with entrypoint.");
return processInfo;
}
currentAttempt++;
if (currentAttempt != maxAttempts)
{
_output.WriteLine(" Waiting {0} ms.", retryMilliseconds);
await Task.Delay(retryMilliseconds);
retryMilliseconds = Math.Min(2 * retryMilliseconds, 500);
}
}
throw new InvalidOperationException("Unable to get process info with entrypoint.");
}
private static void ValidateProcessInfo(int expectedProcessId, ProcessInfo processInfo)
{
Assert.NotNull(processInfo);
Assert.Equal(expectedProcessId, (int)processInfo.ProcessId);
Assert.NotNull(processInfo.CommandLine);
Assert.NotNull(processInfo.OperatingSystem);
Assert.NotNull(processInfo.ProcessArchitecture);
Version clrVersion = ParseVersionRemoveLabel(processInfo.ClrProductVersionString);
Assert.True(clrVersion >= new Version(6, 0, 0));
}
private static Version ParseVersionRemoveLabel(string versionString)
{
Assert.NotNull(versionString);
int prereleaseLabelIndex = versionString.IndexOf('-');
if (prereleaseLabelIndex >= 0)
{
versionString = versionString.Substring(0, prereleaseLabelIndex);
}
return Version.Parse(versionString);
}
}
}