Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Threading.Channels;
#endif

using Microsoft.Testing.Platform;
using Microsoft.Testing.Platform.Configurations;
using Microsoft.Testing.Platform.Helpers;
using Microsoft.Testing.Platform.Logging;
Expand Down Expand Up @@ -37,7 +36,6 @@ internal sealed partial class AppInsightsProvider :
private readonly bool _isCi;
private readonly IEnvironment _environment;
private readonly ITestApplicationCancellationTokenSource _testApplicationCancellationTokenSource;
private readonly ITask _task;
private readonly IClock _clock;
private readonly ITelemetryInformation _telemetryInformation;
private readonly ITelemetryClientFactory _telemetryClientFactory;
Expand Down Expand Up @@ -86,7 +84,6 @@ public AppInsightsProvider(
_environment = environment;
_currentSessionId = sessionId;
_testApplicationCancellationTokenSource = testApplicationCancellationTokenSource;
_task = task;
_clock = clock;
_telemetryInformation = telemetryInformation;
_telemetryClientFactory = telemetryClientFactory;
Expand Down Expand Up @@ -189,7 +186,6 @@ private async Task IngestLoopAsync()
break;
#endif
case bool value:
RoslynDebug.Assert(false, $"Telemetry entry '{pair.Key}' contains a boolean value, boolean values should always be converted to string using: .{nameof(TelemetryExtensions.AsTelemetryBool)}()");
properties.Add(pair.Key, value.AsTelemetryBool());
break;
default:
Expand Down Expand Up @@ -264,7 +260,7 @@ private static void AssertHashed(string key, string value)
return;
}

RoslynDebug.Assert(false, $"Telemetry entry '{key}' contains an unhashed string value '{value}'. Strings need to be hashed using {nameof(Sha256Hasher)}.{nameof(Sha256Hasher.HashWithNormalizedCasing)}(), or white-listed.");
Microsoft.Testing.Platform.RoslynDebug.Assert(false, $"Telemetry entry '{key}' contains an unhashed string value '{value}'. Strings need to be hashed using {nameof(Sha256Hasher)}.{nameof(Sha256Hasher.HashWithNormalizedCasing)}(), or white-listed.");
}

#if NET7_0_OR_GREATER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,67 @@
appInsightsProvider.Dispose();
#endif
}

[TestMethod]
public async Task LogEvent_WithBooleanProperty_ConvertsValueToTelemetryString()
{
Mock<IEnvironment> environment = new();
Mock<IClock> clock = new();
Mock<IConfiguration> config = new();
Mock<ITelemetryInformation> telemetryInformation = new();

Mock<ILoggerFactory> loggerFactory = new();
loggerFactory.Setup(x => x.CreateLogger(It.IsAny<string>())).Returns(new Mock<ILogger>().Object);

var capturedProperties = new Dictionary<string, string>();
using ManualResetEventSlim trackEventCalled = new(initialState: false);
Mock<ITelemetryClient> testTelemetryClient = new();
testTelemetryClient.Setup(x => x.TrackEvent(It.IsAny<string>(), It.IsAny<Dictionary<string, string>>(), It.IsAny<Dictionary<string, double>>()))
.Callback((string _, Dictionary<string, string> properties, Dictionary<string, double> _) =>
{
foreach (KeyValuePair<string, string> pair in properties)
{
capturedProperties[pair.Key] = pair.Value;
}

trackEventCalled.Set();
});

Mock<ITelemetryClientFactory> telemetryClientFactory = new();
telemetryClientFactory.Setup(x => x.Create(It.IsAny<string?>(), It.IsAny<string>())).Returns(testTelemetryClient.Object);

CancellationTokenSource cancellationTokenSource = new();
Mock<ITestApplicationCancellationTokenSource> testApplicationCancellationTokenSource = new();
testApplicationCancellationTokenSource.Setup(x => x.CancellationToken).Returns(cancellationTokenSource.Token);

AppInsightsProvider appInsightsProvider = new(
environment.Object,
testApplicationCancellationTokenSource.Object,
new SystemTask(),
loggerFactory.Object,
clock.Object,
config.Object,
telemetryInformation.Object,
telemetryClientFactory.Object,
"sessionId");

await appInsightsProvider.LogEventAsync(
"Sample",
new Dictionary<string, object> { ["my.bool"] = true },
CancellationToken.None);

// Wait for the consumer loop to actually invoke TrackEvent before disposing,
// otherwise the dispose-time flush window can elapse on slower runners (notably net472)
// before the payload is processed.
Assert.IsTrue(trackEventCalled.Wait(TimeSpan.FromSeconds(30)), "Telemetry consumer did not invoke TrackEvent within the timeout.");

Check failure on line 207 in test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs#L207

test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs(207,23): error MSTEST0049: (NETCORE_ENGINEERING_TELEMETRY=Build) Consider using the overload that accepts a CancellationToken and pass 'TestContext.CancellationToken' (https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0049)

Check failure on line 207 in test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs#L207

test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs(207,23): error MSTEST0049: (NETCORE_ENGINEERING_TELEMETRY=Build) Consider using the overload that accepts a CancellationToken and pass 'TestContext.CancellationToken' (https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0049)

Check failure on line 207 in test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs#L207

test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs(207,23): error MSTEST0049: (NETCORE_ENGINEERING_TELEMETRY=Build) Consider using the overload that accepts a CancellationToken and pass 'TestContext.CancellationToken' (https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0049)

Check failure on line 207 in test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs#L207

test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs(207,23): error MSTEST0049: (NETCORE_ENGINEERING_TELEMETRY=Build) Consider using the overload that accepts a CancellationToken and pass 'TestContext.CancellationToken' (https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0049)

Check failure on line 207 in test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Debug)

