Skip to content

Commit

Permalink
Do not crash data collector if a extension initialize fails. (microso…
Browse files Browse the repository at this point in the history
…ft#1230)

* Do not crash data collector if a extension initialize fails.

* Add unit tests for the scenario.
  • Loading branch information
codito authored and Arun Mahapatra committed Oct 27, 2017
1 parent aec5eb4 commit 0dde783
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -537,9 +537,9 @@ private Dictionary<string, DataCollectionEnvironmentVariable> GetEnvironmentVari
var dataCollectorEnvironmentVariable = new Dictionary<string, DataCollectionEnvironmentVariable>(StringComparer.OrdinalIgnoreCase);
foreach (var dataCollectorInfo in this.RunDataCollectors.Values)
{
dataCollectorInfo.SetTestExecutionEnvironmentVariables();
try
{
dataCollectorInfo.SetTestExecutionEnvironmentVariables();
this.AddCollectorEnvironmentVariables(dataCollectorInfo, dataCollectorEnvironmentVariable);
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal class ProxyExecutionManagerWithDataCollection : ProxyExecutionManager
/// <summary>
/// Initializes a new instance of the <see cref="ProxyExecutionManagerWithDataCollection"/> class.
/// </summary>
/// <param name="testRequestSender">
/// <param name="requestSender">
/// Test request sender instance.
/// </param>
/// <param name="testHostManager">
Expand All @@ -44,6 +44,7 @@ public ProxyExecutionManagerWithDataCollection(IRequestData requestData, ITestRe
this.ProxyDataCollectionManager = proxyDataCollectionManager;
this.DataCollectionRunEventsHandler = new DataCollectionRunEventsHandler();
this.requestData = requestData;
this.dataCollectionEnvironmentVariables = new Dictionary<string, string>();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public DataCollectionParameters BeforeTestRunStart(
ITestMessageEventHandler runEventsHandler)
{
var areTestCaseLevelEventsRequired = false;
IDictionary<string, string> environmentVariables = null;
IDictionary<string, string> environmentVariables = new Dictionary<string, string>();

var dataCollectionEventsPort = 0;
this.InvokeDataCollectionServiceAction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ public void BeforeTestRunStartsShouldInvokeRunEventsHandlerIfExceptionIsThrown()
var result = this.proxyDataCollectionManager.BeforeTestRunStart(true, true, mockRunEventsHandler.Object);

mockRunEventsHandler.Verify(eh => eh.HandleLogMessage(TestMessageLevel.Error, "Exception of type 'System.Exception' was thrown."), Times.Once);
Assert.AreEqual(result.EnvironmentVariables, null);
Assert.AreEqual(result.AreTestCaseLevelEventsRequired, false);
Assert.AreEqual(result.DataCollectionEventsPort, 0);
Assert.AreEqual(0, result.EnvironmentVariables.Count);
Assert.AreEqual(false, result.AreTestCaseLevelEventsRequired);
Assert.AreEqual(0, result.DataCollectionEventsPort);
}

[TestMethod]
Expand Down
11 changes: 11 additions & 0 deletions test/datacollector.UnitTests/DataCollectionManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,17 @@ public void InitializeDataCollectorsShouldLogExceptionToMessageSinkIfInitializat
this.mockMessageSink.Verify(x => x.SendMessage(It.IsAny<DataCollectionMessageEventArgs>()), Times.Once);
}

[TestMethod]
public void InitializeDataCollectorsShouldLogExceptionToMessageSinkIfSetEnvironmentVariableFails()
{
this.mockDataCollector.As<ITestExecutionEnvironmentSpecifier>().Setup(x => x.GetTestExecutionEnvironmentVariables()).Throws<Exception>();

this.dataCollectionManager.InitializeDataCollectors(this.dataCollectorSettings);

Assert.AreEqual(0, this.dataCollectionManager.RunDataCollectors.Count);
this.mockMessageSink.Verify(x => x.SendMessage(It.IsAny<DataCollectionMessageEventArgs>()), Times.Once);
}

[TestMethod]
public void InitializeDataCollectorsShouldReturnFirstEnvironmentVariableIfMoreThanOneVariablesWithSameKeyIsSpecified()
{
Expand Down

0 comments on commit 0dde783

Please sign in to comment.