~ubuntu-branches/ubuntu/karmic/moon/karmic

« back to all changes in this revision

Viewing changes to test/harness/test-runner/TestRunner.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-14 12:01:08 UTC
  • Revision ID: james.westby@ubuntu.com-20090214120108-06539vb25vhbd8bn
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Permission is hereby granted, free of charge, to any person obtaining
 
2
// a copy of this software and associated documentation files (the
 
3
// "Software"), to deal in the Software without restriction, including
 
4
// without limitation the rights to use, copy, modify, merge, publish,
 
5
// distribute, sublicense, and/or sell copies of the Software, and to
 
6
// permit persons to whom the Software is furnished to do so, subject to
 
7
// the following conditions:
 
8
//
 
9
// The above copyright notice and this permission notice shall be
 
10
// included in all copies or substantial portions of the Software.
 
11
//
 
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
13
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
14
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
15
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
16
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
17
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
18
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
19
//
 
20
// Copyright (c) 2007-2008 Novell, Inc.
 
21
//
 
22
// Authors:
 
23
//      Jackson Harper (jackson@ximian.com)
 
24
//
 
25
 
 
26
 
 
27
using System;
 
28
using System.IO;
 
29
using System.Threading;
 
30
using System.Reflection;
 
31
using System.Diagnostics;
 
32
using System.Collections;
 
33
 
 
34
using NDesk.DBus;
 
35
using org.freedesktop.DBus;
 
