Fix tests

This commit is contained in:
Timothy Baldridge 2021-10-23 16:27:59 -06:00
parent 3d61847b0b
commit fc2530d33f
5 changed files with 26 additions and 18 deletions

View File

@ -13,6 +13,7 @@ using Wabbajack.DTOs.Texture;
using Wabbajack.Hashing.PHash;
using Wabbajack.Paths.IO;
using Wabbajack.RateLimiter;
using Wabbajack.Services.OSIntegrated;
using Xunit;
namespace Wabbajack.Compiler.Test;
@ -28,11 +29,13 @@ public class CompilerSanityTests : IAsyncLifetime
private readonly IServiceProvider _serviceProvider;
private Mod _mod;
private ModList? _modlist;
private readonly LoggingRateLimiterReporter _reporter;
public CompilerSanityTests(ILogger<CompilerSanityTests> logger, IServiceProvider serviceProvider,
FileExtractor.FileExtractor fileExtractor,
TemporaryFileManager manager, ParallelOptions parallelOptions)
TemporaryFileManager manager, ParallelOptions parallelOptions, LoggingRateLimiterReporter reporter)
{
_reporter = reporter;
_logger = logger;
_serviceProvider = serviceProvider;
_scope = _serviceProvider.CreateScope();

View File

@ -15,7 +15,7 @@ public class Job<T> : IJob, IDisposable
public void Dispose()
{
if (!_isFinished) return;
if (_isFinished) return;
_isFinished = true;
Resource.Finish(this);
}

View File

@ -23,6 +23,11 @@ public class Startup
s =>
new ("VFS Context", 2));
service
.AddAllSingleton<IResource, IResource<FileHashCache>, Resource<FileHashCache>>(
s =>
new ("File Hash Cache", 2));
// Keep this fixed at 2 so that we can detect deadlocks in the VFS parallelOptions
service.AddSingleton(new ParallelOptions {MaxDegreeOfParallelism = 2});
service.AddSingleton(new FileHashCache(KnownFolders.EntryPoint.Combine("hashcache.sqlite"),

View File

@ -25,14 +25,16 @@ public class Context
public readonly FileExtractor.FileExtractor Extractor;
public readonly FileHashCache HashCache;
public readonly IResource<Context> Limiter;
public readonly IResource<FileHashCache> HashLimiter;
public readonly ILogger<Context> Logger;
public readonly VFSCache VfsCache;
public Context(ILogger<Context> logger, ParallelOptions parallelOptions, TemporaryFileManager manager,
VFSCache vfsCache,
FileHashCache hashCache, IResource<Context> limiter, FileExtractor.FileExtractor extractor)
FileHashCache hashCache, IResource<Context> limiter, IResource<FileHashCache> hashLimiter, FileExtractor.FileExtractor extractor)
{
Limiter = limiter;
HashLimiter = hashLimiter;
Logger = logger;
_manager = manager;
Extractor = extractor;
@ -41,6 +43,7 @@ public class Context
_parallelOptions = parallelOptions;
}
public IndexRoot Index { get; private set; } = IndexRoot.Empty;
public async Task<IndexRoot> AddRoot(AbsolutePath root, CancellationToken token)

View File

@ -162,21 +162,18 @@ public class VirtualFile
IPath relPath, CancellationToken token, int depth = 0)
{
Hash hash;
using (var job = await context.Limiter.Begin("Hash file", 0, token))
{
if (extractedFile is NativeFileStreamFactory)
{
var absPath = (AbsolutePath) extractedFile.Name;
job.Size = absPath.Size();
hash = await context.HashCache.FileHashCachedAsync(absPath, token);
}
else
{
using var job = await context.HashLimiter.Begin("Hashing memory stream", 0, token);
await using var hstream = await extractedFile.GetStream();
job.Size = hstream.Length;
hash = await hstream.HashingCopy(Stream.Null, token, job);
}
}
if (context.VfsCache.TryGetFromCache(context, parent, relPath, extractedFile, hash, out var vself))
return vself;
@ -201,7 +198,7 @@ public class VirtualFile
if (TextureExtensions.Contains(relPath.FileName.Extension) && await DDSSig.MatchesAsync(stream) != null)
try
{
using var job = await context.Limiter.Begin("Perceptual hash", self.Size, token);
using var job = await context.HashLimiter.Begin("Hashing image", 0, token);
self.ImageState = await ImageLoader.Load(stream);
await job.Report((int) self.Size, token);
stream.Position = 0;