Skip to content

Commit 97dc045

Browse files
authored
add binlog serialization for BuildCanceledEventArgs, (#10755)
fix typo in other test class, add tests
1 parent 8cd45e3 commit 97dc045

File tree

9 files changed

+89
-2
lines changed

9 files changed

+89
-2
lines changed

src/Build.UnitTests/BackEnd/NodePackets_Tests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public void VerifyEventType()
7979
GeneratedFileUsedEventArgs generatedFileUsed = new GeneratedFileUsedEventArgs("path", "some content");
8080
BuildSubmissionStartedEventArgs buildSubmissionStarted = new(new Dictionary<string, string> { { "Value1", "Value2" } }, ["Path1"], ["TargetName"], BuildRequestDataFlags.ReplaceExistingProjectInstance, 123);
8181
BuildCheckTracingEventArgs buildCheckTracing = new();
82+
BuildCanceledEventArgs buildCanceled = new("message", DateTime.UtcNow);
8283

8384
VerifyLoggingPacket(buildFinished, LoggingEventType.BuildFinishedEvent);
8485
VerifyLoggingPacket(buildStarted, LoggingEventType.BuildStartedEvent);
@@ -114,6 +115,7 @@ public void VerifyEventType()
114115
VerifyLoggingPacket(generatedFileUsed, LoggingEventType.GeneratedFileUsedEvent);
115116
VerifyLoggingPacket(buildSubmissionStarted, LoggingEventType.BuildSubmissionStartedEvent);
116117
VerifyLoggingPacket(buildCheckTracing, LoggingEventType.BuildCheckTracingEvent);
118+
VerifyLoggingPacket(buildCanceled, LoggingEventType.BuildCanceledEvent);
117119
}
118120

119121
private static BuildEventContext CreateBuildEventContext()

src/Build.UnitTests/BuildEventArgsSerialization_Tests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ public void RoundtripBuildFinishedEventArgs()
9797
e => e.Succeeded.ToString());
9898
}
9999

100+
[Fact]
101+
public void RoundtripBuildCanceledEventArgs()
102+
{
103+
var args = new BuildCanceledEventArgs(
104+
"Message",
105+
eventTimestamp: DateTime.Parse("12/12/2015 06:11:56 PM"));
106+
107+
Roundtrip(args,
108+
e => e.Message,
109+
e => e.Timestamp.ToString());
110+
}
111+
100112
[Fact]
101113
public void RoundtripBuildSubmissionStartedEventArgs()
102114
{

src/Build/Logging/BinaryLogger/BinaryLogRecordKind.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@ public enum BinaryLogRecordKind
4646
BuildCheckTracing,
4747
BuildCheckAcquisition,
4848
BuildSubmissionStarted,
49+
BuildCanceled,
4950
}
5051
}

src/Build/Logging/BinaryLogger/BinaryLogger.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public sealed class BinaryLogger : ILogger
7878
// version 23:
7979
// - new record kinds: BuildCheckMessageEvent, BuildCheckWarningEvent, BuildCheckErrorEvent,
8080
// BuildCheckTracingEvent, BuildCheckAcquisitionEvent, BuildSubmissionStartedEvent
81+
// version 24:
82+
// - new record kind: BuildCanceledEventArgs
8183

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

8991
// The current version of the binary log representation.
9092
// Changes with each update of the binary log format.
91-
internal const int FileFormatVersion = 23;
93+
internal const int FileFormatVersion = 24;
9294

9395
// The minimum version of the binary log reader that can read log of above version.
9496
// This should be changed only when the binary log format is changed in a way that would prevent it from being

src/Build/Logging/BinaryLogger/BuildEventArgsReader.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ void HandleError(FormatErrorMessage msgFactory, bool noThrow, ReaderErrorType re
325325
BinaryLogRecordKind.BuildCheckError => ReadBuildErrorEventArgs(),
326326
BinaryLogRecordKind.BuildCheckTracing => ReadBuildCheckTracingEventArgs(),
327327
BinaryLogRecordKind.BuildCheckAcquisition => ReadBuildCheckAcquisitionEventArgs(),
328+
BinaryLogRecordKind.BuildCanceled => ReadBuildCanceledEventArgs(),
328329
_ => null
329330
};
330331

@@ -1275,6 +1276,15 @@ private BuildEventArgs ReadBuildCheckAcquisitionEventArgs()
12751276
return e;
12761277
}
12771278

1279+
private BuildEventArgs ReadBuildCanceledEventArgs()
1280+
{
1281+
var fields = ReadBuildEventArgsFields();
1282+
var e = new BuildCanceledEventArgs(fields.Message);
1283+
SetCommonFields(e, fields);
1284+
1285+
return e;
1286+
}
1287+
12781288
/// <summary>
12791289
/// For errors and warnings these 8 fields are written out explicitly
12801290
/// (their presence is not marked as a bit in the flags). So we have to

