-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
ExitCodeTests.Unix.cs
67 lines (56 loc) · 2.49 KB
/
ExitCodeTests.Unix.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.DotNet.RemoteExecutor;
using Xunit;
namespace System.Tests
{
public class ExitCodeTests
{
private const int SIGTERM = 15;
[DllImport("libc", SetLastError = true)]
private static extern int kill(int pid, int sig);
[ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/31656", TestRuntimes.Mono)]
[InlineData(null)]
[InlineData(0)]
[InlineData(42)]
[PlatformSpecific(TestPlatforms.AnyUnix)] // SIGTERM signal.
public void SigTermExitCode(int? exitCodeOnSigterm)
{
Action<string> action = (string sigTermExitCode) =>
{
if (!string.IsNullOrEmpty(sigTermExitCode))
{
AppDomain.CurrentDomain.ProcessExit += (sender, args) =>
{
Assert.Same(AppDomain.CurrentDomain, sender);
Environment.ExitCode = int.Parse(sigTermExitCode);
};
}
Console.WriteLine("Application started");
// Wait for SIGTERM
System.Threading.Thread.Sleep(int.MaxValue);
};
RemoteInvokeOptions options = new RemoteInvokeOptions();
options.StartInfo.RedirectStandardOutput = true;
options.CheckExitCode = false;
using (RemoteInvokeHandle remoteExecution = RemoteExecutor.Invoke(action, exitCodeOnSigterm?.ToString() ?? string.Empty, options))
{
Process process = remoteExecution.Process;
// Wait for the process to start and register the ProcessExit handler
string processOutput = process.StandardOutput.ReadLine();
Assert.Equal("Application started", processOutput);
// Send SIGTERM
int rv = kill(process.Id, SIGTERM);
Assert.Equal(0, rv);
// Process exits in a timely manner
bool exited = process.WaitForExit(RemoteExecutor.FailWaitTimeoutMilliseconds);
Assert.True(exited);
// Check exit code
Assert.Equal(exitCodeOnSigterm ?? 128 + SIGTERM, process.ExitCode);
}
}
}
}