Skip to content

Commit 71ca270

Browse files
authored
Fix test execution timeout (#221)
***NO_CI***
1 parent b54003d commit 71ca270

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

source/TestAdapter/Executor.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Executor : ITestExecutor
4848
private const int _timeoutMiliseconds = 1000;
4949

5050
/// test session timeout (from the runsettings file)
51-
private int _testSessionTimeout = 300000;
51+
private int _testSessionTimeout = 30_0000;
5252

5353
private IFrameworkHandle _frameworkHandle = null;
5454

@@ -552,27 +552,36 @@ await Task.Run(async delegate
552552
}
553553

554554
StringBuilder output = new StringBuilder();
555-
bool isFinished = false;
555+
ManualResetEvent testExecutionCompleted = new ManualResetEvent(false);
556+
556557
// attach listener for messages
557558
device.DebugEngine.OnMessage += (message, text) =>
558559
{
559560
_logger.LogMessage(text, Settings.LoggingLevel.Verbose);
560561
output.Append(text);
561562
if (text.Contains(Done))
562563
{
563-
isFinished = true;
564+
// signal test execution completed
565+
testExecutionCompleted.Set();
564566
}
565567
};
566568

567569
device.DebugEngine.RebootDevice(RebootOptions.ClrOnly);
568570

569-
while (!isFinished)
571+
DateTime timeoutForExecution = DateTime.UtcNow.AddMilliseconds(_testSessionTimeout);
572+
573+
if (testExecutionCompleted.WaitOne(_testSessionTimeout))
570574
{
571-
Thread.Sleep(1);
572-
}
575+
_logger.LogMessage($"Tests finished.", Settings.LoggingLevel.Verbose);
573576

574-
_logger.LogMessage($"Tests finished.", Settings.LoggingLevel.Verbose);
575-
ParseTestResults(output.ToString(), results);
577+
ParseTestResults(output.ToString(), results);
578+
}
579+
else
580+
{
581+
_logger.LogMessage($"Tests timed out.", Settings.LoggingLevel.Error);
582+
results.First().Outcome = TestOutcome.Failed;
583+
results.First().ErrorMessage = $"Tests timed out in {device.Description}";
584+
}
576585
}
577586
else
578587
{
@@ -673,8 +682,8 @@ private async Task<List<TestResult>> RunTestOnEmulatorAsync(
673682
.WithArguments(arguments.ToString())
674683
.WithValidation(CommandResultValidation.None);
675684

676-
// setup cancellation token with a timeout of 5 seconds
677-
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)))
685+
// setup cancellation token with the timeout from settings
686+
using (var cts = new CancellationTokenSource(_testSessionTimeout))
678687
{
679688
var cliResult = await cmd.ExecuteBufferedAsync(cts.Token);
680689
var exitCode = cliResult.ExitCode;

0 commit comments

Comments
 (0)