Files
ThreadPilot/Tests/ThreadPilot.Core.Tests/CpuAssignmentModeMigrationPolicyTests.cs
PrimeBuild-pc 7f75d41a36 fix: migrate saved rules off Automatic, not just profiles
1.7.2 moved profiles off Automatic because it applies only Windows CPU Sets,
a soft preference that leaves the affinity mask in Task Manager unchanged. It
did not move the rules already written to persistent_rules.json: a rule stores
the mode it was saved with, so every rule captured before 1.7.1 kept applying
CPU Sets on each process start - invisible to the user, and indistinguishable
from a rule that never runs at all.

ApplyToRules moves those once. Only rules carrying a CpuSelection: with a
legacy mask Automatic already applies a real affinity, and rewriting a working
rule buys nothing. A deliberate CpuSets or IdealProcessor choice is left alone.

The flag is deliberately separate from the profile one. Sharing it would skip
the rules of everyone who already launched 1.7.2, which is most of the people
this fixes. It is persisted only after the rules reach disk, so a failed write
is retried on the next launch instead of being recorded as done.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-26 23:38:13 +02:00

198 lines
7.9 KiB
C#

namespace ThreadPilot.Core.Tests
{
using System.Linq;
using ThreadPilot.Helpers;
using ThreadPilot.Models;
public sealed class CpuAssignmentModeMigrationPolicyTests
{
[Fact]
public void Apply_MovesAnUpgradedProfileOffAutomatic()
{
// The case that shipped broken: 1.7.1 changed the default, but a persisted Automatic
// won over it, so upgrading users kept soft CPU Sets and saw no affinity change.
var settings = new ApplicationSettingsModel
{
DefaultCpuAssignmentMode = CpuAssignmentMode.Automatic,
HasMigratedCpuAssignmentModeDefault = false,
HasSeenCpuAssignmentModeChangeNotice = false,
};
var changed = CpuAssignmentModeMigrationPolicy.Apply(settings);
Assert.True(changed);
Assert.Equal(CpuAssignmentMode.AffinityMask, settings.DefaultCpuAssignmentMode);
Assert.True(settings.HasMigratedCpuAssignmentModeDefault);
Assert.False(settings.HasSeenCpuAssignmentModeChangeNotice);
Assert.True(CpuAssignmentModeMigrationPolicy.ShouldShowNotice(settings));
}
[Theory]
[InlineData(CpuAssignmentMode.AffinityMask)]
[InlineData(CpuAssignmentMode.IdealProcessor)]
[InlineData(CpuAssignmentMode.CpuSets)]
public void Apply_LeavesAnyOtherModeAloneButStillRecordsCompletion(CpuAssignmentMode mode)
{
var settings = new ApplicationSettingsModel
{
DefaultCpuAssignmentMode = mode,
HasMigratedCpuAssignmentModeDefault = false,
HasSeenCpuAssignmentModeChangeNotice = false,
};
var changed = CpuAssignmentModeMigrationPolicy.Apply(settings);
// True because the completion flags changed: they have to be persisted, or the policy
// re-runs next launch and would undo a later deliberate switch to Automatic.
Assert.True(changed);
Assert.Equal(mode, settings.DefaultCpuAssignmentMode);
Assert.True(settings.HasMigratedCpuAssignmentModeDefault);
Assert.False(CpuAssignmentModeMigrationPolicy.ShouldShowNotice(settings));
}
[Fact]
public void Apply_DoesNotOverrideAutomaticChosenAfterTheMigration()
{
// Someone who read the notice and deliberately went back to Automatic must stay there.
var settings = new ApplicationSettingsModel
{
DefaultCpuAssignmentMode = CpuAssignmentMode.Automatic,
HasMigratedCpuAssignmentModeDefault = true,
HasSeenCpuAssignmentModeChangeNotice = true,
};
var changed = CpuAssignmentModeMigrationPolicy.Apply(settings);
Assert.False(changed);
Assert.Equal(CpuAssignmentMode.Automatic, settings.DefaultCpuAssignmentMode);
Assert.False(CpuAssignmentModeMigrationPolicy.ShouldShowNotice(settings));
}
[Fact]
public void Apply_IsIdempotent()
{
var settings = new ApplicationSettingsModel
{
DefaultCpuAssignmentMode = CpuAssignmentMode.Automatic,
};
Assert.True(CpuAssignmentModeMigrationPolicy.Apply(settings));
Assert.False(CpuAssignmentModeMigrationPolicy.Apply(settings));
Assert.Equal(CpuAssignmentMode.AffinityMask, settings.DefaultCpuAssignmentMode);
}
[Fact]
public void ShouldShowNotice_StaysTrueUntilAcknowledged()
{
// The notice is the only thing telling the user their mode moved, so a restart before
// they dismiss it must not swallow it.
var settings = new ApplicationSettingsModel { DefaultCpuAssignmentMode = CpuAssignmentMode.Automatic };
CpuAssignmentModeMigrationPolicy.Apply(settings);
Assert.True(CpuAssignmentModeMigrationPolicy.ShouldShowNotice(settings));
CpuAssignmentModeMigrationPolicy.Apply(settings);
Assert.True(CpuAssignmentModeMigrationPolicy.ShouldShowNotice(settings));
settings.HasSeenCpuAssignmentModeChangeNotice = true;
Assert.False(CpuAssignmentModeMigrationPolicy.ShouldShowNotice(settings));
}
[Fact]
public void CopyFrom_CarriesBothFlags()
{
var source = new ApplicationSettingsModel
{
HasMigratedCpuAssignmentModeDefault = true,
HasSeenCpuAssignmentModeChangeNotice = true,
HasMigratedPersistentRuleCpuAssignmentModes = true,
};
var target = new ApplicationSettingsModel();
target.CopyFrom(source);
Assert.True(target.HasMigratedCpuAssignmentModeDefault);
Assert.True(target.HasSeenCpuAssignmentModeChangeNotice);
Assert.True(target.HasMigratedPersistentRuleCpuAssignmentModes);
}
[Fact]
public void ApplyToRules_MovesASavedRuleWithACpuSelectionOffAutomatic()
{
// A rule saved before 1.7.1 keeps applying CPU Sets on every process start: nothing the
// user can see changes, so the rule is indistinguishable from one that never runs.
var rules = new[] { RuleWithSelection(CpuAssignmentMode.Automatic) };
var migrated = CpuAssignmentModeMigrationPolicy.ApplyToRules(rules);
Assert.NotNull(migrated);
Assert.Equal(CpuAssignmentMode.AffinityMask, migrated!.Single().CpuAssignmentMode);
Assert.Equal(rules[0].Id, migrated.Single().Id);
Assert.Equal(rules[0].CpuSelection, migrated.Single().CpuSelection);
}
[Fact]
public void ApplyToRules_IsIdempotent()
{
var rules = new[] { RuleWithSelection(CpuAssignmentMode.Automatic) };
var migrated = CpuAssignmentModeMigrationPolicy.ApplyToRules(rules);
Assert.Null(CpuAssignmentModeMigrationPolicy.ApplyToRules(migrated!));
}
[Theory]
[InlineData(CpuAssignmentMode.AffinityMask)]
[InlineData(CpuAssignmentMode.IdealProcessor)]
[InlineData(CpuAssignmentMode.CpuSets)]
public void ApplyToRules_LeavesADeliberateModeAlone(CpuAssignmentMode mode)
{
var rules = new[] { RuleWithSelection(mode) };
Assert.Null(CpuAssignmentModeMigrationPolicy.ApplyToRules(rules));
}
[Fact]
public void ApplyToRules_LeavesALegacyMaskRuleAlone()
{
// Automatic with a legacy mask already applies a real affinity, so there is nothing to
// repair - and rewriting it would change a working rule for no reason.
var rules = new[]
{
new PersistentProcessRule
{
ProcessName = "cs2",
IsEnabled = true,
LegacyAffinityMask = 0b1111,
ApplyAffinityOnStart = true,
CpuAssignmentMode = CpuAssignmentMode.Automatic,
},
};
Assert.Null(CpuAssignmentModeMigrationPolicy.ApplyToRules(rules));
}
[Fact]
public void ApplyToRules_LeavesARuleThatDoesNotApplyAffinityAlone()
{
var rules = new[] { RuleWithSelection(CpuAssignmentMode.Automatic) with { ApplyAffinityOnStart = false } };
Assert.Null(CpuAssignmentModeMigrationPolicy.ApplyToRules(rules));
}
private static PersistentProcessRule RuleWithSelection(CpuAssignmentMode mode) =>
new()
{
ProcessName = "cs2",
IsEnabled = true,
ApplyAffinityOnStart = true,
CpuAssignmentMode = mode,
CpuSelection = new CpuSelection
{
CpuSetIds = { 256u },
LogicalProcessors = { new ProcessorRef(0, 0, 0) },
GlobalLogicalProcessorIndexes = { 0 },
},
};
}
}