How to use TestRuntimeProviderInfo class of Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client package

Best Vstest code snippet using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.TestRuntimeProviderInfo

ProxyTestSessionManager.cs

Source:ProxyTestSessionManager.cs Github

copy

Full Screen

...32 private volatile bool _proxySetupFailed;33 private readonly StartTestSessionCriteria _testSessionCriteria;34 private readonly int _maxTesthostCount;35 private TestSessionInfo? _testSessionInfo;36 private readonly Func<TestRuntimeProviderInfo, ProxyOperationManager?> _proxyCreator;37 private readonly List<TestRuntimeProviderInfo> _runtimeProviders;38 private readonly IList<ProxyOperationManagerContainer> _proxyContainerList;39 private readonly IDictionary<string, int> _proxyMap;40 private readonly Stopwatch _testSessionStopwatch;41 private readonly Dictionary<string, TestRuntimeProviderInfo> _sourceToRuntimeProviderInfoMap;42 private Dictionary<string, string?> _testSessionEnvironmentVariables = new();43 private IDictionary<string, string?> TestSessionEnvironmentVariables44 {45 get46 {47 if (_testSessionEnvironmentVariables.Count == 0)48 {49 _testSessionEnvironmentVariables = InferRunSettingsHelper.GetEnvironmentVariables(_testSessionCriteria.RunSettings)50 ?? _testSessionEnvironmentVariables;51 }52 return _testSessionEnvironmentVariables;53 }54 }55 /// <summary>56 /// Initializes a new instance of the <see cref="ProxyTestSessionManager"/> class.57 /// </summary>58 ///59 /// <param name="criteria">The test session criteria.</param>60 /// <param name="maxTesthostCount">The testhost count.</param>61 /// <param name="proxyCreator">The proxy creator.</param>62 public ProxyTestSessionManager(63 StartTestSessionCriteria criteria,64 int maxTesthostCount,65 Func<TestRuntimeProviderInfo, ProxyOperationManager?> proxyCreator,66 List<TestRuntimeProviderInfo> runtimeProviders)67 {68 _testSessionCriteria = criteria;69 _maxTesthostCount = maxTesthostCount;70 _proxyCreator = proxyCreator;71 _runtimeProviders = runtimeProviders;72 _proxyContainerList = new List<ProxyOperationManagerContainer>();73 _proxyMap = new Dictionary<string, int>();74 _testSessionStopwatch = new Stopwatch();75 // Get dictionary from source -> runtimeProviderInfo, that has the type of runtime provider to create for this76 // source, and updated runsettings.77 _sourceToRuntimeProviderInfoMap = _runtimeProviders78 .SelectMany(runtimeProviderInfo => runtimeProviderInfo.SourceDetails.Select(detail => new KeyValuePair<string, TestRuntimeProviderInfo>(detail.Source!, runtimeProviderInfo)))79 .ToDictionary(pair => pair.Key, pair => pair.Value);80 }81 // NOTE: The method is virtual for mocking purposes.82 /// <inheritdoc/>83 public virtual bool StartSession(ITestSessionEventsHandler eventsHandler, IRequestData requestData)84 {85 lock (_lockObject)86 {87 if (_testSessionInfo != null)88 {89 return false;90 }91 _testSessionInfo = new TestSessionInfo();92 }93 var stopwatch = new Stopwatch();94 stopwatch.Start();95 // TODO: Right now we either pre-create 1 testhost if parallel is disabled, or we pre-create as many96 // testhosts as we have sources. In the future we will have a maxParallelLevel set to the actual parallel level97 // (which might be lower than the number of sources) and we should do some kind of thinking here to figure out how to split the sources.98 // To follow the way parallel execution and discovery is (supposed to be) working, there should be as many testhosts99 // as the maxParallel level pre-started, and marked with the Shared, and configuration that they can run.100 // Create all the proxies in parallel, one task per proxy.101 var taskList = new Task[_maxTesthostCount];102 for (int i = 0; i < taskList.Length; ++i)103 {104 // This is similar to what we do in ProxyExecutionManager, and ProxyDiscoveryManager, we split105 // up the payload into multiple smaller pieces. Here it is one source per proxy.106 TPDebug.Assert(_testSessionCriteria.Sources is not null, "_testSessionCriteria.Sources is null");107 var source = _testSessionCriteria.Sources[i];108 var sources = new List<string>() { source };109 var runtimeProviderInfo = _sourceToRuntimeProviderInfoMap[source];110 taskList[i] = Task.Factory.StartNew(() =>111 {112 var proxySetupSucceeded = SetupRawProxy(sources, runtimeProviderInfo);113 if (!proxySetupSucceeded)114 {115 // Set this only in the failed case, so we can check if any proxy failed to setup.116 _proxySetupFailed = true;117 }118 });119 }120 // Wait for proxy creation to be over.121 Task.WaitAll(taskList);122 stopwatch.Stop();123 // Collecting session metrics.124 requestData?.MetricsCollection.Add(125 TelemetryDataConstants.TestSessionId,126 _testSessionInfo.Id);127 requestData?.MetricsCollection.Add(128 TelemetryDataConstants.TestSessionSpawnedTesthostCount,129 _proxyContainerList.Count);130 requestData?.MetricsCollection.Add(131 TelemetryDataConstants.TestSessionTesthostSpawnTimeInSec,132 stopwatch.Elapsed.TotalSeconds);133 // Dispose of all proxies if even one of them failed during setup.134 if (_proxySetupFailed)135 {136 requestData?.MetricsCollection.Add(137 TelemetryDataConstants.TestSessionState,138 TestSessionState.Error.ToString());139 DisposeProxies();140 return false;141 }142 // Make the session available.143 if (!TestSessionPool.Instance.AddSession(_testSessionInfo, this))144 {145 requestData?.MetricsCollection.Add(146 TelemetryDataConstants.TestSessionState,147 TestSessionState.Error.ToString());148 DisposeProxies();149 return false;150 }151 requestData?.MetricsCollection.Add(152 TelemetryDataConstants.TestSessionState,153 TestSessionState.Active.ToString());154 // This counts as the session start time.155 _testSessionStopwatch.Start();156 // Let the caller know the session has been created.157 eventsHandler.HandleStartTestSessionComplete(158 new()159 {160 TestSessionInfo = _testSessionInfo,161 Metrics = requestData?.MetricsCollection.Metrics162 });163 return true;164 }165 // NOTE: The method is virtual for mocking purposes.166 /// <inheritdoc/>167 public virtual bool StopSession(IRequestData requestData)168 {169 var testSessionId = string.Empty;170 lock (_lockObject)171 {172 if (_testSessionInfo == null)173 {174 return false;175 }176 testSessionId = _testSessionInfo.Id.ToString();177 _testSessionInfo = null;178 }179 // Dispose of the pooled testhosts.180 DisposeProxies();181 // Compute session time.182 _testSessionStopwatch.Stop();183 // Collecting session metrics.184 requestData?.MetricsCollection.Add(185 TelemetryDataConstants.TestSessionId,186 testSessionId);187 requestData?.MetricsCollection.Add(188 TelemetryDataConstants.TestSessionTotalSessionTimeInSec,189 _testSessionStopwatch.Elapsed.TotalSeconds);190 requestData?.MetricsCollection.Add(191 TelemetryDataConstants.TestSessionState,192 TestSessionState.Terminated.ToString());193 return true;194 }195 /// <summary>196 /// Dequeues a proxy to be used either by discovery or execution.197 /// </summary>198 ///199 /// <param name="source">The source to be associated to this proxy.</param>200 /// <param name="runSettings">The run settings.</param>201 ///202 /// <returns>The dequeued proxy.</returns>203 public virtual ProxyOperationManager DequeueProxy(string source, string? runSettings)204 {205 ProxyOperationManagerContainer? proxyContainer = null;206 lock (_proxyOperationLockObject)207 {208 // No proxy available means the caller will have to create its own proxy.209 if (!_proxyMap.ContainsKey(source)210 || !_proxyContainerList[_proxyMap[source]].IsAvailable)211 {212 throw new InvalidOperationException(CrossPlatResources.NoAvailableProxyForDeque);213 }214 // We must ensure the current run settings match the run settings from when the215 // testhost was started. If not, throw an exception to force the caller to create216 // its own proxy instead.217 if (!CheckRunSettingsAreCompatible(runSettings))218 {219 EqtTrace.Verbose($"ProxyTestSessionManager.DequeueProxy: A proxy exists, but the runsettings do not match. Skipping it. Incoming settings: {runSettings}, Settings on proxy: {_testSessionCriteria.RunSettings}");220 throw new InvalidOperationException(CrossPlatResources.NoProxyMatchesDescription);221 }222 // Get the actual proxy.223 proxyContainer = _proxyContainerList[_proxyMap[source]];224 // Mark the proxy as unavailable.225 proxyContainer.IsAvailable = false;226 }227 return proxyContainer.Proxy;228 }229 /// <summary>230 /// Enqueues a proxy back once discovery or executions is done with it.231 /// </summary>232 ///233 /// <param name="proxyId">The id of the proxy to be re-enqueued.</param>234 ///235 /// <returns>True if the operation succeeded, false otherwise.</returns>236 public virtual bool EnqueueProxy(int proxyId)237 {238 lock (_proxyOperationLockObject)239 {240 // Check if the proxy exists.241 if (proxyId < 0 || proxyId >= _proxyContainerList.Count)242 {243 throw new ArgumentException(244 string.Format(245 CultureInfo.CurrentCulture,246 CrossPlatResources.NoSuchProxyId,247 proxyId));248 }249 // Get the actual proxy.250 var proxyContainer = _proxyContainerList[proxyId];251 if (proxyContainer.IsAvailable)252 {253 throw new InvalidOperationException(254 string.Format(255 CultureInfo.CurrentCulture,256 CrossPlatResources.ProxyIsAlreadyAvailable,257 proxyId));258 }259 // Mark the proxy as available.260 proxyContainer.IsAvailable = true;261 }262 return true;263 }264 private int EnqueueNewProxy(265 IList<string> sources,266 ProxyOperationManagerContainer operationManagerContainer)267 {268 lock (_proxyOperationLockObject)269 {270 var index = _proxyContainerList.Count;271 // Add the proxy container to the proxy container list.272 _proxyContainerList.Add(operationManagerContainer);273 foreach (var source in sources)274 {275 // Add the proxy index to the map.276 _proxyMap.Add(277 source,278 index);279 }280 return index;281 }282 }283 private bool SetupRawProxy(284 IList<string> sources,285 TestRuntimeProviderInfo runtimeProviderInfo)286 {287 try288 {289 // Create and cache the proxy.290 var operationManagerProxy = _proxyCreator(runtimeProviderInfo);291 if (operationManagerProxy == null)292 {293 return false;294 }295 // Initialize the proxy.296 operationManagerProxy.Initialize(skipDefaultAdapters: false);297 // Start the test host associated to the proxy.298 if (!operationManagerProxy.SetupChannel(sources, runtimeProviderInfo.RunSettings))299 {...

