Refactoring

This commit is contained in:
Michael Schimmel
2025-06-02 13:19:13 +02:00
parent 762e7f83e2
commit 48f4c945ce
9 changed files with 98 additions and 402 deletions
+2 -1
View File
@@ -26,7 +26,8 @@ uses
Myc.Futures in '..\Src\Myc.Futures.pas',
TestFutures in 'TestFutures.pas',
Myc.TaskManager in '..\Src\Myc.TaskManager.pas',
Myc.Core.Futures in '..\Src\Myc.Core.Futures.pas';
Myc.Core.Futures in '..\Src\Myc.Core.Futures.pas',
Myc.Core.Signals in '..\Src\Myc.Core.Signals.pas';
{ keep comment here to protect the following conditional from being removed by the IDE when adding a unit }
{$IFNDEF TESTINSIGHT}
+1
View File
@@ -126,6 +126,7 @@ $(PostBuildEvent)]]></PostBuildEvent>
<DCCReference Include="TestFutures.pas"/>
<DCCReference Include="..\Src\Myc.TaskManager.pas"/>
<DCCReference Include="..\Src\Myc.Core.Futures.pas"/>
<DCCReference Include="..\Src\Myc.Core.Signals.pas"/>
<BuildConfiguration Include="Base">
<Key>Base</Key>
</BuildConfiguration>
+8 -8
View File
@@ -94,7 +94,7 @@ const
CExpectedResult = 42;
begin
// Use TMycLatch.Null for an already set init state [cite: 71, 81, 206, 216, 324, 334]
LInitStateAsState := TMycLatch.Null.State; // Get IMycState from IMycLatch [cite: 61, 196, 314]
LInitStateAsState := State.Null;
LFuture := TMycInitStateFuncFuture<Integer>.Create(FTaskFactory, LInitStateAsState,
function: Integer
@@ -120,7 +120,7 @@ var
const
CExpectedResult = 'ChainCompleted';
begin
LInitLatch := TMycLatch.CreateLatch(1); // Create an init state that is not yet set [cite: 77, 212, 330]
LInitLatch := State.CreateLatch(1); // Create an init state that is not yet set [cite: 77, 212, 330]
LFuture := TMycInitStateFuncFuture<string>.Create(FTaskFactory, LInitLatch.State,
function: string
@@ -155,7 +155,7 @@ const
begin
LExpectedExceptionRaisedByFactory := False;
LLocalTaskFactory := TMycTaskFactory.Create;
LInitStateAsState := TMycLatch.Null.State; // Immediate execution
LInitStateAsState := State.Null; // Immediate execution
LFuture := TMycInitStateFuncFuture<Integer>.Create(LLocalTaskFactory, LInitStateAsState,
function: Integer
@@ -208,7 +208,7 @@ var
LFuture: IMycFuture<Integer>;
LInitLatch: IMycLatch;
begin
LInitLatch := TMycLatch.CreateLatch(1);
LInitLatch := State.CreateLatch(1);
LFuture := TMycInitStateFuncFuture<Integer>.Create(FTaskFactory, LInitLatch.State,
function: Integer
@@ -250,7 +250,7 @@ begin
FProcExecutionCount := 0; // Reset for this test
// Gate Latch: MainFuture waits for this latch, which needs 2 notifications.
LGateLatch := TMycLatch.CreateLatch(2); // [cite: 77, 212, 330]
LGateLatch := State.CreateLatch(2); // [cite: 77, 212, 330]
// Create MainFuture, AInitState is the GateLatch's state.
LMainFuture := TMycInitStateFuncFuture<string>.Create(FTaskFactory, LGateLatch.State,
@@ -262,7 +262,7 @@ begin
// Setup Prerequisite Futures
// PrerequisiteFuture1
LInitStateP1 := TMycLatch.CreateLatch(1); // Controllable init state for PF1
LInitStateP1 := State.CreateLatch(1); // Controllable init state for PF1
LPrerequisiteFuture1 := TMycInitStateFuncFuture<Integer>.Create(FTaskFactory, LInitStateP1.State,
function: Integer
begin
@@ -273,7 +273,7 @@ begin
Subscriptions[1] := LPrerequisiteFuture1.Done.Subscribe(LSub1); // Subscribe to PF1's completion [cite: 56, 188, 306]
// PrerequisiteFuture2
LInitStateP2 := TMycLatch.CreateLatch(1); // Controllable init state for PF2
LInitStateP2 := State.CreateLatch(1); // Controllable init state for PF2
LPrerequisiteFuture2 := TMycInitStateFuncFuture<Integer>.Create(FTaskFactory, LInitStateP2.State,
function: Integer
begin
@@ -322,7 +322,7 @@ begin
LFlagFutureBRan := False;
Self.FSharedCounter := 0; // Reset shared counter for this test
LTriggerLatch := TMycLatch.CreateLatch(1); // Single trigger [cite: 77, 212, 330]
LTriggerLatch := State.CreateLatch(1); // Single trigger [cite: 77, 212, 330]
// Future A
LFutureA := TMycInitStateFuncFuture<Integer>.Create(FTaskFactory, LTriggerLatch.State,
+2 -2
View File
@@ -105,7 +105,7 @@ end;
function TTestMycDirtyFlag.CreateAndPrepareDirtyFlag(StartDirty: Boolean = True): IMycDirty;
begin
Result := TMycDirty.CreateDirty; // Initially dirty
Result := State.CreateDirty; // Initially dirty
if not StartDirty then
Result.Reset; // Reset to make it clean
Assert.AreEqual(StartDirty, Result.State.IsSet, 'CreateAndPrepareDirtyFlag initial state incorrect.');
@@ -127,7 +127,7 @@ procedure TTestMycDirtyFlag.TestCreate_InitialStateIsDirty;
var
dirtyFlag: IMycDirty;
begin
dirtyFlag := TMycDirty.CreateDirty;
dirtyFlag := State.CreateDirty;
Assert.IsTrue(dirtyFlag.State.IsSet, 'Newly created dirty flag should be IsSet (dirty).');
end;
+22 -22
View File
@@ -124,7 +124,7 @@ var
latch: IMycLatch;
begin
// TMycLatch.CreateLatch returns TMycLatch.Null if InitialCount <= 0
latch := TMycLatch.CreateLatch(InitialCount);
latch := State.CreateLatch(InitialCount);
Assert.AreEqual(ExpectedIsSet, latch.State.IsSet, 'Latch initial IsSet state mismatch for count ' + IntToStr(InitialCount) + '.');
end;
@@ -133,7 +133,7 @@ var
latch: IMycLatch;
returnedValueFromNotify: Boolean;
begin
latch := TMycLatch.CreateLatch(InitialCount);
latch := State.CreateLatch(InitialCount);
returnedValueFromNotify := latch.Notify; // This is IMycLatch (as IMycSubscriber).Notify
Assert.AreEqual(ExpectedReturn, returnedValueFromNotify, 'Unexpected return value from Latch.Notify call for initial count ' + IntToStr(InitialCount) + '.');
@@ -144,7 +144,7 @@ procedure TTestMycLatch.TestNotify_DecrementsCounter_StateChanges;
var
latch: IMycLatch;
begin
latch := TMycLatch.CreateLatch(1);
latch := State.CreateLatch(1);
Assert.IsFalse(latch.State.IsSet, 'Latch should initially not be set (Count=1).');
latch.Notify; // Count becomes 0
Assert.IsTrue(latch.State.IsSet, 'Latch should be set after Notify (Count became 0).');
@@ -154,7 +154,7 @@ procedure TTestMycLatch.TestNotify_MultipleNotifies_CounterBecomesNegativeAndSta
var
latch: IMycLatch;
begin
latch := TMycLatch.CreateLatch(1);
latch := State.CreateLatch(1);
latch.Notify; // Count becomes 0, IsSet = True
Assert.IsTrue(latch.State.IsSet, 'Latch should be set after first Notify.');
latch.Notify; // Count becomes -1, IsSet should remain True
@@ -169,7 +169,7 @@ var
mockSub: TMockSubscriber;
subscription: TMycSubscription;
begin
latch := TMycLatch.CreateLatch(1);
latch := State.CreateLatch(1);
mockSub := TMockSubscriber.Create;
subscription := latch.State.Subscribe(mockSub); // Subscribing to the Latch's state
@@ -188,7 +188,7 @@ var
mockSub1, mockSub2, mockSub3: TMockSubscriber;
sub1, sub2, sub3: TMycSubscription; // Keep subscriptions in scope
begin
latch := TMycLatch.CreateLatch(1);
latch := State.CreateLatch(1);
mockSub1 := TMockSubscriber.Create;
mockSub2 := TMockSubscriber.Create;
mockSub3 := TMockSubscriber.Create;
@@ -210,7 +210,7 @@ var
mockSub: TMockSubscriber;
subscription: TMycSubscription;
begin
latch := TMycLatch.CreateLatch(2); // Count = 2
latch := State.CreateLatch(2); // Count = 2
mockSub := TMockSubscriber.Create;
subscription := latch.State.Subscribe(mockSub);
@@ -225,7 +225,7 @@ var
mockSub: TMockSubscriber;
subscription: TMycSubscription;
begin
latch := TMycLatch.CreateLatch(1);
latch := State.CreateLatch(1);
mockSub := TMockSubscriber.Create;
subscription := latch.State.Subscribe(mockSub);
@@ -242,7 +242,7 @@ var
mockSub1, mockSub2: TMockSubscriber;
subscription1, subscription2: TMycSubscription;
begin
latch := TMycLatch.CreateLatch(1); // Create with count 1
latch := State.CreateLatch(1); // Create with count 1
mockSub1 := TMockSubscriber.Create;
subscription1 := latch.State.Subscribe(mockSub1); // Subscribe before set
@@ -270,8 +270,8 @@ var
mockSubForB: TMockSubscriber;
subHandle_B_listens_A, subHandle_Mock_listens_B: TMycSubscription;
begin
latchA := TMycLatch.CreateLatch(1);
latchB := TMycLatch.CreateLatch(1); // latchB is an IMycSubscriber
latchA := State.CreateLatch(1);
latchB := State.CreateLatch(1); // latchB is an IMycSubscriber
mockSubForB := TMockSubscriber.Create;
subHandle_Mock_listens_B := latchB.State.Subscribe(mockSubForB);
@@ -295,9 +295,9 @@ var
mockSubForC: TMockSubscriber;
sub_Mock_C, sub_C_B, sub_B_A: TMycSubscription;
begin
latchA := TMycLatch.CreateLatch(1);
latchB := TMycLatch.CreateLatch(1);
latchC := TMycLatch.CreateLatch(1);
latchA := State.CreateLatch(1);
latchB := State.CreateLatch(1);
latchC := State.CreateLatch(1);
mockSubForC := TMockSubscriber.Create;
sub_Mock_C := latchC.State.Subscribe(mockSubForC);
@@ -320,8 +320,8 @@ var
begin
// LatchA needs 2 notifies to become set. LatchB needs 1 notify to become set.
// LatchB subscribes to LatchA. So LatchB will be notified once LatchA becomes set.
latchA := TMycLatch.CreateLatch(2);
latchB := TMycLatch.CreateLatch(1);
latchA := State.CreateLatch(2);
latchB := State.CreateLatch(1);
mockSubForB := TMockSubscriber.Create;
sub_Mock_B := latchB.State.Subscribe(mockSubForB);
@@ -347,7 +347,7 @@ var
mockSub: TMockSubscriber;
subscription: TMycSubscription;
begin
latch := TMycLatch.CreateLatch(0); // Returns TMycLatch.Null which is set.
latch := State.CreateLatch(0); // Returns TMycLatch.Null which is set.
mockSub := TMockSubscriber.Create;
subscription := latch.State.Subscribe(mockSub); // TMycLatch.Subscribe notifies immediately if already set.
@@ -362,7 +362,7 @@ var
mockSub: TMockSubscriber;
subscription: TMycSubscription;
begin
latch := TMycLatch.CreateLatch(1);
latch := State.CreateLatch(1);
mockSub := TMockSubscriber.Create;
subscription := latch.State.Subscribe(mockSub);
@@ -381,7 +381,7 @@ var
mockSub: TMockSubscriber;
subscription: TMycSubscription;
begin
latch := TMycLatch.CreateLatch(3);
latch := State.CreateLatch(3);
latch.Notify; // Count -> 2
latch.Notify; // Count -> 1
@@ -404,7 +404,7 @@ var
mockSub1, mockSub2: TMockSubscriber;
subscription1, subscription2: TMycSubscription;
begin
latch := TMycLatch.CreateLatch(1);
latch := State.CreateLatch(1);
mockSub1 := TMockSubscriber.Create;
mockSub2 := TMockSubscriber.Create;
@@ -432,7 +432,7 @@ var
mockSub: TMockSubscriber;
// 'subscription' will be declared in an inner scope to control its finalization
begin
latch := TMycLatch.CreateLatch(1);
latch := State.CreateLatch(1);
mockSub := TMockSubscriber.Create;
var noException: Boolean := True;
@@ -468,7 +468,7 @@ var
mockSubInitial, mockSubNew: TMockSubscriber;
subscriptionInitial, subscriptionNew: TMycSubscription;
begin
latch := TMycLatch.CreateLatch(1);
latch := State.CreateLatch(1);
mockSubInitial := TMockSubscriber.Create;
subscriptionInitial := latch.State.Subscribe(mockSubInitial);
+1 -1
View File
@@ -8,7 +8,7 @@ uses
System.Classes,
System.SyncObjs,
Myc.Core.Tasks,
Myc.Signals, // Uses TMycLatch.CreateLatch and TMycLatch.Null
Myc.Signals, Myc.Core.Signals,
Myc.Core.Atomic;
type