Skip to content

Commit

Permalink
Implemented cancellation of individual source files discovery (micros…
Browse files Browse the repository at this point in the history
…oft#2134)

* Implemented the cancellation of discovery request

* Added an end to end tests to cover the discovery cancellation scenario

* Fixed the E2E test to check only for the discovered tests

* Increased the timeout for one of the tests

* Modified the code to perform cancellation between test assemblies

* Fixed failing tests
  • Loading branch information
cltshivash authored and singhsarab committed Sep 24, 2019
1 parent be51dd7 commit 8e6f70f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ private void LoadTestsFromAnExtension(string extensionAssembly, IEnumerable<stri
return;
}

this.DiscoverTestsFromSingleDiscoverer(discoverer, discovererToSourcesMap, context, discoverySink, logger, ref totalAdaptersUsed, ref totalTimeTakenByAdapters);
var result = this.DiscoverTestsFromSingleDiscoverer(discoverer, discovererToSourcesMap[discoverer], context, discoverySink, logger, cancellationToken);
totalAdaptersUsed += result.TotalAdaptersUsed;
totalTimeTakenByAdapters += result.TotalTimeSpentInAdapaters;
}

if (this.discoveryResultCache.TotalDiscoveredTests == 0)
Expand Down Expand Up @@ -191,19 +193,19 @@ private void CollectTelemetryAtEnd(double totalTimeTakenByAdapters, double total
totalAdaptersUsed);
}

private void DiscoverTestsFromSingleDiscoverer(
private DiscoveryResult DiscoverTestsFromSingleDiscoverer(
LazyExtension<ITestDiscoverer, ITestDiscovererCapabilities> discoverer,
Dictionary<LazyExtension<ITestDiscoverer, ITestDiscovererCapabilities>, IEnumerable<string>> discovererToSourcesMap,
IEnumerable<string> sources,
DiscoveryContext context,
TestCaseDiscoverySink discoverySink,
IMessageLogger logger,
ref double totalAdaptersUsed,
ref double totalTimeTakenByAdapters)
CancellationToken cancellationToken)
{
var result = new DiscoveryResult();
if (DiscovererEnumerator.TryToLoadDiscoverer(discoverer, logger, out var discovererType) == false)
{
// Fail to instantiate the discoverer type.
return;
return result;
}

// on instantiated successfully, get tests
Expand All @@ -217,21 +219,27 @@ private void DiscoverTestsFromSingleDiscoverer(
var newTimeStart = DateTime.UtcNow;

this.testPlatformEventSource.AdapterDiscoveryStart(discoverer.Metadata.DefaultExecutorUri.AbsoluteUri);
discoverer.Value.DiscoverTests(discovererToSourcesMap[discoverer], context, logger, discoverySink);
foreach (var testSource in sources)
{
if (cancellationToken.IsCancellationRequested)
{
EqtTrace.Info("DiscovererEnumerator.DiscoverTestsFromSingleDiscoverer: Cancellation Requested. Aborting the discovery");
break;
}

var totalAdapterRunTime = DateTime.UtcNow - newTimeStart;
discoverer.Value.DiscoverTests(new[] { testSource }, context, logger, discoverySink);
}

this.testPlatformEventSource.AdapterDiscoveryStop(this.discoveryResultCache.TotalDiscoveredTests -
currentTotalTests);
var totalAdapterRunTime = DateTime.UtcNow - newTimeStart;
this.testPlatformEventSource.AdapterDiscoveryStop(this.discoveryResultCache.TotalDiscoveredTests - currentTotalTests);

// Record Total Tests Discovered By each Discoverer.
var totalTestsDiscoveredByCurrentDiscoverer = this.discoveryResultCache.TotalDiscoveredTests - currentTotalTests;
this.requestData.MetricsCollection.Add(
string.Format("{0}.{1}", TelemetryDataConstants.TotalTestsByAdapter,
discoverer.Metadata.DefaultExecutorUri), totalTestsDiscoveredByCurrentDiscoverer);

totalAdaptersUsed++;

result.TotalAdaptersUsed++;

EqtTrace.Verbose("DiscovererEnumerator.DiscoverTestsFromSingleDiscoverer: Done loading tests for {0}",
discoverer.Value.GetType().FullName);
Expand All @@ -244,22 +252,18 @@ private void DiscoverTestsFromSingleDiscoverer(
}

// Collecting Data Point for Time Taken to Discover Tests by each Adapter
this.requestData.MetricsCollection.Add(
string.Format("{0}.{1}", TelemetryDataConstants.TimeTakenToDiscoverTestsByAnAdapter,
discoverer.Metadata.DefaultExecutorUri), totalAdapterRunTime.TotalSeconds);
totalTimeTakenByAdapters += totalAdapterRunTime.TotalSeconds;
this.requestData.MetricsCollection.Add($"{TelemetryDataConstants.TimeTakenToDiscoverTestsByAnAdapter}.{discoverer.Metadata.DefaultExecutorUri}", totalAdapterRunTime.TotalSeconds);
result.TotalTimeSpentInAdapaters += totalAdapterRunTime.TotalSeconds;
}
catch (Exception e)
{
var message = string.Format(
CultureInfo.CurrentUICulture,
CrossPlatEngineResources.ExceptionFromLoadTests,
discovererType.Name,
e.Message);
var message = string.Format(CultureInfo.CurrentUICulture, CrossPlatEngineResources.ExceptionFromLoadTests, discovererType.Name, e.Message);

logger.SendMessage(TestMessageLevel.Error, message);
EqtTrace.Error("DiscovererEnumerator.DiscoverTestsFromSingleDiscoverer: {0} ", e);
}

return result;
}

private static bool TryToLoadDiscoverer(LazyExtension<ITestDiscoverer, ITestDiscovererCapabilities> discoverer, IMessageLogger logger, out Type discovererType)
Expand Down Expand Up @@ -495,5 +499,11 @@ private static IEnumerable<LazyExtension<ITestDiscoverer, ITestDiscovererCapabil
}
}

private class DiscoveryResult
{
public double TotalTimeSpentInAdapaters { get; set; }
public int TotalAdaptersUsed { get; set; }
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -637,21 +637,26 @@ public static void Reset()
[Category("managed")]
private class ManagedDllTestDiscoverer : DllTestDiscoverer
{
static ManagedDllTestDiscoverer()
{
Sources = new List<string>();
}

public static bool IsManagedDiscoverTestCalled { get; private set; }

public static IEnumerable<string> Sources { get; set; }
public static List<string> Sources { get; private set; }

public override void DiscoverTests(IEnumerable<string> sources, IDiscoveryContext discoveryContext, IMessageLogger logger, ITestCaseDiscoverySink discoverySink)
{
Sources = sources;
Sources.AddRange(sources);
IsManagedDiscoverTestCalled = true;
base.DiscoverTests(sources, discoveryContext, logger, discoverySink);
}

public static void Reset()
{
IsManagedDiscoverTestCalled = false;
Sources = null;
Sources = new List<string>();
}
}

Expand Down Expand Up @@ -721,9 +726,14 @@ private static bool ShouldTestDiscovered(IEnumerable<string> sources)
[DefaultExecutorUri("discoverer://jsondiscoverer")]
private class JsonTestDiscoverer : ITestDiscoverer
{
static JsonTestDiscoverer()
{
Sources = new List<string>();
}

public static bool IsDiscoverTestCalled { get; private set; }

public static IEnumerable<string> Sources { get; private set; }
public static List<string> Sources { get; private set; }

public static IDiscoveryContext DiscoveryContext { get; private set; }

Expand All @@ -734,7 +744,7 @@ private class JsonTestDiscoverer : ITestDiscoverer
public void DiscoverTests(IEnumerable<string> sources, IDiscoveryContext discoveryContext, IMessageLogger logger, ITestCaseDiscoverySink discoverySink)
{
IsDiscoverTestCalled = true;
Sources = sources;
Sources.AddRange(sources);
DiscoveryContext = discoveryContext;
MessageLogger = logger;
DiscoverySink = discoverySink;
Expand All @@ -743,6 +753,7 @@ public void DiscoverTests(IEnumerable<string> sources, IDiscoveryContext discove
public static void Reset()
{
IsDiscoverTestCalled = false;
Sources = new List<string>();
}
}

Expand Down

0 comments on commit 8e6f70f

Please sign in to comment.