Skip to content

Commit

Permalink
Creating run and discovery start events and exposing it to loggers (#…
Browse files Browse the repository at this point in the history
…1083)

* Creating and invoking run and discovery start events

* Exposing discovery start and run start events to logger extensibility

* re-trigger build checks

* Removing test case filtering
  • Loading branch information
abhishekkumawat23 authored and Faizan2304 committed Sep 19, 2017
1 parent e48d96d commit 8746f3f
Show file tree
Hide file tree
Showing 13 changed files with 407 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ void IDiscoveryRequest.DiscoverAsync()
// Collecting Data Point Number of sources sent for discovery
this.requestData.MetricsCollection.Add(TelemetryDataConstants.NumberOfSourcesSentForDiscovery, (this.DiscoveryCriteria.Sources.Count()).ToString());

// Invoke OnDiscoveryStart event
this.OnDiscoveryStart.SafeInvoke(this, new DiscoveryStartEventArgs(this.DiscoveryCriteria), "DiscoveryRequest.DiscoveryStart");

this.DiscoveryManager.DiscoverTests(this.DiscoveryCriteria, this);
}
catch
Expand Down Expand Up @@ -141,6 +144,11 @@ bool IRequest.WaitForCompletion(int timeout)
return true;
}

/// <summary>
/// Raised when the test discovery starts.
/// </summary>
public event EventHandler<DiscoveryStartEventArgs> OnDiscoveryStart;

/// <summary>
/// Raised when the test discovery completes.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions src/Microsoft.TestPlatform.Client/Execution/TestRunRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public int ExecuteAsync()

// Start the stop watch for calculating the test run time taken overall
this.runRequestTimeTracker.Start();
this.OnRunStart.SafeInvoke(this, new TestRunStartEventArgs(this.testRunCriteria), "TestRun.TestRunStart");
int processId = this.ExecutionManager.StartTestRun(this.testRunCriteria, this);

if (EqtTrace.IsInfoEnabled)
Expand Down Expand Up @@ -298,6 +299,11 @@ public ITestRunConfiguration TestRunConfiguration
/// </summary>
public event EventHandler<TestRunChangedEventArgs> OnRunStatsChange;

/// <summary>
/// Raised when the test run starts.
/// </summary>
public event EventHandler<TestRunStartEventArgs> OnRunStart;

/// <summary>
/// Raised when the test message is received.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public InternalTestLoggerEvents(TestSessionMessageLogger testSessionMessageLogge
/// </summary>
public override event EventHandler<TestRunMessageEventArgs> TestRunMessage;

/// <summary>
/// Raised when a test run starts.
/// </summary>
public override event EventHandler<TestRunStartEventArgs> TestRunStart;

/// <summary>
/// Raised when a test result is received.
/// </summary>
Expand All @@ -95,6 +100,11 @@ public InternalTestLoggerEvents(TestSessionMessageLogger testSessionMessageLogge
/// </summary>
public override event EventHandler<TestRunCompleteEventArgs> TestRunComplete;

/// <summary>
/// Raised when test discovery starts.
/// </summary>
public override event EventHandler<DiscoveryStartEventArgs> DiscoveryStart;

/// <summary>
/// Raised when a discovery message is received.
/// </summary>
Expand Down Expand Up @@ -196,6 +206,32 @@ internal void RaiseTestResult(TestResultEventArgs args)
this.SafeInvokeAsync(() => this.TestResult, args, resultSize, "InternalTestLoggerEvents.SendTestResult");
}

/// <summary>
/// Raises the test run start event to enabled loggers.
/// </summary>
/// <param name="args">Arguments to to be raised.</param>
internal void RaiseTestRunStart(TestRunStartEventArgs args)
{
ValidateArg.NotNull<TestRunStartEventArgs>(args, "args");

CheckDisposed();

this.SafeInvokeAsync(() => this.TestRunStart, args, 0, "InternalTestLoggerEvents.SendTestRunStart");
}

/// <summary>
/// Raises a discovery start event to the enabled loggers.
/// </summary>
/// <param name="args">Arguments to to be raised.</param>
internal void RaiseDiscoveryStart(DiscoveryStartEventArgs args)
{
ValidateArg.NotNull<DiscoveryStartEventArgs>(args, "args");

CheckDisposed();

SafeInvokeAsync(() => this.DiscoveryStart, args, 0, "InternalTestLoggerEvents.SendDiscoveryStart");
}

