Skip to content

Commit

Permalink
Add tests to cover TelemetryEventArg round-trip serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
baronfel committed Jul 30, 2024
1 parent f4b90a0 commit 036459b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Build.UnitTests/BuildEventArgsSerialization_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,7 @@ public void PropertyInitialValueEventArgs()
e => e.HelpKeyword,
e => e.SenderName);
}

[Fact]
public void ReadingCorruptedStreamThrows()
{
Expand Down
57 changes: 57 additions & 0 deletions src/Framework.UnitTests/CustomEventArgSerialization_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -974,5 +974,62 @@ private static void VerifyTaskFinished(TaskFinishedEventArgs genericEvent, TaskF
newGenericEvent.TaskFile.ShouldBe(genericEvent.TaskFile, StringCompareShould.IgnoreCase); // "Expected TaskFile to Match"
newGenericEvent.TaskName.ShouldBe(genericEvent.TaskName, StringCompareShould.IgnoreCase); // "Expected TaskName to Match"
}


[Fact]
public void TestTelemetryEventArgs()
{
// Test using reasonable values
TelemetryEventArgs genericEvent = new TelemetryEventArgs { EventName = "Good", Properties = new Dictionary<string, string> { { "Key", "Value" } } };
genericEvent.BuildEventContext = new BuildEventContext(5, 4, 3, 2);

// Serialize
genericEvent.WriteToStream(_writer);
long streamWriteEndPosition = _stream.Position;

// Deserialize and Verify
_stream.Position = 0;
TelemetryEventArgs newGenericEvent = new TelemetryEventArgs();
newGenericEvent.CreateFromStream(_reader, _eventArgVersion);
_stream.Position.ShouldBe(streamWriteEndPosition); // "Stream End Positions Should Match"
VerifyGenericEventArg(genericEvent, newGenericEvent);
VerifyTelemetryEvent(genericEvent, newGenericEvent);

// Test using null event name
_stream.Position = 0;
genericEvent = new TelemetryEventArgs { EventName = null, Properties = new Dictionary<string, string> { { "Key", "Value" } } };
// Serialize
genericEvent.WriteToStream(_writer);
streamWriteEndPosition = _stream.Position;

// Deserialize and Verify
_stream.Position = 0;
newGenericEvent = new TelemetryEventArgs();
newGenericEvent.CreateFromStream(_reader, _eventArgVersion);
_stream.Position.ShouldBe(streamWriteEndPosition); // "Stream End Positions Should Match"
VerifyGenericEventArg(genericEvent, newGenericEvent);
VerifyTelemetryEvent(genericEvent, newGenericEvent);

// Test using null property value name
_stream.Position = 0;
genericEvent = new TelemetryEventArgs { EventName = "Good", Properties = new Dictionary<string, string> { { "Key", null } } };
// Serialize
genericEvent.WriteToStream(_writer);
streamWriteEndPosition = _stream.Position;

// Deserialize and Verify
_stream.Position = 0;
newGenericEvent = new TelemetryEventArgs();
newGenericEvent.CreateFromStream(_reader, _eventArgVersion);
_stream.Position.ShouldBe(streamWriteEndPosition); // "Stream End Positions Should Match"
VerifyGenericEventArg(genericEvent, newGenericEvent);
VerifyTelemetryEvent(genericEvent, newGenericEvent);
}

private static void VerifyTelemetryEvent(TelemetryEventArgs expected, TelemetryEventArgs actual)
{
actual.EventName.ShouldBe(expected.EventName);
actual.Properties.ShouldBe(expected.Properties);
}
}
}

0 comments on commit 036459b

Please sign in to comment.