src/Build/Logging/BinaryLogger/BuildEventArgsWriter.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ Base types and inheritance ("EventArgs" suffix omitted):
187187
BuildSubmissionStarted
188188
BuildStarted
189189
BuildFinished
190+
BuildCanceled
190191
ProjectEvaluationStarted
191192
ProjectEvaluationFinished
192193
BuildError
@@ -215,6 +216,7 @@ private BinaryLogRecordKind WriteCore(BuildEventArgs e)
215216
case BuildSubmissionStartedEventArgs buildSubmissionStarted: return Write(buildSubmissionStarted);
216217
case BuildStartedEventArgs buildStarted: return Write(buildStarted);
217218
case BuildFinishedEventArgs buildFinished: return Write(buildFinished);
219+
case BuildCanceledEventArgs buildCanceled: return Write(buildCanceled);
218220
case ProjectEvaluationStartedEventArgs projectEvaluationStarted: return Write(projectEvaluationStarted);
219221
case ProjectEvaluationFinishedEventArgs projectEvaluationFinished: return Write(projectEvaluationFinished);
220222
case BuildCheckTracingEventArgs buildCheckTracing: return Write(buildCheckTracing);
@@ -307,6 +309,13 @@ private BinaryLogRecordKind Write(BuildFinishedEventArgs e)
307309
return BinaryLogRecordKind.BuildFinished;
308310
}
309311

312+
private BinaryLogRecordKind Write(BuildCanceledEventArgs e)
313+
{
314+
WriteBuildEventArgsFields(e);
315+
316+
return BinaryLogRecordKind.BuildCanceled;
317+
}
318+
310319
private BinaryLogRecordKind Write(ProjectEvaluationStartedEventArgs e)
311320
{
312321
WriteBuildEventArgsFields(e, writeMessage: false);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
using Shouldly;
11+
using Xunit;
12+
13+
namespace Microsoft.Build.Framework.UnitTests
14+
{
15+
public class BuildCanceledEventArgs_Tests
16+
{
17+
[Fact]
18+
public void SerializationDeserializationTest()
19+
{
20+
var message = "message";
21+
var datetime = DateTime.Today;
22+
23+
BuildCanceledEventArgs args = new(
24+
message,
25+
datetime
26+
);
27+
using MemoryStream stream = new MemoryStream();
28+
using BinaryWriter bw = new BinaryWriter(stream);
29+
args.WriteToStream(bw);
30+
31+
stream.Position = 0;
32+
using BinaryReader br = new BinaryReader(stream);
33+
BuildCanceledEventArgs argDeserialized = new("m");
34+
int packetVersion = (Environment.Version.Major * 10) + Environment.Version.Minor;
35+
36+
argDeserialized.CreateFromStream(br, packetVersion);
37+
argDeserialized.Message.ShouldBe(message);
38+
argDeserialized.Timestamp.ShouldBe(datetime);
39+
}
40+
}
41+
}

src/Framework.UnitTests/BuildSubmissionStartedEventAgs_Tests.cs renamed to src/Framework.UnitTests/BuildSubmissionStartedEventArgs_Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace Microsoft.Build.Framework.UnitTests
1414
{
15-
public class BuildSubmissionStartedEventAgs_Tests
15+
public class BuildSubmissionStartedEventArgs_Tests
1616
{
1717
[Fact]
1818
public void SerializationDeserializationTest()

src/Shared/LogMessagePacketBase.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ internal enum LoggingEventType : int
244244
/// Event is <see cref="BuildSubmissionStartedEventArgs"/>.
245245
/// </summary>
246246
BuildSubmissionStartedEvent = 40,
247+
248+
/// <summary>
249+
/// Event is <see cref="BuildCanceledEventArgs"/>
250+
/// </summary>
251+
BuildCanceledEvent = 41,
247252
}
248253
#endregion
249254

@@ -656,6 +661,7 @@ private BuildEventArgs GetBuildEventArgFromId()
656661
LoggingEventType.BuildCheckTracingEvent => new BuildCheckTracingEventArgs(),
657662
LoggingEventType.EnvironmentVariableReadEvent => new EnvironmentVariableReadEventArgs(),
658663
LoggingEventType.BuildSubmissionStartedEvent => new BuildSubmissionStartedEventArgs(),
664+
LoggingEventType.BuildCanceledEvent => new BuildCanceledEventArgs("Build canceled."),
659665
#endif
660666
_ => throw new InternalErrorException("Should not get to the default of GetBuildEventArgFromId ID: " + _eventType)
661667
};
@@ -799,6 +805,10 @@ private LoggingEventType GetLoggingEventId(BuildEventArgs eventArg)
799805
{
800806
return LoggingEventType.BuildSubmissionStartedEvent;
801807
}
808+
else if (eventType == typeof(BuildCanceledEventArgs))
809+
{
810+
return LoggingEventType.BuildCanceledEvent;
811+
}
802812
#endif
803813
else if (eventType == typeof(TargetStartedEventArgs))
804814
{

0 commit comments

Comments
 (0)