Files
Terminal.Gui/Tests/StressTests/ApplicationStressTests.cs
Copilot 6d53276be2 Fixes #4289 - Simplify Drawing/Color: unify named color handling under StandardColor and remove layered resolvers (#4432)
* Initial plan

* Delete AnsiColorNameResolver and MultiStandardColorNameResolver, add legacy 16-color names to StandardColor

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Refactor and enhance tests for Color, Region, and Lines

Refactored `Color` struct by removing unused methods and simplifying logic. Updated namespaces for better organization. Enhanced test coverage for `Color`, `Region`, and `LineCanvas` with new test cases, parameterized tests, and edge case handling.

Added `StraightLineExtensionsTests`, `StraightLineTests`, and `RegionClassTests` to validate behavior under various scenarios. Improved `MergeRectangles` stability and addressed crash patterns. Removed legacy features and unused code. Enhanced documentation and optimized performance in key methods.

* Improve Color struct and StandardColors functionality

Enhanced the Color struct to fully support the alpha channel for rendering intent while maintaining semantic color identity. Updated TryNameColor to ignore alpha when matching colors, ensuring transparency does not affect color resolution. Expanded XML documentation to clarify alpha channel usage and future alpha blending support.

Improved drawing documentation to explain the lifecycle, deferred rendering, and color support, including 24-bit true color and legacy 16-color compatibility. Added a new section on transparency and its role in rendering.

Revised StandardColors implementation to use modern C# features and ensure consistent ARGB mapping. Added comprehensive tests for StandardColors and Color, covering alpha handling, color parsing, thread safety, and aliased color resolution. Removed outdated tests relying on legacy behavior.

Enhanced code readability, maintainability, and test coverage to ensure correctness and backward compatibility.

* Code cleanup

* Code cleanup

* Fix warnings. Code cleanup

* Add comprehensive unit tests for ColorStrings class

Introduced a new test class `ColorStringsTests` under the
`DrawingTests.ColorTests` namespace to validate the functionality
of the `ColorStrings` class.

Key changes include:
- Added tests for `GetColorName` to verify behavior for standard
  and non-standard colors, ignoring alpha channels, and handling
  known colors.
- Added tests for `GetStandardColorNames` to ensure the method
  returns a non-empty, alphabetically sorted collection containing
  all `StandardColor` enum values.
- Implemented tests for `TryParseStandardColorName` to validate
  case-insensitive parsing, hex color support, handling invalid
  input, and `ReadOnlySpan<char>` compatibility.
- Added tests for `TryParseNamedColor` to verify parsing of named
  and hex colors, handling of aliases, and `ReadOnlySpan<char>`
  support.
- Added round-trip tests to ensure consistency between
  `GetColorName`, `TryParseNamedColor`, `GetStandardColorNames`,
  and `TryParseStandardColorName`.

These tests ensure robust validation of color parsing and naming
functionality.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: tig <585482+tig@users.noreply.github.com>
Co-authored-by: Tig <tig@users.noreply.github.com>
2025-12-03 11:09:02 -07:00

149 lines
5.6 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Diagnostics;
using Xunit.Abstractions;
// ReSharper disable AccessToDisposedClosure
namespace StressTests;
public class ApplicationStressTests
{
private const int NUM_INCREMENTS = 500;
private const int NUM_PASSES = 50;
private const int POLL_MS_DEBUGGER = 500;
private const int POLL_MS_NORMAL = 100;
private static volatile int _tbCounter;
#pragma warning disable IDE1006 // Naming Styles
private static readonly ManualResetEventSlim _wakeUp = new (false);
#pragma warning restore IDE1006 // Naming Styles
/// <summary>
/// Stress test for Application.Invoke to verify that invocations from background threads
/// are not lost or delayed indefinitely. Tests 25,000 concurrent invocations (50 passes × 500 increments).
/// </summary>
/// <remarks>
/// <para>
/// This test automatically adapts its timeout when running under a debugger (500ms vs 100ms)
/// to account for slower iteration times caused by debugger overhead.
/// </para>
/// <para>
/// See InvokeLeakTest_Analysis.md for technical details about the timing improvements made
/// to TimedEvents (Stopwatch-based timing) and Application.Invoke (MainLoop wakeup).
/// </para>
/// </remarks>
[Fact]
public async Task InvokeLeakTest ()
{
IApplication app = Application.Create ();
app.Init ("fake");
Random r = new ();
TextField tf = new ();
var top = new Window ();
top.Add (tf);
_tbCounter = 0;
int pollMs = Debugger.IsAttached ? POLL_MS_DEBUGGER : POLL_MS_NORMAL;
Task task = Task.Run (() => RunTest (app, r, tf, NUM_PASSES, NUM_INCREMENTS, pollMs));
// blocks here until the RequestStop is processed at the end of the test
app.Run (top);
await task; // Propagate exception if any occurred
Assert.Equal (NUM_INCREMENTS * NUM_PASSES, _tbCounter);
top.Dispose ();
app.Dispose ();
return;
void RunTest (IApplication application, Random random, TextField textField, int numPasses, int numIncrements, int pollMsValue)
{
for (var j = 0; j < numPasses; j++)
{
_wakeUp.Reset ();
for (var i = 0; i < numIncrements; i++)
{
Launch (application, random, textField, (j + 1) * numIncrements);
}
int maxWaitMs = pollMsValue * 50; // Maximum total wait time (5s normal, 25s debugger)
var elapsedMs = 0;
while (_tbCounter != (j + 1) * numIncrements) // Wait for tbCounter to reach expected value
{
int tbNow = _tbCounter;
// Wait for Application.TopRunnable to be running to ensure timed events can be processed
var topRunnableWaitMs = 0;
while (application.TopRunnableView is null or IRunnable { IsRunning: false })
{
Thread.Sleep (1);
topRunnableWaitMs++;
if (topRunnableWaitMs > maxWaitMs)
{
application.Invoke (application.Dispose);
throw new TimeoutException (
$"Timeout: TopRunnableView never started running on pass {j + 1}"
);
}
}
_wakeUp.Wait (pollMsValue);
elapsedMs += pollMsValue;
if (_tbCounter != tbNow)
{
elapsedMs = 0; // Reset elapsed time on progress
continue;
}
if (elapsedMs > maxWaitMs)
{
// No change after maximum wait: Idle handlers added via Application.Invoke have gone missing
application.Invoke (application.Dispose);
throw new TimeoutException (
$"Timeout: Increment lost. _tbCounter ({_tbCounter}) didn't "
+ $"change after waiting {maxWaitMs} ms (pollMs={pollMsValue}). "
+ $"Failed to reach {(j + 1) * numIncrements} on pass {j + 1}"
);
}
}
}
application.Invoke (application.Dispose);
}
static void Launch (IApplication application, Random random, TextField textField, int target)
{
Task.Run (() =>
{
Thread.Sleep (random.Next (2, 4));
application.Invoke (() =>
{
textField.Text = $"index{random.Next ()}";
Interlocked.Increment (ref _tbCounter);
if (target == _tbCounter)
{
// On last increment wake up the check
_wakeUp.Set ();
}
}
);
}
);
}
}
}