Files
OpenNest/OpenNest.WinForms.Tests/Controls/ShapePreviewConcurrencyTests.cs
T
ajandClaude Opus 5.5 e5d9e12df9 test(desktop): share the square and rectangle part programs
Ten desktop test classes drew the same squares and the same 4 x 2
rectangle in private helpers, in two equivalent styles. They now use
TestPrograms.Square, ClockwiseSquare and Rectangle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 10:53:24 -05:00

58 lines
2.0 KiB
C#

using System.Drawing;
using System.Windows.Forms;
using OpenNest.CNC;
using OpenNest.Controls;
namespace OpenNest.WinForms.Tests.Controls;
public class ShapePreviewConcurrencyTests
{
// Parallel test collections paint previews on separate STA threads at once.
// GDI+ objects are not thread-safe, so any pen, brush or font shared between
// views fails with "Object is currently in use elsewhere".
[Fact]
public void PreviewsPaintAtTheSameTimeOnSeparateThreads()
{
const int threadCount = 4;
const int paints = 200;
var start = new Barrier(threadCount);
var failures = new List<Exception>();
var threads = new List<Thread>();
for (var t = 0; t < threadCount; t++)
{
var thread = new Thread(() =>
{
try
{
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
using var preview = new ShapePreviewControl { Size = new Size(200, 200) };
preview.ShowDrawing(new Drawing("square", TestPrograms.Square(10)));
preview.SetInfo("10 x 10", "1 part");
using var bitmap = new Bitmap(200, 200);
start.SignalAndWait(TimeSpan.FromSeconds(10));
for (var i = 0; i < paints; i++)
preview.DrawToBitmap(bitmap, new Rectangle(0, 0, 200, 200));
}
catch (Exception ex)
{
lock (failures)
failures.Add(ex);
}
})
{
IsBackground = true,
};
thread.SetApartmentState(ApartmentState.STA);
threads.Add(thread);
thread.Start();
}
foreach (var thread in threads)
Assert.True(thread.Join(TimeSpan.FromMinutes(1)), "A preview thread did not finish.");
Assert.True(failures.Count == 0, string.Join(Environment.NewLine, failures));
}
}