diff --git a/src/Microsoft.DotNet.XHarness.CLI/iOS/iOSTestCommand.cs b/src/Microsoft.DotNet.XHarness.CLI/iOS/iOSTestCommand.cs index 4559d7dd5..6accbe601 100644 --- a/src/Microsoft.DotNet.XHarness.CLI/iOS/iOSTestCommand.cs +++ b/src/Microsoft.DotNet.XHarness.CLI/iOS/iOSTestCommand.cs @@ -53,13 +53,15 @@ protected override async Task InvokeInternal() var simulatorLoader = new SimulatorLoader(processManager); var logs = new Logs(_arguments.OutputDirectory); - var cancellationToken = new CancellationToken(); // TODO: Get cancellation from command line env? Set timeout through it? + + var cts = new CancellationTokenSource(); + cts.CancelAfter(_arguments.Timeout); var exitCode = ExitCode.SUCCESS; foreach (TestTarget target in _arguments.TestTargets) { - var exitCodeForRun = await RunTest(target, logs, processManager, deviceLoader, simulatorLoader, cancellationToken); + var exitCodeForRun = await RunTest(target, logs, processManager, deviceLoader, simulatorLoader, cts.Token); if (exitCodeForRun != ExitCode.SUCCESS) { @@ -156,7 +158,8 @@ private async Task RunTest(TestTarget target, _arguments.LaunchTimeout, deviceName, verbosity: verbosity, - xmlResultJargon: XmlResultJargon.xUnit); + xmlResultJargon: XmlResultJargon.xUnit, + cancellationToken: cancellationToken); if (exitCode != 0) { diff --git a/src/Microsoft.DotNet.XHarness.iOS.Shared/Execution/IProcessManager.cs b/src/Microsoft.DotNet.XHarness.iOS.Shared/Execution/IProcessManager.cs index a7a4ad323..72db39c72 100644 --- a/src/Microsoft.DotNet.XHarness.iOS.Shared/Execution/IProcessManager.cs +++ b/src/Microsoft.DotNet.XHarness.iOS.Shared/Execution/IProcessManager.cs @@ -27,13 +27,13 @@ public interface IProcessManager string MlaunchPath { get; } Version XcodeVersion { get; } - Task ExecuteCommandAsync(string filename, IList args, ILog log, TimeSpan timeout, Dictionary environment_variables = null, CancellationToken? cancellationToken = null); - Task ExecuteCommandAsync(string filename, IList args, ILog log, ILog stdoutLog, ILog stderrLog, TimeSpan timeout, Dictionary environment_variables = null, CancellationToken? cancellationToken = null); - Task ExecuteCommandAsync(MlaunchArguments args, ILog log, TimeSpan timeout, Dictionary environment_variables = null, CancellationToken? cancellation_token = null); + Task ExecuteCommandAsync(string filename, IList args, ILog log, TimeSpan timeout, Dictionary environmentVariables = null, CancellationToken? cancellationToken = null); + Task ExecuteCommandAsync(string filename, IList args, ILog log, ILog stdoutLog, ILog stderrLog, TimeSpan timeout, Dictionary environmentVariables = null, CancellationToken? cancellationToken = null); + Task ExecuteCommandAsync(MlaunchArguments args, ILog log, TimeSpan timeout, Dictionary environmentVariables = null, CancellationToken? cancellationToken = null); Task ExecuteXcodeCommandAsync(string executable, IList args, ILog log, TimeSpan timeout); - Task RunAsync(Process process, ILog log, TimeSpan? timeout = null, Dictionary environment_variables = null, CancellationToken? cancellation_token = null, bool? diagnostics = null); - Task RunAsync(Process process, MlaunchArguments args, ILog log, TimeSpan? timeout = null, Dictionary environment_variables = null, CancellationToken? cancellation_token = null, bool? diagnostics = null); - Task RunAsync(Process process, ILog log, ILog stdoutLog, ILog stderrLog, TimeSpan? timeout = null, Dictionary environment_variables = null, CancellationToken? cancellation_token = null, bool? diagnostics = null); + Task RunAsync(Process process, ILog log, TimeSpan? timeout = null, Dictionary environmentVariables = null, CancellationToken? cancellationToken = null, bool? diagnostics = null); + Task RunAsync(Process process, MlaunchArguments args, ILog log, TimeSpan? timeout = null, Dictionary environmentVariables = null, CancellationToken? cancellationToken = null, bool? diagnostics = null); + Task RunAsync(Process process, ILog log, ILog stdoutLog, ILog stderrLog, TimeSpan? timeout = null, Dictionary environmentVariables = null, CancellationToken? cancellationToken = null, bool? diagnostics = null); Task KillTreeAsync(Process process, ILog log, bool? diagnostics = true); Task KillTreeAsync(int pid, ILog log, bool? diagnostics = true); } diff --git a/src/Microsoft.DotNet.XHarness.iOS.Shared/Execution/ProcessManager.cs b/src/Microsoft.DotNet.XHarness.iOS.Shared/Execution/ProcessManager.cs index 77f2c2dae..05ea160e7 100644 --- a/src/Microsoft.DotNet.XHarness.iOS.Shared/Execution/ProcessManager.cs +++ b/src/Microsoft.DotNet.XHarness.iOS.Shared/Execution/ProcessManager.cs @@ -94,11 +94,11 @@ public Task ExecuteXcodeCommandAsync(string executable, public Task RunAsync(Process process, ILog log, TimeSpan? timeout = null, - Dictionary environment_variables = null, + Dictionary environmentVariables = null, CancellationToken? cancellationToken = null, bool? diagnostics = null) { - return RunAsync(process, log, log, log, timeout, environment_variables, cancellationToken, diagnostics); + return RunAsync(process, log, log, log, timeout, environmentVariables, cancellationToken, diagnostics); } public Task RunAsync(Process process, diff --git a/src/Microsoft.DotNet.XHarness.iOS/AppInstaller.cs b/src/Microsoft.DotNet.XHarness.iOS/AppInstaller.cs index 154185307..5c6bfa004 100644 --- a/src/Microsoft.DotNet.XHarness.iOS/AppInstaller.cs +++ b/src/Microsoft.DotNet.XHarness.iOS/AppInstaller.cs @@ -76,7 +76,7 @@ public AppInstaller(IProcessManager processManager, IHardwareDeviceLoader device var totalSize = Directory.GetFiles(appPath, "*", SearchOption.AllDirectories).Select((v) => new FileInfo(v).Length).Sum(); _mainLog.WriteLine($"Installing '{appPath}' to '{deviceName}' ({totalSize / 1024.0 / 1024.0:N2} MB)"); - ProcessExecutionResult result = await _processManager.ExecuteCommandAsync(args, _mainLog, TimeSpan.FromHours(1), cancellation_token: cancellationToken); + ProcessExecutionResult result = await _processManager.ExecuteCommandAsync(args, _mainLog, TimeSpan.FromHours(1), cancellationToken: cancellationToken); return (deviceName, result); } diff --git a/src/Microsoft.DotNet.XHarness.iOS/AppRunner.cs b/src/Microsoft.DotNet.XHarness.iOS/AppRunner.cs index eeb4a1fa2..c9dee1a1c 100644 --- a/src/Microsoft.DotNet.XHarness.iOS/AppRunner.cs +++ b/src/Microsoft.DotNet.XHarness.iOS/AppRunner.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Threading; using System.Threading.Tasks; using Microsoft.DotNet.XHarness.iOS.Shared; using Microsoft.DotNet.XHarness.iOS.Shared.Execution; @@ -62,7 +63,8 @@ public AppRunner(IProcessManager processManager, bool ensureCleanSimulatorState = false, string variation = null, int verbosity = 1, - XmlResultJargon xmlResultJargon = XmlResultJargon.xUnit) + XmlResultJargon xmlResultJargon = XmlResultJargon.xUnit, + CancellationToken cancellationToken = default) { var args = new MlaunchArguments { @@ -169,6 +171,8 @@ public AppRunner(IProcessManager processManager, null, (level, message) => _mainLog.WriteLine(message)); + using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(testReporter.CancellationToken, cancellationToken); + listener.ConnectedTask .TimeoutAfter(testLaunchTimeout) .ContinueWith(testReporter.LaunchCallback) @@ -246,7 +250,7 @@ public AppRunner(IProcessManager processManager, _mainLog.WriteLine("Starting test run"); - var result = _processManager.ExecuteCommandAsync(args, _mainLog, timeout, cancellation_token: testReporter.CancellationToken); + var result = _processManager.ExecuteCommandAsync(args, _mainLog, timeout, cancellationToken: linkedCts.Token); await testReporter.CollectSimulatorResult(result); @@ -296,6 +300,8 @@ public AppRunner(IProcessManager processManager, null, (level, message) => _mainLog.WriteLine(message)); + using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(testReporter.CancellationToken, cancellationToken); + listener.ConnectedTask .TimeoutAfter(testLaunchTimeout) .ContinueWith(testReporter.LaunchCallback) @@ -330,7 +336,7 @@ public AppRunner(IProcessManager processManager, args, aggregatedLog, timeout, - cancellation_token: testReporter.CancellationToken); + cancellationToken: linkedCts.Token); await testReporter.CollectDeviceResult(runTestTask); } @@ -347,7 +353,8 @@ public AppRunner(IProcessManager processManager, } } - listener.Cancel(); + // TODO: https://github.com/dotnet/xharness/issues/73 + // listener.Cancel(); listener.Dispose(); // check the final status, copy all the required data diff --git a/src/Microsoft.DotNet.XHarness.iOS/AppUninstaller.cs b/src/Microsoft.DotNet.XHarness.iOS/AppUninstaller.cs index fa667efd3..3e02ee936 100644 --- a/src/Microsoft.DotNet.XHarness.iOS/AppUninstaller.cs +++ b/src/Microsoft.DotNet.XHarness.iOS/AppUninstaller.cs @@ -32,7 +32,7 @@ public async Task UninstallApp(string deviceName, string args.Add(new UninstallAppFromDeviceArgument(appBundleId)); args.Add(new DeviceNameArgument(deviceName)); - return await _processManager.ExecuteCommandAsync(args, _mainLog, TimeSpan.FromMinutes(1), cancellation_token: cancellationToken); + return await _processManager.ExecuteCommandAsync(args, _mainLog, TimeSpan.FromMinutes(1), cancellationToken: cancellationToken); } } }