mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-03 00:11:34 -05:00
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>
58 lines
2.0 KiB
C#
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));
|
|
}
|
|
}
|