36
 
 
37
 
 
38
namespace MoonlightTests {
 
39
 
 
40
        //
 
41
        // Feed the server a list of tests and it will execute them all, raising
 
42
        // the test completed event each time a test completes.
 
43
        //
 
44
 
 
45
        // The test enumerator is moved from two places.  The Start method will enumerator through tests but
 
46
        // the actual agviewer process will also enumerate through tests by calling the GetNextTest method.
 
47
        // The only time that tests will be enumerated through Start's loop is during the first test, or if
 
48
        // agviewer crashes/is killed.
 
49
 
 
50
        [Interface ("mono.moonlight.tests.runner.ITestRunner")]
 
51
        public interface ITestRunner {
 
52
                void GetNextTest (out bool available, out string test_path, out int timeout);
 
53
                void TestComplete (string test, bool successful);
 
54
 
 
55
                void MarkTestAsCompleteAndGetNextTest (string test, bool successful, out bool next_available, out string next_test_path, out int timeout);
 
56
 
 
57
                void RequestShutdown ();
 
58
        }
 
59
 
 
60
        public class TestRunner : ITestRunner, IDbusService {
 
61
                private Driver driver;
 
62
                private IList tests;
 
63
                private bool run_complete;
 
64
                private IEnumerator tests_enumerator;
 
65
 
 
66
                private string working_dir;
 
67
                private string process_path;
 
68
                private ExternalProcess agviewer_process;
 
69
 
 
70
                private Test current_test;
 
71
 
 
72
                public TestRunner (IList tests, string working_dir, Driver driver)
 
73
                {
 
74
                        this.driver = driver;
 
75
                        this.tests = tests;
 
76
                        this.working_dir = working_dir;
 
77
 
 
78
                        tests_enumerator = tests.GetEnumerator ();
 
79
                }
 
80
 
 
81
                ObjectPath IDbusService.GetObjectPath ()
 
82
                {
 
83
                        return new ObjectPath ("/mono/moonlight/tests/runner");
 
84
                }
 
85
 
 
86
                public void MarkTestAsCompleteAndGetNextTest (string test, bool successful, out bool next_available, out string next_test_path, out int timeout)
 
87
                {
 
88
                        TestComplete (test, successful);
 
89
                        GetNextTest (out next_available, out next_test_path, out timeout);
 
90
                }
 
91
 
 
92
                public void GetNextTest (out bool available, out string test_path, out int timeout)
 
93
                {
 
94
                        lock (tests_enumerator) {
 
95
                                if (!tests_enumerator.MoveNext ()) {
 
96
                                        run_complete = true;
 
97
                                        test_path = String.Empty;
 
98
                                        timeout = -1;
 
99
                                        available = false;
 
100
                                        return;
 
101
                                }
 
102
 
 
103
                                current_test = (Test) tests_enumerator.Current;
 
104
                                OnBeginTest (current_test);
 
105
                                
 
106
                                test_path = current_test.InputFile;
 
107
                                if (!current_test.Remote)
 
108
                                        test_path = Path.GetFullPath (test_path);
 
109
                                timeout = current_test.Timeout;
 
110
 
 
111
                                available = true;
 
112
                        }
 
113
                }
 
114
 
 
115
                //
 
116
                // This will be called from the agviewer process when a test is completed
 
117
                //
 
118
                public void TestComplete (string test_path, bool successful)
 
119
                {
 
120
                        if (test_path != Path.GetFileName (current_test.InputFile))
 
121
                                Console.WriteLine ("Test complete path does not match current test path.  ({0} vs {1})", test_path, current_test.InputFile);
 
122
                        OnTestComplete (current_test, successful ? TestCompleteReason.Finished : TestCompleteReason.Timedout);
 
123
                }
 
124
 
 
125
                public void RequestShutdown ()
 
126
                {
 
127
                        run_complete = true;
 
128
                }
 
129
 
 
130
                public void Start ()
 
131
                {
 
132
                         do {
 
133
                                 EnsureAgviewerProcess ();
 
134
 
 
135
                                // If agviewer process dies and the tests aren't finished 
 
136
                                // mark the current test as a failure (since it somehow crashed the viewer)
 
137
                                // and move to the next test in the queue.
 
138
                                if (agviewer_process.ExitedEvent.WaitOne ()) {
 
139
                                        agviewer_process.Kill ();
 
140
                                        if (run_complete)
 
141
                                                break;
 
142
                                        if (current_test != null)
 
143
                                                OnTestComplete (current_test, TestCompleteReason.Crashed);
 
144
                                }
 
145
                         } while (!run_complete);
 
146
                }
 
147
 
 
148
                public void Stop ()
 
149
                {
 
150
                        agviewer_process.Kill ();
 
151
                }
 
152
 
 
153
                private void OnBeginTest (Test test)
 
154
                {
 
155
                        test.Setup ();
 
156
 
 
157
                        if (TestBeginEvent != null)
 
158
                                TestBeginEvent (test);
 
159
                }
 
160
 
 
161
                private void OnTestComplete (Test test, TestCompleteReason reason)
 
162
                {
 
163
                        test.CompleteReason = reason;
 
164
                        if (TestCompleteEvent != null)
 
165
                                TestCompleteEvent (test, reason);
 
166
                }
 
167
 
 
168
                private void EnsureAgviewerProcess ()
 
169
                {
 
170
                        if (agviewer_process != null && !agviewer_process.IsRunning) {
 
171
                                agviewer_process.Kill ();
 
172
                                agviewer_process = null;
 
173
                        }
 
174
 
 
175
                        if (agviewer_process == null) {
 
176
                                string args = String.Format ("-working-dir {0}", Path.GetFullPath (working_dir));
 
177
                                string valgrind_args;
 
178
                                string gdb_args;
 
179
                                
 
180
                                if (driver != null && driver.UseValgrind) {
 
181
                                        valgrind_args = string.Format ("{0} {1} {2}", 
 
182
                                                                       System.Environment.GetEnvironmentVariable ("MOONLIGHT_VALGRIND_OPTIONS"), 
 
183
                                                                       GetProcessPath (), args);
 
184
                                        
 
185
                                        agviewer_process = new ExternalProcess ("valgrind", valgrind_args, -1);
 
186
                                } else if (driver != null && driver.UseGdb) {
 
187
                                        gdb_args = string.Format ("--batch --eval-command=run --args {0} {1}", 
 
188
                                                                       GetProcessPath (), args);
 
189
                                        
 
190
                                        agviewer_process = new ExternalProcess ("gdb", gdb_args, -1);
 
191
                                } else {
 
192
                                        agviewer_process = new ExternalProcess (GetProcessPath (), args, -1);
 
193
                                }
 
194
 
 
195
                                agviewer_process.Run (false);
 
196
                        }
 
197
                }
 
198
 
 
199
                private string GetProcessPath ()
 
200
                {
 
201
                        if (!File.Exists (process_path))
 
202
                                process_path = FindAgviewerRecursive (Directory.GetCurrentDirectory ());
 
203
                        if (process_path == null)
 
204
                                process_path = "agviewer";
 
205
                        return process_path;
 
206
                }
 
207
 
 
208
                private string FindAgviewerRecursive (string cd)
 
209
                {
 
210
                        string ag = Path.Combine (cd, "agviewer");
 
211
                        if (File.Exists (ag))
 
212
                                return ag;
 
213
 
 
214
                        foreach (string dir in Directory.GetDirectories (cd)) {
 
215
                                string res = FindAgviewerRecursive (dir);
 
216
                                if (res != null)
 
217
                                        return res;
 
218
                        }
 
219
 
 
220
                        return null;
 
221
                }
 
222
 
 
223
                public Driver Driver {
 
224
                        get {
 
225
                                return driver;
 
226
                        }
 
227
                }
 
228
                
 
229
                public TestCompleteEventHandler TestCompleteEvent;
 
230
                public TestBeginEventHandler TestBeginEvent;
 
231
        }
 
232
}
 
233