summaryrefslogtreecommitdiff
path: root/tests/src/Common/Coreclr.TestWrapper/CoreclrTestWrapperLib.cs
blob: ad828e895fa1536548fb3f8a7ee76f82d7db8517 (plain)
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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
namespace CoreclrTestLib
{
    public class CoreclrTestWrapperLib
    {
        public const int EXIT_SUCCESS_CODE = 0;

        public int RunTest(string cmdLine, string outputfile, string errorfile)
        {
            System.IO.TextWriter output_file = new System.IO.StreamWriter(new FileStream(outputfile, FileMode.Create));
            System.IO.TextWriter err_file = new System.IO.StreamWriter(new FileStream(errorfile, FileMode.Create));

            int exitCode = -100;
            int timeout = 1000 * 60*3;
            using (Process process = new Process())
            {
                process.StartInfo.FileName = cmdLine;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                                

                StringBuilder output = new StringBuilder();
                StringBuilder error = new StringBuilder();

                using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
                using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
                {
                    process.OutputDataReceived += (sender, e) =>
                    {
                        if (e.Data == null)
                        {
                            try
                            {
                                outputWaitHandle.Set();
                            }
                            catch (ObjectDisposedException)
                            {
                                // Noop for access after timeout.
                            }
                        }
                        else
                        {
                            output.AppendLine(e.Data);
                        }
                    };
                    process.ErrorDataReceived += (sender, e) =>
                    {
                        if (e.Data == null)
                        {
                            try
                            {
                                errorWaitHandle.Set();
                            }
                            catch (ObjectDisposedException)
                            {
                                // Noop for access after timeout.
                            }
                        }
                        else
                        {
                            error.AppendLine(e.Data);
                        }
                    };

                    process.Start();

                    process.BeginOutputReadLine();
                    process.BeginErrorReadLine();

                    if (process.WaitForExit(timeout) &&
                        outputWaitHandle.WaitOne(timeout) &&
                        errorWaitHandle.WaitOne(timeout))
                    {
                        // Process completed. Check process.ExitCode here.
                        exitCode = process.ExitCode;
                    }
                    else
                    {
                        // Timed out.
                        output.AppendLine("cmdLine:" + cmdLine + " Timed Out");
                        error.AppendLine("cmdLine:" + cmdLine + " Timed Out");
                    }

                   output_file.WriteLine(output.ToString());
                   output_file.WriteLine("Test Harness Exitcode is : " + exitCode.ToString());
                   output_file.Flush();

                   err_file.WriteLine(error.ToString());
                   err_file.Flush();

                   output_file.Dispose();
                   err_file.Dispose();
                }
            }

            return exitCode;
        }

        
    }
}