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

View File

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

View File

@ -22,6 +22,11 @@ public class Startup
.AddAllSingleton<IResource, IResource<Context>, Resource<Context>>( .AddAllSingleton<IResource, IResource<Context>, Resource<Context>>(
s => s =>
new ("VFS Context", 2)); 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 // Keep this fixed at 2 so that we can detect deadlocks in the VFS parallelOptions
service.AddSingleton(new ParallelOptions {MaxDegreeOfParallelism = 2}); service.AddSingleton(new ParallelOptions {MaxDegreeOfParallelism = 2});

View File

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

View File

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