mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-26 07:47:54 +01:00
Revert incorrect FIFO fix - logic was inverted causing LIFO
Co-authored-by: tig <585482+tig@users.noreply.github.com>
This commit is contained in:
@@ -133,33 +133,24 @@ public class TimedEvents : ITimedEvents
|
||||
k -= 100;
|
||||
}
|
||||
|
||||
_timeouts.Add (NudgeToUniqueKey (k, time == TimeSpan.Zero), timeout);
|
||||
_timeouts.Add (NudgeToUniqueKey (k), timeout);
|
||||
Added?.Invoke (this, new (timeout, k));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds the closest number to <paramref name="k"/> that is not present in <see cref="_timeouts"/>.
|
||||
/// For immediate execution timeouts (decrementForImmediate=true), decrements to maintain FIFO order.
|
||||
/// For delayed timeouts, increments to maintain order.
|
||||
/// Finds the closest number to <paramref name="k"/> that is not present in <see cref="_timeouts"/>
|
||||
/// (incrementally).
|
||||
/// </summary>
|
||||
/// <param name="k">The initial key to try</param>
|
||||
/// <param name="decrementForImmediate">If true, decrements on collision; otherwise increments</param>
|
||||
/// <returns>A unique key</returns>
|
||||
private long NudgeToUniqueKey (long k, bool decrementForImmediate)
|
||||
/// <param name="k"></param>
|
||||
/// <returns></returns>
|
||||
private long NudgeToUniqueKey (long k)
|
||||
{
|
||||
lock (_timeoutsLockToken)
|
||||
{
|
||||
while (_timeouts.ContainsKey (k))
|
||||
{
|
||||
if (decrementForImmediate)
|
||||
{
|
||||
k--;
|
||||
}
|
||||
else
|
||||
{
|
||||
k++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,8 +185,7 @@ public class TimedEvents : ITimedEvents
|
||||
{
|
||||
lock (_timeoutsLockToken)
|
||||
{
|
||||
// When re-adding non-executed timeouts, increment on collision to maintain original order
|
||||
_timeouts.Add (NudgeToUniqueKey (k, decrementForImmediate: false), timeout);
|
||||
_timeouts.Add (NudgeToUniqueKey (k), timeout);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -661,35 +661,6 @@ public class MainLoopTests
|
||||
|
||||
Application.Shutdown ();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TimeSpanZero_Timeouts_Execute_In_FIFO_Order ()
|
||||
{
|
||||
var ml = new MainLoop (new FakeMainLoop ());
|
||||
var executionOrder = new List<int> ();
|
||||
|
||||
// Add multiple timeouts with TimeSpan.Zero in quick succession
|
||||
// They should execute in the order they were added (FIFO)
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
int capturedI = i;
|
||||
ml.TimedEvents.Add (TimeSpan.Zero, () =>
|
||||
{
|
||||
executionOrder.Add (capturedI);
|
||||
return false; // Don't repeat
|
||||
});
|
||||
}
|
||||
|
||||
// Run one iteration to execute all pending timeouts
|
||||
ml.RunIteration ();
|
||||
|
||||
// Verify they executed in FIFO order (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
|
||||
Assert.Equal (10, executionOrder.Count);
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
Assert.Equal (i, executionOrder [i]);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RemoveIdle_Function_NotCalled ()
|
||||
|
||||
Reference in New Issue
Block a user