How to use Dispose method of Microsoft.VisualStudio.TestPlatform.Common.DataCollector.DataCollectionAttachmentManager class

Best Vstest code snippet using Microsoft.VisualStudio.TestPlatform.Common.DataCollector.DataCollectionAttachmentManager.Dispose

DataCollectionManager.cs

Source:DataCollectionManager.cs Github

copy

Full Screen

...167 }168 return executionEnvironmentVariables;169 }170 /// <inheritdoc/>171 public void Dispose()172 {173 this.Dispose(true);174 // Use SupressFinalize in case a subclass175 // of this type implements a finalizer.176 GC.SuppressFinalize(this);177 }178 /// <inheritdoc/>179 public Collection<AttachmentSet> SessionEnded(bool isCancelled = false)180 {181 // Return null if datacollection is not enabled.182 if (!this.isDataCollectionEnabled)183 {184 return new Collection<AttachmentSet>();185 }186 if (isCancelled)187 {188 this.attachmentManager.Cancel();189 return new Collection<AttachmentSet>();190 }191 var endEvent = new SessionEndEventArgs(this.dataCollectionEnvironmentContext.SessionDataCollectionContext);192 this.SendEvent(endEvent);193 var result = new List<AttachmentSet>();194 try195 {196 result = this.attachmentManager.GetAttachments(endEvent.Context);197 }198 catch (Exception ex)199 {200 if (EqtTrace.IsErrorEnabled)201 {202 EqtTrace.Error("DataCollectionManager.SessionEnded: Failed to get attachments : {0}", ex);203 }204 return new Collection<AttachmentSet>(result);205 }206 if (EqtTrace.IsVerboseEnabled)207 {208 this.LogAttachments(result);209 }210 return new Collection<AttachmentSet>(result);211 }212 /// <inheritdoc/>213 public bool SessionStarted()214 {215 // If datacollectors are not configured or datacollection is not enabled, return false.216 if (!this.isDataCollectionEnabled || this.RunDataCollectors.Count == 0)217 {218 return false;219 }220 this.SendEvent(new SessionStartEventArgs(this.dataCollectionEnvironmentContext.SessionDataCollectionContext));221 return this.events.AreTestCaseEventsSubscribed();222 }223 /// <inheritdoc/>224 public void TestCaseStarted(TestCaseStartEventArgs testCaseStartEventArgs)225 {226 if (!this.isDataCollectionEnabled)227 {228 return;229 }230 var context = new DataCollectionContext(this.dataCollectionEnvironmentContext.SessionDataCollectionContext.SessionId, new TestExecId(testCaseStartEventArgs.TestCaseId));231 testCaseStartEventArgs.Context = context;232 this.SendEvent(testCaseStartEventArgs);233 }234 /// <inheritdoc/>235 public Collection<AttachmentSet> TestCaseEnded(TestCaseEndEventArgs testCaseEndEventArgs)236 {237 if (!this.isDataCollectionEnabled)238 {239 return new Collection<AttachmentSet>();240 }241 var context = new DataCollectionContext(this.dataCollectionEnvironmentContext.SessionDataCollectionContext.SessionId, new TestExecId(testCaseEndEventArgs.TestCaseId));242 testCaseEndEventArgs.Context = context;243 this.SendEvent(testCaseEndEventArgs);244 List<AttachmentSet> result = null;245 try246 {247 result = this.attachmentManager.GetAttachments(testCaseEndEventArgs.Context);248 }249 catch (Exception ex)250 {251 if (EqtTrace.IsErrorEnabled)252 {253 EqtTrace.Error("DataCollectionManager.TestCaseEnded: Failed to get attachments : {0}", ex);254 }255 return new Collection<AttachmentSet>(result);256 }257 if (EqtTrace.IsVerboseEnabled)258 {259 this.LogAttachments(result);260 }261 return new Collection<AttachmentSet>(result);262 }263 /// <summary>264 /// The dispose.265 /// </summary>266 /// <param name="disposing">267 /// The disposing.268 /// </param>269 protected virtual void Dispose(bool disposing)270 {271 if (!this.disposed)272 {273 if (disposing)274 {275 }276 this.disposed = true;277 }278 }279 #region Load and Initialize DataCollectors280 /// <summary>281 /// Tries to get uri of the data collector corresponding to the friendly name. If no such data collector exists return null.282 /// </summary>283 /// <param name="friendlyName">The friendly Name.</param>284 /// <param name="dataCollectorUri">The data collector Uri.</param>285 /// <returns><see cref="bool"/></returns>286 protected virtual bool TryGetUriFromFriendlyName(string friendlyName, out string dataCollectorUri)287 {288 var extensionManager = this.dataCollectorExtensionManager;289 foreach (var extension in extensionManager.TestExtensions)290 {291 if (string.Compare(friendlyName, extension.Metadata.FriendlyName, StringComparison.OrdinalIgnoreCase) == 0)292 {293 dataCollectorUri = extension.Metadata.ExtensionUri;294 return true;295 }296 }297 dataCollectorUri = null;298 return false;299 }300 /// <summary>301 /// Gets the extension using uri.302 /// </summary>303 /// <param name="extensionUri">304 /// The extension uri.305 /// </param>306 /// <returns>307 /// The <see cref="DataCollector"/>.308 /// </returns>309 protected virtual DataCollector TryGetTestExtension(string extensionUri)310 {311 return this.DataCollectorExtensionManager.TryGetTestExtension(extensionUri).Value;312 }313 /// <summary>314 /// Loads and initializes data collector using data collector settings.315 /// </summary>316 /// <param name="dataCollectorSettings">317 /// The data collector settings.318 /// </param>319 /// <param name="settingsXml"> runsettings Xml</param>320 private void LoadAndInitialize(DataCollectorSettings dataCollectorSettings, string settingsXml)321 {322 DataCollectorInformation dataCollectorInfo;323 DataCollectorConfig dataCollectorConfig;324 try325 {326 // Look up the extension and initialize it if one is found.327 var extensionManager = this.DataCollectorExtensionManager;328 var dataCollectorUri = string.Empty;329 this.TryGetUriFromFriendlyName(dataCollectorSettings.FriendlyName, out dataCollectorUri);330 DataCollector dataCollector = null;331 if (!string.IsNullOrWhiteSpace(dataCollectorUri))332 {333 dataCollector = this.TryGetTestExtension(dataCollectorUri);334 }335 if (dataCollector == null)336 {337 this.LogWarning(string.Format(CultureInfo.CurrentUICulture, Resources.Resources.DataCollectorNotFound, dataCollectorSettings.FriendlyName));338 return;339 }340 if (this.RunDataCollectors.ContainsKey(dataCollector.GetType()))341 {342 // Collector is already loaded (may be configured twice). Ignore duplicates and return.343 return;344 }345 dataCollectorConfig = new DataCollectorConfig(dataCollector.GetType());346 // Attempt to get the data collector information verifying that all of the required metadata for the collector is available.347 dataCollectorInfo = new DataCollectorInformation(348 dataCollector,349 dataCollectorSettings.Configuration,350 dataCollectorConfig,351 this.dataCollectionEnvironmentContext,352 this.attachmentManager,353 this.events,354 this.messageSink,355 settingsXml);356 }357 catch (Exception ex)358 {359 if (EqtTrace.IsErrorEnabled)360 {361 EqtTrace.Error("DataCollectionManager.LoadAndInitialize: exception while creating data collector {0} : {1}", dataCollectorSettings.FriendlyName, ex);362 }363 // No data collector info, so send the error with no direct association to the collector.364 this.LogWarning(string.Format(CultureInfo.CurrentUICulture, Resources.Resources.DataCollectorInitializationError, dataCollectorSettings.FriendlyName, ex));365 return;366 }367 try368 {369 dataCollectorInfo.InitializeDataCollector();370 lock (this.RunDataCollectors)371 {372 // Add data collectors to run cache.373 this.RunDataCollectors[dataCollectorConfig.DataCollectorType] = dataCollectorInfo;374 }375 }376 catch (Exception ex)377 {378 if (EqtTrace.IsErrorEnabled)379 {380 EqtTrace.Error("DataCollectionManager.LoadAndInitialize: exception while initializing data collector {0} : {1}", dataCollectorSettings.FriendlyName, ex);381 }382 // Log error.383 dataCollectorInfo.Logger.LogError(this.dataCollectionEnvironmentContext.SessionDataCollectionContext, string.Format(CultureInfo.CurrentCulture, Resources.Resources.DataCollectorInitializationError, dataCollectorConfig.FriendlyName, ex.Message));384 // Dispose datacollector.385 dataCollectorInfo.DisposeDataCollector();386 }387 }388 /// <summary>389 /// Finds data collector enabled for the run in data collection settings.390 /// </summary>391 /// <param name="dataCollectionSettings">data collection settings</param>392 /// <returns>List of enabled data collectors</returns>393 private List<DataCollectorSettings> GetDataCollectorsEnabledForRun(DataCollectionRunSettings dataCollectionSettings)394 {395 var runEnabledDataCollectors = new List<DataCollectorSettings>();396 foreach (var settings in dataCollectionSettings.DataCollectorSettingsList)397 {398 if (settings.IsEnabled)399 {400 if (runEnabledDataCollectors.Any(dcSettings => string.Equals(dcSettings.FriendlyName, settings.FriendlyName, StringComparison.OrdinalIgnoreCase)))401 {402 // If Uri or assembly qualified type name is repeated, consider data collector as duplicate and ignore it.403 this.LogWarning(string.Format(CultureInfo.CurrentUICulture, Resources.Resources.IgnoredDuplicateConfiguration, settings.FriendlyName));404 continue;405 }406 runEnabledDataCollectors.Add(settings);407 }408 }409 return runEnabledDataCollectors;410 }411 #endregion412 /// <summary>413 /// Sends a warning message against the session which is not associated with a data collector.414 /// </summary>415 /// <remarks>416 /// This should only be used when we do not have the data collector info yet. After we have the data417 /// collector info we can use the data collectors logger for errors.418 /// </remarks>419 /// <param name="warningMessage">The message to be logged.</param>420 private void LogWarning(string warningMessage)421 {422 this.messageSink.SendMessage(new DataCollectionMessageEventArgs(TestMessageLevel.Warning, warningMessage));423 }424 /// <summary>425 /// Sends the event to all data collectors and fires a callback on the sender, letting it426 /// know when all plugins have completed processing the event427 /// </summary>428 /// <param name="args">The context information for the event</param>429 private void SendEvent(DataCollectionEventArgs args)430 {431 ValidateArg.NotNull(args, nameof(args));432 if (!this.isDataCollectionEnabled)433 {434 if (EqtTrace.IsErrorEnabled)435 {436 EqtTrace.Error("DataCollectionManger:SendEvent: SendEvent called when no collection is enabled.");437 }438 return;439 }440 // do not send events multiple times441 this.events.RaiseEvent(args);442 }443 /// <summary>444 /// The get environment variables.445 /// </summary>446 /// <param name="unloadedAnyCollector">447 /// The unloaded any collector.448 /// </param>449 /// <returns>450 /// Dictionary of variable name as key and collector requested environment variable as value.451 /// </returns>452 private Dictionary<string, DataCollectionEnvironmentVariable> GetEnvironmentVariables(out bool unloadedAnyCollector)453 {454 var failedCollectors = new List<DataCollectorInformation>();455 unloadedAnyCollector = false;456 var dataCollectorEnvironmentVariable = new Dictionary<string, DataCollectionEnvironmentVariable>(StringComparer.OrdinalIgnoreCase);457 foreach (var dataCollectorInfo in this.RunDataCollectors.Values)458 {459 try460 {461 dataCollectorInfo.SetTestExecutionEnvironmentVariables();462 this.AddCollectorEnvironmentVariables(dataCollectorInfo, dataCollectorEnvironmentVariable);463 }464 catch (Exception ex)465 {466 unloadedAnyCollector = true;467 var friendlyName = dataCollectorInfo.DataCollectorConfig.FriendlyName;468 failedCollectors.Add(dataCollectorInfo);469 dataCollectorInfo.Logger.LogError(470 this.dataCollectionEnvironmentContext.SessionDataCollectionContext,471 string.Format(CultureInfo.CurrentCulture, Resources.Resources.DataCollectorErrorOnGetVariable, friendlyName, ex));472 if (EqtTrace.IsErrorEnabled)473 {474 EqtTrace.Error("DataCollectionManager.GetEnvironmentVariables: Failed to get variable for Collector '{0}': {1}", friendlyName, ex);475 }476 }477 }478 this.RemoveDataCollectors(failedCollectors);479 return dataCollectorEnvironmentVariable;480 }481 /// <summary>482 /// Collects environment variable to be set in test process by avoiding duplicates483 /// and detecting override of variable value by multiple adapters.484 /// </summary>485 /// <param name="dataCollectionWrapper">486 /// The data Collection Wrapper.487 /// </param>488 /// <param name="dataCollectorEnvironmentVariables">489 /// Environment variables required for already loaded plugin.490 /// </param>491 private void AddCollectorEnvironmentVariables(492 DataCollectorInformation dataCollectionWrapper,493 Dictionary<string, DataCollectionEnvironmentVariable> dataCollectorEnvironmentVariables)494 {495 if (dataCollectionWrapper.TestExecutionEnvironmentVariables != null)496 {497 var collectorFriendlyName = dataCollectionWrapper.DataCollectorConfig.FriendlyName;498 foreach (var namevaluepair in dataCollectionWrapper.TestExecutionEnvironmentVariables)499 {500 DataCollectionEnvironmentVariable alreadyRequestedVariable;501 if (dataCollectorEnvironmentVariables.TryGetValue(namevaluepair.Key, out alreadyRequestedVariable))502 {503 // Dev10 behavior is to consider environment variables values as case sensitive.504 if (string.Equals(namevaluepair.Value, alreadyRequestedVariable.Value, StringComparison.Ordinal))505 {506 alreadyRequestedVariable.AddRequestingDataCollector(collectorFriendlyName);507 }508 else509 {510 // Data collector is overriding an already requested variable, possibly an error. 511 dataCollectionWrapper.Logger.LogError(512 this.dataCollectionEnvironmentContext.SessionDataCollectionContext,513 string.Format(514 CultureInfo.CurrentUICulture,515 Resources.Resources.DataCollectorRequestedDuplicateEnvironmentVariable,516 collectorFriendlyName,517 namevaluepair.Key,518 namevaluepair.Value,519 alreadyRequestedVariable.FirstDataCollectorThatRequested,520 alreadyRequestedVariable.Value));521 }522 }523 else524 {525 if (EqtTrace.IsVerboseEnabled)526 {527 // new variable, add to the list.528 EqtTrace.Verbose("DataCollectionManager.AddCollectionEnvironmentVariables: Adding Environment variable '{0}' value '{1}'", namevaluepair.Key, namevaluepair.Value);529 }530 dataCollectorEnvironmentVariables.Add(531 namevaluepair.Key,532 new DataCollectionEnvironmentVariable(namevaluepair, collectorFriendlyName));533 }534 }535 }536 }537 /// <summary>538 /// The remove data collectors.539 /// </summary>540 /// <param name="dataCollectorsToRemove">541 /// The data collectors to remove.542 /// </param>543 private void RemoveDataCollectors(IReadOnlyCollection<DataCollectorInformation> dataCollectorsToRemove)544 {545 if (dataCollectorsToRemove == null || !dataCollectorsToRemove.Any())546 {547 return;548 }549 lock (this.RunDataCollectors)550 {551 foreach (var dataCollectorToRemove in dataCollectorsToRemove)552 {553 dataCollectorToRemove.DisposeDataCollector();554 this.RunDataCollectors.Remove(dataCollectorToRemove.DataCollector.GetType());555 }556 if (this.RunDataCollectors.Count == 0)557 {558 this.isDataCollectionEnabled = false;559 }560 }561 }562 private void LogAttachments(List<AttachmentSet> attachmentSets)563 {564 foreach (var entry in attachmentSets)565 {566 foreach (var file in entry.Attachments)567 {...

