How to use DataCollectionRequestHandler class of Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection package

Best Vstest code snippet using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection.DataCollectionRequestHandler

DataCollectionRequestHandler.cs

Source:DataCollectionRequestHandler.cs Github

copy

Full Screen

...28 using CoreUtilitiesConstants = Microsoft.VisualStudio.TestPlatform.CoreUtilities.Constants;29 /// <summary>30 /// Handles test session events received from vstest console process.31 /// </summary>32 internal class DataCollectionRequestHandler : IDataCollectionRequestHandler, IDisposable33 {34 private static readonly object SyncObject = new object();35 private readonly ICommunicationManager communicationManager;36 private IMessageSink messageSink;37 private IDataCollectionManager dataCollectionManager;38 private IDataCollectionTestCaseEventHandler dataCollectionTestCaseEventHandler;39 private Task testCaseEventMonitorTask;40 private IDataSerializer dataSerializer;41 /// <summary>42 /// Use to cancel data collection test case events monitoring if test run is cancelled.43 /// </summary>44 private CancellationTokenSource cancellationTokenSource;45 /// <summary>46 /// Initializes a new instance of the <see cref="DataCollectionRequestHandler"/> class.47 /// </summary>48 /// <param name="messageSink">49 /// The message sink.50 /// </param>51 protected DataCollectionRequestHandler(IMessageSink messageSink)52 : this(53 new SocketCommunicationManager(),54 messageSink,55 DataCollectionManager.Create(messageSink),56 new DataCollectionTestCaseEventHandler(),57 JsonDataSerializer.Instance)58 {59 this.messageSink = messageSink;60 }61 /// <summary>62 /// Initializes a new instance of the <see cref="DataCollectionRequestHandler"/> class.63 /// </summary>64 /// <param name="communicationManager">65 /// The communication manager.66 /// </param>67 /// <param name="messageSink">68 /// The message sink.69 /// </param>70 /// <param name="dataCollectionManager">71 /// The data collection manager.72 /// </param>73 /// <param name="dataCollectionTestCaseEventHandler">74 /// The data collection test case event handler.75 /// </param>76 /// <param name="dataSerializer">77 /// Serializer for serialization and deserialization of the messages.78 /// </param>79 protected DataCollectionRequestHandler(80 ICommunicationManager communicationManager,81 IMessageSink messageSink,82 IDataCollectionManager dataCollectionManager,83 IDataCollectionTestCaseEventHandler dataCollectionTestCaseEventHandler,84 IDataSerializer dataSerializer)85 {86 this.communicationManager = communicationManager;87 this.messageSink = messageSink;88 this.dataCollectionManager = dataCollectionManager;89 this.dataSerializer = dataSerializer;90 this.dataCollectionTestCaseEventHandler = dataCollectionTestCaseEventHandler;91 this.cancellationTokenSource = new CancellationTokenSource();92 }93 /// <summary>94 /// Gets the singleton instance of DataCollectionRequestHandler.95 /// </summary>96 public static DataCollectionRequestHandler Instance { get; private set; }97 /// <summary>98 /// Creates singleton instance of DataCollectionRequestHandler.99 /// </summary>100 /// <param name="communicationManager">101 /// Handles socket communication.102 /// </param>103 /// <param name="messageSink">104 /// Message sink for sending messages to execution process.105 /// </param>106 /// <returns>107 /// The instance of <see cref="DataCollectionRequestHandler"/>.108 /// </returns>109 public static DataCollectionRequestHandler Create(110 ICommunicationManager communicationManager,111 IMessageSink messageSink)112 {113 ValidateArg.NotNull(communicationManager, nameof(communicationManager));114 ValidateArg.NotNull(messageSink, nameof(messageSink));115 if (Instance == null)116 {117 lock (SyncObject)118 {119 if (Instance == null)120 {121 Instance = new DataCollectionRequestHandler(122 communicationManager,123 messageSink,124 DataCollectionManager.Create(messageSink),125 new DataCollectionTestCaseEventHandler(),126 JsonDataSerializer.Instance);127 }128 }129 }130 return Instance;131 }132 /// <inheritdoc />133 public void InitializeCommunication(int port)134 {135 this.communicationManager.SetupClientAsync(new IPEndPoint(IPAddress.Loopback, port));136 }137 /// <inheritdoc />138 public bool WaitForRequestSenderConnection(int connectionTimeout)139 {140 return this.communicationManager.WaitForServerConnection(connectionTimeout);141 }142 /// <summary>143 /// Process requests.144 /// </summary>145 public void ProcessRequests()146 {147 var isSessionEnded = false;148 do149 {150 var message = this.communicationManager.ReceiveMessage();151 if (EqtTrace.IsInfoEnabled)152 {153 EqtTrace.Info("DataCollectionRequestHandler.ProcessRequests : Datacollector received message: {0}", message);154 }155 switch (message.MessageType)156 {157 case MessageType.BeforeTestRunStart:158 this.HandleBeforeTestRunStart(message);159 break;160 case MessageType.AfterTestRunEnd:161 this.HandleAfterTestRunEnd(message);162 isSessionEnded = true;163 break;164 case MessageType.TestHostLaunched:165 var testHostLaunchedPayload = this.dataSerializer.DeserializePayload<TestHostLaunchedPayload>(message);166 this.dataCollectionManager.TestHostLaunched(testHostLaunchedPayload.ProcessId);167 break;168 default:169 EqtTrace.Error("DataCollectionRequestHandler.ProcessRequests : Invalid Message types: {0}", message.MessageType);170 break;171 }172 }173 while (!isSessionEnded);174 }175 /// <summary>176 /// Sends datacollection message.177 /// </summary>178 /// <param name="args">179 /// The args.180 /// </param>181 public void SendDataCollectionMessage(DataCollectionMessageEventArgs args)182 {183 this.communicationManager.SendMessage(MessageType.DataCollectionMessage, args);184 }185 /// <summary>186 /// The dispose.187 /// </summary>188 public void Dispose()189 {190 this.communicationManager?.StopClient();191 }192 /// <summary>193 /// Closes the connection194 /// </summary>195 public void Close()196 {197 this.Dispose();198 EqtTrace.Info("Closing the connection !");199 }200 /// <summary>201 /// Update the test adapter paths provided through run settings to be used by the test plugin cache.202 /// </summary>203 /// <param name="runSettings">204 /// The run Settings.205 /// </param>206 private void AddExtensionAssemblies(string runSettings)207 {208 try209 {210 IEnumerable<string> customTestAdaptersPaths = RunSettingsUtilities.GetTestAdaptersPaths(runSettings);211 if (customTestAdaptersPaths != null)212 {213 var fileHelper = new FileHelper();214 List<string> extensionAssemblies = new List<string>();215 foreach (var customTestAdaptersPath in customTestAdaptersPaths)216 {217 var adapterPath =218 Path.GetFullPath(Environment.ExpandEnvironmentVariables(customTestAdaptersPath));219 if (!fileHelper.DirectoryExists(adapterPath))220 {221 EqtTrace.Warning(string.Format("AdapterPath Not Found:", adapterPath));222 continue;223 }224 extensionAssemblies.AddRange(225 fileHelper.EnumerateFiles(226 adapterPath,227 SearchOption.AllDirectories,228 TestPlatformConstants.DataCollectorEndsWithPattern));229 }230 if (extensionAssemblies.Count > 0)231 {232 TestPluginCache.Instance.UpdateExtensions(extensionAssemblies, skipExtensionFilters: false);233 }234 }235 }236 catch (Exception e)237 {238 // If any exception is throuwn while updating additional assemblies, log the exception in eqt trace.239 if (EqtTrace.IsErrorEnabled)240 {241 EqtTrace.Error("DataCollectionRequestHandler.AddExtensionAssemblies: Exception occured: {0}", e);242 }243 }244 }245 private void HandleBeforeTestRunStart(Message message)246 {247 // Initialize datacollectors and get enviornment variables.248 var payload = this.dataSerializer.DeserializePayload<BeforeTestRunStartPayload>(message);249 this.AddExtensionAssemblies(payload.SettingsXml);250 var envVariables = this.dataCollectionManager.InitializeDataCollectors(payload.SettingsXml);251 var properties = new Dictionary<string, object>();252 properties.Add(CoreUtilitiesConstants.TestSourcesKeyName, payload.Sources);253 var eventArgs = new SessionStartEventArgs(properties);254 var areTestCaseLevelEventsRequired = this.dataCollectionManager.SessionStarted(eventArgs);255 // Open a socket communication port for test level events.256 var testCaseEventsPort = 0;257 if (areTestCaseLevelEventsRequired)258 {259 testCaseEventsPort = this.dataCollectionTestCaseEventHandler.InitializeCommunication();260 this.testCaseEventMonitorTask = Task.Factory.StartNew(261 () =>262 {263 try264 {265 var timeout = EnvironmentHelper.GetConnectionTimeout();266 if (this.dataCollectionTestCaseEventHandler.WaitForRequestHandlerConnection(267 timeout * 1000))268 {269 this.dataCollectionTestCaseEventHandler.ProcessRequests();270 }271 else272 {273 EqtTrace.Error(274 "DataCollectionRequestHandler.HandleBeforeTestRunStart: TestCaseEventHandler timed out while connecting to the Sender.");275 this.dataCollectionTestCaseEventHandler.Close();276 throw new TestPlatformException(277 string.Format(278 CultureInfo.CurrentUICulture,279 CommunicationUtilitiesResources.ConnectionTimeoutErrorMessage,280 CoreUtilitiesConstants.DatacollectorProcessName,281 CoreUtilitiesConstants.TesthostProcessName,282 timeout,283 EnvironmentHelper.VstestConnectionTimeout));284 }285 }286 catch (Exception e)287 {288 EqtTrace.Error("DataCollectionRequestHandler.HandleBeforeTestRunStart : Error occured during initialization of TestHost : {0}", e);289 }290 },291 this.cancellationTokenSource.Token);292 }293 this.communicationManager.SendMessage(294 MessageType.BeforeTestRunStartResult,295 new BeforeTestRunStartResult(envVariables, testCaseEventsPort));296 EqtTrace.Info("DataCollectionRequestHandler.ProcessRequests : DataCollection started.");297 }298 private void HandleAfterTestRunEnd(Message message)299 {300 var isCancelled = this.dataSerializer.DeserializePayload<bool>(message);301 if (isCancelled)302 {303 this.cancellationTokenSource.Cancel();304 }305 try306 {307 this.testCaseEventMonitorTask?.Wait(this.cancellationTokenSource.Token);308 this.dataCollectionTestCaseEventHandler.Close();309 }310 catch (Exception ex)311 {312 EqtTrace.Error("DataCollectionRequestHandler.HandleAfterTestRunEnd : Error while processing event from testhost: {0}", ex.ToString());313 }314 var attachmentsets = this.dataCollectionManager.SessionEnded(isCancelled);315 // Dispose all datacollectors before sending attachements to vstest.console process.316 // As datacollector process exits itself on parent process(vstest.console) exits.317 this.dataCollectionManager?.Dispose();318 this.communicationManager.SendMessage(MessageType.AfterTestRunEndResult, attachmentsets);319 EqtTrace.Info("DataCollectionRequestHandler.ProcessRequests : Session End message received from server. Closing the connection.");320 this.Close();321 EqtTrace.Info("DataCollectionRequestHandler.ProcessRequests : DataCollection completed");322 }323 }324}...

