Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/Build.UnitTests/BackEnd/EventSourceSink_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public void ConsumeEventsGoodEvents()
EventHandlerHelper testHandlers = new EventHandlerHelper(sink, null);
VerifyRegisteredHandlers(RaiseEventHelper.BuildStarted, eventHelper, testHandlers);
VerifyRegisteredHandlers(RaiseEventHelper.BuildFinished, eventHelper, testHandlers);
VerifyRegisteredHandlers(RaiseEventHelper.BuildCanceled, eventHelper, testHandlers);
VerifyRegisteredHandlers(RaiseEventHelper.NormalMessage, eventHelper, testHandlers);
VerifyRegisteredHandlers(RaiseEventHelper.TaskFinished, eventHelper, testHandlers);
VerifyRegisteredHandlers(RaiseEventHelper.CommandLine, eventHelper, testHandlers);
Expand All @@ -66,6 +67,7 @@ public void ConsumeEventsGoodEventsNoHandlers()
RaiseEventHelper eventHelper = new RaiseEventHelper(sink);
eventHelper.RaiseBuildEvent(RaiseEventHelper.BuildStarted);
eventHelper.RaiseBuildEvent(RaiseEventHelper.BuildFinished);
eventHelper.RaiseBuildEvent(RaiseEventHelper.BuildCanceled);
eventHelper.RaiseBuildEvent(RaiseEventHelper.NormalMessage);
eventHelper.RaiseBuildEvent(RaiseEventHelper.TaskFinished);
eventHelper.RaiseBuildEvent(RaiseEventHelper.CommandLine);
Expand Down Expand Up @@ -98,6 +100,7 @@ public void LoggerExceptionInEventHandler()
{
RaiseExceptionInEventHandler(RaiseEventHelper.BuildStarted, exception);
RaiseExceptionInEventHandler(RaiseEventHelper.BuildFinished, exception);
RaiseExceptionInEventHandler(RaiseEventHelper.BuildCanceled, exception);
RaiseExceptionInEventHandler(RaiseEventHelper.NormalMessage, exception);
RaiseExceptionInEventHandler(RaiseEventHelper.TaskFinished, exception);
RaiseExceptionInEventHandler(RaiseEventHelper.CommandLine, exception);
Expand Down Expand Up @@ -127,7 +130,7 @@ public void RaiseGenericBuildEventArgs()
});
}
/// <summary>
/// Verify that shutdown un registers all of the event handlers
/// Verify that shutdown unregisters all of the event handlers
/// </summary>
[Fact]
public void VerifyShutdown()
Expand Down Expand Up @@ -431,6 +434,11 @@ internal EventHandlerHelper(IEventSource source, Exception exceptionToThrow)
source.TaskFinished += Source_TaskFinished;
source.TaskStarted += Source_TaskStarted;
source.WarningRaised += Source_WarningRaised;

if (source is IEventSource5 eventSource5)
{
eventSource5.BuildCanceled += Source_BuildCanceled;
}
}
#endregion

Expand Down Expand Up @@ -671,6 +679,16 @@ private void Source_BuildFinished(object sender, BuildFinishedEventArgs e)
HandleEvent(e);
}

/// <summary>
/// Handle a build canceled event
/// </summary>
/// <param name="sender">Who sent the event</param>
/// <param name="e">Event raised on the event source</param>
private void Source_BuildCanceled(object sender, BuildCanceledEventArgs e)
{
HandleEvent(e);
}

/// <summary>
/// Handle a events raised from the any event source. This source will
/// raise all events no matter the type.
Expand Down Expand Up @@ -715,6 +733,11 @@ internal sealed class RaiseEventHelper
/// </summary>
private static BuildFinishedEventArgs s_buildFinished = new BuildFinishedEventArgs("Message", "Keyword", true);

/// <summary>
/// Build Finished Event
/// </summary>
private static BuildCanceledEventArgs s_buildCanceled = new BuildCanceledEventArgs("Message", "Keyword");

/// <summary>
/// Build Message Event
/// </summary>
Expand Down Expand Up @@ -846,6 +869,17 @@ internal static BuildFinishedEventArgs BuildFinished
}
}

/// <summary>
/// Event which can be raised in multiple tests.
/// </summary>
internal static BuildCanceledEventArgs BuildCanceled
{
get
{
return s_buildCanceled;
}
}

/// <summary>
/// Event which can be raised in multiple tests.
/// </summary>
Expand Down
27 changes: 26 additions & 1 deletion src/Build.UnitTests/BackEnd/LoggingService_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -411,17 +411,28 @@ public void RegisterGoodDistributedAndCentralLoggerTestBuildStartedFinished()
Assert.Equal(1, regularILoggerB.BuildFinishedCount);
Assert.Equal(1, regularILoggerC.BuildFinishedCount);

