提交 dc73585b 编写于 作者: J Jared Parsons 提交者: Jared Parsons

Make IDataStorage async

In preparation for moving to web storage.
上级 6eeb7637
......@@ -36,21 +36,23 @@ public async Task<TestResult> 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)
{
......
......@@ -10,9 +10,9 @@ namespace RunTests.Cache
{
internal interface IDataStorage
{
bool TryGetCachedTestResult(string checksum, out CachedTestResult testResult);
Task<CachedTestResult?> TryGetCachedTestResult(string checksum);
void AddCachedTestResult(ContentFile conentFile, CachedTestResult testResult);
Task AddCachedTestResult(ContentFile conentFile, CachedTestResult testResult);
}
internal struct CachedTestResult
......
......@@ -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<CachedTestResult?> 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)
......
// 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<CachedTestResult?> TryGetCachedTestResult(string checksum)
{
throw new NotImplementedException();
}
}
}
......@@ -26,6 +26,7 @@
<Compile Include="Cache\ContentUtil.cs" />
<Compile Include="Cache\CachingTestExecutor.cs" />
<Compile Include="Cache\ConentFile.cs" />
<Compile Include="Cache\WebDataStorage.cs" />
<Compile Include="Constants.cs" />
<Compile Include="FileUtil.cs" />
<Compile Include="Cache\IDataStorage.cs" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册