1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
6 using System.Collections.Generic;
7 using System.Diagnostics;
10 using System.Threading;
11 namespace CoreclrTestLib
13 public class CoreclrTestWrapperLib
15 public const int EXIT_SUCCESS_CODE = 0;
17 public int RunTest(string cmdLine, string outputfile, string errorfile)
19 System.IO.TextWriter output_file = new System.IO.StreamWriter(new FileStream(outputfile, FileMode.Create));
20 System.IO.TextWriter err_file = new System.IO.StreamWriter(new FileStream(errorfile, FileMode.Create));
23 int timeout = 1000 * 60*10;
24 using (Process process = new Process())
26 process.StartInfo.FileName = cmdLine;
27 process.StartInfo.UseShellExecute = false;
28 process.StartInfo.RedirectStandardOutput = true;
29 process.StartInfo.RedirectStandardError = true;
32 StringBuilder output = new StringBuilder();
33 StringBuilder error = new StringBuilder();
35 using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
36 using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
38 process.OutputDataReceived += (sender, e) =>
44 outputWaitHandle.Set();
46 catch (ObjectDisposedException)
48 // Noop for access after timeout.
53 output.AppendLine(e.Data);
56 process.ErrorDataReceived += (sender, e) =>
62 errorWaitHandle.Set();
64 catch (ObjectDisposedException)
66 // Noop for access after timeout.
71 error.AppendLine(e.Data);
77 process.BeginOutputReadLine();
78 process.BeginErrorReadLine();
80 if (process.WaitForExit(timeout) &&
81 outputWaitHandle.WaitOne(timeout) &&
82 errorWaitHandle.WaitOne(timeout))
84 // Process completed. Check process.ExitCode here.
85 exitCode = process.ExitCode;
90 output.AppendLine("cmdLine:" + cmdLine + " Timed Out");
91 error.AppendLine("cmdLine:" + cmdLine + " Timed Out");
94 output_file.WriteLine(output.ToString());
95 output_file.WriteLine("Test Harness Exitcode is : " + exitCode.ToString());
98 err_file.WriteLine(error.ToString());
101 output_file.Dispose();