ad828e895fa1536548fb3f8a7ee76f82d7db8517
[platform/upstream/coreclr.git] / tests / src / Common / Coreclr.TestWrapper / CoreclrTestWrapperLib.cs
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 //
4
5 using System;
6 using System.Collections.Generic;
7 using System.Diagnostics;
8 using System.IO;
9 using System.Text;
10 using System.Threading;
11 namespace CoreclrTestLib
12 {
13     public class CoreclrTestWrapperLib
14     {
15         public const int EXIT_SUCCESS_CODE = 0;
16
17         public int RunTest(string cmdLine, string outputfile, string errorfile)
18         {
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));
21
22             int exitCode = -100;
23             int timeout = 1000 * 60*3;
24             using (Process process = new Process())
25             {
26                 process.StartInfo.FileName = cmdLine;
27                 process.StartInfo.UseShellExecute = false;
28                 process.StartInfo.RedirectStandardOutput = true;
29                 process.StartInfo.RedirectStandardError = true;
30                                 
31
32                 StringBuilder output = new StringBuilder();
33                 StringBuilder error = new StringBuilder();
34
35                 using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
36                 using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
37                 {
38                     process.OutputDataReceived += (sender, e) =>
39                     {
40                         if (e.Data == null)
41                         {
42                             try
43                             {
44                                 outputWaitHandle.Set();
45                             }
46                             catch (ObjectDisposedException)
47                             {
48                                 // Noop for access after timeout.
49                             }
50                         }
51                         else
52                         {
53                             output.AppendLine(e.Data);
54                         }
55                     };
56                     process.ErrorDataReceived += (sender, e) =>
57                     {
58                         if (e.Data == null)
59                         {
60                             try
61                             {
62                                 errorWaitHandle.Set();
63                             }
64                             catch (ObjectDisposedException)
65                             {
66                                 // Noop for access after timeout.
67                             }
68                         }
69                         else
70                         {
71                             error.AppendLine(e.Data);
72                         }
73                     };
74
75                     process.Start();
76
77                     process.BeginOutputReadLine();
78                     process.BeginErrorReadLine();
79
80                     if (process.WaitForExit(timeout) &&
81                         outputWaitHandle.WaitOne(timeout) &&
82                         errorWaitHandle.WaitOne(timeout))
83                     {
84                         // Process completed. Check process.ExitCode here.
85                         exitCode = process.ExitCode;
86                     }
87                     else
88                     {
89                         // Timed out.
90                         output.AppendLine("cmdLine:" + cmdLine + " Timed Out");
91                         error.AppendLine("cmdLine:" + cmdLine + " Timed Out");
92                     }
93
94                    output_file.WriteLine(output.ToString());
95                    output_file.WriteLine("Test Harness Exitcode is : " + exitCode.ToString());
96                    output_file.Flush();
97
98                    err_file.WriteLine(error.ToString());
99                    err_file.Flush();
100
101                    output_file.Dispose();
102                    err_file.Dispose();
103                 }
104             }
105
106             return exitCode;
107         }
108
109         
110     }
111 }