From cbca05ad0a42a4f3367a6052ea7c539cd1c110d5 Mon Sep 17 00:00:00 2001 From: Jakub Chocholowicz Date: Tue, 16 Jun 2020 10:13:12 +0200 Subject: [PATCH] More tests --- .../Parallel/ParallelRunDataAggregator.cs | 2 +- .../ParallelDataCollectionEventsHandler.cs | 7 +- .../CodeCoverageDataAttachmentsHandler.cs | 8 -- .../VsTestConsoleWrapper.cs | 6 -- .../ParallelProxyExecutionManagerTests.cs | 27 ++++++ ...arallelDataCollectionEventsHandlerTests.cs | 95 +++++++++++++++++++ ...CodeCoverageDataAttachmentsHandlerTests.cs | 42 ++++++-- 7 files changed, 161 insertions(+), 26 deletions(-) create mode 100644 test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/DataCollection/ParallelDataCollectionEventsHandlerTests.cs diff --git a/src/Microsoft.TestPlatform.CrossPlatEngine/Client/Parallel/ParallelRunDataAggregator.cs b/src/Microsoft.TestPlatform.CrossPlatEngine/Client/Parallel/ParallelRunDataAggregator.cs index 2cc3d3c26c..b2d7f238b8 100644 --- a/src/Microsoft.TestPlatform.CrossPlatEngine/Client/Parallel/ParallelRunDataAggregator.cs +++ b/src/Microsoft.TestPlatform.CrossPlatEngine/Client/Parallel/ParallelRunDataAggregator.cs @@ -49,7 +49,7 @@ public ParallelRunDataAggregator() public TimeSpan ElapsedTime { get; set; } - public Collection RunContextAttachments { get; } + public Collection RunContextAttachments { get; set; } public List RunCompleteArgsAttachments { get; } diff --git a/src/Microsoft.TestPlatform.CrossPlatEngine/DataCollection/ParallelDataCollectionEventsHandler.cs b/src/Microsoft.TestPlatform.CrossPlatEngine/DataCollection/ParallelDataCollectionEventsHandler.cs index c743622f2c..0ac7a10312 100644 --- a/src/Microsoft.TestPlatform.CrossPlatEngine/DataCollection/ParallelDataCollectionEventsHandler.cs +++ b/src/Microsoft.TestPlatform.CrossPlatEngine/DataCollection/ParallelDataCollectionEventsHandler.cs @@ -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 { @@ -59,13 +56,13 @@ public override void HandleTestRunComplete( if (parallelRunComplete) { - Collection 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 diff --git a/src/Microsoft.TestPlatform.Utilities/CodeCoverageDataAttachmentsHandler.cs b/src/Microsoft.TestPlatform.Utilities/CodeCoverageDataAttachmentsHandler.cs index 395b8b2451..3181c51476 100644 --- a/src/Microsoft.TestPlatform.Utilities/CodeCoverageDataAttachmentsHandler.cs +++ b/src/Microsoft.TestPlatform.Utilities/CodeCoverageDataAttachmentsHandler.cs @@ -100,14 +100,6 @@ private string MergeCodeCoverageFiles(IList files, CancellationToken can } throw; } - catch (ObjectDisposedException) - { - if (EqtTrace.IsWarningEnabled) - { - EqtTrace.Warning("CodeCoverageDataCollectorAttachmentsHandler: object disposed."); - } - throw; - } catch (Exception ex) { if (EqtTrace.IsErrorEnabled) diff --git a/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleWrapper.cs b/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleWrapper.cs index d5145f5f65..550f72330a 100644 --- a/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleWrapper.cs +++ b/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleWrapper.cs @@ -283,12 +283,6 @@ public Task FinalizeMultiTestRunAsync(ICollection attachments, IM return requestSender.FinalizeMultiTestRunAsync(attachments, testSessionEventsHandler, cancellationToken); } - /// - public void CancelMultiTestRunFinalization() - { - throw new System.NotImplementedException(); - } - #endregion private void EnsureInitialized() diff --git a/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/Client/Parallel/ParallelProxyExecutionManagerTests.cs b/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/Client/Parallel/ParallelProxyExecutionManagerTests.cs index 9a7a439a80..78b7376bcf 100644 --- a/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/Client/Parallel/ParallelProxyExecutionManagerTests.cs +++ b/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/Client/Parallel/ParallelProxyExecutionManagerTests.cs @@ -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; @@ -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(); + this.mockRequestSender = new Mock(); + this.mockDataCollectionManager = new Mock(); + var proxyDataCollectionManager = new ProxyExecutionManagerWithDataCollection(this.mockRequestData.Object, this.mockRequestSender.Object, this.mockTestHostManager.Object, this.mockDataCollectionManager.Object); + var managers = new List>(); + this.proxyManagerFunc = () => + { + this.proxyManagerFuncCalled = true; + var manager = new Mock(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() { diff --git a/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/DataCollection/ParallelDataCollectionEventsHandlerTests.cs b/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/DataCollection/ParallelDataCollectionEventsHandlerTests.cs new file mode 100644 index 0000000000..81bd9a7fe0 --- /dev/null +++ b/test/Microsoft.TestPlatform.CrossPlatEngine.UnitTests/DataCollection/ParallelDataCollectionEventsHandlerTests.cs @@ -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 mockRequestData; + private readonly Mock mockProxyExecutionManager; + private readonly Mock mockTestRunEventsHandler; + private readonly Mock mockParallelProxyExecutionManager; + private readonly Mock mockMultiTestRunFinalizationManager; + private readonly CancellationTokenSource cancellationTokenSource; + private readonly ParallelDataCollectionEventsHandler parallelDataCollectionEventsHandler; + + public ParallelDataCollectionEventsHandlerTests() + { + mockRequestData = new Mock(); + mockProxyExecutionManager = new Mock(); + mockTestRunEventsHandler = new Mock(); + mockParallelProxyExecutionManager = new Mock(); + mockMultiTestRunFinalizationManager = new Mock(); + 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(), It.IsAny(), It.IsAny(), It.IsAny>(), It.IsAny>())).Returns(true); + } + + [TestMethod] + public void HandleTestRunComplete_ShouldCallFinalizerWithAttachmentsAndUseResults() + { + // arrange + List inputAttachments = new List + { + new AttachmentSet(new Uri(uri1), "uri1_input1"), + new AttachmentSet(new Uri(uri2), "uri2_input1"), + new AttachmentSet(new Uri(uri3), "uri3_input1") + }; + + Collection outputAttachments = new Collection + { + new AttachmentSet(new Uri(uri1), "uri1_input1") + }; + + mockMultiTestRunFinalizationManager.Setup(f => f.FinalizeMultiTestRunAsync(It.IsAny>(), It.IsAny())).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(), It.IsAny(), It.Is>(c => c.Count == 1 && c.Contains(outputAttachments[0])), It.IsAny>())); + mockMultiTestRunFinalizationManager.Verify(f => f.FinalizeMultiTestRunAsync(It.Is>(a => a.Count == 3), cancellationTokenSource.Token)); + } + + [TestMethod] + public void HandleTestRunComplete_ShouldCallFinalizerWithAttachmentsAndNotUserResults_IfFinalizerReturnsNull() + { + // arrange + List inputAttachments = new List + { + 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>(), It.IsAny())).Returns(Task.FromResult((Collection)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(), It.IsAny(), It.Is>(c => c.Count == 3), It.IsAny>())); + mockMultiTestRunFinalizationManager.Verify(f => f.FinalizeMultiTestRunAsync(It.Is>(a => a.Count == 3), cancellationTokenSource.Token)); + } + } +} diff --git a/test/Microsoft.TestPlatform.Utilities.UnitTests/CodeCoverageDataAttachmentsHandlerTests.cs b/test/Microsoft.TestPlatform.Utilities.UnitTests/CodeCoverageDataAttachmentsHandlerTests.cs index 9e55591dcb..915ad152b5 100644 --- a/test/Microsoft.TestPlatform.Utilities.UnitTests/CodeCoverageDataAttachmentsHandlerTests.cs +++ b/test/Microsoft.TestPlatform.Utilities.UnitTests/CodeCoverageDataAttachmentsHandlerTests.cs @@ -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; @@ -36,16 +37,45 @@ public void HandleDataCollectionAttachmentSetsShouldReturnEmptySetWhenNoAttachme } [TestMethod] - public void HandleDataCollectionAttachmentSetsShouldReturnEmptySetWhenNoCodeCoverageAttachments() + public void HandleDataCollectionAttachmentSetsShouldThrowIfCancellationRequested() { - Collection attachment = new Collection(); 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 resultAttachmentSets = - coverageDataAttachmentsHandler.HandleDataCollectionAttachmentSets(attachment, CancellationToken.None); + Collection attachment = new Collection + { + attachmentSet + }; - Assert.IsNotNull(resultAttachmentSets); - Assert.IsTrue(resultAttachmentSets.Count == 0); + Assert.ThrowsException(() => 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 attachment = new Collection + { + attachmentSet1, + attachmentSet2 + }; + + var result = coverageDataAttachmentsHandler.HandleDataCollectionAttachmentSets(attachment, cts.Token); + + Assert.AreEqual(2, result.Count); + Assert.IsTrue(result.Contains(attachmentSet1)); + Assert.IsTrue(result.Contains(attachmentSet2)); } } }