Full Screen

Full Screen

DataCollectionRequestHandler

Using AI Code Generation

copy

Full Screen

1using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection;2using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection.Interfaces;3using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.Interfaces;4using Microsoft.VisualStudio.TestPlatform.ObjectModel;5using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;6using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;7using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine;8using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine.ClientProtocol;9using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine.TesthostProtocol;10using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers;11using Microsoft.VisualStudio.TestPlatform.Utilities.Helpers.Interfaces;12{13 {14 private IDataCollectionManager dataCollectionManager;15 private IDataSerializer dataSerializer;16 private ICommunicationManager communicationManager;

Full Screen

Full Screen

DataCollectionRequestHandler

Using AI Code Generation

copy

Full Screen

1using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection;2using System;3using System.Collections.Generic;4using System.Linq;5using System.Text;6using System.Threading.Tasks;7{8 {9 static void Main(string[] args)10 {11 DataCollectionRequestHandler dataCollectionRequestHandler = new DataCollectionRequestHandler();12 dataCollectionRequestHandler.Start();13 Console.ReadLine();14 }15 }16}17using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection;18using System;19using System.Collections.Generic;20using System.Linq;21using System.Text;22using System.Threading.Tasks;23{24 {25 static void Main(string[] args)26 {27 DataCollectionRequestHandler dataCollectionRequestHandler = new DataCollectionRequestHandler();28 dataCollectionRequestHandler.Start();29 Console.ReadLine();30 }31 }32}33using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection;34using System;35using System.Collections.Generic;36using System.Linq;37using System.Text;38using System.Threading.Tasks;39{40 {41 static void Main(string[] args)42 {43 DataCollectionRequestHandler dataCollectionRequestHandler = new DataCollectionRequestHandler();44 dataCollectionRequestHandler.Start();45 Console.ReadLine();46 }47 }48}49using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection;50using System;51using System.Collections.Generic;52using System.Linq;53using System.Text;54using System.Threading.Tasks;55{56 {57 static void Main(string[] args)58 {59 DataCollectionRequestHandler dataCollectionRequestHandler = new DataCollectionRequestHandler();60 dataCollectionRequestHandler.Start();61 Console.ReadLine();62 }

Full Screen

Full Screen

DataCollectionRequestHandler

Using AI Code Generation

copy

Full Screen

1using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection;2using Microsoft.VisualStudio.TestPlatform.ObjectModel;3using System;4using System.Collections.Generic;5using System.Diagnostics;6using System.Linq;7using System.Text;8using System.Threading.Tasks;9{10 {11 public void Initialize(IDataCollectionSink dataCollectionSink)12 {13 }14 public void HandleDataCollectionAttachmentSets(DataCollectionContext dataCollectionContext, ICollection<AttachmentSet> attachmentSets)15 {16 }17 public void HandleLogMessage(DataCollectionContext dataCollectionContext, string message)18 {19 }20 }21}22using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection;23using Microsoft.VisualStudio.TestPlatform.ObjectModel;24using System;25using System.Collections.Generic;26using System.Diagnostics;27using System.Linq;28using System.Text;29using System.Threading.Tasks;30{31 {32 static void Main(string[] args)33 {34 DataCollectionRequestHandler dataCollectionRequestHandler = new DataCollectionRequestHandler();35 ITestPlatform testPlatform = TestPlatformFactory.GetTestPlatform();36 testPlatform.RunTests(new List<string> { @"C:\Users\user\source\repos\TestProject1\bin\Debug\TestProject1.dll" }, dataCollectionRequestHandler, null);37 }38 }39}40using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection;41using Microsoft.VisualStudio.TestPlatform.ObjectModel;42using System;43using System.Collections.Generic;44using System.Diagnostics;45using System.Linq;46using System.Text;47using System.Threading.Tasks;48{49 {50 static void Main(string[] args)51 {52 DataCollectionRequestHandler dataCollectionRequestHandler = new DataCollectionRequestHandler();

Full Screen

Full Screen

DataCollectionRequestHandler

Using AI Code Generation

copy

Full Screen

1using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection;2using System.Collections.Generic;3{4 {5 public override void Initialize(IDictionary<string, string> environmentVariables)6 {7 }8 public override void SessionStarted()9 {10 }11 public override void SessionEnded()12 {13 }14 public override void TestCaseStarted(TestCaseStartEventArgs testCaseStartEventArgs)15 {16 }17 public override void TestCaseEnded(TestCaseEndEventArgs testCaseEndEventArgs)18 {19 }20 }21}22using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection;23using System.Collections.Generic;24{25 [DataCollectorFriendlyName("MyDataCollector")]26 {27 public override void Initialize(IDictionary<string, string> environmentVariables)28 {29 }30 public override void SessionStarted()31 {32 }33 public override void SessionEnded()34 {35 }36 public override void TestCaseStarted(TestCaseStartEventArgs testCaseStartEventArgs)37 {38 }39 public override void TestCaseEnded(TestCaseEndEventArgs testCaseEndEventArgs)40 {41 }42 }43}

Full Screen

Full Screen

DataCollectionRequestHandler

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection;7using Microsoft.VisualStudio.TestPlatform.ObjectModel;8{9 {10 static void Main(string[] args)11 {12 var dataCollectionRequestHandler = DataCollectionRequestHandler.Instance;13 dataCollectionRequestHandler.InitializeCommunication();14 dataCollectionRequestHandler.BeforeTestRunStart(new System.Collections.ObjectModel.Collection<AttachmentSet>(), true, false, null, null);15 Console.ReadLine();16 }17 }18}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Vstest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful