Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / process-stress-3.cs
1
2 using System;
3 using System.Diagnostics;
4 using System.Text;
5 using System.Threading;
6 using System.Threading.Tasks;
7
8 class Driver
9 {
10         static void Main ()
11         {
12                 Action<Process>[] tests = new Action<Process> [] {
13                         new Action<Process> (Test1),
14                         new Action<Process> (Test2),
15                 };
16
17                 ProcessStartInfo psi = new ProcessStartInfo () {
18                         FileName = "find",
19                         Arguments = "../.. -maxdepth 3", // this test should be run from mono/tests, so that will list all files in the repo
20                         UseShellExecute = false,
21                         RedirectStandardOutput = true,
22                         RedirectStandardError = true,
23                 };
24
25                 foreach (Action<Process> test in tests) {
26                         for (int i = 0; i < 200; ++i) {
27                                 test (new Process () { StartInfo = psi });
28                         }
29                 }
30         }
31
32         static void Test1 (Process p)
33         {
34                 ManualResetEvent mre_exit = new ManualResetEvent (false);
35                 ManualResetEvent mre_output = new ManualResetEvent (false);
36                 ManualResetEvent mre_error = new ManualResetEvent (false);
37
38                 p.EnableRaisingEvents = true;
39                 p.Exited += (s, a) => mre_exit.Set ();
40
41                 p.Start ();
42
43                 p.OutputDataReceived += (s, a) => {
44                         if (a.Data == null) {
45                                 mre_output.Set ();
46                                 return;
47                         }
48                 };
49
50                 p.ErrorDataReceived += (s, a) => {
51                         if (a.Data == null) {
52                                 mre_error.Set ();
53                                 return;
54                         }
55                 };
56
57                 p.BeginOutputReadLine ();
58                 p.BeginErrorReadLine ();
59
60                 if (!mre_exit.WaitOne (10000))
61                         Environment.Exit (1);
62                 if (!mre_output.WaitOne (1000))
63                         Environment.Exit (2);
64                 if (!mre_error.WaitOne (1000))
65                         Environment.Exit (3);
66         }
67
68         static void Test2 (Process p)
69         {
70                 ManualResetEvent mre_output = new ManualResetEvent (false);
71                 ManualResetEvent mre_error = new ManualResetEvent (false);
72
73                 p.Start ();
74
75                 p.OutputDataReceived += (s, a) => {
76                         if (a.Data == null) {
77                                 mre_output.Set ();
78                                 return;
79                         }
80                 };
81
82                 p.ErrorDataReceived += (s, a) => {
83                         if (a.Data == null) {
84                                 mre_error.Set ();
85                                 return;
86                         }
87                 };
88
89                 p.BeginOutputReadLine ();
90                 p.BeginErrorReadLine ();
91
92                 if (!p.WaitForExit (10000))
93                         Environment.Exit (4);
94                 if (!mre_output.WaitOne (1000))
95                         Environment.Exit (5);
96                 if (!mre_error.WaitOne (1000))
97                         Environment.Exit (6);
98         }
99 }