Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions tests/Filee.Core.Tests/FakeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ internal sealed class FakeConverter(string id, params ConversionEdge[] edges) :
/// <summary>A notice this test engine leaves on the file (<see cref="ConversionStep.Notice"/>), when set.</summary>
public string? Notice { get; set; }

/// <summary>Converts only once this task is done, when set: keeps a job running while a test queues more.</summary>
public Task? Gate { get; set; }

/// <summary>Document fonts this test engine reports before writing.</summary>
public IReadOnlyList<string> FontsToReport { get; set; } = [];
public ConcurrentQueue<string?> OriginalSources { get; } = new();
Expand All @@ -44,7 +47,17 @@ internal sealed class FakeConverter(string id, params ConversionEdge[] edges) :

public EngineStatus GetStatus() => Available ? EngineStatus.Available() : EngineStatus.Unavailable("engine.reason.not_installed");

public Task<IReadOnlyList<string>> ConvertAsync(ConversionStep step, IProgress<double>? progress, CancellationToken cancellationToken)
public Task<IReadOnlyList<string>> ConvertAsync(ConversionStep step, IProgress<double>? progress, CancellationToken cancellationToken) =>
Gate is { } gate ? ConvertAfterAsync(gate, step, progress, cancellationToken) : Task.FromResult(Convert(step, progress));

private async Task<IReadOnlyList<string>> ConvertAfterAsync(Task gate, ConversionStep step, IProgress<double>? progress,
CancellationToken cancellationToken)
{
await gate.WaitAsync(cancellationToken);
return Convert(step, progress);
}

private IReadOnlyList<string> Convert(ConversionStep step, IProgress<double>? progress)
{
OriginalSources.Enqueue(step.OriginalSourcePath);
Presets.Enqueue(step.Preset);
Expand Down Expand Up @@ -81,7 +94,7 @@ public Task<IReadOnlyList<string>> ConvertAsync(ConversionStep step, IProgress<d
}
}
progress?.Report(1);
return Task.FromResult<IReadOnlyList<string>>(written);
return written;
}
}

Expand Down
7 changes: 5 additions & 2 deletions tests/Filee.Core.Tests/TrashOriginalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,10 @@ public async Task A_source_another_job_still_has_to_convert_is_kept_until_that_o
using var bin = new TempDir();
var source = dir.File("report.docx");
var trash = new FakeTrash(bin.Path);
await using var queue = new JobQueue(new ConverterCatalog([new FakeConverter("fake", new ConversionEdge("docx", "pdf"))]), moveToTrash: trash.Move);
// The first job is held until the second is queued: otherwise it can finish (and trash the source) first.
var gate = new TaskCompletionSource();
var engine = new FakeConverter("fake", new ConversionEdge("docx", "pdf")) { Gate = gate.Task };
await using var queue = new JobQueue(new ConverterCatalog([engine]), moveToTrash: trash.Move);
var finished = new TaskCompletionSource();
var count = 0;
queue.JobFinished += (_, _) =>
Expand All @@ -374,9 +377,9 @@ public async Task A_source_another_job_still_has_to_convert_is_kept_until_that_o

var first = queue.Enqueue([source], Trashing("pdf"), "First");
var second = queue.Enqueue([source], Trashing("pdf"), "Second");
gate.SetResult();
await finished.Task.WaitAsync(TimeSpan.FromSeconds(30), TestContext.Current.CancellationToken);


// Both converted. The first left the source for the second, which was the last one to need it.
Assert.Equal(FileState.Done, first.Files[0].State);
Assert.Equal(FileState.Done, second.Files[0].State);
Expand Down
Loading