Full Screen

Full Screen

DataCollectionAttachmentManager.cs

Source:DataCollectionAttachmentManager.cs Github

copy

Full Screen

...308 args.TestCaseId = testCaseId;309 }310 _messageSink?.SendMessage(args);311 }312 public void Dispose()313 {314 Dispose(disposing: true);315 GC.SuppressFinalize(this);316 }317 protected virtual void Dispose(bool disposing)318 {319 if (disposing)320 {321 _cancellationTokenSource.Dispose();322 }323 }324}...

Full Screen

Full Screen

Dispose

Using AI Code Generation

copy

Full Screen

1using Microsoft.VisualStudio.TestPlatform.Common.DataCollector;2DataCollectionAttachmentManager attachmentManager = new DataCollectionAttachmentManager();3attachmentManager.Dispose();4using Microsoft.VisualStudio.TestPlatform.Common.DataCollector;5DataCollectionEvents events = new DataCollectionEvents();6events.Dispose();7using Microsoft.VisualStudio.TestPlatform.Common.DataCollector;8DataCollectionSink sink = new DataCollectionSink();9sink.Dispose();10using Microsoft.VisualStudio.TestPlatform.Common.DataCollector;11DataCollectionTestCase testCase = new DataCollectionTestCase();12testCase.Dispose();13using Microsoft.VisualStudio.TestPlatform.Common.DataCollector;14DataCollectionTestCase testCase = new DataCollectionTestCase();15testCase.Dispose();16using Microsoft.VisualStudio.TestPlatform.Common.DataCollector;17DataCollectionTestCase testCase = new DataCollectionTestCase();18testCase.Dispose();19using Microsoft.VisualStudio.TestPlatform.Common.DataCollector;20DataCollectionTestCase testCase = new DataCollectionTestCase();21testCase.Dispose();22using Microsoft.VisualStudio.TestPlatform.Common.DataCollector;23DataCollectionTestCase testCase = new DataCollectionTestCase();24testCase.Dispose();25using Microsoft.VisualStudio.TestPlatform.Common.DataCollector;26DataCollectionTestCase testCase = new DataCollectionTestCase();27testCase.Dispose();28using Microsoft.VisualStudio.TestPlatform.Common.DataCollector;29DataCollectionTestCase testCase = new DataCollectionTestCase();30testCase.Dispose();31using Microsoft.VisualStudio.TestPlatform.Common.DataCollector;

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