Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions tests/xharness/AppRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class AppRunner
readonly ISimpleListenerFactory listenerFactory;
readonly IDeviceLoaderFactory devicesLoaderFactory;
readonly ICaptureLogFactory captureLogFactory;
readonly IDeviceLogCapturerFactory deviceLogCapturerFactory;
readonly RunMode mode;
readonly bool isSimulator;
readonly AppRunnerTarget target;
Expand Down Expand Up @@ -94,6 +95,7 @@ public AppRunner (IProcessManager processManager,
ISimpleListenerFactory simpleListenerFactory,
IDeviceLoaderFactory devicesFactory,
ICaptureLogFactory captureLogFactory,
IDeviceLogCapturerFactory deviceLogCapturerFactory,
AppRunnerTarget target,
IHarness harness,
ILog mainLog,
Expand All @@ -113,6 +115,7 @@ public AppRunner (IProcessManager processManager,
this.listenerFactory = simpleListenerFactory ?? throw new ArgumentNullException (nameof (simpleListenerFactory));
this.devicesLoaderFactory = devicesFactory ?? throw new ArgumentNullException (nameof (devicesFactory));
this.captureLogFactory = captureLogFactory ?? throw new ArgumentNullException (nameof (captureLogFactory));
this.deviceLogCapturerFactory = deviceLogCapturerFactory ?? throw new ArgumentNullException (nameof (deviceLogCapturerFactory));
this.harness = harness ?? throw new ArgumentNullException (nameof (harness));
this.MainLog = mainLog ?? throw new ArgumentNullException (nameof (mainLog));
this.projectFilePath = projectFilePath ?? throw new ArgumentNullException (nameof (projectFilePath));
Expand Down Expand Up @@ -421,7 +424,7 @@ public bool TestsSucceeded (AppInformation appInfo, string test_log_path, bool t
public async Task<int> RunAsync ()
{
CrashReportSnapshot crash_reports;
ILog device_system_log = null;
ILog deviceSystemLog = null;
ILog listener_log = null;
ILog run_log = MainLog;

Expand Down Expand Up @@ -648,13 +651,9 @@ public async Task<int> RunAsync ()

AddDeviceName (args);

device_system_log = Logs.Create ($"device-{deviceName}-{Helpers.Timestamp}.log", "Device log");
var logdev = new DeviceLogCapturer () {
Harness = harness,
Log = device_system_log,
DeviceName = deviceName,
};
logdev.StartCapture ();
deviceSystemLog = Logs.Create ($"device-{deviceName}-{Helpers.Timestamp}.log", "Device log");
var deviceLogCapturer = deviceLogCapturerFactory.Create (harness.HarnessLog,deviceSystemLog, deviceName);
deviceLogCapturer.StartCapture ();

try {
await crash_reports.StartCaptureAsync ();
Expand Down Expand Up @@ -693,14 +692,14 @@ public async Task<int> RunAsync ()
success = false;
}
} finally {
logdev.StopCapture ();
device_system_log.Dispose ();
deviceLogCapturer.StopCapture ();
deviceSystemLog.Dispose ();
}

// Upload the system log
if (File.Exists (device_system_log.FullPath)) {
MainLog.WriteLine ("A capture of the device log is: {0}", device_system_log.FullPath);
WrenchLog.WriteLine ("AddFile: {0}", device_system_log.FullPath);
if (File.Exists (deviceSystemLog.FullPath)) {
MainLog.WriteLine ("A capture of the device log is: {0}", deviceSystemLog.FullPath);
WrenchLog.WriteLine ("AddFile: {0}", deviceSystemLog.FullPath);
}
}

Expand Down
68 changes: 54 additions & 14 deletions tests/xharness/DeviceLogCapturer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,56 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using Xharness.Execution;
using Xharness.Logging;
using Xharness.Utilities;

namespace Xharness
{
public class DeviceLogCapturer
{
public IHarness Harness;
public ILog Log;
public string DeviceName;
public interface IDeviceLogCapturerFactory {
IDeviceLogCapturer Create (ILog mainLog, ILog deviceLog, string deviceName);
}

public class DeviceLogCapturerFactory : IDeviceLogCapturerFactory {
readonly IProcessManager processManager;
readonly string xcodeRoot;
readonly string mlaunchPath;

public DeviceLogCapturerFactory (IProcessManager processManager, string xcodeRoot, string mlaunchPath)
{
this.processManager = processManager ?? throw new ArgumentNullException (nameof (processManager));
this.xcodeRoot = xcodeRoot ?? throw new ArgumentNullException (nameof (xcodeRoot));
this.mlaunchPath = mlaunchPath ?? throw new ArgumentNullException (nameof (mlaunchPath));
}

public IDeviceLogCapturer Create (ILog mainLog, ILog deviceLog, string deviceName)
{
return new DeviceLogCapturer (processManager, mainLog, deviceLog, deviceName, xcodeRoot, mlaunchPath);
}
}

public interface IDeviceLogCapturer {
void StartCapture ();
void StopCapture ();
}

public class DeviceLogCapturer : IDeviceLogCapturer {
readonly IProcessManager processManager;
readonly ILog mainLog;
readonly ILog deviceLog;
readonly string deviceName;
readonly string xcodeRoot;
readonly string mlaunchPath;

public DeviceLogCapturer (IProcessManager processManager, ILog mainLog, ILog deviceLog, string deviceName, string xcodeRoot, string mlaunchPath)
{
this.processManager = processManager ?? throw new ArgumentNullException (nameof (processManager));
this.mainLog = mainLog ?? throw new ArgumentNullException (nameof (mainLog));
this.deviceLog = deviceLog ?? throw new ArgumentNullException (nameof (deviceLog));
this.deviceName = deviceName ?? throw new ArgumentNullException (nameof (deviceName));
this.xcodeRoot = xcodeRoot ?? throw new ArgumentNullException (nameof (xcodeRoot));
this.mlaunchPath = mlaunchPath ?? throw new ArgumentNullException (nameof (mlaunchPath));
}

Process process;
CountdownEvent streamEnds;
Expand All @@ -21,12 +61,12 @@ public void StartCapture ()
streamEnds = new CountdownEvent (2);

process = new Process ();
process.StartInfo.FileName = Harness.MlaunchPath;
process.StartInfo.FileName = mlaunchPath;
var sb = new List<string> ();
sb.Add ("--logdev");
sb.Add ("--sdkroot");
sb.Add (Harness.XcodeRoot);
AppRunner.AddDeviceName (sb, DeviceName);
sb.Add (xcodeRoot);
AppRunner.AddDeviceName (sb, deviceName);
process.StartInfo.Arguments = StringUtils.FormatArguments (sb);
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
Expand All @@ -36,21 +76,21 @@ public void StartCapture ()
if (e.Data == null) {
streamEnds.Signal ();
} else {
lock (Log) {
Log.WriteLine (e.Data);
lock (deviceLog) {
deviceLog.WriteLine (e.Data);
}
}
};
process.ErrorDataReceived += (object sender, DataReceivedEventArgs e) => {
if (e.Data == null) {
streamEnds.Signal ();
} else {
lock (Log) {
Log.WriteLine (e.Data);
lock (deviceLog) {
deviceLog.WriteLine (e.Data);
}
}
};
Log.WriteLine ("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments);
deviceLog.WriteLine ("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments);
process.Start ();
process.BeginOutputReadLine ();
process.BeginErrorReadLine ();
Expand All @@ -65,7 +105,7 @@ public void StopCapture ()
if (process.WaitForExit ((int) TimeSpan.FromSeconds (5).TotalMilliseconds))
return;

Harness.ProcessManager.KillTreeAsync (process, Harness.HarnessLog, diagnostics: false).Wait ();
processManager.KillTreeAsync (process, mainLog, diagnostics: false).Wait ();
process.Dispose ();
}
}
Expand Down
3 changes: 3 additions & 0 deletions tests/xharness/Harness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ int Install ()
new SimpleListenerFactory (),
new DeviceLoaderFactory (this, ProcessManager),
new CaptureLogFactory (),
new DeviceLogCapturerFactory (ProcessManager, XcodeRoot, MlaunchPath),
target,
this,
HarnessLog,
Expand All @@ -626,6 +627,7 @@ int Uninstall ()
new SimpleListenerFactory (),
new DeviceLoaderFactory (this, ProcessManager),
new CaptureLogFactory (),
new DeviceLogCapturerFactory (ProcessManager, XcodeRoot, MlaunchPath),
target,
this,
HarnessLog,
Expand All @@ -650,6 +652,7 @@ int Run ()
new SimpleListenerFactory (),
new DeviceLoaderFactory (this, ProcessManager),
new CaptureLogFactory (),
new DeviceLogCapturerFactory (ProcessManager, XcodeRoot, MlaunchPath),
target,
this,
HarnessLog,
Expand Down
2 changes: 2 additions & 0 deletions tests/xharness/Jenkins/TestTasks/RunDeviceTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ protected override async Task RunTestAsync ()
new SimpleListenerFactory (),
new DeviceLoaderFactory (Harness, processManager),
new CaptureLogFactory (),
new DeviceLogCapturerFactory (processManager, Harness.XcodeRoot, Harness.MlaunchPath),
AppRunnerTarget,
Harness,
projectFilePath: ProjectFile,
Expand Down Expand Up @@ -148,6 +149,7 @@ protected override async Task RunTestAsync ()
new SimpleListenerFactory (),
new DeviceLoaderFactory (Harness, processManager),
new CaptureLogFactory (),
new DeviceLogCapturerFactory (processManager, Harness.XcodeRoot, Harness.MlaunchPath),
AppRunnerTarget,
Harness,
projectFilePath: ProjectFile,
Expand Down
1 change: 1 addition & 0 deletions tests/xharness/Jenkins/TestTasks/RunSimulatorTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public async Task SelectSimulatorAsync ()
new SimpleListenerFactory (),
new DeviceLoaderFactory (Harness, processManager),
new CaptureLogFactory (),
new DeviceLogCapturerFactory (processManager, Harness.XcodeRoot, Harness.MlaunchPath),
AppRunnerTarget,
Harness,
mainLog: Logs.Create ($"run-{Device.UDID}-{Timestamp}.log", "Run log"),
Expand Down
6 changes: 6 additions & 0 deletions tests/xharness/Xharness.Tests/Tests/AppRunnerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public void InitializeTest ()
listenerFactory,
devicesFactory,
Mock.Of<ICaptureLogFactory> (),
Mock.Of<IDeviceLogCapturerFactory> (),
AppRunnerTarget.Simulator_iOS64,
new Mock<IHarness> ().Object,
new Mock<ILog>().Object,
Expand All @@ -113,6 +114,7 @@ public void InstallToSimulatorTest ()
listenerFactory,
devicesFactory,
Mock.Of<ICaptureLogFactory> (),
Mock.Of<IDeviceLogCapturerFactory> (),
AppRunnerTarget.Simulator_iOS64,
new Mock<IHarness> ().Object,
new Mock<ILog>().Object,
Expand All @@ -133,6 +135,7 @@ public void UninstallToSimulatorTest ()
listenerFactory,
devicesFactory,
Mock.Of<ICaptureLogFactory> (),
Mock.Of<IDeviceLogCapturerFactory> (),
AppRunnerTarget.Simulator_iOS64,
new Mock<IHarness> ().Object,
new Mock<ILog>().Object,
Expand All @@ -153,6 +156,7 @@ public void InstallWhenNoDevicesTest ()
listenerFactory,
devicesFactory,
Mock.Of<ICaptureLogFactory> (),
Mock.Of<IDeviceLogCapturerFactory> (),
AppRunnerTarget.Device_iOS,
new Mock<IHarness> ().Object,
new Mock<ILog>().Object,
Expand Down Expand Up @@ -192,6 +196,7 @@ public async Task InstallOnDeviceTest ()
listenerFactory,
devicesFactory,
Mock.Of<ICaptureLogFactory> (),
Mock.Of<IDeviceLogCapturerFactory> (),
AppRunnerTarget.Device_iOS,
harnessMock.Object,
mainLog,
Expand Down Expand Up @@ -250,6 +255,7 @@ public async Task UninstallFromDeviceTest ()
listenerFactory,
devicesFactory,
Mock.Of<ICaptureLogFactory> (),
Mock.Of<IDeviceLogCapturerFactory> (),
AppRunnerTarget.Device_iOS,
harnessMock.Object,
mainLog,
Expand Down