Skip to content

Commit

Permalink
More changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubch1 committed Jun 22, 2020
1 parent 6c308b5 commit 4f1ede1
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;

namespace Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.MultiTestRunFinalization
{
Expand Down Expand Up @@ -72,12 +73,12 @@ private async Task<Collection<AttachmentSet>> InternalFinalizeMultiTestRunAsync(

if (completedTask == task)
{
return FinalizeOperation(requestData, new MultiTestRunFinalizationCompleteEventArgs(false, false, null), await task, stopwatch, eventHandler);
return FinalizeOperation(requestData, new MultiTestRunFinalizationCompleteEventArgs(false, null), await task, stopwatch, eventHandler);
}
else
{
eventHandler?.HandleLogMessage(ObjectModel.Logging.TestMessageLevel.Informational, "Finalization was cancelled.");
return FinalizeOperation(requestData, new MultiTestRunFinalizationCompleteEventArgs(true, false, null), attachments, stopwatch, eventHandler);
return FinalizeOperation(requestData, new MultiTestRunFinalizationCompleteEventArgs(true, null), attachments, stopwatch, eventHandler);
}
}
}
Expand All @@ -87,25 +88,26 @@ private async Task<Collection<AttachmentSet>> InternalFinalizeMultiTestRunAsync(
{
EqtTrace.Warning("MultiTestRunFinalizationManager: operation was cancelled.");
}
return FinalizeOperation(requestData, new MultiTestRunFinalizationCompleteEventArgs(true, false, null), attachments, stopwatch, eventHandler);
return FinalizeOperation(requestData, new MultiTestRunFinalizationCompleteEventArgs(true, null), attachments, stopwatch, eventHandler);
}
catch (Exception e)
{
EqtTrace.Error("MultiTestRunFinalizationManager: Exception in FinalizeMultiTestRunAsync: " + e);

eventHandler?.HandleLogMessage(ObjectModel.Logging.TestMessageLevel.Error, e.Message);
return FinalizeOperation(requestData, new MultiTestRunFinalizationCompleteEventArgs(false, true, e), attachments, stopwatch, eventHandler);
return FinalizeOperation(requestData, new MultiTestRunFinalizationCompleteEventArgs(false, e), attachments, stopwatch, eventHandler);
}
}

private Collection<AttachmentSet> ProcessAttachments(Collection<AttachmentSet> attachments, IMultiTestRunFinalizationEventsHandler eventsHandler, CancellationToken cancellationToken)
{
if (attachments == null || !attachments.Any()) return attachments;

var logger = CreateMessageLogger(eventsHandler);

for (int i = 0; i < dataCollectorAttachmentsHandlers.Length; i++)
{
IDataCollectorAttachments dataCollectorAttachmentsHandler = dataCollectorAttachmentsHandlers[i];
string attachmentsHandlerName = dataCollectorAttachmentsHandler.GetType().FullName;
int attachmentsHandlerIndex = i + 1;

Uri attachementUri = dataCollectorAttachmentsHandler.GetExtensionUri();
Expand All @@ -121,9 +123,9 @@ private Collection<AttachmentSet> ProcessAttachments(Collection<AttachmentSet> a

IProgress<int> progressReporter = new Progress<int>((int progress) =>
eventsHandler?.HandleMultiTestRunFinalizationProgress(
new MultiTestRunFinalizationProgressEventArgs(attachmentsHandlerIndex, attachmentsHandlerName, progress, dataCollectorAttachmentsHandlers.Length)));
new MultiTestRunFinalizationProgressEventArgs(attachmentsHandlerIndex, dataCollectorAttachmentsHandler.GetExtensionUri(), progress, dataCollectorAttachmentsHandlers.Length)));

ICollection<AttachmentSet> processedAttachments = dataCollectorAttachmentsHandler.HandleDataCollectionAttachmentSets(new Collection<AttachmentSet>(attachmentsToBeProcessed), progressReporter, cancellationToken);
ICollection<AttachmentSet> processedAttachments = dataCollectorAttachmentsHandler.HandleDataCollectionAttachmentSets(new Collection<AttachmentSet>(attachmentsToBeProcessed), progressReporter, logger, cancellationToken);

foreach (var attachment in processedAttachments)
{
Expand All @@ -140,7 +142,7 @@ private Collection<AttachmentSet> FinalizeOperation(IRequestData requestData, Mu
{
testPlatformEventSource.MultiTestRunFinalizationStop(attachments.Count);
requestData.MetricsCollection.Add(TelemetryDataConstants.NumberOfAttachmentsAfterFinalization, attachments.Count);
requestData.MetricsCollection.Add(TelemetryDataConstants.FinalizationState, completeArgs.IsAborted ? FinalizationFailed : completeArgs.IsCanceled ? FinalizationCanceled : FinalizationCompleted);
requestData.MetricsCollection.Add(TelemetryDataConstants.FinalizationState, completeArgs.Error != null ? FinalizationFailed : completeArgs.IsCanceled ? FinalizationCanceled : FinalizationCompleted);

stopwatch.Stop();
requestData.MetricsCollection.Add(TelemetryDataConstants.TimeTakenInSecForFinalization, stopwatch.Elapsed.TotalSeconds);
Expand All @@ -150,5 +152,32 @@ private Collection<AttachmentSet> FinalizeOperation(IRequestData requestData, Mu

return attachments;
}

private IMessageLogger CreateMessageLogger(IMultiTestRunFinalizationEventsHandler eventsHandler)
{
return eventsHandler != null ? (IMessageLogger)new FinalizationMessageLogger(eventsHandler) : new NullMessageLogger();
}

private class FinalizationMessageLogger : IMessageLogger
{
private readonly IMultiTestRunFinalizationEventsHandler eventsHandler;

public FinalizationMessageLogger(IMultiTestRunFinalizationEventsHandler eventsHandler)
{
this.eventsHandler = eventsHandler ?? throw new ArgumentNullException(nameof(eventsHandler));
}

public void SendMessage(TestMessageLevel testMessageLevel, string message)
{
eventsHandler.HandleLogMessage(testMessageLevel, message);
}
}

private class NullMessageLogger : IMessageLogger
{
public void SendMessage(TestMessageLevel testMessageLevel, string message)
{
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,13 @@ public class MultiTestRunFinalizationCompleteEventArgs : EventArgs
/// Default constructor.
/// </summary>
/// <param name="isCanceled">Specifies whether the finalization is canceled.</param>
/// <param name="isAborted">Specifies whether the finalization is aborted.</param>
/// <param name="error">Specifies the error encountered during the execution of the finalization.</param>
public MultiTestRunFinalizationCompleteEventArgs(bool isCanceled, bool isAborted, Exception error)
public MultiTestRunFinalizationCompleteEventArgs(bool isCanceled, Exception error)
{
this.IsCanceled = isCanceled;
this.IsAborted = isAborted;
this.Error = error;
}

/// <summary>
/// Gets a value indicating whether the finalization is aborted or not.
/// </summary>
[DataMember]
public bool IsAborted { get; private set; }

/// <summary>
/// Gets a value indicating whether the finalization is canceled or not.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ public class MultiTestRunFinalizationProgressEventArgs : EventArgs
/// Default constructor.
/// </summary>
/// <param name="currentHandlerIndex">Specifies current handler index.</param>
/// <param name="currentHandlerName">Specifies current handler name.</param>
/// <param name="currentHandlerUri">Specifies current handler Uri.</param>
/// <param name="currentHandlerProgress">Specifies current handler progress.</param>
/// <param name="handlersCount">Specifies the overall number of handlers.</param>
public MultiTestRunFinalizationProgressEventArgs(long currentHandlerIndex, string currentHandlerName, long currentHandlerProgress, long handlersCount)
public MultiTestRunFinalizationProgressEventArgs(long currentHandlerIndex, Uri currentHandlerUri, long currentHandlerProgress, long handlersCount)
{
CurrentHandlerIndex = currentHandlerIndex;
CurrentHandlerName = currentHandlerName;
CurrentHandlerUri = currentHandlerUri;
CurrentHandlerProgress = currentHandlerProgress;
HandlersCount = handlersCount;
}
Expand All @@ -31,10 +31,10 @@ public MultiTestRunFinalizationProgressEventArgs(long currentHandlerIndex, strin
public long CurrentHandlerIndex { get; private set; }

/// <summary>
/// Gets a current handler name.
/// Gets a current handler URI.
/// </summary>
[DataMember]
public string CurrentHandlerName { get; private set; }
public Uri CurrentHandlerUri { get; private set; }

/// <summary>
/// Gets a current handler progress.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection
{
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using System;
using System.Collections.Generic;
using System.Threading;
Expand All @@ -25,7 +26,7 @@ public interface IDataCollectorAttachments
/// <param name="progressReporter">Progress reporter. Accepts integers from 0 to 100</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Gets the attachment set after Test Run Session</returns>
ICollection<AttachmentSet> HandleDataCollectionAttachmentSets(ICollection<AttachmentSet> dataCollectionAttachments, IProgress<int> progressReporter, CancellationToken cancellationToken);
ICollection<AttachmentSet> HandleDataCollectionAttachmentSets(ICollection<AttachmentSet> dataCollectionAttachments, IProgress<int> progressReporter, IMessageLogger logger, CancellationToken cancellationToken);

/// <summary>
/// Gets the attachment Uri, which is handled by current Collector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ namespace Microsoft.VisualStudio.TestPlatform.Utilities
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Threading;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using Microsoft.VisualStudio.TestPlatform.PlatformAbstractions;

public class CodeCoverageDataAttachmentsHandler : IDataCollectorAttachments
Expand All @@ -34,10 +34,10 @@ public Uri GetExtensionUri()

public ICollection<AttachmentSet> HandleDataCollectionAttachmentSets(ICollection<AttachmentSet> dataCollectionAttachments)
{
return HandleDataCollectionAttachmentSets(dataCollectionAttachments, null, CancellationToken.None);
return HandleDataCollectionAttachmentSets(dataCollectionAttachments, null, null, CancellationToken.None);
}

public ICollection<AttachmentSet> HandleDataCollectionAttachmentSets(ICollection<AttachmentSet> dataCollectionAttachments, IProgress<int> progressReporter, CancellationToken cancellationToken)
public ICollection<AttachmentSet> HandleDataCollectionAttachmentSets(ICollection<AttachmentSet> dataCollectionAttachments, IProgress<int> progressReporter, IMessageLogger logger, CancellationToken cancellationToken)
{
if (dataCollectionAttachments != null && dataCollectionAttachments.Any())
{
Expand All @@ -60,10 +60,15 @@ public ICollection<AttachmentSet> HandleDataCollectionAttachmentSets(ICollection

private string MergeCodeCoverageFiles(IList<string> files, IProgress<int> progressReporter, CancellationToken cancellationToken)
{
string fileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + CoverageFileExtension);
if(files.Count == 1)
{
return files[0];
}

string tempFileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + CoverageFileExtension);
string outputfileName = files[0];

File.Create(fileName).Dispose();
File.Create(tempFileName).Dispose();
var assemblyPath = Path.Combine(Path.GetDirectoryName(typeof(CodeCoverageDataAttachmentsHandler).GetTypeInfo().Assembly.GetAssemblyLocation()), CodeCoverageAnalysisAssemblyName + ".dll");

try
Expand All @@ -76,19 +81,27 @@ private string MergeCodeCoverageFiles(IList<string> files, IProgress<int> progre

if (methodInfo != null)
{
IList<string> filesToDelete = new List<string>(files.Count) { tempFileName };

for (int i = 1; i < files.Count; i++)
{
cancellationToken.ThrowIfCancellationRequested();
progressReporter?.Report(100 * i / files.Count);

cancellationToken.ThrowIfCancellationRequested();
methodInfo.Invoke(null, new object[] { files[i], outputfileName, fileName, true });
methodInfo.Invoke(null, new object[] { files[i], outputfileName, tempFileName, true });

cancellationToken.ThrowIfCancellationRequested();
File.Copy(fileName, outputfileName, true);
File.Copy(tempFileName, outputfileName, true);

filesToDelete.Add(files[i]);
}

File.Delete(fileName);
cancellationToken.ThrowIfCancellationRequested();
foreach (string fileName in filesToDelete)
{
File.Delete(fileName);
}
}

progressReporter?.Report(100);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ private async Task SendMessageAndListenAndReportFinalizationResultAsync(IEnumera
{
EqtTrace.Error("Aborting Test Session End Operation: {0}", exception);
eventHandler.HandleLogMessage(TestMessageLevel.Error, TranslationLayerResources.AbortedMultiTestRunFinalization);
eventHandler.HandleMultiTestRunFinalizationComplete(new MultiTestRunFinalizationCompleteEventArgs(false, true, exception), null);
eventHandler.HandleMultiTestRunFinalizationComplete(new MultiTestRunFinalizationCompleteEventArgs(false, exception), null);

// Earlier we were closing the connection with vstest.console in case of exceptions
// Removing that code because vstest.console might be in a healthy state and letting the client
Expand Down
Loading

0 comments on commit 4f1ede1

Please sign in to comment.