_initializedService.LogBuildCanceled();
Assert.Equal(1, regularILoggerA.BuildCanceledCount);
Assert.Equal(1, regularILoggerB.BuildCanceledCount);
Assert.Equal(1, regularILoggerC.BuildCanceledCount);

// Make sure if we call build started again we only get one other build started event.
_initializedService.LogBuildStarted();
Assert.Equal(2, regularILoggerA.BuildStartedCount);
Assert.Equal(2, regularILoggerB.BuildStartedCount);
Assert.Equal(2, regularILoggerC.BuildStartedCount);

// Make sure if we call build started again we only get one other build started event.
// Make sure if we call build finished again we only get one other build finished event.
_initializedService.LogBuildFinished(true);
Assert.Equal(2, regularILoggerA.BuildFinishedCount);
Assert.Equal(2, regularILoggerB.BuildFinishedCount);
Assert.Equal(2, regularILoggerC.BuildFinishedCount);

// Make sure if we call build canceled again we only get one other build canceled event.
_initializedService.LogBuildCanceled();
Assert.Equal(2, regularILoggerA.BuildCanceledCount);
Assert.Equal(2, regularILoggerB.BuildCanceledCount);
Assert.Equal(2, regularILoggerC.BuildCanceledCount);
}

/// <summary>
Expand Down Expand Up @@ -1421,6 +1432,15 @@ internal int BuildFinishedCount
set;
}

/// <summary>
/// Number of times build finished was logged
/// </summary>
internal int BuildCanceledCount
{
get;
set;
}

/// <summary>
/// Initialize
/// </summary>
Expand Down Expand Up @@ -1452,6 +1472,11 @@ internal void LoggerEventHandler(object sender, BuildEventArgs eventArgs)
{
++BuildFinishedCount;
}

if (eventArgs is BuildCanceledEventArgs)
{
++BuildCanceledCount;
}
}
}

Expand Down
55 changes: 31 additions & 24 deletions src/Build.UnitTests/BackEnd/LoggingServicesLogMethod_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -999,17 +999,21 @@ public void ProjectFinished()
/// Make sure we can log a build started event correctly.
/// Test both the LogOnlyCriticalEvents true and false
/// </summary>
[Fact]
public void LogBuildStarted()
[Theory]
[InlineData(true)]
[InlineData(false)]
public void LogBuildStarted(bool onlyLogCriticalEvents)
{
ProcessBuildEventHelper service =
(ProcessBuildEventHelper)ProcessBuildEventHelper.CreateLoggingService(LoggerMode.Synchronous, 1);

service.OnlyLogCriticalEvents = onlyLogCriticalEvents;
service.LogBuildStarted();

string message = onlyLogCriticalEvents ? string.Empty : ResourceUtilities.GetResourceString("BuildStarted");

BuildStartedEventArgs buildEvent =
new BuildStartedEventArgs(
ResourceUtilities.GetResourceString("BuildStarted"),
message,
null /* no help keyword */,
service.ProcessedBuildEvent.Timestamp);

Expand All @@ -1018,26 +1022,6 @@ public void LogBuildStarted()
new EventArgsEqualityComparer<BuildStartedEventArgs>());
}

[Fact(Skip = "https://github.com/dotnet/msbuild/issues/437")]
[Trait("Category", "netcore-osx-failing")]
[Trait("Category", "netcore-linux-failing")]
public void LogBuildStartedCriticalOnly()
{
ProcessBuildEventHelper service =
(ProcessBuildEventHelper)ProcessBuildEventHelper.CreateLoggingService(LoggerMode.Synchronous, 1);
service.OnlyLogCriticalEvents = true;
service.LogBuildStarted();

BuildStartedEventArgs buildEvent =
new BuildStartedEventArgs(
string.Empty,
null /* no help keyword */);

Assert.IsType<BuildStartedEventArgs>(service.ProcessedBuildEvent);
Assert.Equal(buildEvent, (BuildStartedEventArgs)service.ProcessedBuildEvent,
new EventArgsEqualityComparer<BuildStartedEventArgs>());
}

