mirror of
https://github.com/MaSzyna-EU07/StarterNG.git
synced 2026-11-19 06:55:59 +01:00
fix: scenery files are looked at when they change, not every two seconds
The starter compared every file of every scenery each two seconds: on a full installation 175 sceneries and 378 files, 15-18 ms a look, thirty looks a minute with nothing changed. The system now reports changes in the scenery folder and a look follows once the burst of a save has passed, 400 ms after the last report. The reports are only a hint. A look still comes on coming back to the window and right after the simulator or its editor closes, and every 30 seconds anyway, for network drives, Wine and Linux past its limit of watches, where reports may not come. Where the folder cannot be watched at all the look stays every two seconds as before. A change reported during a look is not lost: one more look follows it.
This commit is contained in:
65
StarterNG.Tests/FolderChangesTests.cs
Normal file
65
StarterNG.Tests/FolderChangesTests.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using StarterNG.Infrastructure.Adapters;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
public sealed class FolderChangesTests : IDisposable
|
||||
{
|
||||
private static readonly TimeSpan Patience = TimeSpan.FromSeconds(10);
|
||||
private readonly string _root = Path.Combine(Path.GetTempPath(), "folder-changes-" + Guid.NewGuid().ToString("N"));
|
||||
|
||||
public FolderChangesTests() => Directory.CreateDirectory(Path.Combine(_root, "sub"));
|
||||
|
||||
public void Dispose() => Directory.Delete(_root, recursive: true);
|
||||
|
||||
private static async Task<bool> Reported(FolderChanges changes, Action change)
|
||||
{
|
||||
var told = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
changes.Changed += () => told.TrySetResult();
|
||||
change();
|
||||
return await Task.WhenAny(told.Task, Task.Delay(Patience)) == told.Task;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_file_written_below_the_folder_is_reported()
|
||||
{
|
||||
string file = Path.Combine(_root, "sub", "a.scm");
|
||||
File.WriteAllText(file, "old");
|
||||
using var changes = new FolderChanges(_root);
|
||||
|
||||
Assert.True(changes.Watching);
|
||||
Assert.True(await Reported(changes, () => File.WriteAllText(file, "new text")));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_save_through_a_renamed_temp_file_is_reported()
|
||||
{
|
||||
string file = Path.Combine(_root, "test.scn");
|
||||
File.WriteAllText(file, "old");
|
||||
using var changes = new FolderChanges(_root);
|
||||
|
||||
Assert.True(await Reported(changes, () =>
|
||||
{
|
||||
string temp = file + ".tmp";
|
||||
File.WriteAllText(temp, "new");
|
||||
File.Move(temp, file, overwrite: true);
|
||||
}));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_file_removed_is_reported()
|
||||
{
|
||||
string file = Path.Combine(_root, "gone.scn");
|
||||
File.WriteAllText(file, "x");
|
||||
using var changes = new FolderChanges(_root);
|
||||
|
||||
Assert.True(await Reported(changes, () => File.Delete(file)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_missing_folder_is_not_watched_and_does_not_throw()
|
||||
{
|
||||
using var changes = new FolderChanges(Path.Combine(_root, "nowhere"));
|
||||
|
||||
Assert.False(changes.Watching);
|
||||
}
|
||||
}
|
||||
80
StarterNG.Tests/OneAtATimeTests.cs
Normal file
80
StarterNG.Tests/OneAtATimeTests.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using StarterNG.Application;
|
||||
|
||||
namespace StarterNG.Tests;
|
||||
|
||||
public class OneAtATimeTests
|
||||
{
|
||||
private static async Task Until(Func<bool> condition)
|
||||
{
|
||||
var giveUp = DateTime.UtcNow.AddSeconds(5);
|
||||
while (!condition())
|
||||
{
|
||||
Assert.True(DateTime.UtcNow < giveUp, "waited too long");
|
||||
await Task.Delay(10);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Asked_once_it_runs_once()
|
||||
{
|
||||
int runs = 0;
|
||||
var once = new OneAtATime(() => { runs++; return Task.CompletedTask; });
|
||||
|
||||
await once.RunAsync();
|
||||
|
||||
Assert.Equal(1, runs);
|
||||
Assert.False(once.IsRunning);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Asked_while_running_it_runs_once_more_after_not_alongside()
|
||||
{
|
||||
int runs = 0, together = 0, most = 0;
|
||||
var gates = new Queue<TaskCompletionSource>();
|
||||
var once = new OneAtATime(async () =>
|
||||
{
|
||||
runs++;
|
||||
most = Math.Max(most, ++together);
|
||||
var gate = new TaskCompletionSource();
|
||||
gates.Enqueue(gate);
|
||||
await gate.Task;
|
||||
together--;
|
||||
});
|
||||
|
||||
var first = once.RunAsync();
|
||||
// Three requests during the first run make one more run, not three.
|
||||
await once.RunAsync();
|
||||
await once.RunAsync();
|
||||
await once.RunAsync();
|
||||
Assert.Equal(1, runs);
|
||||
|
||||
gates.Dequeue().SetResult();
|
||||
await Until(() => Volatile.Read(ref runs) == 2);
|
||||
await Until(() => gates.Count == 1);
|
||||
gates.Dequeue().SetResult();
|
||||
await first;
|
||||
|
||||
Assert.Equal(2, runs);
|
||||
Assert.Equal(1, most);
|
||||
Assert.False(once.IsRunning);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_failed_run_does_not_keep_it_busy()
|
||||
{
|
||||
bool fail = true;
|
||||
int runs = 0;
|
||||
var once = new OneAtATime(() =>
|
||||
{
|
||||
runs++;
|
||||
return fail ? Task.FromException(new IOException("locked")) : Task.CompletedTask;
|
||||
});
|
||||
|
||||
await Assert.ThrowsAsync<IOException>(once.RunAsync);
|
||||
fail = false;
|
||||
await once.RunAsync();
|
||||
|
||||
Assert.Equal(2, runs);
|
||||
Assert.False(once.IsRunning);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user