diff --git a/src/Tools/Source/RunTests/Cache/CachingTestExecutor.cs b/src/Tools/Source/RunTests/Cache/CachingTestExecutor.cs index 66b00a7ed0001b9be3a8358547c35f0f49bd9897..53b684a4ee07b89952bad87bfcc6d8d669cdd8a9 100644 --- a/src/Tools/Source/RunTests/Cache/CachingTestExecutor.cs +++ b/src/Tools/Source/RunTests/Cache/CachingTestExecutor.cs @@ -36,21 +36,23 @@ public async Task RunTestAsync(string assemblyPath, CancellationToke builder.AppendLine("==="); Logger.Log(builder.ToString()); - TestResult testResult; - CachedTestResult cachedTestResult; - if (!_dataStorage.TryGetCachedTestResult(contentFile.Checksum, out cachedTestResult)) + try { - Logger.Log($"{Path.GetFileName(assemblyPath)} - running"); - testResult = await _testExecutor.RunTestAsync(assemblyPath, cancellationToken); - Logger.Log($"{Path.GetFileName(assemblyPath)} - caching"); - CacheTestResult(contentFile, testResult); + var cachedTestResult = await _dataStorage.TryGetCachedTestResult(contentFile.Checksum); + if (cachedTestResult.HasValue) + { + Logger.Log($"{Path.GetFileName(assemblyPath)} - cache hit"); + return Migrate(assemblyPath, cachedTestResult.Value); + } } - else + catch (Exception ex) { - testResult = Migrate(assemblyPath, cachedTestResult); - Logger.Log($"{Path.GetFileName(assemblyPath)} - cache hit"); + Logger.Log($"Error reading cache {ex}"); } + Logger.Log($"{Path.GetFileName(assemblyPath)} - running"); + var testResult = await _testExecutor.RunTestAsync(assemblyPath, cancellationToken); + await CacheTestResult(contentFile, testResult).ConfigureAwait(true); return testResult; } @@ -77,7 +79,7 @@ private TestResult Migrate(string assemblyPath, CachedTestResult cachedTestResul errorOutput: cachedTestResult.ErrorOutput); } - private void CacheTestResult(ContentFile contentFile, TestResult testResult) + private async Task CacheTestResult(ContentFile contentFile, TestResult testResult) { try { @@ -88,7 +90,7 @@ private void CacheTestResult(ContentFile contentFile, TestResult testResult) errorOutput: testResult.ErrorOutput, resultsFileName: Path.GetFileName(testResult.ResultsFilePath), resultsFileContent: resultFileContent); - _dataStorage.AddCachedTestResult(contentFile, cachedTestResult); + await _dataStorage.AddCachedTestResult(contentFile, cachedTestResult).ConfigureAwait(true); } catch (Exception ex) { diff --git a/src/Tools/Source/RunTests/Cache/IDataStorage.cs b/src/Tools/Source/RunTests/Cache/IDataStorage.cs index b93c3ea753a9cd1417ea34e8ebd40fe9ae4671cf..1afdf04721bac07ac32d076fa5fffc9ed60db868 100644 --- a/src/Tools/Source/RunTests/Cache/IDataStorage.cs +++ b/src/Tools/Source/RunTests/Cache/IDataStorage.cs @@ -10,9 +10,9 @@ namespace RunTests.Cache { internal interface IDataStorage { - bool TryGetCachedTestResult(string checksum, out CachedTestResult testResult); + Task TryGetCachedTestResult(string checksum); - void AddCachedTestResult(ContentFile conentFile, CachedTestResult testResult); + Task AddCachedTestResult(ContentFile conentFile, CachedTestResult testResult); } internal struct CachedTestResult diff --git a/src/Tools/Source/RunTests/Cache/LocalDataStorage.cs b/src/Tools/Source/RunTests/Cache/LocalDataStorage.cs index 76f420ee971baae367a0e109ebe3d5bf6a68545e..0e012cb783494e189cac59b71875cdd42403e4e6 100644 --- a/src/Tools/Source/RunTests/Cache/LocalDataStorage.cs +++ b/src/Tools/Source/RunTests/Cache/LocalDataStorage.cs @@ -35,8 +35,20 @@ internal LocalDataStorage(string storagePath = null) _storagePath = storagePath ?? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), DirectoryName); } - public bool TryGetCachedTestResult(string checksum, out CachedTestResult testResult) + public Task TryGetCachedTestResult(string checksum) { + CachedTestResult testResult; + CachedTestResult? value = null; + if (TryGetCachedTestResult(checksum, out testResult)) + { + value = testResult; + } + + return Task.FromResult(value); + } + + public bool TryGetCachedTestResult(string checksum, out CachedTestResult testResult) + { testResult = default(CachedTestResult); var storageFolder = GetStorageFolder(checksum); @@ -70,7 +82,7 @@ public bool TryGetCachedTestResult(string checksum, out CachedTestResult testRes return false; } - public void AddCachedTestResult(ContentFile contentFile, CachedTestResult testResult) + public Task AddCachedTestResult(ContentFile contentFile, CachedTestResult testResult) { var checksum = contentFile.Checksum; var storagePath = Path.Combine(_storagePath, checksum); @@ -78,7 +90,7 @@ public void AddCachedTestResult(ContentFile contentFile, CachedTestResult testRe { if (!FileUtil.EnsureDirectory(storagePath)) { - return; + return Task.FromResult(true); } Write(checksum, StorageKind.ExitCode, testResult.ExitCode.ToString()); @@ -94,6 +106,8 @@ public void AddCachedTestResult(ContentFile contentFile, CachedTestResult testRe Logger.Log($"Failed to log {checksum} {e.Message}"); FileUtil.DeleteDirectory(storagePath); } + + return Task.FromResult(true); } private string GetStorageFolder(string checksum) diff --git a/src/Tools/Source/RunTests/Cache/WebDataStorage.cs b/src/Tools/Source/RunTests/Cache/WebDataStorage.cs new file mode 100644 index 0000000000000000000000000000000000000000..5f8968494553425c612ab461f0ac68bcb29e5a6f --- /dev/null +++ b/src/Tools/Source/RunTests/Cache/WebDataStorage.cs @@ -0,0 +1,24 @@ +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RunTests.Cache +{ + internal sealed class WebDataStorage : IDataStorage + { + public Task AddCachedTestResult(ContentFile conentFile, CachedTestResult testResult) + { + throw new NotImplementedException(); + } + + public Task TryGetCachedTestResult(string checksum) + { + throw new NotImplementedException(); + } + } +} diff --git a/src/Tools/Source/RunTests/RunTests.csproj b/src/Tools/Source/RunTests/RunTests.csproj index b4cf6aa0e30bad568321ef1e208e6866c848923c..6b9f437b1c23c1ab41ada43b3914e77b80791f7b 100644 --- a/src/Tools/Source/RunTests/RunTests.csproj +++ b/src/Tools/Source/RunTests/RunTests.csproj @@ -26,6 +26,7 @@ +