-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessHandling.cs
More file actions
81 lines (76 loc) · 2.99 KB
/
ProcessHandling.cs
File metadata and controls
81 lines (76 loc) · 2.99 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
using Perfy.Testing;
using System.Diagnostics;
namespace Perfy.ProcessHandling
{
class ProcessHandler
{
public ProcessHandler(string cmd, int timeout = 10000)
{
string[] wordList = cmd.Split(' ');
ApplicationName = wordList[0];
Arguments = String.Join(' ', wordList.Skip(1));
Timeout = timeout;
}
public string ApplicationName { get; private set; }
public string Arguments { get; private set; }
public int Timeout { get; private set; }
public TestResult HandleTestCase(TestCase Test)
{
//
Process process = new()
{
StartInfo = new ProcessStartInfo
{
FileName = ApplicationName,
Arguments = Arguments.Replace(":inputs", String.Join(' ', Test.Inputs)),
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
},
EnableRaisingEvents = true,
};
TaskCompletionSource<bool> waitingTaskSrc = new();
List<string> outputs = [];
List<string> errors = [];
process.Exited += (sender, args) => waitingTaskSrc.SetResult(true);
process.OutputDataReceived += (sender, args) => { if (args.Data != null) outputs.Add(args.Data); };
process.ErrorDataReceived += (sender, args) => { if (args.Data != null) errors.Add(args.Data); };
process.Start();
process.BeginOutputReadLine(); process.BeginErrorReadLine();
Task RaceResult = Task.WhenAny(waitingTaskSrc.Task, Task.Delay(Timeout)).GetAwaiter().GetResult();
TestResult result;
if (RaceResult == waitingTaskSrc.Task)
{
process.WaitForExit();
bool passed = true;
if (outputs.Count != Test.Outputs.Length)
{
passed = false;
}
else
for (int i = 0; i < outputs.Count; i++)
{
if (outputs[i] != Test.Outputs[i])
{
passed = false;
break;
}
}
if (process.ExitCode != 0)
{
errors.Add($"Perfy: test case returned exit code {process.ExitCode}");
}
result = new TestResult(Test, passed ? 1 : 0, (long)(process.ExitTime - process.StartTime).TotalMilliseconds, String.Join('\n', errors), [.. outputs]);
}
else
{
process.Kill();
errors.Add($"Perfy: timeout of {Timeout}ms exceeded\n");
result = new TestResult(Test, 0, Timeout, String.Join('\n', errors), [.. outputs]);
}
process.Dispose();
return result;
}
}
}