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:
9
// The above copyright notice and this permission notice shall be
10
// included in all copies or substantial portions of the Software.
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.
20
// Copyright (c) 2007-2008 Novell, Inc.
23
// Jackson Harper (jackson@ximian.com)
29
using System.Threading;
30
using System.Reflection;
31
using System.Diagnostics;
32
using System.Collections;
35
using org.freedesktop.DBus;
38
namespace MoonlightTests {
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.
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.
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);
55
void MarkTestAsCompleteAndGetNextTest (string test, bool successful, out bool next_available, out string next_test_path, out int timeout);
57
void RequestShutdown ();
60
public class TestRunner : ITestRunner, IDbusService {
61
private Driver driver;
63
private bool run_complete;
64
private IEnumerator tests_enumerator;
66
private string working_dir;
67
private string process_path;
68
private ExternalProcess agviewer_process;
70
private Test current_test;
72
public TestRunner (IList tests, string working_dir, Driver driver)
76
this.working_dir = working_dir;
78
tests_enumerator = tests.GetEnumerator ();
81
ObjectPath IDbusService.GetObjectPath ()
83
return new ObjectPath ("/mono/moonlight/tests/runner");
86
public void MarkTestAsCompleteAndGetNextTest (string test, bool successful, out bool next_available, out string next_test_path, out int timeout)
88
TestComplete (test, successful);
89
GetNextTest (out next_available, out next_test_path, out timeout);
92
public void GetNextTest (out bool available, out string test_path, out int timeout)
94
lock (tests_enumerator) {
95
if (!tests_enumerator.MoveNext ()) {
97
test_path = String.Empty;
103
current_test = (Test) tests_enumerator.Current;
104
OnBeginTest (current_test);
106
test_path = current_test.InputFile;
107
if (!current_test.Remote)
108
test_path = Path.GetFullPath (test_path);
109
timeout = current_test.Timeout;
116
// This will be called from the agviewer process when a test is completed
118
public void TestComplete (string test_path, bool successful)
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);
125
public void RequestShutdown ()
133
EnsureAgviewerProcess ();
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 ();
142
if (current_test != null)
143
OnTestComplete (current_test, TestCompleteReason.Crashed);
145
} while (!run_complete);
150
agviewer_process.Kill ();
153
private void OnBeginTest (Test test)
157
if (TestBeginEvent != null)
158
TestBeginEvent (test);
161
private void OnTestComplete (Test test, TestCompleteReason reason)
163
test.CompleteReason = reason;
164
if (TestCompleteEvent != null)
165
TestCompleteEvent (test, reason);
168
private void EnsureAgviewerProcess ()
170
if (agviewer_process != null && !agviewer_process.IsRunning) {
171
agviewer_process.Kill ();
172
agviewer_process = null;
175
if (agviewer_process == null) {
176
string args = String.Format ("-working-dir {0}", Path.GetFullPath (working_dir));
177
string valgrind_args;
180
if (driver != null && driver.UseValgrind) {
181
valgrind_args = string.Format ("{0} {1} {2}",
182
System.Environment.GetEnvironmentVariable ("MOONLIGHT_VALGRIND_OPTIONS"),
183
GetProcessPath (), args);
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);
190
agviewer_process = new ExternalProcess ("gdb", gdb_args, -1);
192
agviewer_process = new ExternalProcess (GetProcessPath (), args, -1);
195
agviewer_process.Run (false);
199
private string GetProcessPath ()
201
if (!File.Exists (process_path))
202
process_path = FindAgviewerRecursive (Directory.GetCurrentDirectory ());
203
if (process_path == null)
204
process_path = "agviewer";
208
private string FindAgviewerRecursive (string cd)
210
string ag = Path.Combine (cd, "agviewer");
211
if (File.Exists (ag))
214
foreach (string dir in Directory.GetDirectories (cd)) {
215
string res = FindAgviewerRecursive (dir);
223
public Driver Driver {
229
public TestCompleteEventHandler TestCompleteEvent;
230
public TestBeginEventHandler TestBeginEvent;