|
11 | 11 | using System.IO; |
12 | 12 | using System.Linq; |
13 | 13 | using System.Reflection; |
| 14 | +using System.Security.Cryptography; |
14 | 15 | using System.Text; |
15 | 16 | using System.Text.Json; |
16 | 17 | using System.Threading; |
@@ -102,7 +103,8 @@ static MSBuildCachePluginBase() => |
102 | 103 | nameof(_fileAccessRepository), |
103 | 104 | nameof(_cacheClient), |
104 | 105 | nameof(_ignoredOutputPatterns), |
105 | | - nameof(_identicalDuplicateOutputPatterns) |
| 106 | + nameof(_identicalDuplicateOutputPatterns), |
| 107 | + nameof(Settings) |
106 | 108 | )] |
107 | 109 | protected bool Initialized { get; private set; } |
108 | 110 |
|
@@ -347,6 +349,22 @@ private async Task<CacheResult> GetCacheResultInnerAsync(BuildRequestData buildR |
347 | 349 | return CacheResult.IndicateNonCacheHit(CacheResultType.CacheMiss); |
348 | 350 | } |
349 | 351 |
|
| 352 | + if (Settings.ForceCacheMissPercentage != 0) |
| 353 | + { |
| 354 | + // use a hash of the project id so that it is repeatable |
| 355 | +#pragma warning disable CA1850 // ComputeHash is not in net472 |
| 356 | + using SHA256 hasher = SHA256.Create(); |
| 357 | + byte[] projectHash = hasher.ComputeHash(Encoding.UTF8.GetBytes(nodeContext.Id)); |
| 358 | + uint hashInt = BitConverter.ToUInt32(projectHash, 0); |
| 359 | +#pragma warning restore CA1850 |
| 360 | + if ((hashInt % 100) < Settings.ForceCacheMissPercentage) |
| 361 | + { |
| 362 | + logger.LogMessage($"Forcing an otherwise 'cache hit' to be a 'cache miss' because of ForceCacheMissPercentage."); |
| 363 | + Interlocked.Increment(ref _cacheMissCount); |
| 364 | + return CacheResult.IndicateNonCacheHit(CacheResultType.CacheMiss); |
| 365 | + } |
| 366 | + } |
| 367 | + |
350 | 368 | CheckForDuplicateOutputs(logger, nodeBuildResult.Outputs, nodeContext); |
351 | 369 |
|
352 | 370 | await FinishNodeAsync(logger, nodeContext, pathSet, nodeBuildResult); |
|
0 commit comments