Improved the pauses between indexing steps and between files of the Batch Processing Assistant (#1240)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-27 14:39:17 +01:00
1 parent a117074972
commit 899139c88b
32 files changed
+787 -425

No files matched your search

@@ -0,0 +1,124 @@
using System.Linq.Expressions;
using AIStudio.Settings;
using AIStudio.Settings.DataModel;
using Lua;
using Lua.Standard;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Abstractions;
namespace AIStudio.Tests.Settings;
/// <summary>
/// Checks how a configuration plugin sets the bounds of the pause between two files of the Batch
/// Processing Assistant.
/// </summary>
/// <remarks>
/// The settings are reached through Program.SERVICE_PROVIDER, as in the app, because a managed setting
/// writes its value and its lock there. The plugin passes the check of PluginConfiguration.
/// </remarks>
[TestFixture]
public sealed class BatchProcessingPauseTests
{
private static readonly Guid PLUGIN_ID = Guid.Parse("5c1e8f27-94ad-4b36-a0f2-7d3b6e91c4a8");
private SettingsManager settingsManager = null!;
private ServiceProvider serviceProvider = null!;
private IServiceProvider previousServiceProvider = null!;
[SetUp]
public void CreateSettings()
{
// Creating the settings data registers every managed setting:
this.settingsManager = new SettingsManager(NullLogger<SettingsManager>.Instance, null!);
this.previousServiceProvider = Program.SERVICE_PROVIDER;
this.serviceProvider = new ServiceCollection().AddSingleton(this.settingsManager).BuildServiceProvider();
Program.SERVICE_PROVIDER = this.serviceProvider;
}
[TearDown]
public void RestoreApplicationState()
{
// The managed state of a setting outlives this fixture, so we release what the plugin took over:
ManagedConfiguration.CleanupLeftOverManagedConfigurations([], new HashSet<Guid>());
Program.SERVICE_PROVIDER = this.previousServiceProvider;
this.serviceProvider.Dispose();
}
[Test]
public async Task APluginLocksTheUpperBound()
{
var processed = await ProcessAsync(x => x.MaximumDelaySeconds, """SETTINGS["DataBatchProcessing.MaximumDelaySeconds"] = 30""");
using (Assert.EnterMultipleScope())
{
Assert.That(processed, Is.True);
Assert.That(this.settingsManager.ConfigurationData.BatchProcessing.MaximumDelaySeconds, Is.EqualTo(30));
Assert.That(DataBatchProcessing.IsMaximumPauseLocked, Is.True);
Assert.That(DataBatchProcessing.IsMinimumPauseLocked, Is.False);
}
}
[TestCase(5, TestName = "An upper bound below 6 seconds is rejected")]
[TestCase(301, TestName = "An upper bound above 300 seconds is rejected")]
public async Task AnUpperBoundOutsideTheLimitsIsRejected(int configuredSeconds)
{
this.settingsManager.ConfigurationData.BatchProcessing.MaximumDelaySeconds = 20;
var processed = await ProcessAsync(x => x.MaximumDelaySeconds, $"""SETTINGS["DataBatchProcessing.MaximumDelaySeconds"] = {configuredSeconds}""");
using (Assert.EnterMultipleScope())
{
Assert.That(processed, Is.False);
Assert.That(this.settingsManager.ConfigurationData.BatchProcessing.MaximumDelaySeconds, Is.EqualTo(20), "A rejected value leaves the choice of the user untouched.");
Assert.That(DataBatchProcessing.IsMaximumPauseLocked, Is.False, "A rejected value locks nothing.");
}
}
[Test]
public async Task ABoundWhichUsersMayChangeIsNoLock()
{
const string SETTINGS = """
SETTINGS["DataBatchProcessing.MinimumDelaySeconds"] = 12
SETTINGS["DataBatchProcessing.MinimumDelaySeconds.AllowUserOverride"] = true
SETTINGS["DataBatchProcessing.MaximumDelaySeconds"] = 30
SETTINGS["DataBatchProcessing.MaximumDelaySeconds.AllowUserOverride"] = true
""";
var minimumProcessed = await ProcessAsync(x => x.MinimumDelaySeconds, SETTINGS);
var maximumProcessed = await ProcessAsync(x => x.MaximumDelaySeconds, SETTINGS);
using (Assert.EnterMultipleScope())
{
Assert.That(minimumProcessed, Is.True);
Assert.That(maximumProcessed, Is.True);
Assert.That(DataBatchProcessing.IsMinimumPauseLocked, Is.False, "Users may change a lower bound which the organization only presets.");
Assert.That(DataBatchProcessing.IsMaximumPauseLocked, Is.False, "Users may change an upper bound which the organization only presets.");
}
}
/// <summary>
/// Lets a configuration plugin set a bound of the pause.
/// </summary>
/// <param name="bound">The bound to process.</param>
/// <param name="settings">The Lua lines which fill the table SETTINGS of the plugin.</param>
/// <returns>Whether the plugin's value was taken.</returns>
private static async Task<bool> ProcessAsync(Expression<Func<DataBatchProcessing, int>> bound, string settings)
{
var state = LuaState.Create();
state.OpenBasicLibrary();
await state.DoStringAsync($"""
SETTINGS = {"{}"}
{settings}
""");
if (!state.Environment["SETTINGS"].TryRead<LuaTable>(out var settingsTable))
throw new InvalidOperationException("The settings of this test are not a Lua table.");
return ManagedConfiguration.TryProcessConfiguration(x => x.BatchProcessing, bound, PLUGIN_ID, settingsTable, false, validator: DataBatchProcessing.PAUSE_LIMITS.Allows);
}
}
-72
View File
@@ -1,72 +0,0 @@
using AIStudio.Settings.DataModel;
using AIStudio.Tools.Services.Indexing;
namespace AIStudio.Tests.Tools;
/// <summary>
/// Checks which bounds of the pause between two indexing steps apply, as the user set them and as configuration plugins lock them.
/// </summary>
[TestFixture]
public sealed class PauseRangeTests
{
[Test]
public void BoundsInOrderStayAsTheyAre()
{
Assert.That(PauseRange.GetEffective(10, false, 20, false), Is.EqualTo(new PauseRange(10, 20)));
}
[Test]
public void AnUpperBoundBelowTheLowerOneIsRaisedToIt()
{
Assert.That(PauseRange.GetEffective(60, false, 20, false), Is.EqualTo(new PauseRange(60, 60)));
}
[Test]
public void ALockedLowerBoundRaisesTheUpperOne()
{
Assert.That(PauseRange.GetEffective(60, true, 20, false), Is.EqualTo(new PauseRange(60, 60)));
}
[Test]
public void ALockedUpperBoundLowersTheLowerOne()
{
Assert.That(PauseRange.GetEffective(60, false, 20, true), Is.EqualTo(new PauseRange(20, 20)));
}
[Test]
public void BothBoundsLockedTheWrongWayRoundLetTheLongerPauseWin()
{
Assert.That(PauseRange.GetEffective(60, true, 20, true), Is.EqualTo(new PauseRange(60, 60)));
}
[Test]
public void BoundsOutsideTheAllowedRangeAreClamped()
{
Assert.That(PauseRange.GetEffective(-5, false, 1_000, false), Is.EqualTo(new PauseRange(0, DataDataSourceIndexing.MAX_PAUSE_BETWEEN_STEPS_SECONDS)));
}
[Test]
public void BoundsInOrderComeFromTheSettingsWithoutAskingForLocks()
{
var settings = new DataDataSourceIndexing
{
MinimumPauseBetweenStepsSeconds = 10,
MaximumPauseBetweenStepsSeconds = 20
};
Assert.That(PauseRange.FromSettings(settings), Is.EqualTo(new PauseRange(10, 20)));
}
[Test]
public void OnlyZeroForBothBoundsMeansNoPause()
{
using (Assert.EnterMultipleScope())
{
Assert.That(PauseRange.GetEffective(0, false, 0, false).IsNone, Is.True);
Assert.That(PauseRange.GetEffective(0, false, 5, false).IsNone, Is.False);
Assert.That(PauseRange.GetEffective(30, false, 0, false), Is.EqualTo(new PauseRange(30, 30)));
Assert.That(new DataDataSourceIndexing().MinimumPauseBetweenStepsSeconds, Is.Zero);
Assert.That(new DataDataSourceIndexing().MaximumPauseBetweenStepsSeconds, Is.Zero);
}
}
}
@@ -1,6 +1,6 @@
using AIStudio.Tools.Services.Indexing;
using AIStudio.Tools.Pauses;
namespace AIStudio.Tests.Tools;
namespace AIStudio.Tests.Tools.Pauses;
/// <summary>
/// Checks the pause between two indexing steps, which spares an embedding server that many people share.
+147
View File
@@ -0,0 +1,147 @@
using AIStudio.Settings.DataModel;
using AIStudio.Tools.Pauses;
namespace AIStudio.Tests.Tools.Pauses;
/// <summary>
/// Checks which bounds of a random pause apply, as the user set them, as configuration plugins lock them, and within the limits of each feature.
/// </summary>
[TestFixture]
public sealed class PauseRangeTests
{
private static readonly Func<bool> LOCKED = () => true;
private static readonly Func<bool> UNLOCKED = () => false;
private static readonly PauseLimits INDEXING = DataDataSourceIndexing.PAUSE_LIMITS;
private static readonly PauseLimits BATCH_PROCESSING = DataBatchProcessing.PAUSE_LIMITS;
[Test]
public void BoundsInOrderStayAsTheyAre()
{
Assert.That(PauseRange.GetEffective(INDEXING, 10, UNLOCKED, 20, UNLOCKED), Is.EqualTo(new PauseRange(10, 20)));
}
[Test]
public void AnUpperBoundBelowTheLowerOneIsRaisedToIt()
{
Assert.That(PauseRange.GetEffective(INDEXING, 60, UNLOCKED, 20, UNLOCKED), Is.EqualTo(new PauseRange(60, 60)));
}
[Test]
public void ALockedLowerBoundRaisesTheUpperOne()
{
Assert.That(PauseRange.GetEffective(INDEXING, 60, LOCKED, 20, UNLOCKED), Is.EqualTo(new PauseRange(60, 60)));
}
[Test]
public void ALockedUpperBoundLowersTheLowerOne()
{
Assert.That(PauseRange.GetEffective(INDEXING, 60, UNLOCKED, 20, LOCKED), Is.EqualTo(new PauseRange(20, 20)));
}
[Test]
public void BothBoundsLockedTheWrongWayRoundLetTheLongerPauseWin()
{
Assert.That(PauseRange.GetEffective(INDEXING, 60, LOCKED, 20, LOCKED), Is.EqualTo(new PauseRange(60, 60)));
}
[Test]
public void BoundsOutsideTheLimitsAreClamped()
{
using (Assert.EnterMultipleScope())
{
Assert.That(PauseRange.GetEffective(INDEXING, -5, UNLOCKED, 1_000, UNLOCKED), Is.EqualTo(new PauseRange(0, 300)));
Assert.That(PauseRange.GetEffective(BATCH_PROCESSING, -5, UNLOCKED, 1_000, UNLOCKED), Is.EqualTo(new PauseRange(6, 300)));
}
}
[Test]
public void BoundsBelowTheShortestPauseOfAFeatureAreRaisedToIt()
{
using (Assert.EnterMultipleScope())
{
Assert.That(PauseRange.GetEffective(BATCH_PROCESSING, 0, UNLOCKED, 3, UNLOCKED), Is.EqualTo(new PauseRange(6, 6)));
Assert.That(PauseRange.GetEffective(BATCH_PROCESSING, 2, UNLOCKED, 20, UNLOCKED), Is.EqualTo(new PauseRange(6, 20)));
}
}
[Test]
public void ALockedUpperBoundLowersTheLowerOneOnlyToTheShortestPauseOfAFeature()
{
Assert.That(PauseRange.GetEffective(BATCH_PROCESSING, 60, UNLOCKED, 2, LOCKED), Is.EqualTo(new PauseRange(6, 6)));
}
[Test]
public void TheLocksAreAskedOnlyWhileTheBoundsAreTheWrongWayRound()
{
var asked = 0;
var countingLock = () =>
{
asked++;
return false;
};
PauseRange.GetEffective(INDEXING, 10, countingLock, 10, countingLock);
PauseRange.GetEffective(INDEXING, 10, countingLock, 20, countingLock);
PauseRange.GetEffective(BATCH_PROCESSING, 0, countingLock, 3, countingLock);
Assert.That(asked, Is.Zero);
PauseRange.GetEffective(INDEXING, 20, countingLock, 10, countingLock);
Assert.That(asked, Is.Positive);
}
[Test]
public void TheIndexingTakesItsBoundsFromItsSettings()
{
var settings = new DataDataSourceIndexing
{
MinimumPauseBetweenStepsSeconds = 10,
MaximumPauseBetweenStepsSeconds = 20
};
Assert.That(settings.EffectivePauseRange, Is.EqualTo(new PauseRange(10, 20)));
}
[Test]
public void TheBatchProcessingPausesSixToTenSecondsByDefault()
{
var settings = new DataBatchProcessing();
Assert.That(PauseRange.GetEffective(BATCH_PROCESSING, settings.MinimumDelaySeconds, UNLOCKED, settings.MaximumDelaySeconds, UNLOCKED), Is.EqualTo(new PauseRange(6, 10)));
}
[Test]
public void OnlyZeroForBothBoundsMeansNoPause()
{
using (Assert.EnterMultipleScope())
{
Assert.That(PauseRange.GetEffective(INDEXING, 0, UNLOCKED, 0, UNLOCKED).IsNone, Is.True);
Assert.That(PauseRange.GetEffective(INDEXING, 0, UNLOCKED, 5, UNLOCKED).IsNone, Is.False);
Assert.That(PauseRange.GetEffective(INDEXING, 30, UNLOCKED, 0, UNLOCKED), Is.EqualTo(new PauseRange(30, 30)));
Assert.That(new DataDataSourceIndexing().EffectivePauseRange.IsNone, Is.True);
}
}
[Test]
public void TheBatchProcessingAlwaysPauses()
{
Assert.That(PauseRange.GetEffective(BATCH_PROCESSING, 0, UNLOCKED, 0, UNLOCKED).IsNone, Is.False);
}
[Test]
public void TheLimitsAllowTheirShortestAndLongestPause()
{
using (Assert.EnterMultipleScope())
{
Assert.That(INDEXING.Allows(0), Is.True);
Assert.That(INDEXING.Allows(-1), Is.False);
Assert.That(INDEXING.Allows(300), Is.True);
Assert.That(INDEXING.Allows(301), Is.False);
Assert.That(BATCH_PROCESSING.Allows(5), Is.False);
Assert.That(BATCH_PROCESSING.Allows(6), Is.True);
Assert.That(BATCH_PROCESSING.Allows(300), Is.True);
Assert.That(BATCH_PROCESSING.Allows(301), Is.False);
}
}
}