test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs#L207

test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs(207,23): error MSTEST0049: (NETCORE_ENGINEERING_TELEMETRY=Build) Consider using the overload that accepts a CancellationToken and pass 'TestContext.CancellationToken' (https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0049)

Check failure on line 207 in test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Debug)

test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs#L207

test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs(207,23): error MSTEST0049: (NETCORE_ENGINEERING_TELEMETRY=Build) Consider using the overload that accepts a CancellationToken and pass 'TestContext.CancellationToken' (https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0049)

Check failure on line 207 in test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Release)

test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs#L207

test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs(207,23): error MSTEST0049: (NETCORE_ENGINEERING_TELEMETRY=Build) Consider using the overload that accepts a CancellationToken and pass 'TestContext.CancellationToken' (https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0049)

Check failure on line 207 in test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Release)

test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs#L207

test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs(207,23): error MSTEST0049: (NETCORE_ENGINEERING_TELEMETRY=Build) Consider using the overload that accepts a CancellationToken and pass 'TestContext.CancellationToken' (https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0049)

Check failure on line 207 in test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx

test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs#L207

test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs(207,23): error MSTEST0049: (NETCORE_ENGINEERING_TELEMETRY=Build) Consider using the overload that accepts a CancellationToken and pass 'TestContext.CancellationToken' (https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0049)

Check failure on line 207 in test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx

test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs#L207

test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs(207,23): error MSTEST0049: (NETCORE_ENGINEERING_TELEMETRY=Build) Consider using the overload that accepts a CancellationToken and pass 'TestContext.CancellationToken' (https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0049)

Check failure on line 207 in test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx

test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs#L207

test/UnitTests/Microsoft.Testing.Extensions.UnitTests/AppInsightsProviderTests.cs(207,23): error MSTEST0049: (NETCORE_ENGINEERING_TELEMETRY=Build) Consider using the overload that accepts a CancellationToken and pass 'TestContext.CancellationToken' (https://learn.microsoft.com/dotnet/core/testing/mstest-analyzers/mstest0049)

#if NETCOREAPP
await appInsightsProvider.DisposeAsync();
Comment thread
Evangelink marked this conversation as resolved.
#else
appInsightsProvider.Dispose();
#endif

Assert.IsTrue(capturedProperties.ContainsKey("my.bool"));
Assert.AreEqual(TelemetryProperties.True, capturedProperties["my.bool"]);
}
}
Loading