Files
spark/src/client/spark_core/TestRunner/TestRunner.cs
T

121 lines
4.3 KiB
C#

using bts.api;
using bts.api.core;
using bts.api.test;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace bts.test {
public class TestRunner {
public TestRunner() { }
const int TIMEOUT = -1;
public int run_tests(IEnumerable<TestMethod> tests, uint timeout, bool self_contained) {
using (var trr = new TestResultReceiver(false)) {
var listening = trr.listen();
trr.print_begin();
foreach (var t in tests) {
int count = trr.ResultCount;
var res = run_one(t, timeout, self_contained);
if (res == TIMEOUT) {
trr.print_result(new TestResult() {
TestName = t.full_name,
Status = TestResult.Types.Step.Timeout
});
}
else {
// If we didn't time out and no results were reported we crashed or something
if (count == trr.ResultCount) {
trr.print_result(new TestResult() {
TestName = t.full_name,
Status = TestResult.Types.Step.Crash
});
}
}
}
trr.print_summary();
return trr.ErrorCount;
}
}
int run_one(TestMethod test, uint timeout, bool self_contained) {
TimeSpan time_out = timeout == 0u ? TimeSpan.FromSeconds(test.timeout) : TimeSpan.FromSeconds(timeout);
Process sparkd = new Process();
if(self_contained){
//if insular (selfcontained) start sparkd
sparkd.StartInfo.FileName = "sparkd";
sparkd.StartInfo.Arguments = test.config_path != null ? "run " + test.config_path : "run";
sparkd.Start();
}
string filename = Environment.GetCommandLineArgs().First();
string args = $"test {test.full_name}";
if(filename.Substring(filename.Length - 3) == "dll") {
args = filename + " " + args;
filename = "dotnet";
}
var psi = new ProcessStartInfo();
psi.FileName = filename;
psi.Arguments = args;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
try{
var p = Process.Start(psi);
p.WaitForExit((int)time_out.TotalMilliseconds);
var errorlevel = p.ExitCode;
string stdout = p.StandardOutput.ReadToEnd();
string stderr = p.StandardError.ReadToEnd();
if (p.HasExited) {
return 0;
}
while (!p.HasExited) {
p.Kill();
System.Threading.Thread.Sleep(100);
}
}
finally {
if(self_contained){
sparkd.Kill();
}
}
//ConsoleEx.WriteLine("Stdout: ", ConsoleColor.Magenta);
//ConsoleEx.WriteLine(p.StandardOutput.ReadToEnd(), ConsoleColor.Magenta);
//ConsoleEx.WriteLine("Stderr: ", ConsoleColor.Magenta);
//ConsoleEx.WriteLine(p.StandardError.ReadToEnd(), ConsoleColor.Magenta);
return TIMEOUT;
}
#region IPC
internal static (ServiceContext ctx, uint port) create_service_context(uint port = 0) {
// a hack to get a free port.
// Probably makes sense to get it from quickbus at some point
if (port == 0) {
TcpListener listener = new TcpListener(IPAddress.Loopback, 0);
listener.Start();
port = (uint)((IPEndPoint)listener.LocalEndpoint).Port;
listener.Stop();
}
var cfg = new config.QuickbusNodeCfg() { my_port = port };
var stream = new MemoryStream();
ProtoBuf.Serializer.Serialize(stream, cfg);
IntPtr qbus = ApiNativeMethods.api_create_quickbus(stream.ToArray(), stream.Length);
return (new ServiceContext(qbus), port);
}
#endregion
}
}