/// <summary>
/// Make sure we can log a build finished event correctly.
/// Verify the success cases as well as OnlyLogCriticalEvents
Expand All @@ -1062,6 +1046,29 @@ public void LogBuildFinished()
Assert.True(((BuildFinishedEventArgs)service.ProcessedBuildEvent).IsEquivalent(buildEvent));
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void LogBuildCanceled(bool onlyLogCriticalEvents)
{
ProcessBuildEventHelper service =
(ProcessBuildEventHelper)ProcessBuildEventHelper.CreateLoggingService(LoggerMode.Synchronous, 1);
service.OnlyLogCriticalEvents = onlyLogCriticalEvents;
service.LogBuildCanceled();

string message = onlyLogCriticalEvents ? string.Empty : ResourceUtilities.GetResourceString("AbortingBuild");

BuildCanceledEventArgs buildEvent =
new BuildCanceledEventArgs(
message,
null /* no help keyword */,
service.ProcessedBuildEvent.Timestamp);

Assert.IsType<BuildCanceledEventArgs>(service.ProcessedBuildEvent);
Assert.Equal(buildEvent, (BuildCanceledEventArgs)service.ProcessedBuildEvent,
new EventArgsEqualityComparer<BuildCanceledEventArgs>());
}

/// <summary>
/// Exercise Asynchronous code path, this method should return right away as there are no events to process.
/// This will be further tested in the LoggingService_Tests class.
Expand Down
4 changes: 4 additions & 0 deletions src/Build.UnitTests/BackEnd/MockLoggingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,10 @@ public void LogBuildFinished(bool success)
{
}

/// <inheritdoc />
public void LogBuildCanceled()
{
}

/// <inheritdoc />
public BuildEventContext CreateEvaluationBuildEventContext(int nodeId, int submissionId)
Expand Down
4 changes: 4 additions & 0 deletions src/Build.UnitTests/ConsoleLogger_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ public void NullEventFields()
es.Consume(new BuildFinishedEventArgs(null, null, true));
es.Consume(new BuildFinishedEventArgs(null, null, true));
es.Consume(new MyCustomBuildEventArgs2());
es.Consume(new BuildCanceledEventArgs(null, null));
// No exception raised
}

Expand Down Expand Up @@ -511,6 +512,8 @@ public void NullEventFieldsParallel()
pfea.BuildEventContext = buildEventContext;
BuildFinishedEventArgs bfea = new BuildFinishedEventArgs(null, null, true);
bfea.BuildEventContext = buildEventContext;
BuildCanceledEventArgs bcea = new BuildCanceledEventArgs(null, null);
bcea.BuildEventContext = buildEventContext;
MyCustomBuildEventArgs2 mcea = new MyCustomBuildEventArgs2();
mcea.BuildEventContext = buildEventContext;

Expand All @@ -530,6 +533,7 @@ public void NullEventFieldsParallel()
es.Consume(bfea);
es.Consume(bfea);
es.Consume(bfea);
es.Consume(bcea);
es.Consume(mcea);
// No exception raised
}
Expand Down
3 changes: 3 additions & 0 deletions src/Build/BackEnd/BuildManager/BuildManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,9 @@ public void CancelAllSubmissions()

private void CancelAllSubmissions(bool async)
{
ILoggingService loggingService = ((IBuildComponentHost)this).LoggingService;
loggingService.LogBuildCanceled();

var parentThreadCulture = _buildParameters != null
? _buildParameters.Culture
: CultureInfo.CurrentCulture;
Expand Down
13 changes: 13 additions & 0 deletions src/Build/BackEnd/Components/Logging/BuildEventArgTransportSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ public bool HaveLoggedBuildFinishedEvent
set;
}

/// <inheritdoc />
public bool HaveLoggedBuildCanceledEvent
{
get;
set;
}

/// <summary>
/// This property is ignored by this event sink and relies on the receiver to treat warnings as errors.
/// </summary>
Expand Down Expand Up @@ -115,6 +122,7 @@ public IDictionary<int, ISet<string>> WarningsAsMessagesByProject
/// This property is ignored by this event sink and relies on the receiver to keep track of whether or not any errors have been logged.
/// </summary>
public ISet<int> BuildSubmissionIdsThatHaveLoggedErrors { get; } = null;

#endregion
#region IBuildEventSink Methods

Expand Down Expand Up @@ -145,6 +153,11 @@ public void Consume(BuildEventArgs buildEvent, int sinkId)
HaveLoggedBuildFinishedEvent = true;
return;
}
else if (buildEvent is BuildCanceledEventArgs)
{
HaveLoggedBuildFinishedEvent = true;
return;
}

LogMessagePacket logPacket = new LogMessagePacket(new KeyValuePair<int, BuildEventArgs>(sinkId, buildEvent));
_sendDataDelegate(logPacket);
Expand Down
Loading