Skip to content

Commit

Permalink
add binlog serialization for BuildCanceledEventArgs, (#10755)
Browse files Browse the repository at this point in the history
fix typo in other test class,
add tests
  • Loading branch information
JanProvaznik authored Oct 8, 2024
1 parent 8cd45e3 commit 97dc045
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Build.UnitTests/BackEnd/NodePackets_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public void VerifyEventType()
GeneratedFileUsedEventArgs generatedFileUsed = new GeneratedFileUsedEventArgs("path", "some content");
BuildSubmissionStartedEventArgs buildSubmissionStarted = new(new Dictionary<string, string> { { "Value1", "Value2" } }, ["Path1"], ["TargetName"], BuildRequestDataFlags.ReplaceExistingProjectInstance, 123);
BuildCheckTracingEventArgs buildCheckTracing = new();
BuildCanceledEventArgs buildCanceled = new("message", DateTime.UtcNow);

VerifyLoggingPacket(buildFinished, LoggingEventType.BuildFinishedEvent);
VerifyLoggingPacket(buildStarted, LoggingEventType.BuildStartedEvent);
Expand Down Expand Up @@ -114,6 +115,7 @@ public void VerifyEventType()
VerifyLoggingPacket(generatedFileUsed, LoggingEventType.GeneratedFileUsedEvent);
VerifyLoggingPacket(buildSubmissionStarted, LoggingEventType.BuildSubmissionStartedEvent);
VerifyLoggingPacket(buildCheckTracing, LoggingEventType.BuildCheckTracingEvent);
VerifyLoggingPacket(buildCanceled, LoggingEventType.BuildCanceledEvent);
}

private static BuildEventContext CreateBuildEventContext()
Expand Down
12 changes: 12 additions & 0 deletions src/Build.UnitTests/BuildEventArgsSerialization_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ public void RoundtripBuildFinishedEventArgs()
e => e.Succeeded.ToString());
}

[Fact]
public void RoundtripBuildCanceledEventArgs()
{
var args = new BuildCanceledEventArgs(
"Message",
eventTimestamp: DateTime.Parse("12/12/2015 06:11:56 PM"));

Roundtrip(args,
e => e.Message,
e => e.Timestamp.ToString());
}

[Fact]
public void RoundtripBuildSubmissionStartedEventArgs()
{
Expand Down
1 change: 1 addition & 0 deletions src/Build/Logging/BinaryLogger/BinaryLogRecordKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ public enum BinaryLogRecordKind
BuildCheckTracing,
BuildCheckAcquisition,
BuildSubmissionStarted,
BuildCanceled,
}
}
4 changes: 3 additions & 1 deletion src/Build/Logging/BinaryLogger/BinaryLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public sealed class BinaryLogger : ILogger
// version 23:
// - new record kinds: BuildCheckMessageEvent, BuildCheckWarningEvent, BuildCheckErrorEvent,
// BuildCheckTracingEvent, BuildCheckAcquisitionEvent, BuildSubmissionStartedEvent
// version 24:
// - new record kind: BuildCanceledEventArgs

// MAKE SURE YOU KEEP BuildEventArgsWriter AND StructuredLogViewer.BuildEventArgsWriter IN SYNC WITH THE CHANGES ABOVE.
// Both components must stay in sync to avoid issues with logging or event handling in the products.
Expand All @@ -88,7 +90,7 @@ public sealed class BinaryLogger : ILogger

// The current version of the binary log representation.
// Changes with each update of the binary log format.
internal const int FileFormatVersion = 23;
internal const int FileFormatVersion = 24;

// The minimum version of the binary log reader that can read log of above version.
// This should be changed only when the binary log format is changed in a way that would prevent it from being
Expand Down
10 changes: 10 additions & 0 deletions src/Build/Logging/BinaryLogger/BuildEventArgsReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ void HandleError(FormatErrorMessage msgFactory, bool noThrow, ReaderErrorType re
BinaryLogRecordKind.BuildCheckError => ReadBuildErrorEventArgs(),
BinaryLogRecordKind.BuildCheckTracing => ReadBuildCheckTracingEventArgs(),
BinaryLogRecordKind.BuildCheckAcquisition => ReadBuildCheckAcquisitionEventArgs(),
BinaryLogRecordKind.BuildCanceled => ReadBuildCanceledEventArgs(),
_ => null
};

Expand Down Expand Up @@ -1275,6 +1276,15 @@ private BuildEventArgs ReadBuildCheckAcquisitionEventArgs()
return e;
}

private BuildEventArgs ReadBuildCanceledEventArgs()
{
var fields = ReadBuildEventArgsFields();
var e = new BuildCanceledEventArgs(fields.Message);
SetCommonFields(e, fields);

return e;
}

