Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubch1 committed Jun 16, 2020
1 parent e16e3ad commit cbca05a
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public ParallelRunDataAggregator()

public TimeSpan ElapsedTime { get; set; }

public Collection<AttachmentSet> RunContextAttachments { get; }
public Collection<AttachmentSet> RunContextAttachments { get; set; }

public List<AttachmentSet> RunCompleteArgsAttachments { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ namespace Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection
using System.Threading;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Interfaces;
using Microsoft.VisualStudio.TestPlatform.CoreUtilities.Tracing;
using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.Parallel;
using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.MultiTestRunFinalization;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine;
using Microsoft.VisualStudio.TestPlatform.Utilities;

internal class ParallelDataCollectionEventsHandler : ParallelRunEventsHandler
{
Expand Down Expand Up @@ -59,13 +56,13 @@ public override void HandleTestRunComplete(

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

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

// Add Metrics from Test Host
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,6 @@ private string MergeCodeCoverageFiles(IList<string> files, CancellationToken can
}
throw;
}
catch (ObjectDisposedException)
{
if (EqtTrace.IsWarningEnabled)
{
EqtTrace.Warning("CodeCoverageDataCollectorAttachmentsHandler: object disposed.");
}
throw;
}
catch (Exception ex)
{
if (EqtTrace.IsErrorEnabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,6 @@ public Task FinalizeMultiTestRunAsync(ICollection<AttachmentSet> attachments, IM
return requestSender.FinalizeMultiTestRunAsync(attachments, testSessionEventsHandler, cancellationToken);
}

/// <inheritdoc/>
public void CancelMultiTestRunFinalization()
{
throw new System.NotImplementedException();
}

#endregion

private void EnsureInitialized()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace TestPlatform.CrossPlatEngine.UnitTests.Client
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client;
using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.Parallel;
using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection;
using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection.Interfaces;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
Expand Down Expand Up @@ -171,6 +172,32 @@ public void HandlePartialRunCompleteShouldCreateNewProxyExecutionManagerIfDataCo
Assert.IsTrue(this.proxyManagerFuncCalled);
}

[TestMethod]
public void HandlePartialRunCompleteShouldCreateNewProxyExecutionManagerIfDataCollectionEnabledAndCreatorWithDataCollection()
{
var completeArgs = new TestRunCompleteEventArgs(null, true, true, null, null, TimeSpan.Zero);
this.mockTestHostManager = new Mock<ITestRuntimeProvider>();
this.mockRequestSender = new Mock<ITestRequestSender>();
this.mockDataCollectionManager = new Mock<IProxyDataCollectionManager>();
var proxyDataCollectionManager = new ProxyExecutionManagerWithDataCollection(this.mockRequestData.Object, this.mockRequestSender.Object, this.mockTestHostManager.Object, this.mockDataCollectionManager.Object);
var managers = new List<Mock<ProxyExecutionManagerWithDataCollection>>();
this.proxyManagerFunc = () =>
{
this.proxyManagerFuncCalled = true;
var manager = new Mock<ProxyExecutionManagerWithDataCollection>(this.mockRequestData.Object, this.mockRequestSender.Object, this.mockTestHostManager.Object, this.mockDataCollectionManager.Object);
managers.Add(manager);
return manager.Object;
};
var parallelExecutionManager = this.SetupExecutionManager(this.proxyManagerFunc, 2, setupTestCases: true);

this.proxyManagerFuncCalled = false;
parallelExecutionManager.HandlePartialRunComplete(proxyDataCollectionManager, completeArgs, null, null, null);
Assert.IsTrue(this.proxyManagerFuncCalled);

var handler = parallelExecutionManager.GetHandlerForGivenManager(managers.Last().Object);
Assert.IsTrue(handler is ParallelDataCollectionEventsHandler);
}

[TestMethod]
public void HandlePartialRunCompleteShouldCreateNewProxyExecutionManagerIfIsAbortedIsTrue()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.TestPlatform.CrossPlatEngine.UnitTests.DataCollection
{
using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.Parallel;
using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;

[TestClass]
public class ParallelDataCollectionEventsHandlerTests
{
private const string uri1 = "datacollector://microsoft/some1/1.0";
private const string uri2 = "datacollector://microsoft/some2/2.0";
private const string uri3 = "datacollector://microsoft/some3/2.0";

private readonly Mock<IRequestData> mockRequestData;
private readonly Mock<IProxyExecutionManager> mockProxyExecutionManager;
private readonly Mock<ITestRunEventsHandler> mockTestRunEventsHandler;
private readonly Mock<IParallelProxyExecutionManager> mockParallelProxyExecutionManager;
private readonly Mock<IMultiTestRunFinalizationManager> mockMultiTestRunFinalizationManager;
private readonly CancellationTokenSource cancellationTokenSource;
private readonly ParallelDataCollectionEventsHandler parallelDataCollectionEventsHandler;

public ParallelDataCollectionEventsHandlerTests()
{
mockRequestData = new Mock<IRequestData>();
mockProxyExecutionManager = new Mock<IProxyExecutionManager>();
mockTestRunEventsHandler = new Mock<ITestRunEventsHandler>();
mockParallelProxyExecutionManager = new Mock<IParallelProxyExecutionManager>();
mockMultiTestRunFinalizationManager = new Mock<IMultiTestRunFinalizationManager>();
cancellationTokenSource = new CancellationTokenSource();
parallelDataCollectionEventsHandler = new ParallelDataCollectionEventsHandler(mockRequestData.Object, mockProxyExecutionManager.Object, mockTestRunEventsHandler.Object,
mockParallelProxyExecutionManager.Object, new ParallelRunDataAggregator(), mockMultiTestRunFinalizationManager.Object, cancellationTokenSource.Token);

mockParallelProxyExecutionManager.Setup(m => m.HandlePartialRunComplete(It.IsAny<IProxyExecutionManager>(), It.IsAny<TestRunCompleteEventArgs>(), It.IsAny<TestRunChangedEventArgs>(), It.IsAny<ICollection<AttachmentSet>>(), It.IsAny<ICollection<string>>())).Returns(true);
}

[TestMethod]
public void HandleTestRunComplete_ShouldCallFinalizerWithAttachmentsAndUseResults()
{
// arrange
List<AttachmentSet> inputAttachments = new List<AttachmentSet>
{
new AttachmentSet(new Uri(uri1), "uri1_input1"),
new AttachmentSet(new Uri(uri2), "uri2_input1"),
new AttachmentSet(new Uri(uri3), "uri3_input1")
};

Collection<AttachmentSet> outputAttachments = new Collection<AttachmentSet>
{
new AttachmentSet(new Uri(uri1), "uri1_input1")
};

mockMultiTestRunFinalizationManager.Setup(f => f.FinalizeMultiTestRunAsync(It.IsAny<ICollection<AttachmentSet>>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(outputAttachments));

// act
parallelDataCollectionEventsHandler.HandleTestRunComplete(new TestRunCompleteEventArgs(null, false, false, null, null, TimeSpan.FromSeconds(1)), null, inputAttachments, null);

// assert
mockTestRunEventsHandler.Verify(h => h.HandleTestRunComplete(It.IsAny<TestRunCompleteEventArgs>(), It.IsAny<TestRunChangedEventArgs>(), It.Is<ICollection<AttachmentSet>>(c => c.Count == 1 && c.Contains(outputAttachments[0])), It.IsAny<ICollection<string>>()));
mockMultiTestRunFinalizationManager.Verify(f => f.FinalizeMultiTestRunAsync(It.Is<ICollection<AttachmentSet>>(a => a.Count == 3), cancellationTokenSource.Token));
}

[TestMethod]
public void HandleTestRunComplete_ShouldCallFinalizerWithAttachmentsAndNotUserResults_IfFinalizerReturnsNull()
{
// arrange
List<AttachmentSet> inputAttachments = new List<AttachmentSet>
{
new AttachmentSet(new Uri(uri1), "uri1_input1"),
new AttachmentSet(new Uri(uri2), "uri2_input1"),
new AttachmentSet(new Uri(uri3), "uri3_input1")
};

mockMultiTestRunFinalizationManager.Setup(f => f.FinalizeMultiTestRunAsync(It.IsAny<ICollection<AttachmentSet>>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult((Collection<AttachmentSet>)null));

// act
parallelDataCollectionEventsHandler.HandleTestRunComplete(new TestRunCompleteEventArgs(null, false, false, null, null, TimeSpan.FromSeconds(1)), null, inputAttachments, null);

// assert
mockTestRunEventsHandler.Verify(h => h.HandleTestRunComplete(It.IsAny<TestRunCompleteEventArgs>(), It.IsAny<TestRunChangedEventArgs>(), It.Is<ICollection<AttachmentSet>>(c => c.Count == 3), It.IsAny<ICollection<string>>()));
mockMultiTestRunFinalizationManager.Verify(f => f.FinalizeMultiTestRunAsync(It.Is<ICollection<AttachmentSet>>(a => a.Count == 3), cancellationTokenSource.Token));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Microsoft.TestPlatform.Utilities.UnitTests
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.Utilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Collections.ObjectModel;
using System.Threading;
Expand Down Expand Up @@ -36,16 +37,45 @@ public void HandleDataCollectionAttachmentSetsShouldReturnEmptySetWhenNoAttachme
}

[TestMethod]
public void HandleDataCollectionAttachmentSetsShouldReturnEmptySetWhenNoCodeCoverageAttachments()
public void HandleDataCollectionAttachmentSetsShouldThrowIfCancellationRequested()
{
Collection<AttachmentSet> attachment = new Collection<AttachmentSet>();
var attachmentSet = new AttachmentSet(new Uri("//badrui//"), string.Empty);
attachmentSet.Attachments.Add(new UriDataAttachment(new Uri("C:\\temp\\aa"), "coverage"));
CancellationTokenSource cts = new CancellationTokenSource();
cts.Cancel();

ICollection<AttachmentSet> resultAttachmentSets =
coverageDataAttachmentsHandler.HandleDataCollectionAttachmentSets(attachment, CancellationToken.None);
Collection<AttachmentSet> attachment = new Collection<AttachmentSet>
{
attachmentSet
};

Assert.IsNotNull(resultAttachmentSets);
Assert.IsTrue(resultAttachmentSets.Count == 0);
Assert.ThrowsException<OperationCanceledException>(() => coverageDataAttachmentsHandler.HandleDataCollectionAttachmentSets(attachment, cts.Token));

Assert.AreEqual(1, attachment.Count);
}

[TestMethod]
public void HandleDataCollectionAttachmentSetsShouldReturnExistingAttachmentsIfFailedToLoadLibrary()
{
var attachmentSet1 = new AttachmentSet(new Uri("//badrui//"), string.Empty);
attachmentSet1.Attachments.Add(new UriDataAttachment(new Uri("C:\\temp\\aa"), "coverage"));

var attachmentSet2 = new AttachmentSet(new Uri("//badruj//"), string.Empty);
attachmentSet2.Attachments.Add(new UriDataAttachment(new Uri("C:\\temp\\ab"), "coverage"));

CancellationTokenSource cts = new CancellationTokenSource();

Collection<AttachmentSet> attachment = new Collection<AttachmentSet>
{
attachmentSet1,
attachmentSet2
};

var result = coverageDataAttachmentsHandler.HandleDataCollectionAttachmentSets(attachment, cts.Token);

Assert.AreEqual(2, result.Count);
Assert.IsTrue(result.Contains(attachmentSet1));
Assert.IsTrue(result.Contains(attachmentSet2));
}
}
}

0 comments on commit cbca05a

Please sign in to comment.