Skip to content

Commit

Permalink
fix: correctly await init task
Browse files Browse the repository at this point in the history
  • Loading branch information
vchirikov committed Oct 26, 2022
1 parent c1056d5 commit 5f8b296
Showing 1 changed file with 36 additions and 38 deletions.
74 changes: 36 additions & 38 deletions src/dotnet/Logger/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,55 +128,53 @@ private void OnTestResult(TestResult result)
if (_params.GH_VSTEST_DBG.asBool())
Console.WriteLine($"[GitHub.VsTest.Logger]: {nameof(OnTestResult)}(result:{result.DisplayName})");

if (_status != TestRunStatus.Started)
throw new($"Unexpected test run status: '{_status}'.");

// annotate only failed tests
if (result.Outcome != TestOutcome.Failed && result.Outcome != TestOutcome.NotFound)
if (result.Outcome == TestOutcome.Failed || result.Outcome == TestOutcome.NotFound)
{
if (!_testResults.TryAdd(result, Task.CompletedTask))
var task = Task.Run(async () => {
try
{
if (_status != TestRunStatus.Started)
await _initializationTask.ConfigureAwait(false);
await OnTestResultInternalAsync(result).ConfigureAwait(false);
}
catch (Exception ex)
{
_gh.Output.Error($"Exception: {ex.Message}");
using var _ = _gh.Output.Block("Exception info");
_gh.Output.Error(ex.ToString());
}
});

if (!_testResults.TryAdd(result, task))
_gh.Output.Error($"Dictionary already contains the testResult for {result.DisplayName}");
return;
}
var task = Task.Run(async () => {
try
{
await OnTestResultInternalAsync(result).ConfigureAwait(false);
}
catch (Exception ex)
{
_gh.Output.Error($"Exception: {ex.Message}");
using var _ = _gh.Output.Block("Exception info");
_gh.Output.Error(ex.ToString());
}
});

if (!_testResults.TryAdd(result, task))
_gh.Output.Error($"Dictionary already contains the testResult for {result.DisplayName}");

async Task OnTestResultInternalAsync(TestResult result)
{
if (_status != TestRunStatus.Started)
await _initializationTask.ConfigureAwait(false);

if (_annotationWriter == null)
throw new($"annotationWriter must not be null, test run status: {_status}");

var stackTraces = StackTraceParser.Parse(result.ErrorStackTrace, (f, t, m, pl, ps, fn, ln) => new
{
Frame = f,
Type = t,
Method = m,
ParameterList = pl,
Parameters = ps,
File = fn,
Line = ln,
});

var sb = new StringBuilder(1024);

if (!await _locker.WaitAsync(_timeout).ConfigureAwait(false))
throw new TimeoutException($"{nameof(OnTestResult)}: Waiting for the lock is too long");
try
{
if (_status != TestRunStatus.Started)
throw new($"Unexpected TestRun status: {_status}");

if (_annotationWriter == null)
throw new($"annotationWriter must not be null, test run status: {_status}");

var stackTraces = StackTraceParser.Parse(result.ErrorStackTrace, (f, t, m, pl, ps, fn, ln) => new
{
Frame = f,
Type = t,
Method = m,
ParameterList = pl,
Parameters = ps,
File = fn,
Line = ln,
});
var sb = new StringBuilder(1024);
foreach (var st in stackTraces)
{
if (!int.TryParse(st.Line, NumberStyles.Integer, CultureInfo.InvariantCulture, out int line))
Expand Down

0 comments on commit 5f8b296

Please sign in to comment.