/// <summary>
/// For errors and warnings these 8 fields are written out explicitly
/// (their presence is not marked as a bit in the flags). So we have to
Expand Down
9 changes: 9 additions & 0 deletions src/Build/Logging/BinaryLogger/BuildEventArgsWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ Base types and inheritance ("EventArgs" suffix omitted):
BuildSubmissionStarted
BuildStarted
BuildFinished
BuildCanceled
ProjectEvaluationStarted
ProjectEvaluationFinished
BuildError
Expand Down Expand Up @@ -215,6 +216,7 @@ private BinaryLogRecordKind WriteCore(BuildEventArgs e)
case BuildSubmissionStartedEventArgs buildSubmissionStarted: return Write(buildSubmissionStarted);
case BuildStartedEventArgs buildStarted: return Write(buildStarted);
case BuildFinishedEventArgs buildFinished: return Write(buildFinished);
case BuildCanceledEventArgs buildCanceled: return Write(buildCanceled);
case ProjectEvaluationStartedEventArgs projectEvaluationStarted: return Write(projectEvaluationStarted);
case ProjectEvaluationFinishedEventArgs projectEvaluationFinished: return Write(projectEvaluationFinished);
case BuildCheckTracingEventArgs buildCheckTracing: return Write(buildCheckTracing);
Expand Down Expand Up @@ -307,6 +309,13 @@ private BinaryLogRecordKind Write(BuildFinishedEventArgs e)
return BinaryLogRecordKind.BuildFinished;
}

private BinaryLogRecordKind Write(BuildCanceledEventArgs e)
{
WriteBuildEventArgsFields(e);

return BinaryLogRecordKind.BuildCanceled;
}

private BinaryLogRecordKind Write(ProjectEvaluationStartedEventArgs e)
{
WriteBuildEventArgsFields(e, writeMessage: false);
Expand Down
41 changes: 41 additions & 0 deletions src/Framework.UnitTests/BuildCanceledEventArgs_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Shouldly;
using Xunit;

namespace Microsoft.Build.Framework.UnitTests
{
public class BuildCanceledEventArgs_Tests
{
[Fact]
public void SerializationDeserializationTest()
{
var message = "message";
var datetime = DateTime.Today;

BuildCanceledEventArgs args = new(
message,
datetime
);
using MemoryStream stream = new MemoryStream();
using BinaryWriter bw = new BinaryWriter(stream);
args.WriteToStream(bw);

stream.Position = 0;
using BinaryReader br = new BinaryReader(stream);
BuildCanceledEventArgs argDeserialized = new("m");
int packetVersion = (Environment.Version.Major * 10) + Environment.Version.Minor;

argDeserialized.CreateFromStream(br, packetVersion);
argDeserialized.Message.ShouldBe(message);
argDeserialized.Timestamp.ShouldBe(datetime);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Microsoft.Build.Framework.UnitTests
{
public class BuildSubmissionStartedEventAgs_Tests
public class BuildSubmissionStartedEventArgs_Tests
{
[Fact]
public void SerializationDeserializationTest()
Expand Down
10 changes: 10 additions & 0 deletions src/Shared/LogMessagePacketBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ internal enum LoggingEventType : int
/// Event is <see cref="BuildSubmissionStartedEventArgs"/>.
/// </summary>
BuildSubmissionStartedEvent = 40,

/// <summary>
/// Event is <see cref="BuildCanceledEventArgs"/>
/// </summary>
BuildCanceledEvent = 41,
}
#endregion

Expand Down Expand Up @@ -656,6 +661,7 @@ private BuildEventArgs GetBuildEventArgFromId()
LoggingEventType.BuildCheckTracingEvent => new BuildCheckTracingEventArgs(),
LoggingEventType.EnvironmentVariableReadEvent => new EnvironmentVariableReadEventArgs(),
LoggingEventType.BuildSubmissionStartedEvent => new BuildSubmissionStartedEventArgs(),
LoggingEventType.BuildCanceledEvent => new BuildCanceledEventArgs("Build canceled."),
#endif
_ => throw new InternalErrorException("Should not get to the default of GetBuildEventArgFromId ID: " + _eventType)
};
Expand Down Expand Up @@ -799,6 +805,10 @@ private LoggingEventType GetLoggingEventId(BuildEventArgs eventArg)
{
return LoggingEventType.BuildSubmissionStartedEvent;
}
else if (eventType == typeof(BuildCanceledEventArgs))
{
return LoggingEventType.BuildCanceledEvent;
}
#endif
else if (eventType == typeof(TargetStartedEventArgs))
{
Expand Down

0 comments on commit 97dc045

Please sign in to comment.