Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / process-stress-2.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 = "echo",
19                         Arguments = "hello",
20                         UseShellExecute = false,
21                         RedirectStandardOutput = true,
22                 };
23
24                 foreach (Action<Process> test in tests) {
25                         for (int i = 0; i < 500; ++i) {
26                                 test (new Process () { StartInfo = psi });
27                         }
28                 }
29         }
30
31         static void Test1 (Process p)
32         {
33                 StringBuilder sb = new StringBuilder ();
34                 ManualResetEvent mre_exit = new ManualResetEvent (false);
35                 ManualResetEvent mre_output = new ManualResetEvent (false);
36
37                 p.EnableRaisingEvents = true;
38                 p.Exited += (s, a) => mre_exit.Set ();
39
40                 p.Start ();
41
42                 p.OutputDataReceived += (s, a) => {
43                         if (a.Data == null) {
44                                 mre_output.Set ();
45                                 return;
46                         }
47                         sb.Append (a.Data);
48                 };
49
50                 p.BeginOutputReadLine ();
51
52                 if (!mre_exit.WaitOne (1000))
53                         Environment.Exit (1);
54                 if (!mre_output.WaitOne (1000))
55                         Environment.Exit (2);
56
57                 if (sb.ToString () != "hello") {
58                         Console.WriteLine ("process output = '{0}'", sb.ToString ());
59                         Environment.Exit (3);
60                 }
61         }
62
63         static void Test2 (Process p)
64         {
65                 StringBuilder sb = new StringBuilder ();
66                 ManualResetEvent mre_output = new ManualResetEvent (false);
67
68                 p.Start ();
69
70                 p.OutputDataReceived += (s, a) => {
71                         if (a.Data == null) {
72                                 mre_output.Set ();
73                                 return;
74                         }
75
76                         sb.Append (a.Data);
77                 };
78
79                 p.BeginOutputReadLine ();
80
81                 if (!p.WaitForExit (1000))
82                         Environment.Exit (4);
83                 if (!mre_output.WaitOne (1000))
84                         Environment.Exit (5);
85
86                 if (sb.ToString () != "hello") {
87                         Console.WriteLine ("process output = '{0}'", sb.ToString ());
88                         Environment.Exit (6);
89                 }
90         }
91 }