refactoring tasks

This commit is contained in:
Michael Schimmel
2025-06-02 01:14:33 +02:00
parent 63484cd495
commit 86e2729a52
3 changed files with 30 additions and 76 deletions
+7 -47
View File
@@ -32,8 +32,6 @@ type
[Test]
procedure TestRunDelayedJobExecutesAfterAllNotifies;
[Test]
procedure TestRunDelayedJobDoesNotExecutePrematurely;
[Test]
procedure TestWaitForAlreadySetState;
[Test]
procedure TestThreadInfoMethods;
@@ -95,12 +93,12 @@ begin
jobExecuted := False;
jobCompletedLatch := TMycLatch.CreateLatch(1); // Changed from Signals.CreateLatch
FFactory.Run(0,
FFactory.Run(
procedure
begin
jobExecuted := True;
jobCompletedLatch.Notify;
end);
end).Notify;
FFactory.WaitFor(jobCompletedLatch.State);
Assert.IsTrue(jobExecuted, 'Immediate job should have executed');
@@ -111,13 +109,11 @@ var
jobExecuted: Boolean;
jobCompletedLatch: IMycLatch;
subscriber: IMycSubscriber;
startCount: Integer;
begin
jobExecuted := False;
startCount := 2;
jobCompletedLatch := TMycLatch.CreateLatch(1); // Changed from Signals.CreateLatch
subscriber := FFactory.Run(startCount,
subscriber := FFactory.Run(
procedure
begin
jobExecuted := True;
@@ -128,48 +124,12 @@ begin
// Use TMycLatch.Null for comparison
Assert.AreNotEqual<IMycSubscriber>(TMycLatch.Null, subscriber, 'Subscriber should not be TMycLatch.Null for StartCount > 0');
subscriber.Notify;
subscriber.Notify;
FFactory.WaitFor(jobCompletedLatch.State);
Assert.IsTrue(jobExecuted, 'Delayed job should execute after all notifications');
end;
procedure TMycTaskFactoryTests.TestRunDelayedJobDoesNotExecutePrematurely;
var
jobExecuted: Boolean;
jobCompleted: IMycLatch;
subscriber: IMycSubscriber;
startCount: Integer;
dummyWait: IMycLatch;
begin
jobExecuted := False;
startCount := 3;
jobCompleted := TMycLatch.CreateLatch(1); // Changed from Signals.CreateLatch
dummyWait := TMycLatch.CreateLatch(1); // Changed from Signals.CreateLatch
subscriber := FFactory.Run(startCount,
procedure
begin
jobExecuted := True;
jobCompleted.Notify;
end);
Assert.IsNotNull(subscriber, 'Run should return a subscriber for delayed job_P2');
subscriber.Notify;
Assert.IsFalse(jobExecuted, 'Job should not execute after one notification_P2');
subscriber.Notify;
Assert.IsFalse(jobExecuted, 'Job should not execute after two notifications_P2');
FFactory.Run(0, procedure begin dummyWait.Notify; end);
FFactory.WaitFor(dummyWait.State);
Assert.IsFalse(jobExecuted, 'Job should still not have executed before third notify_P2');
end;
procedure TMycTaskFactoryTests.TestWaitForAlreadySetState;
var
@@ -200,7 +160,7 @@ begin
Assert.IsTrue(FFactory.InMainThread, 'Test method itself should be in main thread');
Assert.IsFalse(FFactory.InWorkerThread, 'Test method itself should not be in a factory worker thread');
FFactory.Run(0,
FFactory.EnqueueJob(
procedure
begin
inMainThreadInJob := FFactory.InMainThread;
@@ -220,7 +180,7 @@ var
begin
jobDoneLatch := TMycLatch.CreateLatch(1);
FFactory.Run(0,
FFactory.EnqueueJob(
procedure
begin
try
@@ -255,7 +215,7 @@ begin
Assert.WillRaise(
procedure
begin
FFactory.Run(0, procedure begin end);
FFactory.EnqueueJob(procedure begin end);
end,
TMycTaskFactory.ETaskException,
'Running a job on a torn-down factory should raise ETaskException'
@@ -272,7 +232,7 @@ begin
jobDoneLatch := TMycLatch.CreateLatch(1);
dummyStateToWaitFor := TMycLatch.CreateLatch(1).State;
FFactory.Run(0,
FFactory.EnqueueJob(
procedure
begin
try