Skip to content

Commit

Permalink
Unit tests for manager
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubch1 committed Jun 15, 2020
1 parent a9eee9c commit 08b646a
Show file tree
Hide file tree
Showing 4 changed files with 513 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -14,11 +15,19 @@ namespace Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine
internal interface IMultiTestRunFinalizationManager
{
/// <summary>
/// Finalizes multi test run
/// Finalizes multi test run and provides results through handler
/// </summary>
/// <param name="attachments">Attachments</param>
/// <param name="eventHandler">EventHandler for handling multi test run finalization events from Engine</param>
/// <param name="cancellationToken">Cancellation token</param>
Task FinalizeMultiTestRunAsync(ICollection<AttachmentSet> attachments, IMultiTestRunFinalizationEventsHandler eventHandler, CancellationToken cancellationToken);

/// <summary>
/// Finalizes multi test
/// </summary>
/// <param name="attachments">Attachments</param>
/// <param name="eventHandler">EventHandler for handling multi test run finalization events from Engine</param>
/// <param name="cancellationToken">Cancellation token</param>
Task<Collection<AttachmentSet>> FinalizeMultiTestRunAsync(ICollection<AttachmentSet> attachments, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection
{
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Interfaces;
Expand Down Expand Up @@ -58,13 +59,13 @@ public override void HandleTestRunComplete(

if (parallelRunComplete)
{
finalizationManager.FinalizeMultiTestRunAsync(runDataAggregator.RunContextAttachments, null, cancellationToken).Wait();
Collection<AttachmentSet> attachments = finalizationManager.FinalizeMultiTestRunAsync(runDataAggregator.RunContextAttachments, cancellationToken).Result;

var completedArgs = new TestRunCompleteEventArgs(this.runDataAggregator.GetAggregatedRunStats(),
this.runDataAggregator.IsCanceled,
this.runDataAggregator.IsAborted,
this.runDataAggregator.GetAggregatedException(),
this.runDataAggregator.RunContextAttachments,
attachments,
this.runDataAggregator.ElapsedTime);

// Add Metrics from Test Host
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,25 @@ public MultiTestRunFinalizationManager(ITestPlatformEventSource testPlatformEven
this.dataCollectorAttachmentsHandlers = dataCollectorAttachmentsHandlers ?? throw new ArgumentNullException(nameof(dataCollectorAttachmentsHandlers));
}

/// <summary>
/// Finalizes multi test run
/// </summary>
/// <param name="attachments">Attachments</param>
/// <param name="eventHandler">EventHandler for handling multi test run finalization events from Engine</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <inheritdoc/>
public async Task FinalizeMultiTestRunAsync(ICollection<AttachmentSet> attachments, IMultiTestRunFinalizationEventsHandler eventHandler, CancellationToken cancellationToken)
{
await InternalFinalizeMultiTestRunAsync(new Collection<AttachmentSet>(attachments.ToList()), eventHandler, cancellationToken);
}

/// <inheritdoc/>
public Task<Collection<AttachmentSet>> FinalizeMultiTestRunAsync(ICollection<AttachmentSet> attachments, CancellationToken cancellationToken)
{
return InternalFinalizeMultiTestRunAsync(new Collection<AttachmentSet>(attachments.ToList()), null, cancellationToken);
}

private async Task<Collection<AttachmentSet>> InternalFinalizeMultiTestRunAsync(Collection<AttachmentSet> attachments, IMultiTestRunFinalizationEventsHandler eventHandler, CancellationToken cancellationToken)
{
try
{
cancellationToken.ThrowIfCancellationRequested();
testPlatformEventSource.MultiTestRunFinalizationStart(attachments?.Count ?? 0);

testPlatformEventSource.MultiTestRunFinalizationStart(attachments.Count);
cancellationToken.ThrowIfCancellationRequested();

var taskCompletionSource = new TaskCompletionSource<object>();
cancellationToken.Register(() =>
Expand All @@ -54,21 +60,26 @@ public async Task FinalizeMultiTestRunAsync(ICollection<AttachmentSet> attachmen

Task task = Task.Run(() =>
{
HandleAttachements(attachments, cancellationToken);
HandleAttachements(attachments, cancellationToken);
});

var completedTask = await Task.WhenAny(task, taskCompletionSource.Task);

if (completedTask == task)
{
await task;
eventHandler?.HandleMultiTestRunFinalizationComplete(attachments);
testPlatformEventSource.MultiTestRunFinalizationStop(attachments.Count);
return attachments;
}
else
{
eventHandler?.HandleLogMessage(ObjectModel.Logging.TestMessageLevel.Informational, "Finalization was cancelled.");
eventHandler?.HandleMultiTestRunFinalizationComplete(null);
testPlatformEventSource.MultiTestRunFinalizationStop(0);
testPlatformEventSource.MultiTestRunFinalizationStop(0);
}

return null;
}
catch (Exception e)
{
Expand All @@ -77,6 +88,7 @@ public async Task FinalizeMultiTestRunAsync(ICollection<AttachmentSet> attachmen
eventHandler?.HandleLogMessage(ObjectModel.Logging.TestMessageLevel.Error, e.Message);
eventHandler?.HandleMultiTestRunFinalizationComplete(null);
testPlatformEventSource.MultiTestRunFinalizationStop(0);
return null;
}
}

Expand Down
Loading

0 comments on commit 08b646a

Please sign in to comment.