Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions MSBuildCache.sln
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.MSBuildCache.Loca
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.MSBuildCache.Repack.Tests", "src\Repack.Tests\Microsoft.MSBuildCache.Repack.Tests.csproj", "{3BCB6452-B087-4A03-8418-C79F2715DDE7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.MSBuildCache.AzurePipelines.Tests", "src\AzurePipelines.Tests\Microsoft.MSBuildCache.AzurePipelines.Tests.csproj", "{A8156008-A0EC-4F17-BCF3-A6F07AEF4D22}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Expand Down Expand Up @@ -68,6 +70,9 @@ Global
{3BCB6452-B087-4A03-8418-C79F2715DDE7}.Debug|x64.Build.0 = Debug|x64
{3BCB6452-B087-4A03-8418-C79F2715DDE7}.Release|x64.ActiveCfg = Release|x64
{3BCB6452-B087-4A03-8418-C79F2715DDE7}.Release|x64.Build.0 = Release|x64
{A8156008-A0EC-4F17-BCF3-A6F07AEF4D22}.Debug|x64.ActiveCfg = Debug|x64
{A8156008-A0EC-4F17-BCF3-A6F07AEF4D22}.Debug|x64.Build.0 = Debug|x64
{A8156008-A0EC-4F17-BCF3-A6F07AEF4D22}.Release|x64.ActiveCfg = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -80,6 +85,7 @@ Global
{97357681-C75E-445D-8547-46F312D01CED} = {EFFB5949-347C-4F28-8964-571D5C6B6209}
{F6586428-E047-42C8-B0AC-048DF6DFAF18} = {EFFB5949-347C-4F28-8964-571D5C6B6209}
{3BCB6452-B087-4A03-8418-C79F2715DDE7} = {EFFB5949-347C-4F28-8964-571D5C6B6209}
{A8156008-A0EC-4F17-BCF3-A6F07AEF4D22} = {EFFB5949-347C-4F28-8964-571D5C6B6209}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F1CDA78F-A666-431B-BF44-56DA7DF193BA}
Expand Down
39 changes: 39 additions & 0 deletions src/AzurePipelines.Tests/LoggingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using BuildXL.Cache.ContentStore.Interfaces.Logging;
using BuildXL.Cache.ContentStore.Interfaces.Tracing;
using BuildXL.Cache.ContentStore.Logging;
using Microsoft.MSBuildCache.AzurePipelines;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.MSBuildCache.Tests;

[TestClass]
public class LoggingTests
{
[TestMethod]
public void CacheContextIsEmbedded()
{
using ILogger logger = new Logger();
Context defaultContext = new(logger);
Context cacheContext1 = new(logger);
string message1 = "Hello world!";
string embedded = PipelineCachingCacheClient.EmbedCacheContext(cacheContext1, message1);
PipelineCachingCacheClient.TryExtractContext(embedded, defaultContext, out Context cacheContext2, out string message2);
Assert.AreEqual(message1, message2);
Assert.AreEqual(cacheContext1.TraceId, cacheContext2.TraceId);
}

[TestMethod]
public void CacheContextIsNotEmbedded()
{
using ILogger logger = new Logger();
Context defaultContext = new(logger);
Context cacheContext1 = new(logger);
string message1 = "Hello world!";
PipelineCachingCacheClient.TryExtractContext(message1, defaultContext, out Context cacheContext2, out string message2);
Assert.AreEqual(message1, message2);
Assert.AreEqual(defaultContext.TraceId, cacheContext2.TraceId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- Only supports x64 due to the RocksDB dependency -->
<Platform>x64</Platform>
<Platforms>x64</Platforms>
<TargetFrameworks>net472;net8.0</TargetFrameworks>
<RootNamespace>Microsoft.MSBuildCache.AzurePipelines.Tests</RootNamespace>
<!-- Suppress "Avoid constant arrays as arguments". UTs have many one-off test data arrays. -->
<NoWarn>$(NoWarn);CA1861</NoWarn>
<!-- Suppress "Nested types should not be visible". UTs need to be "public" for MSTest but aren't actualy public, so nested typed might need to be exposed. -->
<NoWarn>$(NoWarn);CA1034</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="MSTest.TestAdapter" />
<PackageReference Include="MSTest.TestFramework" />
<PackageReference Include="coverlet.collector" />
<PackageReference Include="morelinq" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AzurePipelines\Microsoft.MSBuildCache.AzurePipelines.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<Platforms>$(Platform)</Platforms>
<TargetFrameworks>net472;net8.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="Microsoft.MSBuildCache.AzurePipelines.Tests" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Common\Microsoft.MSBuildCache.Common.csproj" />
</ItemGroup>
Expand Down
25 changes: 14 additions & 11 deletions src/AzurePipelines/PipelineCachingCacheClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public PipelineCachingCacheClient(
_azureDevopsTracer = new CallbackAppTraceSource(
(rawMessage, level) =>
{
TryExtractContext(rawMessage, out Context cacheContext, out string message);
TryExtractContext(rawMessage, RootContext, out Context cacheContext, out string message);
message = $"PipelineCachingCacheClient [{level}]: {message}";
switch (level)
{
Expand Down Expand Up @@ -778,26 +778,29 @@ private string ConvertAbsolutePathToUriPath(string path)
return $"/{path}";
}

private const string EmbeddedCacheContextHeader = "[[CacheContext:";
private static readonly Regex extractCacheContext = new Regex(@"\[\[CacheContext:(.*)\]\](.*)", RegexOptions.Compiled);
private const string EmbeddedCacheContextHeader = "<<<CacheContext:";
private const string EmbeddedCacheContextTail = ">>>";
private static readonly Regex extractCacheContext = new Regex(@$"(.*){EmbeddedCacheContextHeader}(.*){EmbeddedCacheContextTail}(.*)", RegexOptions.Compiled);

private static string EmbedCacheContext(Context cacheContext, string message) =>
$"{EmbeddedCacheContextHeader}{cacheContext.TraceId}]]{message}";
internal static string EmbedCacheContext(Context cacheContext, string message) =>
$"{EmbeddedCacheContextHeader}{cacheContext.TraceId}{EmbeddedCacheContextTail}{message}";

private void TryExtractContext(string both, out Context context, out string message)
internal static void TryExtractContext(string both, Context defaultContext, out Context context, out string message)
{
Match match;
if (both.StartsWith(EmbeddedCacheContextHeader, StringComparison.Ordinal) &&
#pragma warning disable CA2249 // cross-compiling for netfx
if (both.IndexOf(EmbeddedCacheContextHeader, StringComparison.Ordinal) >= 0 &&
(match = extractCacheContext.Match(both)).Success &&
Guid.TryParse(match.Captures[0].Value, out Guid contextGuid))
Guid.TryParse(match.Groups[2].Value, out Guid contextGuid))
{
context = new Context(contextGuid, RootContext.Logger);
message = match.Captures[1].Value;
#pragma warning restore CA2249 // cross-compiling for netfx
context = new Context(contextGuid, defaultContext.Logger);
message = match.Groups[1].Value + match.Groups[3].Value;
}
else
{
message = both;
context = RootContext;
context = defaultContext;
}
}
}