Skip to content

Commit 19503bb

Browse files
authored
Rework launching real device (#211)
1 parent 5ee93a9 commit 19503bb

File tree

1 file changed

+54
-23
lines changed

1 file changed

+54
-23
lines changed

source/TestAdapter/Executor.cs

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,52 @@ private async Task<List<TestResult>> RunTestOnHardwareAsync(List<TestCase> tests
194194
List<TestResult> results = PrepareListResult(tests);
195195
List<byte[]> assemblies = new List<byte[]>();
196196
int retryCount = 0;
197+
NanoDeviceBase device = null;
197198

198-
var serialDebugClient = PortBase.CreateInstanceForSerial(true, 2000);
199+
bool realHardwarePortSet = !string.IsNullOrEmpty(_settings.RealHardwarePort);
199200

200-
retryConnection:
201+
PortBase serialDebugClient;
201202

202-
if (string.IsNullOrEmpty(_settings.RealHardwarePort))
203+
if (realHardwarePortSet)
203204
{
204-
_logger.LogMessage($"Waiting for device enumeration to complete.", Settings.LoggingLevel.Verbose);
205+
serialDebugClient = PortBase.CreateInstanceForSerial(false);
206+
207+
_logger.LogMessage($"Checking device on port {_settings.RealHardwarePort}.", Settings.LoggingLevel.Verbose);
208+
209+
try
210+
{
211+
serialDebugClient.AddDevice(_settings.RealHardwarePort);
212+
213+
device = serialDebugClient.NanoFrameworkDevices[0];
214+
215+
// all good here, proceed to execute tests
216+
goto executeTests;
217+
}
218+
#if DEBUG
219+
catch (Exception ex)
220+
#else
221+
catch
222+
#endif
223+
{
224+
results.First().Outcome = TestOutcome.Failed;
225+
results.First().ErrorMessage = $"Couldn't find any valid nanoDevice @ {_settings.RealHardwarePort}. Maybe try to disable the device watchers in Visual Studio Extension! If the situation persists reboot the device and/or disconnect and connect it again.";
226+
227+
_logger.LogMessage($"Couldn't find any valid nanoDevice @ {_settings.RealHardwarePort}.", Settings.LoggingLevel.Verbose);
228+
229+
return results;
230+
}
205231
}
206232
else
207233
{
208-
_logger.LogMessage($"Checking device on port {_settings.RealHardwarePort}.", Settings.LoggingLevel.Verbose);
234+
serialDebugClient = PortBase.CreateInstanceForSerial(true,
235+
2000);
236+
}
237+
238+
retryConnection:
239+
240+
if (string.IsNullOrEmpty(_settings.RealHardwarePort))
241+
{
242+
_logger.LogMessage($"Waiting for device enumeration to complete.", Settings.LoggingLevel.Verbose);
209243
}
210244

211245
while (!serialDebugClient.IsDevicesEnumerationComplete)
@@ -220,40 +254,34 @@ private async Task<List<TestResult>> RunTestOnHardwareAsync(List<TestCase> tests
220254
if (retryCount > _numberOfRetries)
221255
{
222256
results.First().Outcome = TestOutcome.Failed;
223-
results.First().ErrorMessage = $"Couldn't find any device, please try to disable the device scanning in the Visual Studio Extension! If the situation persists reboot the device as well.";
257+
results.First().ErrorMessage = "Couldn't find any valid nanoDevice. Maybe try to disable the device watchers in Visual Studio Extension! If the situation persists reboot the device and/or disconnect and connect it again.";
258+
259+
_logger.LogMessage("Couldn't find any valid nanoDevice.", Settings.LoggingLevel.Verbose);
260+
224261
return results;
225262
}
226263
else
227264
{
265+
// add retry counter before trying again
228266
retryCount++;
267+
268+
// re-scan devices
229269
serialDebugClient.ReScanDevices();
270+
230271
goto retryConnection;
231272
}
232273
}
233274

234275
retryCount = 0;
235-
NanoDeviceBase device;
236-
237-
if (serialDebugClient.NanoFrameworkDevices.Count > 1
238-
&& !string.IsNullOrEmpty(_settings.RealHardwarePort))
239-
{
240-
// get the device at the requested COM port (if there is one)
241-
device = serialDebugClient.NanoFrameworkDevices.FirstOrDefault(m => m.SerialNumber == _settings.RealHardwarePort);
242276

243-
// sanity check
244-
if (device is null)
245-
{
246-
// no device, done here
247-
_logger.LogMessage($"No device available at {_settings.RealHardwarePort}.", Settings.LoggingLevel.Verbose);
248-
return results;
249-
}
250-
}
251-
else
277+
if (serialDebugClient.NanoFrameworkDevices.Count > 1)
252278
{
253-
// no COM port requested, just grab the 1st one
279+
// grab the 1st device available
254280
device = serialDebugClient.NanoFrameworkDevices[0];
255281
}
256282

283+
executeTests:
284+
257285
_logger.LogMessage(
258286
$"Getting things ready with {device.Description}",
259287
Settings.LoggingLevel.Detailed);
@@ -290,6 +318,7 @@ private async Task<List<TestResult>> RunTestOnHardwareAsync(List<TestCase> tests
290318
}
291319

292320
retryCount = 0;
321+
293322
retryErase:
294323
// erase the device
295324
_logger.LogMessage($"Erase deployment block storage. Attempt {retryCount}/{_numberOfRetries}.", Settings.LoggingLevel.Verbose);
@@ -300,6 +329,7 @@ private async Task<List<TestResult>> RunTestOnHardwareAsync(List<TestCase> tests
300329
null);
301330

302331
_logger.LogMessage($"Erase result is {eraseResult}.", Settings.LoggingLevel.Verbose);
332+
303333
if (!eraseResult)
304334
{
305335
if (retryCount < _numberOfRetries)
@@ -318,6 +348,7 @@ private async Task<List<TestResult>> RunTestOnHardwareAsync(List<TestCase> tests
318348
}
319349

320350
retryCount = 0;
351+
321352
// initial check
322353
if (device.DebugEngine.IsDeviceInInitializeState())
323354
{

0 commit comments

Comments
 (0)