/// <summary>
/// Raises a discovery message event to the enabled loggers.
/// </summary>
Expand Down
24 changes: 24 additions & 0 deletions src/Microsoft.TestPlatform.Common/Logging/TestLoggerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ public void RegisterTestRunEvents(ITestRunRequest testRunRequest)

// Redirect the events to the InternalTestLoggerEvents
testRunRequest.TestRunMessage += this.TestRunMessageHandler;
testRunRequest.OnRunStart += this.TestRunStartHandler;
testRunRequest.OnRunStatsChange += this.TestRunStatsChangedHandler;
testRunRequest.OnRunCompletion += this.TestRunCompleteHandler;
testRunRequest.DataCollectionMessage += this.DataCollectionMessageHandler;
Expand All @@ -355,6 +356,7 @@ public void RegisterDiscoveryEvents(IDiscoveryRequest discoveryRequest)
this.CheckDisposed();
this.discoveryRequest = discoveryRequest;
discoveryRequest.OnDiscoveryMessage += this.DiscoveryMessageHandler;
discoveryRequest.OnDiscoveryStart += this.DiscoveryStartHandler;
discoveryRequest.OnDiscoveredTests += this.DiscoveredTestsHandler;
discoveryRequest.OnDiscoveryComplete += this.DiscoveryCompleteHandler;
}
Expand All @@ -368,6 +370,7 @@ public void UnregisterTestRunEvents(ITestRunRequest testRunRequest)
ValidateArg.NotNull<ITestRunRequest>(testRunRequest, "testRunRequest");

testRunRequest.TestRunMessage -= this.TestRunMessageHandler;
testRunRequest.OnRunStart -= this.TestRunStartHandler;
testRunRequest.OnRunStatsChange -= this.TestRunStatsChangedHandler;
testRunRequest.OnRunCompletion -= this.TestRunCompleteHandler;
testRunRequest.DataCollectionMessage -= this.DataCollectionMessageHandler;
Expand All @@ -381,6 +384,7 @@ public void UnregisterDiscoveryEvents(IDiscoveryRequest discoveryRequest)
{
ValidateArg.NotNull<IDiscoveryRequest>(discoveryRequest, "discoveryRequest");
discoveryRequest.OnDiscoveryMessage -= this.DiscoveryMessageHandler;
discoveryRequest.OnDiscoveryStart -= this.DiscoveryStartHandler;
discoveryRequest.OnDiscoveredTests -= this.DiscoveredTestsHandler;
discoveryRequest.OnDiscoveryComplete -= this.DiscoveryCompleteHandler;
}
Expand Down Expand Up @@ -441,6 +445,7 @@ protected virtual void Dispose(bool disposing)
if (this.runRequest != null)
{
this.runRequest.TestRunMessage -= this.TestRunMessageHandler;
this.runRequest.OnRunStart -= this.TestRunStartHandler;
this.runRequest.OnRunStatsChange -= this.TestRunStatsChangedHandler;
this.runRequest.OnRunCompletion -= this.TestRunCompleteHandler;
this.runRequest.DataCollectionMessage -= this.DataCollectionMessageHandler;
Expand All @@ -449,6 +454,7 @@ protected virtual void Dispose(bool disposing)
if (this.discoveryRequest != null)
{
this.discoveryRequest.OnDiscoveryMessage -= this.DiscoveryMessageHandler;
this.discoveryRequest.OnDiscoveryStart -= this.DiscoveryStartHandler;
this.discoveryRequest.OnDiscoveredTests -= this.DiscoveredTestsHandler;
this.discoveryRequest.OnDiscoveryComplete -= this.DiscoveryCompleteHandler;
}
Expand Down Expand Up @@ -536,6 +542,14 @@ private void TestRunStatsChangedHandler(object sender, TestRunChangedEventArgs e
}
}

/// <summary>
/// Called when a test run starts.
/// </summary>
private void TestRunStartHandler(object sender, TestRunStartEventArgs e)
{
this.loggerEvents.RaiseTestRunStart(e);
}

