Skip to content

Commit

Permalink
Clear extensions cache after discover/execution complete. (#853)
Browse files Browse the repository at this point in the history
* Clear cache after discover/execution complete.

* Sort usings

* Fix for issue:
1) #632
2) #844

* nitpick: Spelling

* Addressed PR comment

* spelling correction

* nitpick: function name

* Removed unused usings
  • Loading branch information
Faizan2304 committed Jun 15, 2017
1 parent f59a108 commit 0eef86b
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 14 deletions.
12 changes: 5 additions & 7 deletions src/Microsoft.TestPlatform.Client/Execution/TestRunRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@

namespace Microsoft.VisualStudio.TestPlatform.Client.Execution
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;

using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Engine;
using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging;
using Microsoft.VisualStudio.TestPlatform.Utilities;

using ClientResources = Microsoft.VisualStudio.TestPlatform.Client.Resources.Resources;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Threading;
using ClientResources = Microsoft.VisualStudio.TestPlatform.Client.Resources.Resources;

public class TestRunRequest : ITestRunRequest, ITestRunEventsHandler
{
Expand Down
8 changes: 8 additions & 0 deletions src/Microsoft.TestPlatform.Client/TestPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ public void UpdateExtensions(IEnumerable<string> pathToAdditionalExtensions, boo
.UseAdditionalExtensions(pathToAdditionalExtensions, loadOnlyWellKnownExtensions);
}

/// <summary>
/// Clear test plugin cache
/// </summary>
public void ClearExtensions()
{
TestPluginCache.Instance.ClearExtensions();
}

/// <summary>
/// Update the test adapter paths provided through run settings to be used by the test service
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,20 @@ public void UpdateExtensions(IEnumerable<string> additionalExtensionsPath, bool
}
}

/// <summary>
/// Clear test plugin cache
/// </summary>
public void ClearExtensions()
{
this.pathToExtensions = null;
this.TestExtensions?.InvalidateCache();

if (EqtTrace.IsVerboseEnabled)
{
EqtTrace.Verbose("TestPluginCache: Clearing test plugin cache");
}
}

#endregion

#region Utility methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public interface ITestPlatform : IDisposable
/// <param name="loadOnlyWellKnownExtensions">Specifies whether only well known extensions should be loaded.</param>
void UpdateExtensions(IEnumerable<string> pathToAdditionalExtensions, bool loadOnlyWellKnownExtensions);

/// <summary>
/// Clear the extension
/// </summary>
void ClearExtensions();

/// <summary>
/// Creates a discovery request
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions src/vstest.console/TestPlatformHelpers/TestRequestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ ex is SettingsException ||
{
this.testLoggerManager?.UnregisterDiscoveryEvents(discoveryRequest);
discoveryEventsRegistrar?.UnregisterDiscoveryEvents(discoveryRequest);

// Clear the extensions once request is complete, so that new request will not use extension from last request.
this.testPlatform.ClearExtensions();
}
}

Expand Down Expand Up @@ -340,6 +343,9 @@ ex is SettingsException ||
this.testLoggerManager.UnregisterTestRunEvents(testRunRequest);
this.testRunResultAggregator.UnregisterTestRunEvents(testRunRequest);
testRunEventsRegistrar?.UnregisterTestRunEvents(testRunRequest);

// Clear the extensions once request is complete, so that new request will not use extension from last request.
this.testPlatform.ClearExtensions();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,46 @@ public void UpdateAdditionalExtensionsShouldUpdatePathsThatDoNotExist()
Assert.AreEqual(1, updatedExtensions.Count());
}

[Ignore]
[TestMethod]
public void UpdateAdditionalExtensionsShouldResetExtensionsDiscoveredFlag()
{
SetupMockAdditionalPathExtensions();

TestPluginCache.Instance.DiscoverTestExtensions<TestDiscovererPluginInformation, ITestDiscoverer>(TestPlatformConstants.TestAdapterRegexPattern);

Assert.IsTrue(TestPluginCache.Instance.TestExtensions.AreTestDiscoverersCached);

// update extensions
var additionalExtensions = new List<string> { "foo.dll" };
TestPluginCache.Instance.UpdateExtensions(additionalExtensions, true);

Assert.IsFalse(TestPluginCache.Instance.TestExtensions.AreTestDiscoverersCached);
}

[TestMethod]
public void ClearExtensionsShouldClearExtensionPath()
{
var additionalExtensions = new List<string> { "foo.dll" };
TestPluginCache.Instance.UpdateExtensions(additionalExtensions, true);

TestPluginCache.Instance.ClearExtensions();

Assert.IsNull(TestPluginCache.Instance.PathToExtensions);
}

[TestMethod]
public void ClearExtensionsShouldClearTestExtensionsCache()
{
SetupMockAdditionalPathExtensions();

TestPluginCache.Instance.DiscoverTestExtensions<TestDiscovererPluginInformation, ITestDiscoverer>(TestPlatformConstants.TestAdapterRegexPattern);

Assert.IsTrue(TestPluginCache.Instance.TestExtensions.AreTestDiscoverersCached);

// Clear cache
TestPluginCache.Instance.ClearExtensions();

Assert.IsFalse(TestPluginCache.Instance.TestExtensions.AreTestDiscoverersCached);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,23 @@ public void DiscoverTestsShouldReadTheBatchSizeFromSettingsAndSetItForDiscoveryC
Assert.AreEqual(15, actualDiscoveryCriteria.FrequencyOfDiscoveredTestsEvent);
}

[TestMethod]
public void DiscoverTestsShouldClearCacheAfterDiscovery()
{
var payload = new DiscoveryRequestPayload()
{
Sources = new List<string>() { "a" },
};

var mockDiscoveryRequest = new Mock<IDiscoveryRequest>();
this.mockTestPlatform.Setup(mt => mt.CreateDiscoveryRequest(It.IsAny<DiscoveryCriteria>(), It.IsAny<ProtocolConfig>())).Callback(
(DiscoveryCriteria discoveryCriteria, ProtocolConfig config) => { }).Returns(mockDiscoveryRequest.Object);

var success = this.testRequestManager.DiscoverTests(payload, new Mock<ITestDiscoveryEventsRegistrar>().Object, It.IsAny<ProtocolConfig>());

this.mockTestPlatform.Verify(tp => tp.ClearExtensions(), Times.Once);
}

[TestMethod]
public void DiscoverTestsShouldCallTestPlatformAndSucceed()
{
Expand Down Expand Up @@ -198,10 +215,10 @@ public void CancelTestRunShouldWaitForCreateTestRunRequest()
this.mockTestPlatform.Setup(mt => mt.CreateTestRunRequest(It.IsAny<TestRunCriteria>(), It.IsAny<ProtocolConfig>())).Callback(
(TestRunCriteria runCriteria, ProtocolConfig config) =>
{
Thread.Sleep(1);
createRunRequestTime = sw.ElapsedMilliseconds;
observedCriteria = runCriteria;
}).Returns(mockRunRequest.Object);
Thread.Sleep(1);
createRunRequestTime = sw.ElapsedMilliseconds;
observedCriteria = runCriteria;
}).Returns(mockRunRequest.Object);

mockRunRequest.Setup(mr => mr.CancelAsync()).Callback(() =>
{
Expand Down Expand Up @@ -290,6 +307,22 @@ public void RunTestsShouldReadTheBatchSizeFromSettingsAndSetItForTestRunCriteria
Assert.AreEqual(15, actualTestRunCriteria.FrequencyOfRunStatsChangeEvent);
}

[TestMethod]
public void RunTestsShouldCallClearExtensionsAfterRun()
{
var payload = new TestRunRequestPayload()
{
Sources = new List<string>() { "a" },
};

var mockDiscoveryRequest = new Mock<ITestRunRequest>();
this.mockTestPlatform.Setup(mt => mt.CreateTestRunRequest(It.IsAny<TestRunCriteria>(), It.IsAny<ProtocolConfig>())).Callback(
(TestRunCriteria runCriteria, ProtocolConfig config) => { }).Returns(mockDiscoveryRequest.Object);

var success = this.testRequestManager.RunTests(payload, new Mock<ITestHostLauncher>().Object, new Mock<ITestRunEventsRegistrar>().Object, It.IsAny<ProtocolConfig>());
this.mockTestPlatform.Verify(tp => tp.ClearExtensions(), Times.Once);
}

[TestMethod]
public void RunTestsWithSourcesShouldCallTestPlatformAndSucceed()
{
Expand Down Expand Up @@ -597,7 +630,7 @@ public void RunTestsShouldUpdateDesignModeIfRunnerIsInDesignMode(bool designMode
var payload = new TestRunRequestPayload
{
RunSettings = runsettings,
Sources = new List<string> {"c:\\testproject.dll"}
Sources = new List<string> { "c:\\testproject.dll" }
};
this.commandLineOptions.IsDesignMode = designModeValue;

Expand All @@ -612,7 +645,7 @@ private static DiscoveryRequestPayload CreateDiscoveryPayload(string runsettings
var discoveryPayload = new DiscoveryRequestPayload
{
RunSettings = runsettings,
Sources = new[] {"c:\\testproject.dll"}
Sources = new[] { "c:\\testproject.dll" }
};
return discoveryPayload;
}
Expand Down

0 comments on commit 0eef86b

Please sign in to comment.