Full Screen

Full Screen

TestSessionPoolTests.cs

Source:TestSessionPoolTests.cs Github

copy

Full Screen

...20 var proxyTestSessionManager = new ProxyTestSessionManager(21 new StartTestSessionCriteria(),22 1,23 _ => null,24 new List<TestRuntimeProviderInfo>());25 Assert.IsNotNull(TestSessionPool.Instance);26 Assert.IsTrue(TestSessionPool.Instance.AddSession(testSessionInfo, proxyTestSessionManager));27 Assert.IsFalse(TestSessionPool.Instance.AddSession(testSessionInfo, proxyTestSessionManager));28 }29 [TestMethod]30 public void KillSessionShouldSucceedIfTestSessionExists()31 {32 TestSessionPool.Instance = null;33 var testSessionInfo = new TestSessionInfo();34 var mockProxyTestSessionManager = new Mock<ProxyTestSessionManager>(35 new StartTestSessionCriteria(),36 1,37 (Func<TestRuntimeProviderInfo, ProxyOperationManager>)(_ => null!),38 new List<TestRuntimeProviderInfo>());39 var mockRequestData = new Mock<IRequestData>();40 mockProxyTestSessionManager.SetupSequence(tsm => tsm.StopSession(It.IsAny<IRequestData>()))41 .Returns(true)42 .Returns(false);43 Assert.IsNotNull(TestSessionPool.Instance);44 Assert.IsFalse(TestSessionPool.Instance.KillSession(testSessionInfo, mockRequestData.Object));45 mockProxyTestSessionManager.Verify(tsm => tsm.StopSession(It.IsAny<IRequestData>()), Times.Never);46 Assert.IsTrue(TestSessionPool.Instance.AddSession(testSessionInfo, mockProxyTestSessionManager.Object));47 Assert.IsTrue(TestSessionPool.Instance.KillSession(testSessionInfo, mockRequestData.Object));48 mockProxyTestSessionManager.Verify(tsm => tsm.StopSession(mockRequestData.Object), Times.Once);49 Assert.IsTrue(TestSessionPool.Instance.AddSession(testSessionInfo, mockProxyTestSessionManager.Object));50 Assert.IsFalse(TestSessionPool.Instance.KillSession(testSessionInfo, mockRequestData.Object));51 mockProxyTestSessionManager.Verify(tsm => tsm.StopSession(mockRequestData.Object), Times.Exactly(2));52 }53 [TestMethod]54 public void TakeProxyShouldSucceedIfMatchingCriteriaAreCorrect()55 {56 TestSessionPool.Instance = null;57 var testSessionInfo = new TestSessionInfo();58 var mockRequestData = new Mock<IRequestData>();59 var mockProxyTestSessionManager = new Mock<ProxyTestSessionManager>(60 new StartTestSessionCriteria(),61 1,62 (Func<TestRuntimeProviderInfo, ProxyOperationManager>)(_ => null!),63 new List<TestRuntimeProviderInfo>());64 mockProxyTestSessionManager.SetupSequence(tsm => tsm.DequeueProxy(It.IsAny<string>(), It.IsAny<string>()))65 .Throws(new InvalidOperationException("Test Exception"))66 .Returns(new ProxyOperationManager(null, null!, null!, Framework.DefaultFramework));67 Assert.IsNotNull(TestSessionPool.Instance);68 // Take proxy fails because test session is invalid.69 Assert.IsNull(TestSessionPool.Instance.TryTakeProxy(new TestSessionInfo(), string.Empty, string.Empty, mockRequestData.Object));70 mockProxyTestSessionManager.Verify(tsm => tsm.DequeueProxy(It.IsAny<string>(), It.IsAny<string>()), Times.Never);71 Assert.IsTrue(TestSessionPool.Instance.AddSession(testSessionInfo, mockProxyTestSessionManager.Object));72 // First TakeProxy fails because of throwing, see setup sequence.73 Assert.IsNull(TestSessionPool.Instance.TryTakeProxy(testSessionInfo, string.Empty, string.Empty, mockRequestData.Object));74 mockProxyTestSessionManager.Verify(tsm => tsm.DequeueProxy(It.IsAny<string>(), It.IsAny<string>()), Times.Once);75 // Second TakeProxy succeeds, see setup sequence.76 Assert.IsNotNull(TestSessionPool.Instance.TryTakeProxy(testSessionInfo, string.Empty, string.Empty, mockRequestData.Object));77 mockProxyTestSessionManager.Verify(tsm => tsm.DequeueProxy(It.IsAny<string>(), It.IsAny<string>()), Times.Exactly(2));78 }79 [TestMethod]80 public void ReturnProxyShouldSucceedIfProxyIdIsValid()81 {82 TestSessionPool.Instance = null;83 var testSessionInfo = new TestSessionInfo();84 var mockProxyTestSessionManager = new Mock<ProxyTestSessionManager>(85 new StartTestSessionCriteria(),86 1,87 (Func<TestRuntimeProviderInfo, ProxyOperationManager>)(_ => null!),88 new List<TestRuntimeProviderInfo>());89 mockProxyTestSessionManager.SetupSequence(tsm => tsm.EnqueueProxy(It.IsAny<int>()))90 .Throws(new ArgumentException("Test Exception"))91 .Throws(new InvalidOperationException("Test Exception"))92 .Returns(true);93 Assert.IsNotNull(TestSessionPool.Instance);94 Assert.IsFalse(TestSessionPool.Instance.ReturnProxy(new TestSessionInfo(), 0));95 mockProxyTestSessionManager.Verify(tsm => tsm.EnqueueProxy(It.IsAny<int>()), Times.Never);96 Assert.IsTrue(TestSessionPool.Instance.AddSession(testSessionInfo, mockProxyTestSessionManager.Object));97 // Simulates proxy id not found (see setup sequence).98 Assert.ThrowsException<ArgumentException>(() => TestSessionPool.Instance.ReturnProxy(testSessionInfo, 0));99 mockProxyTestSessionManager.Verify(tsm => tsm.EnqueueProxy(It.IsAny<int>()), Times.Once);100 // Simulates proxy already available (see setup sequence).101 Assert.IsFalse(TestSessionPool.Instance.ReturnProxy(testSessionInfo, 0));102 mockProxyTestSessionManager.Verify(tsm => tsm.EnqueueProxy(It.IsAny<int>()), Times.Exactly(2));...

Full Screen

Full Screen

TestRuntimeProviderInfo.cs

Source:TestRuntimeProviderInfo.cs Github

copy

Full Screen

...3using System;4using System.Collections.Generic;5using Microsoft.VisualStudio.TestPlatform.ObjectModel;6namespace Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client;7public class TestRuntimeProviderInfo8{9 public Type? Type { get; }10 public bool Shared { get; }11 public string? RunSettings { get; }12 public List<SourceDetail> SourceDetails { get; }13 public TestRuntimeProviderInfo(Type? type, bool shared, string? runSettings, List<SourceDetail> sourceDetails)14 {15 Type = type;16 Shared = shared;17 RunSettings = runSettings;18 SourceDetails = sourceDetails;19 }20}...

Full Screen

Full Screen

TestRuntimeProviderInfo

Using AI Code Generation

copy

Full Screen

1using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client;2using Microsoft.VisualStudio.TestPlatform.ObjectModel;3using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;4using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine;5using System;6using System.Collections.Generic;7using System.Linq;8using System.Text;9using System.Threading.Tasks;10{11 {12 static void Main(string[] args)13 {14 var testRuntimeProviderInfo = new TestRuntimeProviderInfo("TestRuntimeProvider", new Version(1, 0), "TestRuntimeProviderPath");15 var testRuntimeProviderExtension = new TestRuntimeProviderExtension(testRuntimeProviderInfo, "TestRuntimeProviderPath");16 var testRuntimeProviderExtensions = new List<ITestRuntimeProviderExtension>();17 testRuntimeProviderExtensions.Add(testRuntimeProviderExtension);18 var testRuntimeProviderExtensionManager = new TestRuntimeProviderExtensionManager(testRuntimeProviderExtensions);19 var testHostManager = TestHostManagerFactory.GetTestHostManager(testRuntimeProviderExtensionManager, "TestRuntimeProvider");20 var testHostLauncher = testHostManager.GetTestHostLauncher();21 var testHostLauncherInfo = new TestHostLauncherInfo(testHostLauncher, "TestRuntimeProvider");22 var testHostLauncherInfoList = new List<ITestHostLauncherInfo>();23 testHostLauncherInfoList.Add(testHostLauncherInfo);24 var testHostLauncherInfoManager = new TestHostLauncherInfoManager(testHostLauncherInfoList);25 var testHostLauncherFactory = new TestHostLauncherFactory(testHostLauncherInfoManager);26 var testHostLauncher = testHostLauncherFactory.GetTestHostLauncher("TestRuntimeProvider");27 var processId = testHostLauncher.LaunchTestHost("TestHostPath", "TestHostArgs");28 }29 }30}31using Microsoft.VisualStudio.TestPlatform.ObjectModel;32using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;33using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine;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 var testRuntimeProviderInfo = new TestRuntimeProviderInfo("TestRuntimeProvider", new Version(1, 0), "TestRuntimeProviderPath");44 var testRuntimeProviderExtension = new TestRuntimeProviderExtension(testRuntimeProviderInfo, "TestRuntimeProviderPath");

Full Screen

Full Screen

TestRuntimeProviderInfo

Using AI Code Generation

copy

Full Screen

1using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client;2using Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection;3using Microsoft.VisualStudio.TestPlatform.ObjectModel;4using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;5using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection;6using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine;7using System;8using System.Collections.Generic;9using System.Linq;10using System.Reflection;11using System.Text;12using System.Threading.Tasks;13{14 {15 static void Main(string[] args)16 {17 var testHostManager = new TestHostManager();18 var testHostManagerFactory = new TestHostManagerFactory();19 testHostManagerFactory.RegisterTestHostManager(TestHostManager.TestHostManagerName, testHostManager);20 var testHostManagerInfo = new TestHostManagerInfo(TestHostManager.TestHostManagerName, testHostManagerFactory);21 var testHostManagerInfoProvider = new TestHostManagerInfoProvider();22 testHostManagerInfoProvider.RegisterTestHostManagerInfo(testHostManagerInfo);23 var testRuntimeProviderInfo = new TestRuntimeProviderInfo(TestHostManager.TestHostManagerName, typeof(Program).GetTypeInfo().Assembly.Location);24 var testRuntimeProviderInfoProvider = new TestRuntimeProviderInfoProvider();25 testRuntimeProviderInfoProvider.RegisterTestRuntimeProviderInfo(testRuntimeProviderInfo);26 var dataCollectionManager = new DataCollectionManager();27 var dataCollectionManagerFactory = new DataCollectionManagerFactory();28 dataCollectionManagerFactory.RegisterDataCollectionManager(DataCollectionManager.DataCollectionManagerName, dataCollectionManager);29 var dataCollectionManagerInfo = new DataCollectionManagerInfo(DataCollectionManager.DataCollectionManagerName, dataCollectionManagerFactory);30 var dataCollectionManagerInfoProvider = new DataCollectionManagerInfoProvider();31 dataCollectionManagerInfoProvider.RegisterDataCollectionManagerInfo(dataCollectionManagerInfo);32 var dataCollectionController = new DataCollectionController();33 var dataCollectionControllerFactory = new DataCollectionControllerFactory();34 dataCollectionControllerFactory.RegisterDataCollectionController(DataCollectionController.DataCollectionControllerName, dataCollectionController);35 var dataCollectionControllerInfo = new DataCollectionControllerInfo(DataCollectionController.DataCollectionControllerName, dataCollectionControllerFactory);36 var dataCollectionControllerInfoProvider = new DataCollectionControllerInfoProvider();37 dataCollectionControllerInfoProvider.RegisterDataCollectionControllerInfo(dataCollectionControllerInfo);38 var dataCollectionAttachmentManager = new DataCollectionAttachmentManager();39 var dataCollectionAttachmentManagerFactory = new DataCollectionAttachmentManagerFactory();

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.

Most used methods in TestRuntimeProviderInfo

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful