Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[xharness] Do not mark DeviceLoaders as loaded when loading fails #9367

Merged
merged 3 commits into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,53 @@ public async Task LoadAsyncProcessSuccess (bool extraData)
Assert.AreEqual (0, devices.ConnectedTV.Count ());
}

private void AssertArgumentValue (MlaunchArgument arg, string expected, string message = null)
[Test]
public async Task FindAndCacheDevicesWithFailingMlaunchTest ()
{
var value = arg.AsCommandLineArgument ().Split (new char [] { '=' }, 2).LastOrDefault ();
Assert.AreEqual (expected, value, message);
string processPath = null;
MlaunchArguments passedArguments = null;

// Moq.SetupSequence doesn't allow custom callbacks so we need to count ourselves
var calls = 0;

// moq It.Is is not working as nicelly as we would like it, we capture data and use asserts
processManager.Setup (p => p.RunAsync (It.IsAny<Process> (), It.IsAny<MlaunchArguments> (), It.IsAny<ILog> (), It.IsAny<TimeSpan?> (), It.IsAny<Dictionary<string, string>> (), It.IsAny<CancellationToken?> (), It.IsAny<bool?> ()))
.Returns<Process, MlaunchArguments, ILog, TimeSpan?, Dictionary<string, string>, CancellationToken?, bool?> ((p, args, log, t, env, token, d) => {
calls++;

if (calls == 1) {
// Mlaunch can sometimes time out and we are testing that a subsequent Load will trigger it again
return Task.FromResult (new ProcessExecutionResult { ExitCode = 137, TimedOut = true });
}

processPath = p.StartInfo.FileName;
passedArguments = args;

// we get the temp file that was passed as the args, and write our sample xml, which will be parsed to get the devices :)
var tempPath = args.Where (a => a is ListDevicesArgument).First ().AsCommandLineArgument ();
tempPath = tempPath.Substring (tempPath.IndexOf ('=') + 1).Replace ("\"", string.Empty);

var name = GetType ().Assembly.GetManifestResourceNames ().Where (a => a.EndsWith ("devices.xml", StringComparison.Ordinal)).FirstOrDefault ();
using (var outputStream = new StreamWriter (tempPath))
using (var sampleStream = new StreamReader (GetType ().Assembly.GetManifestResourceStream (name))) {
string line;
while ((line = sampleStream.ReadLine ()) != null)
outputStream.WriteLine (line);
}
return Task.FromResult (new ProcessExecutionResult { ExitCode = 0, TimedOut = false });
});

Assert.ThrowsAsync<Exception> (async () => await devices.LoadDevices (executionLog.Object));

Assert.IsEmpty (devices.ConnectedDevices);
Assert.AreEqual (1, calls);
await devices.LoadDevices (executionLog.Object);
Assert.AreEqual (2, calls);
Assert.IsNotEmpty (devices.ConnectedDevices);
await devices.LoadDevices (executionLog.Object);
Assert.AreEqual (2, calls);
await devices.LoadDevices (executionLog.Object);
Assert.AreEqual (2, calls);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,41 @@ public async Task FindAsyncDoNotCreateTest (TestTarget target, int expected)
Assert.AreEqual (expected, sims.Count (), $"{target} simulators count");
}

private void AssertArgumentValue (MlaunchArgument arg, string expected, string message = null)
[Test]
public async Task FindAndCacheSimulatorsWithFailingMlaunchTest ()
{
var value = arg.AsCommandLineArgument ().Split (new char [] { '=' }, 2).LastOrDefault ();
Assert.AreEqual (expected, value, message);
// Moq.SetupSequence doesn't allow custom callbacks so we need to count ourselves
var calls = 0;

processManager
.Setup (p => p.RunAsync (It.IsAny<Process>(), It.Is<MlaunchArguments> (args => args.Any (a => a is ListSimulatorsArgument)), It.IsAny<ILog> (), It.IsAny<TimeSpan> (), It.IsAny<Dictionary<string, string>> (), It.IsAny<CancellationToken?> (), It.IsAny<bool?> ()))
.Returns<Process, MlaunchArguments, ILog, TimeSpan, Dictionary<string, string>, CancellationToken?, bool?> ((process, args, log, t, env, token, diagnostics) => {
calls++;

if (calls == 1) {
// Mlaunch can sometimes time out and we are testing that a subsequent Load will trigger it again
return Task.FromResult (new ProcessExecutionResult { ExitCode = 137, TimedOut = true });
}

// We get the temp file that was passed as the args, and write our sample xml, which will be parsed to get the devices
var tempPath = args.Where (a => a is ListSimulatorsArgument).First ().AsCommandLineArgument ();
tempPath = tempPath.Substring (tempPath.IndexOf ('=') + 1).Replace ("\"", string.Empty);

CopySampleData (tempPath);
return Task.FromResult (new ProcessExecutionResult { ExitCode = 0, TimedOut = false });
});

Assert.ThrowsAsync<Exception> (async () => await simulators.LoadDevices (executionLog.Object));

Assert.IsEmpty (simulators.AvailableDevices);
Assert.AreEqual (1, calls);
await simulators.LoadDevices (executionLog.Object);
Assert.AreEqual (2, calls);
Assert.IsNotEmpty (simulators.AvailableDevices);
await simulators.LoadDevices (executionLog.Object);
Assert.AreEqual (2, calls);
await simulators.LoadDevices (executionLog.Object);
Assert.AreEqual (2, calls);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public interface IHardwareDeviceLoader : IDeviceLoader {
}

public class HardwareDeviceLoader : IHardwareDeviceLoader {
readonly SemaphoreSlim semaphore = new SemaphoreSlim (1);
readonly IProcessManager processManager;
bool loaded;

Expand All @@ -46,14 +47,16 @@ public HardwareDeviceLoader (IProcessManager processManager)

public async Task LoadDevices (ILog log, bool includeLocked = false, bool forceRefresh = false, bool listExtraData = false)
{
await semaphore.WaitAsync ();

if (loaded) {
if (!forceRefresh)
if (!forceRefresh) {
semaphore.Release ();
return;
}
connectedDevices.Reset ();
}

loaded = true;

var tmpfile = Path.GetTempFileName ();
try {
using (var process = new Process ()) {
Expand Down Expand Up @@ -92,11 +95,14 @@ public async Task LoadDevices (ILog log, bool includeLocked = false, bool forceR
}
connectedDevices.Add (d);
}

loaded = true;
}
} finally {
connectedDevices.SetCompleted ();
File.Delete (tmpfile);
log.Flush ();
semaphore.Release ();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public async Task LoadDevices (ILog log, bool includeLocked = false, bool forceR
available_devices.Reset ();
available_device_pairs.Reset ();
}
loaded = true;

await Task.Run (async () => {
var tmpfile = Path.GetTempFileName ();
Expand Down Expand Up @@ -115,6 +114,8 @@ await Task.Run (async () => {
});
}
}

loaded = true;
} finally {
supported_runtimes.SetCompleted ();
supported_device_types.SetCompleted ();
Expand Down