Skip to content

Commit

Permalink
Passing TestPlatform properties to DataCollector extensions (#1962)
Browse files Browse the repository at this point in the history
* Passing TestPlatform properties to DataCollector extensions
  • Loading branch information
vagisha-nidhi authored Mar 25, 2019
1 parent d5d56a7 commit 07d948f
Show file tree
Hide file tree
Showing 21 changed files with 426 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,16 @@ public void TestHostLaunched(int processId)
}

/// <inheritdoc/>
public bool SessionStarted()
public bool SessionStarted(SessionStartEventArgs sessionStartEventArgs)
{
// If datacollectors are not configured or datacollection is not enabled, return false.
if (!this.isDataCollectionEnabled || this.RunDataCollectors.Count == 0)
{
return false;
}

this.SendEvent(new SessionStartEventArgs(this.dataCollectionEnvironmentContext.SessionDataCollectionContext));
sessionStartEventArgs.Context = new DataCollectionContext(this.dataCollectionEnvironmentContext.SessionDataCollectionContext.SessionId);
this.SendEvent(sessionStartEventArgs);

return this.events.AreTestCaseEventsSubscribed();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ internal interface IDataCollectionManager : IDisposable
/// <summary>
/// Raises SessionStart event to all data collectors configured for run.
/// </summary>
/// <param name="sessionStartEventArgs">
/// The session start Event Args.
/// </param>
/// <returns>bolean value specifying whether test case events are subscribed by datacollectors. Based on this execution process will decide whether to send TestCaseStart and TestCaseEnd events to dataCollector process.</returns>
bool SessionStarted();
bool SessionStarted(SessionStartEventArgs sessionStartEventArgs);

/// <summary>
/// Raises SessionEnd event to all data collectors configured for run.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,16 @@ private void AddExtensionAssemblies(string runSettings)
private void HandleBeforeTestRunStart(Message message)
{
// Initialize datacollectors and get enviornment variables.
var settingXml = this.dataSerializer.DeserializePayload<string>(message);
this.AddExtensionAssemblies(settingXml);
var payload = this.dataSerializer.DeserializePayload<BeforeTestRunStartPayload>(message);
this.AddExtensionAssemblies(payload.SettingsXml);

var envVariables = this.dataCollectionManager.InitializeDataCollectors(settingXml);
var areTestCaseLevelEventsRequired = this.dataCollectionManager.SessionStarted();
var envVariables = this.dataCollectionManager.InitializeDataCollectors(payload.SettingsXml);

var properties = new Dictionary<string, object>();
properties.Add(CoreUtilitiesConstants.TestSourcesKeyName, payload.Sources);
var eventArgs = new SessionStartEventArgs(properties);

var areTestCaseLevelEventsRequired = this.dataCollectionManager.SessionStarted(eventArgs);

// Open a socket communication port for test level events.
var testCaseEventsPort = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection
{
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Net;
Expand All @@ -14,7 +15,6 @@ namespace Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollect
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;

using CommonResources = Microsoft.VisualStudio.TestPlatform.Common.Resources.Resources;
Expand Down Expand Up @@ -109,17 +109,23 @@ public void SendTestHostLaunched(TestHostLaunchedPayload testHostLaunchedPayload
}

/// <inheritdoc/>
public BeforeTestRunStartResult SendBeforeTestRunStartAndGetResult(string settingsXml, ITestMessageEventHandler runEventsHandler)
public BeforeTestRunStartResult SendBeforeTestRunStartAndGetResult(string settingsXml, IEnumerable<string> sources, ITestMessageEventHandler runEventsHandler)
{
var isDataCollectionStarted = false;
BeforeTestRunStartResult result = null;

if (EqtTrace.IsVerboseEnabled)
{
EqtTrace.Verbose("DataCollectionRequestSender.SendBeforeTestRunStartAndGetResult : Send BeforeTestRunStart message with settingsXml: {0}", settingsXml);
EqtTrace.Verbose("DataCollectionRequestSender.SendBeforeTestRunStartAndGetResult : Send BeforeTestRunStart message with settingsXml {0} and sources {1}: ", settingsXml, sources.ToString());
}

this.communicationManager.SendMessage(MessageType.BeforeTestRunStart, settingsXml);
var payload = new BeforeTestRunStartPayload
{
SettingsXml = settingsXml,
Sources = sources
};

this.communicationManager.SendMessage(MessageType.BeforeTestRunStart, payload);

while (!isDataCollectionStarted)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection.Interfaces
{
using System.Collections.Generic;
using System.Collections.ObjectModel;

using Microsoft.VisualStudio.TestPlatform.Common.DataCollection;
Expand Down Expand Up @@ -47,13 +48,16 @@ internal interface IDataCollectionRequestSender
/// <param name="settingXml">
/// Run settings for test run.
/// </param>
/// <param name="sources">
/// Test run sources
/// </param>
/// <param name="runEventsHandler">
/// Test message event handler for handling messages.
/// </param>
/// <returns>
/// BeforeTestRunStartResult containing environment variables
/// </returns>
BeforeTestRunStartResult SendBeforeTestRunStartAndGetResult(string settingXml, ITestMessageEventHandler runEventsHandler);
BeforeTestRunStartResult SendBeforeTestRunStartAndGetResult(string settingXml, IEnumerable<string> sources, ITestMessageEventHandler runEventsHandler);

/// <summary>
/// Sends the AfterTestRunStart event and waits for result
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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.VisualStudio.TestPlatform.CommunicationUtilities.ObjectModel
{
using System.Collections.Generic;

/// <summary>
/// The before test run start payload
/// </summary>
public class BeforeTestRunStartPayload
{
/// <summary>
/// Gets or sets run settings xml.
/// </summary>
public string SettingsXml { get; set; }

/// <summary>
/// Gets or sets list of test sources.
/// </summary>
public IEnumerable<string> Sources { get; set; }
}
}
5 changes: 5 additions & 0 deletions src/Microsoft.TestPlatform.CoreUtilities/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,10 @@ public class Constants
/// Environment Variable Specified by user to setup Culture.
/// </summary>
public const string DotNetUserSpecifiedCulture = "DOTNET_CLI_UI_LANGUAGE";

/// <summary>
/// Test sources key name
/// </summary>
public const string TestSourcesKeyName = "TestSources";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection.Interfaces
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection.Interfaces;
Expand All @@ -20,6 +21,16 @@ public interface IProxyDataCollectionManager : IDisposable
/// </summary>
void Initialize();

/// <summary>
/// The settings xml
/// </summary>
string SettingsXml { get; }

/// <summary>
/// List of test sources
/// </summary>
IEnumerable<string> Sources { get; }

/// <summary>
/// Invoked before starting of test run
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,20 @@ internal class ProxyDataCollectionManager : IProxyDataCollectionManager
private IDataCollectionRequestSender dataCollectionRequestSender;
private IDataCollectionLauncher dataCollectionLauncher;
private IProcessHelper processHelper;
private string settingsXml;
private IRequestData requestData;
private int dataCollectionPort;
private int dataCollectionProcessId;

/// <summary>
/// The settings xml
/// </summary>
public string SettingsXml { get; }

/// <summary>
/// List of test sources
/// </summary>
public IEnumerable<string> Sources { get; }

/// <summary>
/// Initializes a new instance of the <see cref="ProxyDataCollectionManager"/> class.
/// </summary>
Expand All @@ -58,8 +67,11 @@ internal class ProxyDataCollectionManager : IProxyDataCollectionManager
/// <param name="settingsXml">
/// Runsettings that contains the datacollector related configuration.
/// </param>
public ProxyDataCollectionManager(IRequestData requestData, string settingsXml)
: this(requestData, settingsXml, new ProcessHelper())
/// <param name="sources">
/// Test Run sources
/// </param>
public ProxyDataCollectionManager(IRequestData requestData, string settingsXml, IEnumerable<string> sources)
: this(requestData, settingsXml, sources, new ProcessHelper())
{
}

Expand All @@ -72,10 +84,13 @@ public ProxyDataCollectionManager(IRequestData requestData, string settingsXml)
/// <param name="settingsXml">
/// The settings xml.
/// </param>
/// <param name="sources">
/// Test Run sources
/// </param>
/// <param name="processHelper">
/// The process helper.
/// </param>
internal ProxyDataCollectionManager(IRequestData requestData, string settingsXml, IProcessHelper processHelper) : this(requestData, settingsXml, new DataCollectionRequestSender(), processHelper, DataCollectionLauncherFactory.GetDataCollectorLauncher(processHelper, settingsXml))
internal ProxyDataCollectionManager(IRequestData requestData, string settingsXml, IEnumerable<string> sources, IProcessHelper processHelper) : this(requestData, settingsXml, sources, new DataCollectionRequestSender(), processHelper, DataCollectionLauncherFactory.GetDataCollectorLauncher(processHelper, settingsXml))
{
}

Expand All @@ -88,6 +103,9 @@ public ProxyDataCollectionManager(IRequestData requestData, string settingsXml)
/// <param name="settingsXml">
/// Runsettings that contains the datacollector related configuration.
/// </param>
/// <param name="sources">
/// Test Run sources
/// </param>
/// <param name="dataCollectionRequestSender">
/// Handles communication with datacollector process.
/// </param>
Expand All @@ -98,12 +116,14 @@ public ProxyDataCollectionManager(IRequestData requestData, string settingsXml)
/// Launches datacollector process.
/// </param>
internal ProxyDataCollectionManager(IRequestData requestData, string settingsXml,
IEnumerable<string> sources,
IDataCollectionRequestSender dataCollectionRequestSender, IProcessHelper processHelper,
IDataCollectionLauncher dataCollectionLauncher)
{
// DataCollector process needs the information of the Extensions folder
// Add the Extensions folder path to runsettings.
this.settingsXml = UpdateExtensionsFolderInRunSettings(settingsXml);
this.SettingsXml = UpdateExtensionsFolderInRunSettings(settingsXml);
this.Sources = sources;
this.requestData = requestData;

this.dataCollectionRequestSender = dataCollectionRequestSender;
Expand Down Expand Up @@ -166,7 +186,7 @@ public DataCollectionParameters BeforeTestRunStart(
() =>
{
EqtTrace.Info("ProxyDataCollectionManager.BeforeTestRunStart: Get env variable and port for datacollector processId: {0} port: {1}", this.dataCollectionProcessId, this.dataCollectionPort);
var result = this.dataCollectionRequestSender.SendBeforeTestRunStartAndGetResult(this.settingsXml, runEventsHandler);
var result = this.dataCollectionRequestSender.SendBeforeTestRunStartAndGetResult(this.SettingsXml, this.Sources, runEventsHandler);
environmentVariables = result.EnvironmentVariables;
dataCollectionEventsPort = result.DataCollectionEventsPort;
Expand Down Expand Up @@ -361,7 +381,7 @@ private void LogEnabledDataCollectors()
return;
}

var dataCollectionSettings = XmlRunSettingsUtilities.GetDataCollectionRunSettings(this.settingsXml);
var dataCollectionSettings = XmlRunSettingsUtilities.GetDataCollectionRunSettings(this.SettingsXml);

if (dataCollectionSettings == null || !dataCollectionSettings.IsCollectionEnabled)
{
Expand Down
12 changes: 11 additions & 1 deletion src/Microsoft.TestPlatform.CrossPlatEngine/TestEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Microsoft.VisualStudio.TestPlatform.CrossPlatEngine
{
using System;
using System.Collections.Generic;
using System.Linq;

using Microsoft.VisualStudio.TestPlatform.Common;
Expand Down Expand Up @@ -131,7 +132,7 @@ public IProxyExecutionManager GetExecutionManager(IRequestData requestData, ITes

var requestSender = new TestRequestSender(requestData.ProtocolConfig, hostManager.GetTestHostConnectionInfo());

return isDataCollectorEnabled ? new ProxyExecutionManagerWithDataCollection(requestData, requestSender, hostManager, new ProxyDataCollectionManager(requestData, testRunCriteria.TestRunSettings))
return isDataCollectorEnabled ? new ProxyExecutionManagerWithDataCollection(requestData, requestSender, hostManager, new ProxyDataCollectionManager(requestData, testRunCriteria.TestRunSettings, GetSourcesFromTestRunCriteria(testRunCriteria)))
: new ProxyExecutionManager(requestData, requestSender, hostManager);
};

Expand Down Expand Up @@ -305,5 +306,14 @@ private IRequestData GetRequestData(bool isTelemetryOptedIn)
IsTelemetryOptedIn = isTelemetryOptedIn
};
}

/// <summary>
/// Gets test sources from test run criteria
/// </summary>
/// <returns>test sources</returns>
private IEnumerable<string> GetSourcesFromTestRunCriteria(TestRunCriteria testRunCriteria)
{
return testRunCriteria.HasSpecificTests ? testRunCriteria.Tests.Select(tc => tc.Source).Distinct() : testRunCriteria.Sources;
}
}
}
Loading

0 comments on commit 07d948f

Please sign in to comment.