/// <summary>
/// Called when a test run is complete.
/// </summary>
Expand Down Expand Up @@ -593,6 +607,16 @@ private void DiscoveryCompleteHandler(object sender, DiscoveryCompleteEventArgs
{
this.loggerEvents.RaiseDiscoveryComplete(e);
}

/// <summary>
/// Called when test discovery starts.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DiscoveryStartHandler(object sender, DiscoveryStartEventArgs e)
{
this.loggerEvents.RaiseDiscoveryStart(e);
}
#endregion

#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.VisualStudio.TestPlatform.ObjectModel.Client
{
using System;
using System.Diagnostics;

using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;

/// <summary>
/// Event arguments used when test discovery starts
/// </summary>
public class DiscoveryStartEventArgs : EventArgs
{
/// <summary>
/// Constructor for creating event args object
/// </summary>
/// <param name="discoveryCriteria"> Discovery criteria to be used for test discovery. </param>
public DiscoveryStartEventArgs(DiscoveryCriteria discoveryCriteria)
{
DiscoveryCriteria = discoveryCriteria;
}

/// <summary>
/// Discovery criteria to be used for test discovery
/// </summary>
public DiscoveryCriteria DiscoveryCriteria { get; private set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Microsoft.VisualStudio.TestPlatform.ObjectModel.Client
{
using System;
using System.Diagnostics;

using Microsoft.VisualStudio.TestPlatform.ObjectModel.Adapter;

/// <summary>
/// Event arguments used when test run starts
/// </summary>
public class TestRunStartEventArgs : EventArgs
{
/// <summary>
/// Constructor for creating event args object
/// </summary>
/// <param name="testRunCriteria"> Test run criteria to be used for test run. </param>
public TestRunStartEventArgs(TestRunCriteria testRunCriteria)
{
TestRunCriteria = testRunCriteria;
}

/// <summary>
/// Test run criteria to be used for test run
/// </summary>
public TestRunCriteria TestRunCriteria { get; private set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ namespace Microsoft.VisualStudio.TestPlatform.ObjectModel.Client
/// </summary>
public interface IDiscoveryRequest : IRequest
{
/// <summary>
/// Handler for notifying discovery process is started
/// </summary>
event EventHandler<DiscoveryStartEventArgs> OnDiscoveryStart;

/// <summary>
/// Handler for notifying discovery process is complete
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ ITestRunConfiguration TestRunConfiguration
/// </summary>
event EventHandler<TestRunChangedEventArgs> OnRunStatsChange;

/// <summary>
/// Handler for notifying test run started
/// </summary>
event EventHandler<TestRunStartEventArgs> OnRunStart;

/// <summary>
/// Handler for notifying test run is complete
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions src/Microsoft.TestPlatform.ObjectModel/Logger/TestLoggerEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ protected TestLoggerEvents()
/// </summary>
public abstract event EventHandler<TestRunMessageEventArgs> TestRunMessage;

/// <summary>
/// Raised when a test run starts.
/// </summary>
public abstract event EventHandler<TestRunStartEventArgs> TestRunStart;

/// <summary>
/// Raised when a test result is received.
/// </summary>
Expand All @@ -39,6 +44,11 @@ protected TestLoggerEvents()
/// </summary>
public abstract event EventHandler<TestRunCompleteEventArgs> TestRunComplete;

/// <summary>
/// Raised when test discovery starts
/// </summary>
public abstract event EventHandler<DiscoveryStartEventArgs> DiscoveryStart;

/// <summary>
/// Raised when a discovery message is received.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,21 @@ public void HandleDiscoveryCompleteShouldCloseDiscoveryManagerBeforeRaiseDiscove
Assert.AreEqual("complete", events[1]);
}

/// <summary>
/// DiscoverAsync should invoke OnDiscoveryStart event.
/// </summary>
[TestMethod]
public void DiscoverAsyncShouldInvokeOnDiscoveryStart()
{
bool onDiscoveryStartHandlerCalled = false;
this.discoveryRequest.OnDiscoveryStart += (s, e) => onDiscoveryStartHandlerCalled = true;

// Action
this.discoveryRequest.DiscoverAsync();

// Assert
Assert.IsTrue(onDiscoveryStartHandlerCalled, "DiscoverAsync should invoke OnDiscoveryStart event");
}

[TestMethod]
public void HandleDiscoveryCompleteShouldCollectMetrics()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,5 +342,21 @@ public void LaunchProcessWithDebuggerAttachedShouldCallCustomLauncherIfLauncherI

mockCustomLauncher.Verify(ml => ml.LaunchTestHost(testProcessStartInfo), Times.Once);
}

/// <summary>
/// ExecuteAsync should invoke OnRunStart event.
/// </summary>
[TestMethod]
public void ExecuteAsyncShouldInvokeOnRunStart()
{
bool onRunStartHandlerCalled = false;
this.testRunRequest.OnRunStart += (s, e) => onRunStartHandlerCalled = true;

// Action
this.testRunRequest.ExecuteAsync();

// Assert
Assert.IsTrue(onRunStartHandlerCalled, "ExecuteAsync should invoke OnRunstart event");
}
}
}
Loading

0 comments on commit 8746f3f

Please sign in to comment.